From 05ef9510554d0f3922605bc641c0bed0c0d7bb2d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 25 Aug 2020 10:22:58 +0300 Subject: [PATCH] compiler: support `iota` --- pkg/compiler/codegen.go | 7 ++++++- pkg/compiler/constant_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index a9cac98d8..e418a2693 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -408,7 +408,12 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { for _, spec := range n.Specs { vs := spec.(*ast.ValueSpec) for i := range vs.Names { - c.constMap[c.getIdentName("", vs.Names[i].Name)] = c.typeAndValueOf(vs.Values[i]) + info := c.buildInfo.program.Package(c.currPkg.Path()) + obj := info.Defs[vs.Names[i]] + c.constMap[c.getIdentName("", vs.Names[i].Name)] = types.TypeAndValue{ + Type: obj.Type(), + Value: obj.(*types.Const).Val(), + } } } return nil diff --git a/pkg/compiler/constant_test.go b/pkg/compiler/constant_test.go index 725ff1dba..42456659a 100644 --- a/pkg/compiler/constant_test.go +++ b/pkg/compiler/constant_test.go @@ -72,3 +72,17 @@ func TestGlobalsWithFunctionParams(t *testing.T) { ` eval(t, src, []byte("FOO")) } + +func TestIota(t *testing.T) { + src := `package foo + const ( + a = 2 << iota + b + + c = 11 + ) + func Main() int { + return a + b + c + }` + eval(t, src, big.NewInt(17)) +}