-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter_test.go
96 lines (91 loc) · 1.2 KB
/
interpreter_test.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
package stack
import (
"testing"
"github.com/antlr4-go/antlr/v4"
)
func TestInterpreter_Run(t *testing.T) {
tests := []struct {
Name string
Code string
}{
{Name: "factorial.pcode", Code: `
.def fact: args=1, locals=0
; if n < 2 return 1
load 0
iconst 2
ilt
brf cont
iconst 1
ret
cont:
; return n * fact(n-1)
load 0
load 0
iconst 1
isub
call fact()
imul
ret
.def main: args=0, locals=0
; print fact(10)
iconst 10
call fact()
print
halt
`},
{Name: "loop.pcode", Code: `
.globals 2; n, i
; n = 10000
iconst 10000
gstore 0
; i = 0
iconst 0
gstore 1
; while i<n:
start:
gload 1
gload 0
ilt
brf done
; i = i + 1
gload 1
iconst 1
iadd
gstore 1
br start
done:
; print "looped "+n+" times."
sconst "done"
print
halt
`},
{Name: "struct.pcode", Code: `
; T t
.globals 1
.def main: args=0, locals=0
; t = new T()
struct 2
gstore 0
; t.x = 1
iconst 1
gload 0
fstore 0
; t.y = "foo"
sconst "foo"
gload 0
fstore 1
; print t.x
gload 0
fload 0
print
halt
`},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
i := NewInterpreter(antlr.NewInputStream(test.Code),
WithEnableTrace(), WithEnableDump(), WithDisassemble())
i.Run()
})
}
}