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:
parent
0cb6dc47e4
commit
e21015233b
2 changed files with 24 additions and 0 deletions
|
@ -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 {
|
||||
|
|
21
pkg/compiler/global_test.go
Normal file
21
pkg/compiler/global_test.go
Normal 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))
|
||||
}
|
Loading…
Reference in a new issue