compiler: implement shadowing of global variables

Before introducing slots it was hard to change global variables
preserving changes across multiple function calls.
This commit implements such possibility.
Closes #638.
This commit is contained in:
Evgenii Stratonikov 2020-05-08 16:10:33 +03:00
parent 0cb6dc47e4
commit e21015233b
2 changed files with 24 additions and 0 deletions

View file

@ -410,6 +410,9 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
if !multiRet {
c.registerDebugVariable(t.Name, n.Rhs[i])
}
if t.Name != "_" {
c.scope.newLocal(t.Name)
}
fallthrough
default:
if i == 0 || !multiRet {

View file

@ -0,0 +1,21 @@
package compiler_test
import (
"math/big"
"testing"
)
func TestChangeGlobal(t *testing.T) {
src := `package foo
var a int
func Main() int {
setLocal()
set42()
setLocal()
return a
}
func set42() { a = 42 }
func setLocal() { a := 10; _ = a }`
eval(t, src, big.NewInt(42))
}