mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Add SequenceNode to compiler
This commit is contained in:
@@ -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})
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user