-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.go
548 lines (517 loc) · 12 KB
/
stmt.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package djson
import (
"bytes"
"errors"
"fmt"
"strconv"
)
var (
errExit = errors.New("__exit__")
)
type stmt struct {
next *stmt // next stmt should try, the priority of this stmt is always lower than next
name string // stmt name
handle func(val Value, ctx Context, token *Token) (handled bool, ret Value, err error) // match token and handle the stmt
scanner TokenScanner // scanner
opt *option // opt
}
func (e *stmt) Value(val Value, ctx Context) (ret Value, err error) {
terminal := e.next == nil
var matched, end, nextTried bool
var ht Value
for {
if end, err = e.scanner.Scan(); err != nil || end {
return
}
if terminal {
_, ret, err = e.handle(val, ctx, e.scanner.Token())
if e.opt.debug {
fmt.Printf("%s\n", e.name)
}
return
// try this level
} else if matched, ht, err = e.handle(val, ctx, e.scanner.Token()); err != nil {
return
} else if matched {
nextTried = true
val = ht
ret = val
if !e.opt.debug {
continue
}
fmt.Printf("%s\n", e.name)
} else if !nextTried {
// try higher priorities
if val, err = e.next.Value(val, ctx); err != nil {
return
}
ret = val
nextTried = true
} else {
return
}
}
}
func Exit() {
panic(errExit)
}
func Assign(scanner TokenScanner) *stmt {
e := &stmt{scanner: scanner, name: "Assign"}
e.handle = func(val Value, ctx Context, token *Token) (matched bool, ret Value, err error) {
if token.Type != TokenAssignation {
return
}
matched = true
e.scanner.Forward()
var right Value
if right, err = e.next.Value(NullValue(), ctx); err != nil {
return
}
if val.Type != ValueIdentifier {
err = errors.New("only identifier can assign to")
return
}
err = val.Value.(Identifier).Assign(right)
ret = right
return
}
return e
}
func Reduction(scanner TokenScanner) *stmt {
e := &stmt{scanner: scanner, name: "Reduction"}
e.handle = func(val Value, ctx Context, token *Token) (matched bool, ret Value, err error) {
if token.Type != TokenReduction {
return
}
matched = true
e.scanner.Forward()
var right Value
if right, err = e.next.Value(NullValue(), ctx); err != nil {
return
}
if val.Bool() {
ret = right
}
return
}
return e
}
func Or(scanner TokenScanner) *stmt {
e := &stmt{scanner: scanner, name: "Or"}
e.handle = func(val Value, ctx Context, token *Token) (matched bool, ret Value, err error) {
if token.Type != TokenOr {
return
}
matched = true
e.scanner.Forward()
if !val.Bool() {
return
}
var right Value
if right, err = e.next.Value(val, ctx); err != nil {
return
}
ret = val.Or(right)
return
}
return e
}
func And(scanner TokenScanner) *stmt {
e := &stmt{scanner: scanner, name: "And"}
e.handle = func(val Value, ctx Context, token *Token) (matched bool, ret Value, err error) {
if token.Type != TokenAnd {
return
}
matched = true
e.scanner.Forward()
if !val.Bool() {
return
}
var right Value
if right, err = e.next.Value(val, ctx); err != nil {
return
}
ret = val.And(right)
return
}
return e
}
func Compare(scanner TokenScanner) *stmt {
e := &stmt{scanner: scanner, name: "Compare"}
e.handle = func(val Value, ctx Context, token *Token) (matched bool, ret Value, err error) {
switch token.Type {
case TokenEqual, TokenNotEqual:
matched = true
scanner.Forward()
var right Value
right, err = e.next.Value(val, ctx)
if err != nil {
return
}
boo := val.Equal(right)
if TokenNotEqual == token.Type {
boo = !boo
}
ret = BoolValue(boo)
return
case TokenGreateThan, TokenGreateThanEqual, TokenLessThan, TokenLessThanEqual:
matched = true
scanner.Forward()
var right Value
right, err = e.next.Value(val, ctx)
if err != nil {
return
}
var com int
com, err = val.Compare(right)
if err != nil {
return
}
switch token.Type {
case TokenGreateThan:
ret = BoolValue(com > 0)
case TokenGreateThanEqual:
ret = BoolValue(com >= 0)
case TokenLessThan:
ret = BoolValue(com < 0)
case TokenLessThanEqual:
ret = BoolValue(com <= 0)
}
return
}
return
}
return e
}
func AddOrMinus(scanner TokenScanner) *stmt {
e := &stmt{scanner: scanner, name: "AddOrMinus"}
e.handle = func(val Value, ctx Context, token *Token) (matched bool, ret Value, err error) {
switch token.Type {
case TokenAddition:
matched = true
scanner.Forward()
var term Value
if term, err = e.next.Value(val, ctx); err != nil {
return
}
ret, err = val.Add(term)
return
case TokenMinus:
matched = true
scanner.Forward()
var term Value
if term, err = e.next.Value(val, ctx); err != nil {
return
}
ret, err = val.Minus(term)
return
}
return
}
return e
}
func MultiplyOrDevide(scanner TokenScanner) *stmt {
e := &stmt{scanner: scanner, name: "MultiplyOrDevide"}
e.handle = func(val Value, ctx Context, token *Token) (matched bool, ret Value, err error) {
switch token.Type {
case TokenMultiplication:
matched = true
scanner.Forward()
var term Value
if term, err = e.next.Value(val, ctx); err != nil {
return
}
ret, err = val.Multiply(term)
return
case TokenDevision:
matched = true
scanner.Forward()
var term Value
if term, err = e.next.Value(val, ctx); err != nil {
return
}
ret, err = val.Devide(term)
return
}
return
}
return e
}
func Mod(scanner TokenScanner) *stmt {
e := &stmt{scanner: scanner, name: "Mod"}
e.handle = func(val Value, ctx Context, token *Token) (matched bool, ret Value, err error) {
if token.Type != TokenMod {
return
}
matched = true
scanner.Forward()
var term Value
if term, err = e.next.Value(val, ctx); err != nil {
return
}
ret, err = val.Mod(term)
return
}
return e
}
func Call(scanner TokenScanner) *stmt {
e := &stmt{scanner: scanner, name: "Call"}
e.handle = func(val Value, ctx Context, token *Token) (matched bool, ret Value, err error) {
if !(val.Type == ValueIdentifier && e.scanner.Token().Type == TokenParenthesesOpen) {
return
}
matched = true
scanner.Forward()
identifier := val.Value.(Identifier)
e.scanner.PushEnds(TokenParenthesesClose)
defer e.scanner.PopEnds(TokenParenthesesClose)
if ret, err = identifier.Call(e.scanner, ctx); err != nil {
return
}
return
}
return e
}
func Dot(scanner TokenScanner) *stmt {
e := &stmt{scanner: scanner, name: "Dot"}
e.handle = func(val Value, ctx Context, token *Token) (matched bool, ret Value, err error) {
if token.Type != TokenDot {
return
}
matched = true
scanner.Forward()
var right Value
right, err = e.next.Value(NullValue(), ctx)
if err != nil {
return
}
if right.Type != ValueIdentifier {
err = errors.New("an identifier must be followed by dot")
}
right.Value.(Identifier).SetParent(val)
ret = right
return
}
return e
}
func Range(scanner TokenScanner) *stmt {
e := &stmt{scanner: scanner, name: "Range"}
e.handle = func(val Value, ctx Context, token *Token) (matched bool, ret Value, err error) {
if token.Type != TokenRange {
return
}
matched = true
scanner.Forward()
var begin, end int64
if inter, ok := val.Value.(Inter); ok {
if begin, err = inter.Int(); err != nil {
err = fmt.Errorf("can't convert to int for range begin: %w", err)
return
}
} else {
err = errors.New("range ... must follow an int and be followed by an int too")
return
}
var right Value
right, err = e.next.Value(val, ctx)
if err != nil {
return
}
if inter, ok := right.Value.(Inter); ok {
if end, err = inter.Int(); err != nil {
err = fmt.Errorf("can't convert to int for range end: %w", err)
return
}
} else {
err = errors.New("range ... must follow an int and be followed by an int too")
return
}
ret = RangeValue(int(begin), int(end))
return
}
return e
}
func Factor(scanner TokenScanner) *stmt {
e := &stmt{scanner: scanner, name: "Factor"}
e.handle = func(val Value, ctx Context, token *Token) (matched bool, ret Value, err error) {
scanner.Forward()
switch token.Type {
case TokenIdentifier:
ret = Value{Type: ValueIdentifier, Value: &identifier{
name: token.Raw,
vars: ctx.Copy(),
}}
case TokenExit:
Exit()
case TokenReturn:
ret = ReturnValue()
case TokenTrue:
ret = BoolValue(true)
case TokenFalse:
ret = BoolValue(false)
case TokenString:
ret = StringValue(token.Raw...)
case TokenNumber:
if bytes.Contains(token.Raw, []byte{'.'}) {
v, _ := strconv.ParseFloat(string(token.Raw), 64)
ret = FloatValue(v)
return
}
v, _ := strconv.ParseInt(string(token.Raw), 10, 64)
ret = IntValue(v)
case TokenParenthesesOpen:
e.scanner.PushEnds(TokenParenthesesClose)
defer e.scanner.PopEnds(TokenParenthesesClose)
sub := NewStmtExecutor(e.scanner, ctx)
if err = sub.Execute(); err == nil {
ret = sub.value
}
case TokenBracketsOpen:
sub := newArrayExecutor(e.scanner, ctx)
if err = sub.execute(); err == nil {
ret = sub.value
}
case TokenBraceOpen:
sub := newObjectExecutor(e.scanner, ctx)
if err = sub.execute(); err == nil {
ret = sub.value
}
default:
err = fmt.Errorf("unexpected token [%s] at %d, %d", token.Name(), token.Row, token.Col)
}
return
}
return e
}
type stmts []*stmt
func (es stmts) init(opt *option) *stmt {
for i := 0; i < len(es); i++ {
es[i].opt = opt
if i < len(es)-1 {
es[i].next = es[i+1]
}
}
return es[0]
}
type stmtExecutor struct {
scanner TokenScanner
expr *stmt
value Value
ctx Context
opt *option
exited bool
}
type option struct {
debug bool
}
type StmtOption func(opt *option)
func Debug() StmtOption {
return func(opt *option) {
opt.debug = true
}
}
func NewStmtExecutor(scanner TokenScanner, ctx Context, opts ...StmtOption) *stmtExecutor {
es := stmts([]*stmt{
Assign(scanner),
Reduction(scanner),
Or(scanner),
And(scanner),
Compare(scanner),
AddOrMinus(scanner),
MultiplyOrDevide(scanner),
Mod(scanner),
Call(scanner),
Dot(scanner),
Range(scanner),
Factor(scanner),
})
opt := &option{}
for _, apply := range opts {
apply(opt)
}
return &stmtExecutor{
expr: es.init(opt), scanner: scanner, opt: opt,
ctx: ctx,
}
}
type stmtExecOption struct {
endWhen []TokenType
val Value
valSetted bool
}
func For(val Value) func(opt *stmtExecOption) {
return func(opt *stmtExecOption) {
opt.val = val
opt.valSetted = true
}
}
func EndWhen(tt ...TokenType) func(opt *stmtExecOption) {
return func(opt *stmtExecOption) {
opt.endWhen = tt
}
}
func (ns *stmtExecutor) Execute(applyOpt ...func(opt *stmtExecOption)) (err error) {
defer func() {
if ns.scanner.Token().Type != TokenEOF {
ns.scanner.Forward()
}
}()
defer func() {
if r := recover(); r != nil {
if err, ok := r.(error); ok && errors.Is(err, errExit) {
ns.exited = true
return
}
panic(r)
}
}()
var end bool
var returned bool
var val Value
if ns.ctx == nil {
ns.ctx = NewContext()
}
var opt stmtExecOption
for _, apply := range applyOpt {
apply(&opt)
}
if opt.valSetted {
ns.value = opt.val
}
if len(opt.endWhen) > 0 {
ns.scanner.PushEnds(opt.endWhen...)
defer ns.scanner.PopEnds(opt.endWhen...)
}
ns.ctx.PushScope()
defer ns.ctx.PopScope()
for {
if end, err = ns.scanner.Scan(); end || err != nil {
return
}
if ns.scanner.Token().Type == TokenSemicolon {
ns.scanner.Forward()
continue
}
if returned {
// drop all the rest token
continue
}
if val, err = ns.expr.Value(val, ns.ctx); err != nil {
return
}
if val.Type == ValueReturn {
returned = true
continue
}
ns.value = val
}
}
func (ns *stmtExecutor) AssignVar(name []byte, val Value) {
ns.ctx.Assign(name, val)
}
func (ns *stmtExecutor) Exited() bool {
return ns.exited
}
func (ns *stmtExecutor) Value() Value {
return ns.value
}