-
Notifications
You must be signed in to change notification settings - Fork 5
/
ast.go
224 lines (172 loc) · 3.29 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package rules
import (
"errors"
"fmt"
"strconv"
"time"
)
type Expr interface {
String() string
Type() string
}
type Literal interface {
Expr
Value() interface{}
}
type parentExpr struct {
Expr Expr
}
func (e *parentExpr) String() string {
return fmt.Sprintf("(%s)", e.Expr.String())
}
func (e *parentExpr) Type() string {
return "nested expression"
}
type binaryExpr struct {
Op Op
LHS Expr
RHS Expr
}
func (e *binaryExpr) String() string {
return fmt.Sprintf("%s %s %s", e.LHS.String(), e.Op, e.RHS.String())
}
func (e *binaryExpr) Type() string {
return "binary expression"
}
type identifier struct {
Val string
}
func (v *identifier) String() string {
return v.Val
}
func (v *identifier) Type() string {
return "variable"
}
type numberLiteral struct {
Val float64
}
func (l *numberLiteral) String() string {
return strconv.FormatFloat(l.Val, 'f', 3, 64)
}
func (l *numberLiteral) Type() string {
return "number"
}
func (l *numberLiteral) Value() interface{} {
return l.Val
}
type stringLiteral struct {
Val string
}
func (l *stringLiteral) String() string {
return l.Val
}
func (l *stringLiteral) Type() string {
return "string"
}
func (l *stringLiteral) Value() interface{} {
return l.Val
}
type boolLiteral struct {
Val bool
}
func (l *boolLiteral) String() string {
return fmt.Sprintf("%v", l.Val)
}
func (l *boolLiteral) Type() string {
return "bool"
}
func (l *boolLiteral) Value() interface{} {
return l.Val
}
type timeLiteral struct {
Val time.Time
}
func (l *timeLiteral) String() string {
return fmt.Sprintf("%v", l.Val)
}
func (l *timeLiteral) Type() string {
return "time"
}
func (l *timeLiteral) Value() interface{} {
return l.Val
}
type numberSliceLiteral struct {
Val []float64
}
func (l *numberSliceLiteral) String() string {
return fmt.Sprintf("%v", l.Val)
}
func (l *numberSliceLiteral) Type() string {
return "number slice"
}
func (l *numberSliceLiteral) Value() interface{} {
return l.Val
}
type stringSliceLiteral struct {
Val []string
}
func (l *stringSliceLiteral) String() string {
return fmt.Sprintf("%v", l.Val)
}
func (l *stringSliceLiteral) Type() string {
return "string slice"
}
func (l *stringSliceLiteral) Value() interface{} {
return l.Val
}
type durationLiteral struct {
Val time.Duration
}
func (l *durationLiteral) String() string {
return l.Val.String()
}
func (l *durationLiteral) Type() string {
return "duration"
}
func (l *durationLiteral) Value() interface{} {
return l.Val
}
type Function func(args ...interface{}) (interface{}, error)
type functionExpression struct {
Name string
Arguments []Expr
}
func (l *functionExpression) String() string {
return l.Name
}
func (l *functionExpression) Type() string {
return "function"
}
type WalkFunc func(expr Expr, err error) error
func Walk(expr Expr, fn WalkFunc) error {
if expr == nil {
return fn(nil, errors.New("expression must not be nil"))
}
return walk(expr, fn)
}
func walk(expr Expr, fn WalkFunc) error {
if expr == nil {
return nil
}
err := fn(expr, nil)
if err != nil {
return err
}
switch e := expr.(type) {
case *binaryExpr:
err = walk(e.LHS, fn)
if err != nil {
return err
}
err = walk(e.RHS, fn)
if err != nil {
return err
}
case *parentExpr:
err = walk(e.Expr, fn)
if err != nil {
return err
}
}
return nil
}