-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluator.go
187 lines (174 loc) · 8.14 KB
/
evaluator.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
package ysgo
import (
"fmt"
"math"
"github.com/remieven/ysgo/variable"
)
func evaluateExpression(e *variable.Expression, retriever variable.Retriever, caller functionCaller, smartVariables map[string]*variable.Expression) (*variable.Value, error) {
switch {
case e.VariableID != nil:
if expression, ok := smartVariables[*e.VariableID]; ok {
return evaluateExpression(expression, retriever, caller, smartVariables)
}
value, ok := retriever.GetValue(*e.VariableID)
if !ok {
return nil, fmt.Errorf("variable [%v] not found in storage", *e.VariableID)
}
return value, nil
case e.FunctionCall != nil:
return evaluateFunctionCall(e.FunctionCall, retriever, caller, smartVariables)
case e.Value != nil:
return e.Value, nil
case e.NegativeExpression != nil:
negativeExpressionValue, err := evaluateExpression(e.NegativeExpression, retriever, caller, smartVariables)
if err != nil {
return nil, fmt.Errorf("failed to evaluate negative expression: %w", err)
} else if negativeExpressionValue.Number == nil {
return nil, fmt.Errorf("cannot compute the negative value of something that is not a number")
}
return variable.NewNumber(-*negativeExpressionValue.Number), nil
case e.NotExpression != nil:
notExpressionValue, err := evaluateExpression(e.NotExpression, retriever, caller, smartVariables)
if err != nil {
return nil, fmt.Errorf("failed to evaluate not-expression: %w", err)
} else if notExpressionValue.Boolean == nil {
return nil, fmt.Errorf("cannot compute the negation of something that is not a boolean")
}
return variable.NewBoolean(!*notExpressionValue.Boolean), nil
case e.Operator != nil:
return evaluateBinaryOperation(*e.Operator, e.LeftOperand, e.RightOperand, retriever, caller, smartVariables)
}
return nil, nil
}
func evaluateBinaryOperation(operator int, leftOperand, rightOperand *variable.Expression, retriever variable.Retriever, caller functionCaller, smartVariables map[string]*variable.Expression) (*variable.Value, error) {
leftOperandValue, err := evaluateExpression(leftOperand, retriever, caller, smartVariables)
if err != nil {
return nil, fmt.Errorf("failed to evaluate left operand of expression: %w", err)
}
// lazy-evaluation if possible
if operator == variable.AndBinaryOperator {
if leftOperandValue.Boolean == nil {
return nil, fmt.Errorf("cannot evaluate AND expression if left operand is not a boolean")
}
if !*leftOperandValue.Boolean {
return leftOperandValue, nil
}
}
if operator == variable.OrBinaryOperator {
if leftOperandValue.Boolean == nil {
return nil, fmt.Errorf("cannot evaluate OR expression if left operand is not a boolean")
}
if *leftOperandValue.Boolean {
return leftOperandValue, nil
}
}
rightOperandValue, err := evaluateExpression(rightOperand, retriever, caller, smartVariables)
if err != nil {
return nil, fmt.Errorf("failed to evaluate right operand of expression: %w", err)
}
bothOperandsAreNumbers := leftOperandValue.Number != nil && rightOperandValue.Number != nil
bothOperandsAreBooleans := leftOperandValue.Boolean != nil && rightOperandValue.Boolean != nil
bothOperandsAreStrings := leftOperandValue.String != nil && rightOperandValue.String != nil
if !(bothOperandsAreNumbers || bothOperandsAreBooleans || bothOperandsAreStrings) {
return nil, fmt.Errorf("cannot evaluate a binary expression if operands have different types")
}
switch operator {
case variable.MultiplicationBinaryOperator:
if bothOperandsAreNumbers {
return variable.NewNumber((*leftOperandValue.Number) * (*rightOperandValue.Number)), nil
}
return nil, fmt.Errorf("cannot multiply two values that are not both numbers")
case variable.DivisionBinaryOperator:
if bothOperandsAreNumbers {
return variable.NewNumber((*leftOperandValue.Number) / (*rightOperandValue.Number)), nil
}
return nil, fmt.Errorf("cannot divide two values that are not both numbers")
case variable.ModuloBinaryOperator:
if bothOperandsAreNumbers {
return variable.NewNumber(math.Mod(*leftOperandValue.Number, *rightOperandValue.Number)), nil
}
return nil, fmt.Errorf("cannot divide two values that are not both numbers")
case variable.AdditionBinaryOperator:
if bothOperandsAreNumbers {
return variable.NewNumber((*leftOperandValue.Number) + (*rightOperandValue.Number)), nil
} else if bothOperandsAreStrings {
return variable.NewString((*leftOperandValue.String) + (*rightOperandValue.String)), nil
}
return nil, fmt.Errorf("cannot add two values that are not either both numbers or both strings")
case variable.SubtractionBinaryOperator:
if bothOperandsAreNumbers {
return variable.NewNumber((*leftOperandValue.Number) - (*rightOperandValue.Number)), nil
}
return nil, fmt.Errorf("cannot subtract two values that are not both numbers")
case variable.LessThanEqualsBinaryOperator:
if bothOperandsAreNumbers {
return variable.NewBoolean((*leftOperandValue.Number) <= (*rightOperandValue.Number)), nil
}
return nil, fmt.Errorf("cannot compare two values with LTE that are not both numbers")
case variable.GreaterThanEqualsBinaryOperator:
if bothOperandsAreNumbers {
return variable.NewBoolean((*leftOperandValue.Number) >= (*rightOperandValue.Number)), nil
}
return nil, fmt.Errorf("cannot compare two values with GTE that are not both numbers")
case variable.LessBinaryOperator:
if bothOperandsAreNumbers {
return variable.NewBoolean((*leftOperandValue.Number) < (*rightOperandValue.Number)), nil
}
return nil, fmt.Errorf("cannot compare two values with LT that are not both numbers")
case variable.GreaterBinaryOperator:
if bothOperandsAreNumbers {
return variable.NewBoolean((*leftOperandValue.Number) > (*rightOperandValue.Number)), nil
}
return nil, fmt.Errorf("cannot compare two values with GT that are not both numbers")
case variable.EqualsBinaryOperator:
if bothOperandsAreNumbers {
return variable.NewBoolean((*leftOperandValue.Number) == (*rightOperandValue.Number)), nil
} else if bothOperandsAreBooleans {
return variable.NewBoolean((*leftOperandValue.Boolean) == (*rightOperandValue.Boolean)), nil
} else if bothOperandsAreStrings {
return variable.NewBoolean((*leftOperandValue.String) == (*rightOperandValue.String)), nil
}
case variable.NotEqualsBinaryOperator:
if bothOperandsAreNumbers {
return variable.NewBoolean((*leftOperandValue.Number) != (*rightOperandValue.Number)), nil
} else if bothOperandsAreBooleans {
return variable.NewBoolean((*leftOperandValue.Boolean) != (*rightOperandValue.Boolean)), nil
} else if bothOperandsAreStrings {
return variable.NewBoolean((*leftOperandValue.String) != (*rightOperandValue.String)), nil
}
case variable.AndBinaryOperator:
if bothOperandsAreBooleans {
return rightOperandValue, nil // leftOperandValue has already been accounted for when checking if lazy-evaluation was possible
}
return nil, fmt.Errorf("cannot AND two values that are not both booleans")
case variable.OrBinaryOperator:
if bothOperandsAreBooleans {
return rightOperandValue, nil // leftOperandValue has already been accounted for when checking if lazy-evaluation was possible
}
return nil, fmt.Errorf("cannot OR two values that are not both booleans")
case variable.XorBinaryOperator:
if bothOperandsAreBooleans {
return variable.NewBoolean(xor(*leftOperandValue.Boolean, *rightOperandValue.Boolean)), nil
}
return nil, fmt.Errorf("cannot XOR two values that are not both booleans")
}
return nil, fmt.Errorf("unknown operator")
}
func xor(a, b bool) bool {
return (a && !b) || (!a && b)
}
func evaluateFunctionCall(call *variable.FunctionCall, retriever variable.Retriever, caller functionCaller, smartVariables map[string]*variable.Expression) (*variable.Value, error) {
evaluatedArgs := make([]*variable.Value, 0, len(call.Arguments))
for i := range call.Arguments {
evaluatedArg, err := evaluateExpression(call.Arguments[i], retriever, caller, smartVariables)
if err != nil {
return nil, fmt.Errorf("failed to evaluate argument number %d: %w", i, err)
}
evaluatedArgs = append(evaluatedArgs, evaluatedArg)
}
result, err := caller.call(call.FunctionID, evaluatedArgs)
if err != nil {
return nil, fmt.Errorf("call to function %s failed: %w", call.FunctionID, err)
}
return result, nil
}