Merge pull request #618 from nspcc-dev/feature/for

compiler: support for loops with no init/post condition
This commit is contained in:
Roman Khimov 2020-01-23 13:19:55 +03:00 committed by GitHub
commit 67fe99b0ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View file

@ -585,7 +585,9 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
) )
// Walk the initializer and condition. // Walk the initializer and condition.
ast.Walk(c, n.Init) if n.Init != nil {
ast.Walk(c, n.Init)
}
// Set label and walk the condition. // Set label and walk the condition.
c.setLabel(fstart) c.setLabel(fstart)
@ -596,7 +598,9 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
// Walk body followed by the iterator (post stmt). // Walk body followed by the iterator (post stmt).
ast.Walk(c, n.Body) ast.Walk(c, n.Body)
ast.Walk(c, n.Post) if n.Post != nil {
ast.Walk(c, n.Post)
}
// Jump back to condition. // Jump back to condition.
emitJmp(c.prog.BinWriter, opcode.JMP, int16(fstart)) emitJmp(c.prog.BinWriter, opcode.JMP, int16(fstart))

View file

@ -370,3 +370,30 @@ func TestForLoopBigIter(t *testing.T) {
` `
eval(t, src, big.NewInt(99999)) eval(t, src, big.NewInt(99999))
} }
func TestForLoopNoInit(t *testing.T) {
src := `
package foo
func Main() int {
i := 0
for ; i < 10; i++ {
}
return i
}
`
eval(t, src, big.NewInt(10))
}
func TestForLoopNoPost(t *testing.T) {
src := `
package foo
func Main() int {
i := 0
for i < 10 {
i++
}
return i
}
`
eval(t, src, big.NewInt(10))
}