-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
expression.go
334 lines (314 loc) · 10.2 KB
/
expression.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
package interpreter
import (
"net"
"strings"
"time"
"github.com/pkg/errors"
"github.com/ysugimoto/falco/ast"
"github.com/ysugimoto/falco/interpreter/exception"
"github.com/ysugimoto/falco/interpreter/function"
"github.com/ysugimoto/falco/interpreter/operator"
"github.com/ysugimoto/falco/interpreter/value"
"github.com/ysugimoto/falco/types"
)
func (i *Interpreter) IdentValue(val string, withCondition bool) (value.Value, error) {
// Extra lookups identity - call additional ident finder if defined
// This feature is implemented for testing, typically we do not use for interpreter working
if i.IdentResolver != nil {
if v := i.IdentResolver(val); v != nil {
return v, nil
}
}
if v, ok := i.ctx.Backends[val]; ok {
return v, nil
} else if v, ok := i.ctx.Acls[val]; ok {
return v, nil
} else if _, ok := i.ctx.Tables[val]; ok {
return &value.Ident{Value: val, Literal: true}, nil
} else if _, ok := i.ctx.Gotos[val]; ok {
return &value.Ident{Value: val, Literal: true}, nil
} else if _, ok := i.ctx.Penaltyboxes[val]; ok {
return &value.Ident{Value: val, Literal: true}, nil
} else if _, ok := i.ctx.Ratecounters[val]; ok {
return &value.Ident{Value: val, Literal: true}, nil
} else if strings.HasPrefix(val, "var.") {
if v, err := i.localVars.Get(val); err != nil {
return value.Null, errors.WithStack(err)
} else {
return v, nil
}
} else if v, err := i.vars.Get(i.ctx.Scope, val); err != nil {
if withCondition {
return value.Null, nil
} else {
return value.Null, errors.WithStack(err)
}
} else {
return v, nil
}
}
// Evaluate expression
// withCondition boolean is special flag for evaluating expression,
// used for if condition, parenthesis wrapped expression.
// On if condition, prefix expression could use "!" prefix operator for null value.
//
// For example:
//
// withCondition: true -> if (!req.http.Foo) { ... } // Valid, req.http.Foo is nullable string but can be inverse as false
// withCondition: false -> set var.bool = (!req.http.Foo); // Complicated but valid, "!" prefix operator could use for right expression
// withCondition: false -> set var.bool = !req.http.Foo; // Invalid, bare "!" prefix operator could not use for right expression
func (i *Interpreter) ProcessExpression(exp ast.Expression, withCondition bool) (value.Value, error) {
switch t := exp.(type) {
// Underlying VCL type expressions
case *ast.Ident:
return i.IdentValue(t.Value, withCondition)
case *ast.IP:
return &value.IP{Value: net.ParseIP(t.Value), Literal: true}, nil
case *ast.Boolean:
return &value.Boolean{Value: t.Value, Literal: true}, nil
case *ast.Integer:
return &value.Integer{Value: t.Value, Literal: true}, nil
case *ast.String:
return &value.String{Value: t.Value, Literal: true}, nil
case *ast.Float:
return &value.Float{Value: t.Value, Literal: true}, nil
case *ast.RTime:
var val time.Duration
var err error
switch {
case strings.HasSuffix(t.Value, "d"):
num := strings.TrimSuffix(t.Value, "d")
val, err = time.ParseDuration(num + "h")
if err != nil {
return nil, exception.Runtime(&exp.GetMeta().Token, "Failed to parse duration: %s", err)
}
val *= 24
case strings.HasSuffix(t.Value, "y"):
num := strings.TrimSuffix(t.Value, "y")
val, err = time.ParseDuration(num + "h")
if err != nil {
return nil, exception.Runtime(&exp.GetMeta().Token, "Failed to parse duration: %s", err)
}
val *= 24 * 365
default:
val, err = time.ParseDuration(t.Value)
if err != nil {
return nil, exception.Runtime(&exp.GetMeta().Token, "Failed to parse duration: %s", err)
}
}
return &value.RTime{Value: val, Literal: true}, nil
// Combined expressions
case *ast.PrefixExpression:
return i.ProcessPrefixExpression(t, withCondition)
case *ast.GroupedExpression:
return i.ProcessGroupedExpression(t)
case *ast.InfixExpression:
return i.ProcessInfixExpression(t, withCondition)
case *ast.IfExpression:
return i.ProcessIfExpression(t)
case *ast.FunctionCallExpression:
return i.ProcessFunctionCallExpression(t, withCondition)
default:
return value.Null, exception.Runtime(&exp.GetMeta().Token, "Undefined expression found")
}
}
func (i *Interpreter) ProcessPrefixExpression(exp *ast.PrefixExpression, withCondition bool) (value.Value, error) {
v, err := i.ProcessExpression(exp.Right, withCondition)
if err != nil {
return value.Null, errors.WithStack(err)
}
switch exp.Operator {
case "!":
switch t := v.(type) {
case *value.Boolean:
return &value.Boolean{Value: !t.Value}, nil
case *value.String:
// If withCondition is enabled, STRING could be converted to BOOL
if !withCondition {
return value.Null, errors.WithStack(
exception.Runtime(&exp.GetMeta().Token, `Unexpected "!" prefix operator for %v`, v),
)
}
if t.IsNotSet {
return &value.Boolean{Value: true}, nil
}
return &value.Boolean{Value: false}, nil
default:
return value.Null, errors.WithStack(
exception.Runtime(&exp.GetMeta().Token, `Unexpected "!" prefix operator for %v`, v),
)
}
case "-":
switch t := v.(type) {
case *value.Integer:
t.Value = -t.Value
return t, nil
case *value.Float:
t.Value = -t.Value
return t, nil
case *value.RTime:
t.Value = -t.Value
return t, nil
default:
return value.Null, errors.WithStack(
exception.Runtime(&exp.GetMeta().Token, `Unexpected "-" prefix operator for %v`, v),
)
}
case "+":
// I'm wondering what calculate to?
return v, nil
default:
return value.Null, errors.WithStack(
exception.Runtime(&exp.GetMeta().Token, "Unexpected prefix operator: %s", exp.Operator),
)
}
}
func (i *Interpreter) ProcessPostfixExpression(exp *ast.PostfixExpression) (value.Value, error) {
v, err := i.ProcessExpression(exp.Left, false)
if err != nil {
return value.Null, errors.WithStack(err)
}
if exp.Operator != "%" {
return value.Null, errors.WithStack(
exception.Runtime(&exp.GetMeta().Token, "Unexpected postfix operator: %s", exp.Operator),
)
}
t, ok := v.(*value.Integer)
if !ok {
return value.Null, errors.WithStack(
exception.Runtime(&exp.GetMeta().Token, `Unexpected "%%" postfix operator for %v`, v),
)
}
return t, nil
}
func (i *Interpreter) ProcessGroupedExpression(exp *ast.GroupedExpression) (value.Value, error) {
v, err := i.ProcessExpression(exp.Right, true)
if err != nil {
return value.Null, errors.WithStack(err)
}
return v, nil
}
func (i *Interpreter) ProcessIfExpression(exp *ast.IfExpression) (value.Value, error) {
cond, err := i.ProcessExpression(exp.Condition, true)
if err != nil {
return value.Null, errors.WithStack(err)
}
switch t := cond.(type) {
case *value.Boolean:
if t.Value {
return i.ProcessExpression(exp.Consequence, false)
}
case *value.String:
if !t.IsNotSet {
return i.ProcessExpression(exp.Consequence, false)
}
default:
if cond == value.Null {
return i.ProcessExpression(exp.Alternative, false)
}
return value.Null, exception.Runtime(&exp.GetMeta().Token, "If condition returns not boolean")
}
return i.ProcessExpression(exp.Alternative, false)
}
func (i *Interpreter) ProcessFunctionCallExpression(exp *ast.FunctionCallExpression, withCondition bool) (value.Value, error) {
if sub, ok := i.ctx.SubroutineFunctions[exp.Function.Value]; ok {
if len(exp.Arguments) > 0 {
return value.Null, exception.Runtime(
&exp.GetMeta().Token,
"Function subroutine %s could not accept any arguments",
exp.Function.Value,
)
}
if _, ok := types.ValueTypeMap[sub.ReturnType.Value]; !ok {
return value.Null, exception.Runtime(
&sub.GetMeta().Token,
"subroutine %s has invalid return type %s",
sub.Name,
sub.ReturnType,
)
}
// Functional subroutine may change status
v, _, err := i.ProcessFunctionSubroutine(sub, DebugPass)
if err != nil {
return v, errors.WithStack(err)
}
return v, nil
}
fn, err := function.Exists(i.ctx.Scope, exp.Function.Value)
if err != nil {
return value.Null, errors.WithStack(err)
}
args := make([]value.Value, len(exp.Arguments))
for j := range exp.Arguments {
if fn.IsIdentArgument(j) {
// If function accepts ID type, pass the string as Ident value without processing expression.
// This is because some function uses collection value like req.http.Cookie as ID type,
// But the processor passes *value.String as primitive value normally.
// In order to treat collection value inside, ensure ident argument is treated as correspond types.
if ident, ok := exp.Arguments[j].(*ast.Ident); ok {
args[j] = &value.Ident{Value: ident.Value}
} else {
return value.Null, errors.WithStack(
exception.Runtime(
&exp.Arguments[j].GetMeta().Token,
"Function %s of %d argument must be an Ident", exp.Function.Value, j,
),
)
}
} else {
a, err := i.ProcessExpression(exp.Arguments[j], withCondition)
if err != nil {
return value.Null, errors.WithStack(err)
}
args[j] = a
}
}
return fn.Call(i.ctx, args...)
}
func (i *Interpreter) ProcessInfixExpression(exp *ast.InfixExpression, withCondition bool) (value.Value, error) {
left, err := i.ProcessExpression(exp.Left, withCondition)
if err != nil {
return value.Null, errors.WithStack(err)
}
right, err := i.ProcessExpression(exp.Right, withCondition)
if err != nil {
return value.Null, errors.WithStack(err)
}
var result value.Value
var opErr error
switch exp.Operator {
case "==":
result, opErr = operator.Equal(left, right)
case "!=":
result, opErr = operator.NotEqual(left, right)
case ">":
result, opErr = operator.GreaterThan(left, right)
case "<":
result, opErr = operator.LessThan(left, right)
case ">=":
result, opErr = operator.GreaterThanEqual(left, right)
case "<=":
result, opErr = operator.LessThanEqual(left, right)
case "~":
result, opErr = operator.Regex(i.ctx, left, right)
case "!~":
result, opErr = operator.NotRegex(i.ctx, left, right)
case "||":
result, opErr = operator.LogicalOr(left, right)
case "&&":
result, opErr = operator.LogicalAnd(left, right)
// "+" means string concatenation
case "+":
result, opErr = operator.Concat(left, right)
default:
return value.Null, errors.WithStack(
exception.Runtime(&exp.GetMeta().Token, "Unexpected infix operator: %s", exp.Operator),
)
}
if opErr != nil {
return value.Null, errors.WithStack(
exception.Runtime(&exp.GetMeta().Token, opErr.Error()),
)
}
return result, nil
}