forked from rcarmo/gisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators.go
134 lines (113 loc) · 2.88 KB
/
operators.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
package generator
import (
"go/ast"
"go/token"
log "github.com/sirupsen/logrus"
"github.com/zylisp/zylisp/parser"
)
var (
callableOperators = []string{">", ">=", "<", "<=", "=", "+", "-", "*", "/", "mod"}
logicOperatorMap = map[string]token.Token{
"and": token.LAND,
"or": token.LOR,
}
unaryOperatorMap = map[string]token.Token{
"!": token.NOT,
}
)
func isCallableOperator(node *parser.CallNode) bool {
if node.Callee.Type() != parser.NodeIdent {
return false
}
ident := node.Callee.(*parser.IdentNode).Ident
return isInSlice(ident, callableOperators)
}
// We handle comparisons as a call to some go code, since you can only
// compare ints, floats, cmplx, and such, you know...
// We handle arithmetic operations as function calls, since all args are evaluated
func makeNAryCallableExpr(node *parser.CallNode) *ast.CallExpr {
op := node.Callee.(*parser.IdentNode).Ident
args := EvalExprs(node.Args)
var selector string
// TODO: abstract this away into a map!!!
switch op {
case ">":
selector = "GT"
case ">=":
selector = "GTEQ"
case "<":
selector = "LT"
case "<=":
selector = "LTEQ"
case "=":
selector = "EQ"
case "+":
selector = "ADD"
case "-":
selector = "SUB"
case "*":
selector = "MUL"
case "/":
selector = "DIV"
case "mod":
argsCount := len(node.Args)
requiredCount := 2
if argsCount > requiredCount {
log.Error(TooManyArgsError, "mod", requiredCount)
}
selector = "MOD"
}
return makeFuncCall(makeSelectorExpr(ast.NewIdent("core"), ast.NewIdent(selector)), args)
}
func isLogicOperator(node *parser.CallNode) bool {
if node.Callee.Type() != parser.NodeIdent {
return false
}
_, ok := logicOperatorMap[node.Callee.(*parser.IdentNode).Ident]
argsCount := len(node.Args)
requiredCount := 2
if argsCount < requiredCount && ok {
log.Error(TooManyArgsError, "a logical operator", requiredCount)
}
return ok
}
// But logical comparisons are done properly, since those can short-circuit
func makeNAryLogicExpr(node *parser.CallNode) *ast.BinaryExpr {
op := logicOperatorMap[node.Callee.(*parser.IdentNode).Ident]
outer := makeBinaryExpr(op, EvalExpr(node.Args[0]), EvalExpr(node.Args[1]))
for i := 2; i < len(node.Args); i++ {
outer = makeBinaryExpr(op, outer, EvalExpr(node.Args[i]))
}
return outer
}
func makeBinaryExpr(op token.Token, x, y ast.Expr) *ast.BinaryExpr {
return &ast.BinaryExpr{
X: x,
Y: y,
Op: op,
}
}
func isUnaryOperator(node *parser.CallNode) bool {
if node.Callee.Type() != parser.NodeIdent {
return false
}
_, ok := unaryOperatorMap[node.Callee.(*parser.IdentNode).Ident]
if len(node.Args) != 1 && ok {
log.Error(UnaryArgsCountError)
}
return ok
}
func makeUnaryExpr(op token.Token, x ast.Expr) *ast.UnaryExpr {
return &ast.UnaryExpr{
X: x,
Op: op,
}
}
func isInSlice(elem string, slice []string) bool {
for _, el := range slice {
if elem == el {
return true
}
}
return false
}