Skip to content

Commit 4ae7e34

Browse files
committed
Add SequenceNode to compiler
1 parent 8076723 commit 4ae7e34

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

compiler/compiler.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ func (c *compiler) compile(node ast.Node) {
266266
c.PointerNode(n)
267267
case *ast.VariableDeclaratorNode:
268268
c.VariableDeclaratorNode(n)
269+
case *ast.SequenceNode:
270+
c.SequenceNode(n)
269271
case *ast.ConditionalNode:
270272
c.ConditionalNode(n)
271273
case *ast.ArrayNode:
@@ -1142,6 +1144,15 @@ func (c *compiler) VariableDeclaratorNode(node *ast.VariableDeclaratorNode) {
11421144
c.endScope()
11431145
}
11441146

1147+
func (c *compiler) SequenceNode(node *ast.SequenceNode) {
1148+
for i, n := range node.Nodes {
1149+
c.compile(n)
1150+
if i < len(node.Nodes)-1 {
1151+
c.emit(OpPop)
1152+
}
1153+
}
1154+
}
1155+
11451156
func (c *compiler) beginScope(name string, index int) {
11461157
c.scopes = append(c.scopes, scope{name, index})
11471158
}

compiler/compiler_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,24 @@ func TestCompile(t *testing.T) {
377377
Arguments: []int{0, 0, 1, 12},
378378
},
379379
},
380+
{
381+
`1; 2; 3`,
382+
vm.Program{
383+
Constants: []any{
384+
1,
385+
2,
386+
3,
387+
},
388+
Bytecode: []vm.Opcode{
389+
vm.OpPush,
390+
vm.OpPop,
391+
vm.OpPush,
392+
vm.OpPop,
393+
vm.OpPush,
394+
},
395+
Arguments: []int{0, 0, 1, 0, 2},
396+
},
397+
},
380398
}
381399

382400
for _, test := range tests {

expr_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,14 @@ func TestExpr(t *testing.T) {
13181318
`if "a" < "b" {let x = "a"; x} else {"abc"}`,
13191319
"a",
13201320
},
1321+
{
1322+
`1; 2; 3`,
1323+
3,
1324+
},
1325+
{
1326+
`let a = 1; Add(2, 2); let b = 2; a + b`,
1327+
3,
1328+
},
13211329
}
13221330

13231331
for _, tt := range tests {

0 commit comments

Comments
 (0)