-
Notifications
You must be signed in to change notification settings - Fork 171
/
vm_test.go
284 lines (250 loc) · 4.71 KB
/
vm_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
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
package vm
import (
"fmt"
"path/filepath"
"testing"
"github.com/goby-lang/goby/compiler/bytecode"
"github.com/goby-lang/goby/compiler/lexer"
"github.com/goby-lang/goby/compiler/parser"
)
func TestVM_REPLExec(t *testing.T) {
tests := []struct {
inputs []string
expected interface{}
}{
{
[]string{
`
x = [1, 2, 3]
y = 0
`,
`
x.each do |i|
y += i
if i == 2
break
end
end
`,
`y`,
},
3,
},
{
[]string{
`
a, b = [3, 6]
a + b
`,
},
9,
}, {
[]string{
`
a, _, c = [1, 2, 3]
c
`,
},
3,
}, {
[]string{`
def foo(x)
yield(x + 10)
end
def bar(y)
foo(y) do |f|
yield(f)
end
end
def baz(z)
bar(z + 100) do |b|
yield(b)
end
end
a = 0
baz(100) do |b|
a = b
end
a
`,
`
class Foo
def bar
100
end
end
module Baz
class Bar
def bar
Foo.new.bar
end
end
end
`,
`
Baz::Bar.new.bar + a
`,
},
310,
},
{
[]string{`
def foo
123
end
`, `
foo
`},
123},
{
[]string{
`
class Foo
def bar(x)
x + 10
end
end
`,
`
Foo.new.bar(90)
`},
100},
{
[]string{
`
def foo
123
end
`,
`
foo
`,
`
def foo
345
end
`,
`
foo
`,
}, 345},
}
for i, test := range tests {
v := initTestVM()
v.InitForREPL()
// Initialize parser, lexer is not important here
p := parser.New(lexer.New(""))
p.Mode = parser.REPLMode
program, _ := p.ParseProgram()
// Initialize code generator, and it will behavior a little different in REPL mode.
g := bytecode.NewGenerator()
g.REPL = true
g.InitTopLevelScope(program)
for _, input := range test.inputs {
p := parser.New(lexer.New(input))
p.Mode = parser.REPLMode
program, _ := p.ParseProgram()
sets := g.GenerateInstructions(program.Statements)
v.REPLExec(sets)
}
evaluated := v.GetExecResult()
VerifyExpected(t, i, evaluated, test.expected)
// Because REPL should maintain a base call frame so that the whole program won't exit
v.checkCFP(t, i, 1)
}
}
func TestVM_REPLExecFail(t *testing.T) {
tests := []struct {
inputs []string
expected string
}{
{
[]string{
`raise ArgumentError`,
},
"ArgumentError: ArgumentError",
},
{
[]string{
"NonExistentBuiltinMethod",
},
"NameError: uninitialized constant NonExistentBuiltinMethod",
},
{
[]string{
"Hash.notExist",
},
"NoMethodError: Undefined Method 'notExist' for Hash",
},
}
for i, test := range tests {
v := initTestVM()
v.InitForREPL()
// Initialize parser, lexer is not important here
p := parser.New(lexer.New(""))
p.Mode = parser.REPLMode
program, _ := p.ParseProgram()
// Initialize code generator, and it will behavior a little different in REPL mode.
g := bytecode.NewGenerator()
g.REPL = true
g.InitTopLevelScope(program)
for _, input := range test.inputs {
p := parser.New(lexer.New(input))
p.Mode = parser.REPLMode
// prevent parse errors from panicking tests
program, Err := p.ParseProgram()
if Err != nil {
t.Fatalf("At case %d unexpected parse error %q", i, Err.Message)
}
sets := g.GenerateInstructions(program.Statements)
v.REPLExec(sets)
}
evaluated := v.GetExecResult()
if evaluated.ToString() != test.expected {
t.Fatalf("At case %d expected %s got %s", i,
test.expected, evaluated.ToString())
}
// Because REPL should maintain a base call frame so that the whole program won't exit
v.checkCFP(t, i, 1)
}
}
func TestAutoIncrementLocalVariable(t *testing.T) {
input := `
a1 = 1
a2 = 1
a3 = 1
a4 = 1
a5 = 1
# defaults to 5 variables, auto increment must work to set 6th
a6 = 10
(a1 + a2 + a3 + a4 + a5 + a6).to_s
`
expected := `15`
vm := initTestVM()
evaluated := vm.testEval(t, input, getFilename())
VerifyExpected(t, i, evaluated, expected)
vm.checkCFP(t, i, 0)
vm.checkSP(t, i, 1)
}
func TestLoadingGobyLibraryFail(t *testing.T) {
vm := initTestVM()
libFileFullPath := filepath.Join(vm.libPath, "_library_not_existing.gb")
expectedErrorMessage := fmt.Sprintf("open %s: no such file or directory", libFileFullPath)
err := vm.mainThread.execGobyLib("_library_not_existing.gb")
if err.Error() != expectedErrorMessage {
t.Errorf("Unexpected error message: %s", err.Error())
}
}
func (v *VM) checkCFP(t *testing.T, index, expectedCFP int) {
t.Helper()
if v.mainThread.callFrameStack.pointer != expectedCFP {
t.Errorf("At case %d expect main thread's cfp to be %d. got: %d", index, expectedCFP, v.mainThread.callFrameStack.pointer)
}
}
func (v *VM) checkSP(t *testing.T, index, expectedSp int) {
t.Helper()
if v.mainThread.Stack.pointer != expectedSp {
fmt.Println(v.mainThread.Stack.inspect())
t.Errorf("At case %d expect main thread's sp to be %d. got: %d", index, expectedSp, v.mainThread.Stack.pointer)
}
}