-
Notifications
You must be signed in to change notification settings - Fork 48
/
ast.go
149 lines (123 loc) · 2.82 KB
/
ast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//Copyright 2012 Vastech SA (PTY) LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ast
import (
"strconv"
"strings"
"github.com/goccmack/gocc/example/bools/token"
"github.com/goccmack/gocc/example/bools/util"
)
type Attrib interface{}
type Val interface {
Attrib
Eval() bool
String() string
}
type Op int
func (o Op) String() string {
switch o {
case OR:
return "|"
case AND:
return "&"
}
return ""
}
const (
NOOP = Op(0)
OR = Op(1)
AND = Op(2)
)
type BoolExpr struct {
A Val
B Val
Op Op
}
func NewBoolAndExpr(a, b Attrib) (*BoolExpr, error) {
return &BoolExpr{a.(Val), b.(Val), AND}, nil
}
func NewBoolOrExpr(a, b Attrib) (*BoolExpr, error) {
return &BoolExpr{a.(Val), b.(Val), OR}, nil
}
func NewBoolGroupExpr(a Attrib) (*BoolExpr, error) {
return &BoolExpr{a.(Val), nil, NOOP}, nil
}
func (this *BoolExpr) Eval() bool {
switch this.Op {
case OR:
return this.A.Eval() || this.B.Eval()
case AND:
return this.A.Eval() && this.B.Eval()
}
return this.A.Eval()
}
func (this *BoolExpr) String() string {
return this.A.String() + " " + this.Op.String() + " " + this.B.String()
}
type val bool
func (v val) Eval() bool {
return bool(v)
}
func (v val) String() string {
if v {
return "true"
}
return "false"
}
var (
TRUE = val(true)
FALSE = val(false)
)
type LessThanExpr struct {
A int64
B int64
}
func NewLessThanExpr(a, b Attrib) (*LessThanExpr, error) {
aint, err := util.IntValue(a.(*token.Token).Lit)
if err != nil {
return nil, err
}
bint, err := util.IntValue(b.(*token.Token).Lit)
if err != nil {
return nil, err
}
return &LessThanExpr{aint, bint}, nil
}
func (this *LessThanExpr) Eval() bool {
return this.A < this.B
}
func (this *LessThanExpr) String() string {
return strconv.FormatInt(this.A, 10) + " < " + strconv.FormatInt(this.B, 10)
}
type SubStringExpr struct {
A string
B string
}
func NewSubStringExpr(a, b Attrib) (*SubStringExpr, error) {
astr, err := strconv.Unquote(string(a.(*token.Token).Lit))
if err != nil {
return nil, err
}
bstr, err := strconv.Unquote(string(b.(*token.Token).Lit))
if err != nil {
return nil, err
}
return &SubStringExpr{astr, bstr}, nil
}
func (this *SubStringExpr) Eval() bool {
return strings.Contains(this.B, this.A)
}
func (this *SubStringExpr) String() string {
return this.A + " in " + this.B
}