mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 03:41:34 +00:00
compiler: support for loops with no init/post condition
Make it possible to use `for` loop with a single condition.
This commit is contained in:
parent
01e16e68ad
commit
328267ca6f
2 changed files with 33 additions and 2 deletions
|
@ -585,7 +585,9 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
|
|||
)
|
||||
|
||||
// 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.
|
||||
c.setLabel(fstart)
|
||||
|
@ -596,7 +598,9 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
|
|||
|
||||
// Walk body followed by the iterator (post stmt).
|
||||
ast.Walk(c, n.Body)
|
||||
ast.Walk(c, n.Post)
|
||||
if n.Post != nil {
|
||||
ast.Walk(c, n.Post)
|
||||
}
|
||||
|
||||
// Jump back to condition.
|
||||
emitJmp(c.prog.BinWriter, opcode.JMP, int16(fstart))
|
||||
|
|
|
@ -370,3 +370,30 @@ func TestForLoopBigIter(t *testing.T) {
|
|||
`
|
||||
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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue