Add SequenceNode to compiler

This commit is contained in:
Anton Medvedev
2025-03-08 12:46:59 +01:00
parent 3a6c36ef12
commit 5aada75b87
3 changed files with 37 additions and 0 deletions
+11
View File
@@ -266,6 +266,8 @@ func (c *compiler) compile(node ast.Node) {
c.PointerNode(n)
case *ast.VariableDeclaratorNode:
c.VariableDeclaratorNode(n)
case *ast.SequenceNode:
c.SequenceNode(n)
case *ast.ConditionalNode:
c.ConditionalNode(n)
case *ast.ArrayNode:
@@ -1142,6 +1144,15 @@ func (c *compiler) VariableDeclaratorNode(node *ast.VariableDeclaratorNode) {
c.endScope()
}
func (c *compiler) SequenceNode(node *ast.SequenceNode) {
for i, n := range node.Nodes {
c.compile(n)
if i < len(node.Nodes)-1 {
c.emit(OpPop)
}
}
}
func (c *compiler) beginScope(name string, index int) {
c.scopes = append(c.scopes, scope{name, index})
}
+18
View File
@@ -377,6 +377,24 @@ func TestCompile(t *testing.T) {
Arguments: []int{0, 0, 1, 12},
},
},
{
`1; 2; 3`,
vm.Program{
Constants: []any{
1,
2,
3,
},
Bytecode: []vm.Opcode{
vm.OpPush,
vm.OpPop,
vm.OpPush,
vm.OpPop,
vm.OpPush,
},
Arguments: []int{0, 0, 1, 0, 2},
},
},
}
for _, test := range tests {
+8
View File
@@ -1318,6 +1318,14 @@ func TestExpr(t *testing.T) {
`if "a" < "b" {let x = "a"; x} else {"abc"}`,
"a",
},
{
`1; 2; 3`,
3,
},
{
`let a = 1; Add(2, 2); let b = 2; a + b`,
3,
},
}
for _, tt := range tests {