From b4bad11699478670a502be8e8ea9b94108b18730 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 19 May 2020 16:47:43 +0300 Subject: [PATCH] compiler: count the number of variables correctly --- pkg/compiler/analysis.go | 6 +++--- pkg/compiler/func_scope.go | 9 ++------- pkg/compiler/global_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/pkg/compiler/analysis.go b/pkg/compiler/analysis.go index 09681d69e..f096159e8 100644 --- a/pkg/compiler/analysis.go +++ b/pkg/compiler/analysis.go @@ -70,16 +70,16 @@ func (c *codegen) traverseGlobals(f ast.Node) { // countGlobals counts the global variables in the program to add // them with the stack size of the function. -func countGlobals(f ast.Node) (i int64) { +func countGlobals(f ast.Node) (i int) { ast.Inspect(f, func(node ast.Node) bool { - switch node.(type) { + switch n := node.(type) { // Skip all function declarations. case *ast.FuncDecl: return false // After skipping all funcDecls we are sure that each value spec // is a global declared variable or constant. case *ast.ValueSpec: - i++ + i += len(n.Names) } return true }) diff --git a/pkg/compiler/func_scope.go b/pkg/compiler/func_scope.go index d9e81916d..e5bb6e084 100644 --- a/pkg/compiler/func_scope.go +++ b/pkg/compiler/func_scope.go @@ -102,13 +102,8 @@ func (c *funcScope) countLocals() int { case *ast.ReturnStmt, *ast.IfStmt: size++ // This handles the inline GenDecl like "var x = 2" - case *ast.GenDecl: - switch t := n.Specs[0].(type) { - case *ast.ValueSpec: - if len(t.Values) > 0 { - size++ - } - } + case *ast.ValueSpec: + size += len(n.Names) } return true }) diff --git a/pkg/compiler/global_test.go b/pkg/compiler/global_test.go index ede29b849..277681ccd 100644 --- a/pkg/compiler/global_test.go +++ b/pkg/compiler/global_test.go @@ -19,3 +19,27 @@ func TestChangeGlobal(t *testing.T) { eval(t, src, big.NewInt(42)) } + +func TestMultiDeclaration(t *testing.T) { + src := `package foo + var a, b, c int + func Main() int { + a = 1 + b = 2 + c = 3 + return a + b + c + }` + eval(t, src, big.NewInt(6)) +} + +func TestMultiDeclarationLocal(t *testing.T) { + src := `package foo + func Main() int { + var a, b, c int + a = 1 + b = 2 + c = 3 + return a + b + c + }` + eval(t, src, big.NewInt(6)) +}