compiler: support range loops with value variables

Closes #958.
This commit is contained in:
Evgenii Stratonikov 2020-05-19 18:07:29 +03:00
parent b126056f04
commit d0735257ce
3 changed files with 21 additions and 18 deletions

View file

@ -883,13 +883,6 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
return nil
case *ast.RangeStmt:
// currently only simple for-range loops are supported
// for i := range ...
if n.Value != nil {
c.prog.Err = errors.New("range loops with value variable are not supported")
return nil
}
start, label := c.generateLabel(labelStart)
end := c.newNamedLabel(labelEnd, label)
post := c.newNamedLabel(labelPost, label)
@ -914,6 +907,11 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
emit.Syscall(c.prog.BinWriter, "Neo.Iterator.Key")
c.emitStoreVar(n.Key.(*ast.Ident).Name)
}
if n.Value != nil {
emit.Opcode(c.prog.BinWriter, opcode.DUP)
emit.Syscall(c.prog.BinWriter, "Neo.Enumerator.Value")
c.emitStoreVar(n.Value.(*ast.Ident).Name)
}
ast.Walk(c, n.Body)