compiler: set variable index on first declaration

This way variables will have indices corresponding to their
order of appearance in a source file.
This commit is contained in:
Evgenii Stratonikov 2020-04-02 17:19:07 +03:00
parent 457e7e006a
commit da826522f8

View file

@ -287,23 +287,24 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
switch t := spec.(type) {
case *ast.ValueSpec:
for _, id := range t.Names {
c.scope.newLocal(id.Name)
c.registerDebugVariable(id.Name, t.Type)
}
if len(t.Values) != 0 {
for i, val := range t.Values {
ast.Walk(c, val)
l := c.scope.newLocal(t.Names[i].Name)
l := c.scope.loadLocal(t.Names[i].Name)
c.emitStoreLocal(l)
}
} else if c.isCompoundArrayType(t.Type) {
emit.Opcode(c.prog.BinWriter, opcode.PUSH0)
emit.Opcode(c.prog.BinWriter, opcode.NEWARRAY)
l := c.scope.newLocal(t.Names[0].Name)
l := c.scope.loadLocal(t.Names[0].Name)
c.emitStoreLocal(l)
} else if n, ok := c.isStructType(t.Type); ok {
emit.Int(c.prog.BinWriter, int64(n))
emit.Opcode(c.prog.BinWriter, opcode.NEWSTRUCT)
l := c.scope.newLocal(t.Names[0].Name)
l := c.scope.loadLocal(t.Names[0].Name)
c.emitStoreLocal(l)
}
}