ast_test.go
4.4 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package astexpr
import (
"encoding/json"
"fmt"
"testing"
)
func TestExecA(t *testing.T) {
exp := "1+2"
exec(exp)
}
func TestExecB(t *testing.T) {
exp := "1+2-4"
exec(exp)
}
func TestExecC(t *testing.T) {
exp := "1+2-4*3-8"
exec(exp)
}
func TestExecD(t *testing.T) {
exp := "1+2-(4*3-8)"
exec(exp)
}
func TestExecE(t *testing.T) {
exp := "1+2-(4*3+(1-8))"
exec(exp)
}
func TestExecF(t *testing.T) {
exp := "1+(2-(4*3+(1-8)))"
exec(exp)
}
func TestExecG(t *testing.T) {
exp := "((1-2)*(3-8))*((((9+2222))))"
exec(exp)
}
func TestExecH(t *testing.T) {
exp := "0.8888-0.1 * 444 -0.2"
exec(exp)
}
func TestExecI(t *testing.T) {
exp := "0.8888-0.1 * (444 -0.2)"
exec(exp)
}
func TestExecJ(t *testing.T) {
exp := "1_234_567*2-3"
exec(exp)
}
func TestExecK(t *testing.T) {
exp := "2.3e4*4/3"
exec(exp)
}
func TestExecL(t *testing.T) {
exp := "-1+9-88"
exec(exp)
}
func TestExecM(t *testing.T) {
exp := "-1+9-88+(88)"
exec(exp)
}
func TestExecN(t *testing.T) {
exp := "-1+9-88+(-88)*666-1"
exec(exp)
}
func TestExecO(t *testing.T) {
exp := "-(1)+(3)-(-3)*7-((-3))"
exec(exp)
}
func TestExecP(t *testing.T) {
exp := "-(-9+3)"
exec(exp)
}
func TestExecQ(t *testing.T) {
exp := "2e-3*2+2e2+1"
exec(exp)
}
func TestExecR(t *testing.T) {
exp := "3.8 - 56 / (1-1) - 4"
exec(exp)
}
func TestExecS(t *testing.T) {
exp := "noerr(3.8 - 56 / (1-1) - 4)"
exec(exp)
}
func TestFunCaller(t *testing.T) {
funs := []struct {
Name string
Argc int
Fun func(expr ...ExprAST) float64
Exp string
R float64
}{
//{
// "double",
// 1,
// func(expr ...engine.ExprAST) float64 {
// return engine.ExprASTResult(expr[0]) * 2
// },
// "double(6)",
// 12,
//},
{
"sum",
-1,
nil,
"sum(if(100+10,table.a,20))",
10,
},
{
"sum",
-1,
nil,
"sum(if(100<10,table.a,20))",
10,
},
{
"sum",
-1,
nil,
"sum(if(100<10,table.a,20+30))",
10,
},
{
"sum",
-1,
nil,
"sum(if(table.a<table.b,table.a,20+30))",
10,
},
{
"sum",
-1,
nil,
"sum(if(table.a<=table.b,table.a,20+30))",
10,
},
}
for _, f := range funs {
if f.Fun != nil {
_ = RegFunction(f.Name, f.Argc, f.Fun)
}
r, err := Parse(f.Exp)
if err != nil {
}
if r != 0 {
}
}
}
func TestFunCaller2(t *testing.T) {
funs := []struct {
Name string
Exp []string
}{
{
"sum",
[]string{"sum(table.a)"},
},
{
"sumif",
[]string{"sumif(table.month,10,table.count)"},
},
{
"if",
[]string{"if(table.month>10,table.count1,table.count2)"},
},
{
"and",
[]string{"and(table.year=2011,table.month=6)"},
},
{
"or",
[]string{"or(table.year=2011,table.year=2012)"},
},
{
"month",
[]string{"month(\"1991-1-1\")"},
},
{
"year",
[]string{"year(\"1991-1-1\")"},
},
{
"round",
[]string{
"round(1.56)",
"round(table.a)",
},
},
{
"rounddown",
[]string{
"rounddown(1.56)",
"rounddown(table.a)",
},
},
{
"roundup",
[]string{
"roundup(1.56)",
"roundup(table.a)",
},
},
{
"count",
[]string{
"count(1.56)",
"count(table.a)",
},
},
{
"&",
[]string{
"table.a&table.b",
},
},
}
for _, f := range funs {
for _, exp := range f.Exp {
r, err := Parse(exp)
if err != nil {
t.Error(err)
}
if r != 0 {
}
}
}
}
func Parse(s string) (r float64, err error) {
toks, err := ParseToken(s)
if err != nil {
return 0, err
}
ast := NewAST(toks, s)
if ast.Err != nil {
return 0, ast.Err
}
ar := ast.ParseExpression()
if ast.Err != nil {
return 0, ast.Err
}
defer func() {
if e := recover(); e != nil {
err = e.(error)
}
}()
if ar != nil {
fmt.Printf("ExprAST: %+v\n", ar)
}
arData, _ := json.Marshal(ar)
fmt.Printf("%s\n", string(arData))
return 0, err
}
// call engine
func exec(exp string) {
// input text -> []token
toks, err := ParseToken(exp)
if err != nil {
fmt.Println("ERROR: " + err.Error())
return
}
// []token -> AST Tree
ast := NewAST(toks, exp)
if ast.Err != nil {
fmt.Println("ERROR: " + ast.Err.Error())
return
}
// AST builder
ar := ast.ParseExpression()
if ast.Err != nil {
fmt.Println("ERROR: " + ast.Err.Error())
return
}
fmt.Printf("ExprAST: %+v\n", ar)
// catch runtime errors
defer func() {
if e := recover(); e != nil {
fmt.Println("ERROR: ", e)
}
}()
// AST traversal -> result
r := ExprASTResult(ar)
fmt.Println("progressing ...\t", r)
fmt.Printf("%s = %v\n", exp, r)
}