-
Notifications
You must be signed in to change notification settings - Fork 8
/
lambda.go
56 lines (44 loc) · 1.21 KB
/
lambda.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
package vfilter
import (
"context"
"github.com/Velocidex/ordereddict"
"github.com/alecthomas/participle"
"www.velocidex.com/golang/vfilter/types"
)
var (
lambdaParser = participle.MustBuild(
&Lambda{},
participle.Lexer(vqlLexer),
participle.Elide("Comment", "MLineComment", "VQLComment"))
)
type Lambda struct {
Parameters *_ParameterList ` @@ `
LetOperator string ` @"=>" `
Expression *_AndExpression ` @@ `
}
func (self *Lambda) GetParameters() []string {
result := []string{}
if self.Parameters != nil {
visitor(self.Parameters, &result)
}
return result
}
func (self *Lambda) Reduce(ctx context.Context, scope types.Scope, parameters []Any) Any {
my_parameters := self.GetParameters()
if len(my_parameters) != len(parameters) {
scope.Log("ERROR:Incorrect number of parameters in Lambda call")
return Null{}
}
vars := ordereddict.NewDict()
for idx, name := range my_parameters {
vars.Set(name, parameters[idx])
}
subscope := scope.Copy().AppendVars(vars)
defer subscope.Close()
return self.Expression.Reduce(ctx, subscope)
}
func ParseLambda(expression string) (*Lambda, error) {
lambda := &Lambda{}
err := lambdaParser.ParseString(expression, lambda)
return lambda, err
}