-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
compiler.go
981 lines (834 loc) · 20.4 KB
/
compiler.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
// This file contains the code which walks the AST, which was created by
// the parser, and generates our bytecode-program, along with appropriate
// constants.
package evalfilter
import (
"encoding/binary"
"fmt"
"sort"
"github.com/skx/evalfilter/v2/ast"
"github.com/skx/evalfilter/v2/code"
"github.com/skx/evalfilter/v2/environment"
"github.com/skx/evalfilter/v2/object"
)
// compile is core-code for converting the AST into a series of bytecodes.
func (e *Eval) compile(node ast.Node) error {
switch node := node.(type) {
case *ast.Program:
for _, s := range node.Statements {
err := e.compile(s)
if err != nil {
return err
}
}
case *ast.BlockStatement:
for _, s := range node.Statements {
err := e.compile(s)
if err != nil {
return err
}
}
case *ast.BooleanLiteral:
if node.Value {
e.emit(code.OpTrue)
} else {
e.emit(code.OpFalse)
}
case *ast.FloatLiteral:
str := &object.Float{Value: node.Value}
e.emit(code.OpConstant, e.addConstant(str))
case *ast.IntegerLiteral:
// Get the value of the literal
v := node.Value
// If this is an integer between 0 & 65535 we
// can push it naturally.
if v == int64(float64(v)) && v >= 0 && v <= 65534 {
e.emit(code.OpPush, int(v))
} else {
//
// Otherwise we emit it as a constant
// to our pool.
//
integer := &object.Integer{Value: node.Value}
e.emit(code.OpConstant, e.addConstant(integer))
}
case *ast.StringLiteral:
str := &object.String{Value: node.Value}
e.emit(code.OpConstant, e.addConstant(str))
case *ast.RegexpLiteral:
// The regexp body
val := node.Value
// The regexp flags
if node.Flags != "" {
// Which we pretend were part of the body
// because that is what Golang expects.
val = "(?" + node.Flags + ")" + val
}
// The value + flags
reg := &object.Regexp{Value: val}
e.emit(code.OpConstant, e.addConstant(reg))
case *ast.ArrayLiteral:
for _, el := range node.Elements {
err := e.compile(el)
if err != nil {
return err
}
}
e.emit(code.OpArray, len(node.Elements))
case *ast.HashLiteral:
keys := []ast.Expression{}
// get the keys
for k := range node.Pairs {
keys = append(keys, k)
}
// sort them
sort.Slice(keys, func(i, j int) bool {
return keys[i].String() < keys[j].String()
})
// for each key + value compile them
for _, k := range keys {
err := e.compile(k)
if err != nil {
return err
}
err = e.compile(node.Pairs[k])
if err != nil {
return err
}
}
// Now the number of key+values we've saved
e.emit(code.OpHash, len(node.Pairs)*2)
case *ast.ReturnStatement:
err := e.compile(node.ReturnValue)
if err != nil {
return err
}
e.emit(code.OpReturn)
case *ast.ExpressionStatement:
err := e.compile(node.Expression)
if err != nil {
return err
}
case *ast.InfixExpression:
err := e.compile(node.Left)
if err != nil {
return err
}
err = e.compile(node.Right)
if err != nil {
return err
}
switch node.Operator {
// mutators
// special-handling here:
// foo += 3;
// -> foo
// -> 3
// OpAdd
// OpSet foo
//
case "+=", "-=", "*=", "/=":
l, ok := node.Left.(*ast.Identifier)
if !ok {
return fmt.Errorf("left-most operand for %s must be an identifier", node.Operator)
}
if node.Operator == "+=" {
e.emit(code.OpAdd)
}
if node.Operator == "-=" {
e.emit(code.OpSub)
}
if node.Operator == "*=" {
e.emit(code.OpMul)
}
if node.Operator == "/=" {
e.emit(code.OpDiv)
}
str := &object.String{Value: l.Token.Literal}
e.emit(code.OpConstant, e.addConstant(str))
// And make it work.
e.emit(code.OpSet)
// maths
case "+":
e.emit(code.OpAdd)
case "-":
e.emit(code.OpSub)
case "*":
e.emit(code.OpMul)
case "/":
e.emit(code.OpDiv)
case "%":
e.emit(code.OpMod)
case "**":
e.emit(code.OpPower)
// comparisons
case "<":
e.emit(code.OpLess)
case "<=":
e.emit(code.OpLessEqual)
case ">":
e.emit(code.OpGreater)
case ">=":
e.emit(code.OpGreaterEqual)
case "==":
e.emit(code.OpEqual)
case "!=":
e.emit(code.OpNotEqual)
// special matches - regexp and array membership
case "~=":
e.emit(code.OpMatches)
case "!~":
e.emit(code.OpNotMatches)
case "in":
e.emit(code.OpArrayIn)
case ".":
e.emit(code.OpIndex)
// misc
case "..":
e.emit(code.OpRange)
// logical operators
case "&&":
e.emit(code.OpAnd)
case "||":
e.emit(code.OpOr)
default:
return fmt.Errorf("unknown operator %s", node.Operator)
}
case *ast.PrefixExpression:
err := e.compile(node.Right)
if err != nil {
return err
}
switch node.Operator {
case "!":
e.emit(code.OpBang)
case "-":
e.emit(code.OpMinus)
case "√":
e.emit(code.OpSquareRoot)
default:
return fmt.Errorf("unknown operator %s", node.Operator)
}
case *ast.PostfixExpression:
if node.Operator == "++" {
name := &object.String{Value: node.Token.Literal}
e.emit(code.OpInc, e.addConstant(name))
} else if node.Operator == "--" {
name := &object.String{Value: node.Token.Literal}
e.emit(code.OpDec, e.addConstant(name))
} else {
return fmt.Errorf("unknown postfix operator %s", node.Operator)
}
case *ast.LocalVariable:
// get the name and declare it as local.
e.emit(code.OpConstant, e.addConstant(&object.String{Value: node.Token.Literal}))
e.emit(code.OpLocal)
case *ast.ForeachStatement:
// Put the array on the stack
err := e.compile(node.Value)
if err != nil {
return err
}
// Reset the iteration state of the
// object - in case it has been iterated
// over in the post.
e.emit(code.OpIterationReset)
// Now we're at the start of our loop,
// we'll jump back to this point each
// time round.
start := len(e.instructions)
// Store the name of the index-variable.
str := &object.String{Value: node.Index}
e.emit(code.OpConstant, e.addConstant(str))
// Set the name of the body-variable
str = &object.String{Value: node.Ident}
e.emit(code.OpConstant, e.addConstant(str))
// Get the next piece of the iterable,
// or push False if that fails..
e.emit(code.OpIterationNext)
// jump end
end := e.emit(code.OpJumpIfFalse, 9999)
// Output the body
err = e.compile(node.Body)
if err != nil {
return nil
}
// repeat
e.emit(code.OpJump, start)
// back-patch
e.changeOperand(end, len(e.instructions))
// Finally add a "Nop" instruction, one that will not
// be optimized away.
//
// Because our "jmp END" will jump to an instruction which
// doesn't exist otherwise
e.emit(code.OpPlaceholder)
return nil
case *ast.FunctionDefinition:
//
// Hack: Reset the instructions.
//
// What we're doing here is ensuring that
// we start compiling each function-body as
// a new set of bytecode.
//
// Because things like `if` and our `iterators`
// have offsets in the generated bytecode we're
// going to end up with a chunk of bytecode
// for each function that starts from offset
// ZERO.
//
// So:
// blah ..
// blah ..
// function foo() { ... }
// blah ..
// blah ..
//
// Will _ALWAYS_ result in a new set of bytecode
// for the function that has an instruction pointer
// starting at offset ZERO. Regardless of the length
// of any preceding bytecode that has already been
// generated.
//
// This is hacky, but it is also safe, because we're
// single-threaded. We CANNOT compile N-function
// definitions at the same time. We'll only do so
// sequentially, and nothing else will mess with
// vm.instructions behind our back.
//
before := e.instructions
e.instructions = code.Instructions{}
// Compile the body of the function
err := e.compile(node.Body)
if err != nil {
// reset our instructions if we
// have an error.
//
// This is not required as errors
// will cause termination of our
// compiler-function but it feels
// like a neat thing to do.
e.instructions = before
return err
}
//
// Ensure that every function will return something.
//
// We're doing this because we'll be executing the
// compiled functions in (essentially) a child-VM.
//
// Our VM will terminate execution when it hits a
// return-statement - so this guarantees that will
// happen even in the case of a function like:
//
// function alive() { printf("We're alive now\n" ); }
//
// Without an explicit return there is .. no return
// value, and no clean termination. Instead we'd walk
// off the end of our bytecode array.
//
if len(e.instructions) == 0 ||
code.Opcode(e.instructions[len(e.instructions)-1]) != code.OpReturn {
e.emit(code.OpVoid)
e.emit(code.OpReturn)
}
// Save the bytecode away, remember we generated
// in our "internal" instruction space, which we
// swapped out for safety.
x := environment.UserFunction{Bytecode: e.instructions}
// Copy the function-arguments.
for _, nm := range node.Parameters {
x.Arguments = append(x.Arguments, nm.Value)
}
// And save this function-reference by name.
e.functions[node.Token.Literal] = x
// Now we can restore our bytecode to what it was
// before we started to deal with the body.
e.instructions = before
case *ast.IfExpression:
// Compile the expression.
err := e.compile(node.Condition)
if err != nil {
return err
}
//
// Assume the following input:
//
// if ( blah ) {
// // A
// }
// else {
// // B
// }
// // C
//
// We've now compiled `blah`, which is the expression
// above.
//
// So now we generate an `OpJumpIfFalse` to handle the case
// where the if statement is not true. (If the `blah` condition
// was true we just continue running it ..)
//
// Then the jump we're generating here will jump to either
// B - if there is an else-block - or C if there is not.
//
jumpNotTruthyPos := e.emit(code.OpJumpIfFalse, 9999)
//
// Compile the code in block A
//
err = e.compile(node.Consequence)
if err != nil {
return err
}
//
// Here we're calculating the length END of A.
//
// Because if the expression was false we want to
// jump to the START of B.
//
afterConsequencePos := len(e.instructions)
e.changeOperand(jumpNotTruthyPos, afterConsequencePos)
//
// If we don't have an `else` block then we're done.
//
// If we do then the end of the A-block needs to jump
// to C - to skip over the else-branch.
//
// If there is no else block then we're all good, we only
// needed to jump over the first block if the condition
// was not true - and we've already handled that case.
//
if node.Alternative != nil {
//
// Add a jump to the end of A - which will
// take us to C.
//
// Emit an `OpJump` with a bogus value
jumpPos := e.emit(code.OpJump, 9999)
//
// We're jumping to the wrong place here,
// so we have to cope with the updated target
//
// (We're in the wrong place because we just
// added a JUMP at the end of A)
//
afterConsequencePos = len(e.instructions)
e.changeOperand(jumpNotTruthyPos, afterConsequencePos)
//
// Compile the block
//
err := e.compile(node.Alternative)
if err != nil {
return err
}
//
// Now we change the offset to be C, which
// is the end of B.
//
afterAlternativePos := len(e.instructions)
e.changeOperand(jumpPos, afterAlternativePos)
}
//
// Hopefully that is clear.
//
// We end up with a simple case where there is no else-clause:
//
// if ..
// JUMP IF NOT B:
// body
// body
// B:
//
// And when there are both we have a pair of jumps:
//
// if ..
// JUMP IF NOT B:
// body
// body
// JUMP C:
//
// B: // else clause
// body
// body
// // fall-through
// C:
//
// Finally add a "Nop" instruction, one that will not
// be optimized away.
//
// Because our "jmp END" will jump to an instruction which
// doesn't exist otherwise
e.emit(code.OpPlaceholder)
case *ast.TernaryExpression:
//
// We'll have
//
// x = COND ? bar : baz
//
// We'll emit
//
// if ! COND jmp BAZ
// bar
// jmp END
// BAZ:
// baz
// END:
//
//
// Compile COND
//
err := e.compile(node.Condition)
if err != nil {
return err
}
//
// Jump to BAZ if this fails - placeholder
//
jumpNotTruthyPos := e.emit(code.OpJumpIfFalse, 9999)
//
// Compile the bar-code
//
err = e.compile(node.IfTrue)
if err != nil {
return err
}
//
// Jump to the end of this statement - placeholder
//
jumpEnd := e.emit(code.OpJump, 9999)
//
// Now we're at the start of the baz-handler
// we can update our `jump false` to come to this
// offset.
//
e.changeOperand(jumpNotTruthyPos, len(e.instructions))
//
// Compile the baz-code
//
err = e.compile(node.IfFalse)
if err != nil {
return err
}
//
// And now we know our ending position.
//
// Update our placeholder.
//
e.changeOperand(jumpEnd, len(e.instructions))
case *ast.SwitchExpression:
//
// So a switch statement will look like this:
//
// switch( foo ) {
// case "one" {
// one_code;
// ..
// }
// case "two" {
// two_code;
// ..
// }
// default {
// fail_code;
//
// }
//
// We want to compile that into:
//
// if ! foo eq one
// jmp two
// one_code
// jmp END
// two:
// if ! foo eq two
// jmp three
// two_code
// jmp END
// three:
// default:
// fail_code
// END:
//
//
// NOTE: This means that multiple cases cannot match.
//
// This is because at the end of each block
// we add a jump to the position AFTER the default
// block.
//
// TLDR: We run either ONE block, or the default.
// We cannot run multiple matches.
// is valid:
//
// switch (foo ) {
// // The first case wins.
// case 1 { printf("ONE\n"); }
// case 1 { printf("ONE - again\n"); }
// }
//
//
//
//
//
patches := []int{}
// We have to assemble each choice
for _, opt := range node.Choices {
// skipping the default-case, which we'll
// handle later.
if opt.Default {
continue
}
// Look at any expression we've got in this case.
for _, val := range opt.Expr {
// OK so we have an expression.
//
// Emit "test Value Expression"
// jump if false to end
// compile the thing we're testing
err := e.compile(node.Value)
if err != nil {
return err
}
// now compile the express
err = e.compile(val)
if err != nil {
return err
}
// the comparison
e.emit(code.OpCase)
// Now the jump over the block to run
// if it matches - for the case when it
// actually DOESN'T.
pos := e.emit(code.OpJumpIfFalse, 9999)
// finally the block
err = e.compile(opt.Block)
if err != nil {
return err
}
// And jump to after the default-block
//
end := e.emit(code.OpJump, 9999)
patches = append(patches, end)
// now we know the end of the block.
e.changeOperand(pos, len(e.instructions))
}
}
//
// Now the default-block
//
for _, opt := range node.Choices {
if !opt.Default {
continue
}
// Compile the block
err := e.compile(opt.Block)
if err != nil {
return err
}
}
// And now we're after the default - if there wasn't on,
// or after the last choice if here wasn't.
for _, offset := range patches {
e.changeOperand(offset, len(e.instructions))
}
// Finally add a "Nop" instruction, one that will not
// be optimized away.
//
// Because our "jmp END" will jump to an instruction which
// doesn't exist otherwise
e.emit(code.OpPlaceholder)
case *ast.WhileStatement:
//
// Record our starting position
//
cur := len(e.instructions)
//
// Compile the condition.
//
err := e.compile(node.Condition)
if err != nil {
return err
}
//
// Assume the following input:
//
// // A
// while ( cond ) {
// // B
// statement(s)
// // b2 -> jump to A to retest the condition
// }
// // C
//
// We've now compiled `cond`, which is the expression
// above.
//
// If the condition is false we jump to C, skipping the
// body.
//
// If the condition is true we fall through, and at
// B2 we jump back to A
//
//
// So now we generate an `OpJumpIfFalse` to handle the case
// where the condition is not true.
//
// This will jump to C, the position after the body.
//
jumpNotTruthyPos := e.emit(code.OpJumpIfFalse, 9999)
//
// Compile the code in the body
//
err = e.compile(node.Body)
if err != nil {
return err
}
//
// Append the b2 jump to retry the loop
//
e.emit(code.OpJump, cur)
//
// Change the jump to skip the block if the condition
// was false.
//
e.changeOperand(jumpNotTruthyPos, len(e.instructions))
// Finally add a "Nop" instruction, one that will not
// be optimized away.
//
// Because our "jmp END" will jump to an instruction which
// doesn't exist otherwise
e.emit(code.OpPlaceholder)
case *ast.AssignStatement:
// Get the value
err := e.compile(node.Value)
if err != nil {
return err
}
// Store the name
str := &object.String{Value: node.Name.String()}
e.emit(code.OpConstant, e.addConstant(str))
// And make it work.
e.emit(code.OpSet)
case *ast.Identifier:
str := &object.String{Value: node.Value}
e.emit(code.OpLookup, e.addConstant(str))
case *ast.CallExpression:
//
// call to print(1) will have the stack setup as:
//
// 1
// print
// call 1
//
// call to print( "steve", "kemp" ) will have:
//
// "steve"
// "kemp"
// "print"
// call 2
//
// i.e. We store the arguments on the stack and
// emit `OpCall NN` where NN is the number of arguments
// to pop and invoke the function with.
//
args := len(node.Arguments)
for _, a := range node.Arguments {
err := e.compile(a)
if err != nil {
return err
}
}
// call - has the string on the stack
str := &object.String{Value: node.Function.String()}
e.emit(code.OpConstant, e.addConstant(str))
// then a call instruction with the number of args.
e.emit(code.OpCall, args)
case *ast.IndexExpression:
err := e.compile(node.Left)
if err != nil {
return err
}
err = e.compile(node.Index)
if err != nil {
return err
}
e.emit(code.OpIndex)
default:
return fmt.Errorf("unknown node type %T %v", node, node)
}
return nil
}
// addConstant adds a constant to the pool
func (e *Eval) addConstant(obj object.Object) int {
//
// Look to see if the constant is present already
//
for i, c := range e.constants {
//
// If the existing constant has the same
// type and value - then return the offset.
//
if c.Type() == obj.Type() &&
c.Inspect() == obj.Inspect() {
return i
}
}
//
// Otherwise this is a distinct constant and should
// be added.
//
e.constants = append(e.constants, obj)
return len(e.constants) - 1
}
// emit generates a bytecode operation, and adds it to our program-array.
func (e *Eval) emit(op code.Opcode, operands ...int) int {
ins := make([]byte, 1)
ins[0] = byte(op)
if len(operands) == 1 {
// Make a buffer for the arg
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(operands[0]))
// append
ins = append(ins, b...)
}
posNewInstruction := len(e.instructions)
e.instructions = append(e.instructions, ins...)
return posNewInstruction
}
// changeOperand is designed to patch the operand of
// an instruction.
//
// This function is used to rewrite the target of our jump
// instructions in the handling of `if`, `while` and our
// ternary expressions.
func (e *Eval) changeOperand(opPos int, operand int) {
//
// We're pointed at the instruction,
// so our offset will be something like
//
// OpBlah
// opPos: OpJump target
//
// In terms of our bytecode that translates
// to our e.instructions containing something
// like this:
//
// ..
// e.instructions[opPos] = OpJump
// e.instructions[opPos+1] = arg1
// e.instructions[opPos+2] = arg2
// ..
//
// So we ignore the opcode, which doesn't
// change, and just update the argument-bytes
// in-place.
//
// Make a buffer for the arg, which we can
// use to split it into two bytes.
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(operand))
// replace the argument in-place
e.instructions[opPos+1] = b[0]
e.instructions[opPos+2] = b[1]
}