compiler: process index expresstions in a generic way

All type errors such as string index in slice will be catched during parse phase.
This commit is contained in:
Evgenii Stratonikov 2020-05-19 15:35:57 +03:00
parent aeaa4a8210
commit 87f6ba34db

View file

@ -10,7 +10,6 @@ import (
"go/types" "go/types"
"math" "math"
"sort" "sort"
"strconv"
"strings" "strings"
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "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]) ast.Walk(c, n.Rhs[i])
name := t.X.(*ast.Ident).Name name := t.X.(*ast.Ident).Name
c.emitLoadVar(name) c.emitLoadVar(name)
switch ind := t.Index.(type) { ast.Walk(c, t.Index)
case *ast.BasicLit: emit.Opcode(c.prog.BinWriter, opcode.ROT)
indexStr := ind.Value emit.Opcode(c.prog.BinWriter, opcode.SETITEM)
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
}
} }
} }
return nil 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. // Walk the expression, this could be either an Ident or SelectorExpr.
// This will load local whatever X is. // This will load local whatever X is.
ast.Walk(c, n.X) ast.Walk(c, n.X)
ast.Walk(c, n.Index)
switch n.Index.(type) {
case *ast.BasicLit:
c.emitLoadConst(c.typeAndValueOf(n.Index))
default:
ast.Walk(c, n.Index)
}
emit.Opcode(c.prog.BinWriter, opcode.PICKITEM) // just pickitem here emit.Opcode(c.prog.BinWriter, opcode.PICKITEM) // just pickitem here
return nil return nil