From 2cc58c3c9ee3faf3c393b4554defc1a8c62183b8 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 19 May 2020 16:50:20 +0300 Subject: [PATCH] compiler: allow to declare multiple compound types in a var decl --- pkg/compiler/codegen.go | 20 ++++++-------------- pkg/compiler/global_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index da8599e53..c100c1d81 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -360,27 +360,19 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { } c.registerDebugVariable(id.Name, t.Type) } - if len(t.Values) != 0 { - for i, val := range t.Values { - ast.Walk(c, val) - c.emitStoreVar(t.Names[i].Name) - } - } else { - typ := c.typeOf(t.Type) - if isCompoundSlice(typ) { + for i := range t.Names { + if len(t.Values) != 0 { + ast.Walk(c, t.Values[i]) + } else if typ := c.typeOf(t.Type); isCompoundSlice(typ) { emit.Opcode(c.prog.BinWriter, opcode.PUSH0) emit.Opcode(c.prog.BinWriter, opcode.NEWARRAY) - c.emitStoreVar(t.Names[0].Name) } else if s, ok := typ.Underlying().(*types.Struct); ok { emit.Int(c.prog.BinWriter, int64(s.NumFields())) emit.Opcode(c.prog.BinWriter, opcode.NEWSTRUCT) - c.emitStoreVar(t.Names[0].Name) } else { - for _, id := range t.Names { - c.emitDefault(t.Type) - c.emitStoreVar(id.Name) - } + c.emitDefault(t.Type) } + c.emitStoreVar(t.Names[i].Name) } } } diff --git a/pkg/compiler/global_test.go b/pkg/compiler/global_test.go index 277681ccd..190d50fd5 100644 --- a/pkg/compiler/global_test.go +++ b/pkg/compiler/global_test.go @@ -43,3 +43,15 @@ func TestMultiDeclarationLocal(t *testing.T) { }` eval(t, src, big.NewInt(6)) } + +func TestMultiDeclarationLocalCompound(t *testing.T) { + src := `package foo + func Main() int { + var a, b, c []int + a = append(a, 1) + b = append(b, 2) + c = append(c, 3) + return a[0] + b[0] + c[0] + }` + eval(t, src, big.NewInt(6)) +}