From 87f6ba34dbdecd9241c3f4d697112f4254fa2459 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 19 May 2020 15:35:57 +0300 Subject: [PATCH] compiler: process index expresstions in a generic way All type errors such as string index in slice will be catched during parse phase. --- pkg/compiler/codegen.go | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 5720f948b..a1cdc3d27 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -10,7 +10,6 @@ import ( "go/types" "math" "sort" - "strconv" "strings" "github.com/nspcc-dev/neo-go/pkg/encoding/address" @@ -446,23 +445,9 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { ast.Walk(c, n.Rhs[i]) name := t.X.(*ast.Ident).Name c.emitLoadVar(name) - switch ind := t.Index.(type) { - case *ast.BasicLit: - indexStr := ind.Value - index, err := strconv.Atoi(indexStr) - if err != nil { - c.prog.Err = fmt.Errorf("failed to convert slice index to integer") - return nil - } - c.emitStoreStructField(index) - case *ast.Ident: - c.emitLoadVar(ind.Name) - emit.Opcode(c.prog.BinWriter, opcode.ROT) - emit.Opcode(c.prog.BinWriter, opcode.SETITEM) - default: - c.prog.Err = fmt.Errorf("unsupported index expression") - return nil - } + ast.Walk(c, t.Index) + emit.Opcode(c.prog.BinWriter, opcode.ROT) + emit.Opcode(c.prog.BinWriter, opcode.SETITEM) } } return nil @@ -832,14 +817,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { // Walk the expression, this could be either an Ident or SelectorExpr. // This will load local whatever X is. ast.Walk(c, n.X) - - switch n.Index.(type) { - case *ast.BasicLit: - c.emitLoadConst(c.typeAndValueOf(n.Index)) - default: - ast.Walk(c, n.Index) - } - + ast.Walk(c, n.Index) emit.Opcode(c.prog.BinWriter, opcode.PICKITEM) // just pickitem here return nil