-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfunction.go
More file actions
132 lines (113 loc) · 3.39 KB
/
function.go
File metadata and controls
132 lines (113 loc) · 3.39 KB
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
package expr
import (
"fmt"
"github.com/lqiz/expr/node"
"go/ast"
"go/token"
"strings"
)
type BinaryBoolExpr struct{}
type BinaryStrExpr struct{}
type BinaryIntExpr struct{}
type CallExpr struct {
fn string // one of "in_array", "ver_compare"
args []ast.Expr
}
func (b BinaryBoolExpr) Invoke(x, y node.ValueNode, op token.Token) node.ValueNode {
xb, xok := x.(node.BoolNode)
yb, yok := y.(node.BoolNode)
if !xok || !yok {
return node.NewBadNode(x.GetTextValue() + y.GetTextValue())
}
switch op {
case token.LAND:
return node.NewBoolNode(xb.True && yb.True)
case token.LOR:
return node.NewBoolNode(xb.True || yb.True)
}
return node.NewBadNode(fmt.Sprintf("unsupported binary operator: %s", op.String()))
}
func (b BinaryStrExpr) Invoke(x, y node.ValueNode, op token.Token) node.ValueNode {
xs, xok := x.(node.StrNode)
ys, yok := y.(node.StrNode)
if !xok || !yok {
return node.NewBadNode("x: " + x.GetTextValue() + "y: " + y.GetTextValue())
}
switch op {
case token.EQL: // ==
return node.NewBoolNode(strings.Compare(xs.GetValue(), ys.GetValue()) == 0)
case token.LSS: // <
return node.NewBoolNode(strings.Compare(xs.GetValue(), ys.GetValue()) == -1)
case token.GTR: // >
return node.NewBoolNode(strings.Compare(xs.GetValue(), ys.GetValue()) == +1)
case token.GEQ: // >=
return node.NewBoolNode(strings.Compare(xs.GetValue(), ys.GetValue()) >= 0)
case token.LEQ: // <=
return node.NewBoolNode(strings.Compare(xs.GetValue(), ys.GetValue()) <= 0)
}
return node.NewBadNode(fmt.Sprintf("unsupported binary operator: %s", op.String()))
}
func (b BinaryIntExpr) Invoke(x, y node.ValueNode, op token.Token) node.ValueNode {
xs, xok := x.(node.IntNode)
ys, yok := y.(node.IntNode)
if !xok || !yok {
return node.NewBadNode(x.GetTextValue() + y.GetTextValue())
}
switch op {
case token.EQL: // ==
return node.BoolNode{
True: xs.Value == ys.Value,
}
case token.LSS: // <
return node.BoolNode{
True: xs.Value < ys.Value,
}
case token.GTR: // >
return node.NewBoolNode(xs.Value > ys.Value)
case token.GEQ: // >=
return node.NewBoolNode(xs.Value >= ys.Value)
case token.LEQ: // <=
return node.NewBoolNode(xs.Value <= ys.Value)
}
return node.NewBadNode(fmt.Sprintf("unsupported binary operator: %s", op.String()))
}
func (c CallExpr) Invoke(mem map[string]node.ValueNode) node.ValueNode {
switch c.fn {
case "in_array":
parm := eval(mem, c.args[0])
if parm.GetType() == node.TypeBad {
return parm
}
vRange, ok := c.args[1].(*ast.CompositeLit)
if !ok {
return node.NewBadNode("func in_array 2ed params is not a composite lit")
}
eltNodes := make([]node.ValueNode, 0, len(vRange.Elts))
for _, p := range vRange.Elts {
elt := eval(mem, p)
eltNodes = append(eltNodes, elt)
}
has := false
for _, v := range eltNodes {
if v.GetType() == parm.GetType() && v.GetTextValue() == parm.GetTextValue() {
has = true
}
}
return node.NewBoolNode(has)
case "ver_compare":
if len(c.args) != 3 {
return node.NewBadNode("func ver_compare doesn't have enough params")
}
args := make([]string, 0, 3)
for _, v := range c.args {
arg := eval(mem, v)
if arg.GetType() != node.TypeStr {
return node.NewBadNode("func ver_compare params type error")
}
args = append(args, arg.GetTextValue())
}
ret := VersionCompare(args[0], args[1], args[2])
return node.NewBoolNode(ret)
}
panic(fmt.Sprintf("unsupported function call: %s", c.fn))
}