-
Notifications
You must be signed in to change notification settings - Fork 19
/
tree.go
362 lines (280 loc) · 8.79 KB
/
tree.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package excellent
import (
"fmt"
"strings"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/excellent/functions"
"github.com/nyaruka/goflow/excellent/operators"
"github.com/nyaruka/goflow/excellent/types"
)
// Expression is the base interface of all syntax elements
type Expression interface {
Evaluate(envs.Environment, *Scope) types.XValue
String() string
}
// ContextReference is an identifier which is a function name or root variable in the context
type ContextReference struct {
name string
}
func (x *ContextReference) Evaluate(env envs.Environment, scope *Scope) types.XValue {
value, exists := scope.Get(x.name)
if !exists {
return types.NewXErrorf("context has no property '%s'", x.name)
}
return value
}
func (x *ContextReference) String() string {
return strings.ToLower(x.name)
}
type DotLookup struct {
container Expression
lookup string
}
func (x *DotLookup) Evaluate(env envs.Environment, scope *Scope) types.XValue {
containerVal := x.container.Evaluate(env, scope)
if types.IsXError(containerVal) {
return containerVal
}
return resolveLookup(env, containerVal, types.NewXText(x.lookup), lookupNotationDot)
}
func (x *DotLookup) String() string {
return fmt.Sprintf("%s.%s", x.container.String(), x.lookup)
}
type ArrayLookup struct {
container Expression
lookup Expression
}
func (x *ArrayLookup) Evaluate(env envs.Environment, scope *Scope) types.XValue {
containerVal := x.container.Evaluate(env, scope)
if types.IsXError(containerVal) {
return containerVal
}
lookupVal := x.lookup.Evaluate(env, scope)
if types.IsXError(lookupVal) {
return lookupVal
}
return resolveLookup(env, containerVal, lookupVal, lookupNotationArray)
}
func (x *ArrayLookup) String() string {
return fmt.Sprintf("%s[%s]", x.container.String(), x.lookup.String())
}
type FunctionCall struct {
function Expression
params []Expression
}
func (x *FunctionCall) Evaluate(env envs.Environment, scope *Scope) types.XValue {
funcVal := x.function.Evaluate(env, scope)
if types.IsXError(funcVal) {
return funcVal
}
asFunction, isFunction := funcVal.(*types.XFunction)
if !isFunction {
return types.NewXErrorf("%s is not a function", x.function.String())
}
params := make([]types.XValue, len(x.params))
for i := range x.params {
params[i] = x.params[i].Evaluate(env, scope)
}
return asFunction.Call(env, params)
}
func (x *FunctionCall) String() string {
params := make([]string, len(x.params))
for i := range x.params {
params[i] = x.params[i].String()
}
return fmt.Sprintf("%s(%s)", x.function.String(), strings.Join(params, ", "))
}
type AnonFunction struct {
args []string
body Expression
}
func (x *AnonFunction) Evaluate(env envs.Environment, scope *Scope) types.XValue {
// create an XFunction which wraps our body expression
fn := func(env envs.Environment, args ...types.XValue) types.XValue {
// create new context that includes the args
argsMap := make(map[string]types.XValue, len(x.args))
for i := range x.args {
argsMap[x.args[i]] = args[i]
}
childScope := NewScope(types.NewXObject(argsMap), scope)
return x.body.Evaluate(env, childScope)
}
return types.NewXFunction("", functions.NumArgsCheck(len(x.args), fn))
}
func (x *AnonFunction) String() string {
return fmt.Sprintf("(%s) => %s", strings.Join(x.args, ", "), x.body)
}
type Concatenation struct {
exp1 Expression
exp2 Expression
}
func (x *Concatenation) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.Concatenate(env, x.exp1.Evaluate(env, scope), x.exp2.Evaluate(env, scope))
}
func (x *Concatenation) String() string {
return fmt.Sprintf("%s & %s", x.exp1.String(), x.exp2.String())
}
type Addition struct {
exp1 Expression
exp2 Expression
}
func (x *Addition) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.Add(env, x.exp1.Evaluate(env, scope), x.exp2.Evaluate(env, scope))
}
func (x *Addition) String() string {
return fmt.Sprintf("%s + %s", x.exp1.String(), x.exp2.String())
}
type Subtraction struct {
exp1 Expression
exp2 Expression
}
func (x *Subtraction) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.Subtract(env, x.exp1.Evaluate(env, scope), x.exp2.Evaluate(env, scope))
}
func (x *Subtraction) String() string {
return fmt.Sprintf("%s - %s", x.exp1.String(), x.exp2.String())
}
type Multiplication struct {
exp1 Expression
exp2 Expression
}
func (x *Multiplication) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.Multiply(env, x.exp1.Evaluate(env, scope), x.exp2.Evaluate(env, scope))
}
func (x *Multiplication) String() string {
return fmt.Sprintf("%s * %s", x.exp1.String(), x.exp2.String())
}
type Division struct {
exp1 Expression
exp2 Expression
}
func (x *Division) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.Divide(env, x.exp1.Evaluate(env, scope), x.exp2.Evaluate(env, scope))
}
func (x *Division) String() string {
return fmt.Sprintf("%s / %s", x.exp1.String(), x.exp2.String())
}
type Exponent struct {
expression Expression
exponent Expression
}
func (x *Exponent) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.Exponent(env, x.expression.Evaluate(env, scope), x.exponent.Evaluate(env, scope))
}
func (x *Exponent) String() string {
return fmt.Sprintf("%s ^ %s", x.expression.String(), x.exponent.String())
}
type Negation struct {
exp Expression
}
func (x *Negation) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.Negate(env, x.exp.Evaluate(env, scope))
}
func (x *Negation) String() string {
return fmt.Sprintf("-%s", x.exp.String())
}
type Equality struct {
exp1 Expression
exp2 Expression
}
func (x *Equality) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.Equal(env, x.exp1.Evaluate(env, scope), x.exp2.Evaluate(env, scope))
}
func (x *Equality) String() string {
return fmt.Sprintf("%s = %s", x.exp1.String(), x.exp2.String())
}
type InEquality struct {
exp1 Expression
exp2 Expression
}
func (x *InEquality) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.NotEqual(env, x.exp1.Evaluate(env, scope), x.exp2.Evaluate(env, scope))
}
func (x *InEquality) String() string {
return fmt.Sprintf("%s != %s", x.exp1.String(), x.exp2.String())
}
type LessThan struct {
exp1 Expression
exp2 Expression
}
func (x *LessThan) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.LessThan(env, x.exp1.Evaluate(env, scope), x.exp2.Evaluate(env, scope))
}
func (x *LessThan) String() string {
return fmt.Sprintf("%s < %s", x.exp1.String(), x.exp2.String())
}
type LessThanOrEqual struct {
exp1 Expression
exp2 Expression
}
func (x *LessThanOrEqual) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.LessThanOrEqual(env, x.exp1.Evaluate(env, scope), x.exp2.Evaluate(env, scope))
}
func (x *LessThanOrEqual) String() string {
return fmt.Sprintf("%s <= %s", x.exp1.String(), x.exp2.String())
}
type GreaterThan struct {
exp1 Expression
exp2 Expression
}
func (x *GreaterThan) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.GreaterThan(env, x.exp1.Evaluate(env, scope), x.exp2.Evaluate(env, scope))
}
func (x *GreaterThan) String() string {
return fmt.Sprintf("%s > %s", x.exp1.String(), x.exp2.String())
}
type GreaterThanOrEqual struct {
exp1 Expression
exp2 Expression
}
func (x *GreaterThanOrEqual) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return operators.GreaterThanOrEqual(env, x.exp1.Evaluate(env, scope), x.exp2.Evaluate(env, scope))
}
func (x *GreaterThanOrEqual) String() string {
return fmt.Sprintf("%s >= %s", x.exp1.String(), x.exp2.String())
}
type Parentheses struct {
exp Expression
}
func (x *Parentheses) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return x.exp.Evaluate(env, scope)
}
func (x *Parentheses) String() string {
return fmt.Sprintf("(%s)", x.exp.String())
}
type TextLiteral struct {
val types.XText
}
func (x *TextLiteral) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return x.val
}
func (x *TextLiteral) String() string {
return x.val.Describe()
}
// NumberLiteral is a literal number like 123 or 1.5
type NumberLiteral struct {
val types.XNumber
}
func (x *NumberLiteral) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return x.val
}
func (x *NumberLiteral) String() string {
return x.val.Describe()
}
// BooleanLiteral is a literal bool
type BooleanLiteral struct {
val types.XBoolean
}
func (x *BooleanLiteral) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return x.val
}
func (x *BooleanLiteral) String() string {
return x.val.Describe()
}
type NullLiteral struct{}
func (x *NullLiteral) Evaluate(env envs.Environment, scope *Scope) types.XValue {
return nil
}
func (x *NullLiteral) String() string {
return "null"
}