-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.go
190 lines (156 loc) · 5.76 KB
/
interpreter.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
package gojacego
import (
"errors"
"fmt"
"math"
)
type interpreter struct {
}
/*
A Formula represents a function that will be execution given the input parameters.
*/
type Formula func(vars map[string]interface{}) (float64, error)
func (*interpreter) execute(op operation, vars formulaVariables, functionRegistry *functionRegistry, constantRegistry *constantRegistry) (ret float64, err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New(r.(string))
}
}()
ret = execute(op, vars, functionRegistry, constantRegistry)
return ret, err
}
func (*interpreter) buildFormula(op operation, functionRegistry *functionRegistry, constantRegistry *constantRegistry) Formula {
return func(vars map[string]interface{}) (ret float64, err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New(r.(string))
}
}()
ret = execute(op, vars, functionRegistry, constantRegistry)
return ret, err
}
}
func execute(op operation, vars formulaVariables, functionRegistry *functionRegistry, constantRegistry *constantRegistry) float64 {
if op == nil {
panic("operation cannot be nil")
}
if cop, ok := op.(*constantOperation); ok {
if cop.Metadata.DataType == integer {
return toFloat64Panic(cop.Value)
} else {
return cop.Value.(float64)
}
} else if cop, ok := op.(*variableOperation); ok {
variableValue, err := vars.Get(cop.Name)
if err == nil {
return toFloat64Panic(variableValue)
} else {
panic("The variable '" + cop.Name + "' used is not defined.")
}
} else if cop, ok := op.(*multiplicationOperation); ok {
left := execute(cop.OperationOne, vars, functionRegistry, constantRegistry)
right := execute(cop.OperationTwo, vars, functionRegistry, constantRegistry)
return left * right
} else if cop, ok := op.(*addOperation); ok {
left := execute(cop.OperationOne, vars, functionRegistry, constantRegistry)
right := execute(cop.OperationTwo, vars, functionRegistry, constantRegistry)
return left + right
} else if cop, ok := op.(*subtractionOperation); ok {
left := execute(cop.OperationOne, vars, functionRegistry, constantRegistry)
right := execute(cop.OperationTwo, vars, functionRegistry, constantRegistry)
return left - right
} else if cop, ok := op.(*divisorOperation); ok {
left := execute(cop.Dividend, vars, functionRegistry, constantRegistry)
right := execute(cop.Divisor, vars, functionRegistry, constantRegistry)
return left / right
} else if cop, ok := op.(*moduloOperation); ok {
left := execute(cop.Dividend, vars, functionRegistry, constantRegistry)
right := execute(cop.Divisor, vars, functionRegistry, constantRegistry)
return math.Mod(left, right)
} else if cop, ok := op.(*exponentiationOperation); ok {
left := execute(cop.Base, vars, functionRegistry, constantRegistry)
right := execute(cop.Exponent, vars, functionRegistry, constantRegistry)
return math.Pow(left, right)
} else if cop, ok := op.(*unaryMinusOperation); ok {
arg := execute(cop.Operation, vars, functionRegistry, constantRegistry)
return -arg
} else if cop, ok := op.(*andOperation); ok {
left := execute(cop.OperationOne, vars, functionRegistry, constantRegistry)
right := execute(cop.OperationTwo, vars, functionRegistry, constantRegistry)
if left != 0 && right != 0 {
return 1.0
}
return 0.0
} else if cop, ok := op.(*orOperation); ok {
left := execute(cop.OperationOne, vars, functionRegistry, constantRegistry)
right := execute(cop.OperationTwo, vars, functionRegistry, constantRegistry)
if left != 0 || right != 0 {
return 1.0
}
return 0.0
} else if cop, ok := op.(*lessThanOperation); ok {
left := execute(cop.OperationOne, vars, functionRegistry, constantRegistry)
right := execute(cop.OperationTwo, vars, functionRegistry, constantRegistry)
if left < right {
return 1.0
}
return 0.0
} else if cop, ok := op.(*lessOrEqualThanOperation); ok {
left := execute(cop.OperationOne, vars, functionRegistry, constantRegistry)
right := execute(cop.OperationTwo, vars, functionRegistry, constantRegistry)
if left <= right {
return 1.0
}
return 0.0
} else if cop, ok := op.(*greaterThanOperation); ok {
left := execute(cop.OperationOne, vars, functionRegistry, constantRegistry)
right := execute(cop.OperationTwo, vars, functionRegistry, constantRegistry)
if left > right {
return 1.0
}
return 0.0
} else if cop, ok := op.(*greaterOrEqualThanOperation); ok {
left := execute(cop.OperationOne, vars, functionRegistry, constantRegistry)
right := execute(cop.OperationTwo, vars, functionRegistry, constantRegistry)
if left >= right {
return 1.0
}
return 0.0
} else if cop, ok := op.(*equalOperation); ok {
left := execute(cop.OperationOne, vars, functionRegistry, constantRegistry)
right := execute(cop.OperationTwo, vars, functionRegistry, constantRegistry)
if left == right {
return 1.0
}
return 0.0
} else if cop, ok := op.(*notEqualOperation); ok {
left := execute(cop.OperationOne, vars, functionRegistry, constantRegistry)
right := execute(cop.OperationTwo, vars, functionRegistry, constantRegistry)
if left != right {
return 1.0
}
return 0.0
} else if cop, ok := op.(*functionOperation); ok {
fn, _ := functionRegistry.get(cop.Name)
arguments := make([]interface{}, len(cop.Arguments))
for idx, fnParam := range cop.Arguments {
arg := execute(fnParam, vars, functionRegistry, constantRegistry)
arguments[idx] = arg
}
ret, err := runDelegate(fn, arguments)
if err != nil {
panic(err.Error())
}
return ret
}
panic(fmt.Sprintf("not implemented %T", op))
}
func runDelegate(fn *functionInfo, arguments []interface{}) (ret float64, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("function '%s': runtime error (%T)", fn.name, r)
}
}()
ret = fn.function(arguments...)
return ret, err
}