Skip to content

Commit 3eb3fca

Browse files
committed
Fix compile tests
1 parent c78443a commit 3eb3fca

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

compiler/compiler_test.go

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,33 @@ func TestCompile(t *testing.T) {
4444
`65535`,
4545
vm.Program{
4646
Constants: []interface{}{
47-
int(math.MaxUint16),
47+
math.MaxUint16,
4848
},
4949
Bytecode: []vm.Opcode{
50-
vm.OpPush, 0,
50+
vm.OpPush,
5151
},
52+
Arguments: []int{0},
5253
},
5354
},
5455
{
5556
`.5`,
5657
vm.Program{
5758
Constants: []interface{}{
58-
float64(.5),
59+
.5,
5960
},
6061
Bytecode: []vm.Opcode{
61-
vm.OpPush, 0,
62+
vm.OpPush,
6263
},
64+
Arguments: []int{0},
6365
},
6466
},
6567
{
6668
`true`,
6769
vm.Program{
6870
Bytecode: []vm.Opcode{
69-
vm.OpTrue, 0,
71+
vm.OpTrue,
7072
},
73+
Arguments: []int{0},
7174
},
7275
},
7376
{
@@ -77,8 +80,9 @@ func TestCompile(t *testing.T) {
7780
"string",
7881
},
7982
Bytecode: []vm.Opcode{
80-
vm.OpPush, 0,
83+
vm.OpPush,
8184
},
85+
Arguments: []int{0},
8286
},
8387
},
8488
{
@@ -88,10 +92,11 @@ func TestCompile(t *testing.T) {
8892
"string",
8993
},
9094
Bytecode: []vm.Opcode{
91-
vm.OpPush, 0,
92-
vm.OpPush, 0,
93-
vm.OpEqualString, 0,
95+
vm.OpPush,
96+
vm.OpPush,
97+
vm.OpEqualString,
9498
},
99+
Arguments: []int{0, 0, 0},
95100
},
96101
},
97102
{
@@ -101,33 +106,51 @@ func TestCompile(t *testing.T) {
101106
int64(1000000),
102107
},
103108
Bytecode: []vm.Opcode{
104-
vm.OpPush, 0,
105-
vm.OpPush, 0,
106-
vm.OpEqualInt, 0,
109+
vm.OpPush,
110+
vm.OpPush,
111+
vm.OpEqualInt,
107112
},
113+
Arguments: []int{0, 0, 0},
108114
},
109115
},
110116
{
111117
`-1`,
112118
vm.Program{
113119
Constants: []interface{}{-1},
114120
Bytecode: []vm.Opcode{
115-
vm.OpPush, 0,
121+
vm.OpPush,
116122
},
123+
Arguments: []int{0},
117124
},
118125
},
119126
{
120127
`true && true || true`,
121128
vm.Program{
122129
Bytecode: []vm.Opcode{
123-
vm.OpTrue, 0,
124-
vm.OpJumpIfFalse, 4,
125-
vm.OpPop, 0,
126-
vm.OpTrue, 0,
127-
vm.OpJumpIfTrue, 4,
128-
vm.OpPop, 0,
129-
vm.OpTrue, 0,
130+
vm.OpTrue,
131+
vm.OpJumpIfFalse,
132+
vm.OpPop,
133+
vm.OpTrue,
134+
vm.OpJumpIfTrue,
135+
vm.OpPop,
136+
vm.OpTrue,
130137
},
138+
Arguments: []int{0, 2, 0, 0, 2, 0, 0},
139+
},
140+
},
141+
{
142+
`true && (true || true)`,
143+
vm.Program{
144+
Bytecode: []vm.Opcode{
145+
vm.OpTrue,
146+
vm.OpJumpIfFalse,
147+
vm.OpPop,
148+
vm.OpTrue,
149+
vm.OpJumpIfTrue,
150+
vm.OpPop,
151+
vm.OpTrue,
152+
},
153+
Arguments: []int{0, 5, 0, 0, 2, 0, 0},
131154
},
132155
},
133156
{
@@ -140,8 +163,9 @@ func TestCompile(t *testing.T) {
140163
},
141164
},
142165
Bytecode: []vm.Opcode{
143-
vm.OpFetchEnvField, 0,
166+
vm.OpFetchEnvField,
144167
},
168+
Arguments: []int{0},
145169
},
146170
},
147171
{
@@ -158,10 +182,11 @@ func TestCompile(t *testing.T) {
158182
},
159183
},
160184
Bytecode: []vm.Opcode{
161-
vm.OpFetchEnvField, 0,
162-
vm.OpJumpIfNil, 2,
163-
vm.OpFetchField, 1,
185+
vm.OpFetchEnvField,
186+
vm.OpJumpIfNil,
187+
vm.OpFetchField,
164188
},
189+
Arguments: []int{0, 1, 1},
165190
},
166191
},
167192
{
@@ -178,10 +203,11 @@ func TestCompile(t *testing.T) {
178203
},
179204
},
180205
Bytecode: []vm.Opcode{
181-
vm.OpFetchEnvField, 0,
182-
vm.OpJumpIfNil, 2,
183-
vm.OpFetchField, 1,
206+
vm.OpFetchEnvField,
207+
vm.OpJumpIfNil,
208+
vm.OpFetchField,
184209
},
210+
Arguments: []int{0, 1, 1},
185211
},
186212
},
187213
{
@@ -199,11 +225,12 @@ func TestCompile(t *testing.T) {
199225
},
200226
},
201227
Bytecode: []vm.Opcode{
202-
vm.OpFetchEnvField, 0,
203-
vm.OpPush, 1,
204-
vm.OpFetch, 0,
205-
vm.OpFetchField, 2,
228+
vm.OpFetchEnvField,
229+
vm.OpPush,
230+
vm.OpFetch,
231+
vm.OpFetchField,
206232
},
233+
Arguments: []int{0, 1, 0, 2},
207234
},
208235
},
209236
}
@@ -223,9 +250,10 @@ func TestCompile_cast(t *testing.T) {
223250
1,
224251
},
225252
Bytecode: []vm.Opcode{
226-
vm.OpPush, 0,
227-
vm.OpCast, 1,
253+
vm.OpPush,
254+
vm.OpCast,
228255
},
256+
Arguments: []int{0, 1},
229257
}
230258

231259
tree, err := parser.Parse(input)

vm/program_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ func TestProgram_Disassemble(t *testing.T) {
1111
for op := vm.OpPush; op < vm.OpEnd; op++ {
1212
program := vm.Program{
1313
Constants: []interface{}{true},
14-
Bytecode: []vm.Opcode{op, 0},
14+
Bytecode: []vm.Opcode{op},
15+
Arguments: []int{0},
1516
}
1617
d := program.Disassemble()
1718
if strings.Contains(d, "\t0x") {

0 commit comments

Comments
 (0)