Fix a case where a global symbol was overwritten

This commit is contained in:
Marc Vertes
2018-11-13 14:17:26 +01:00
parent 1d303762bc
commit 6637e21011
4 changed files with 32 additions and 12 deletions

View File

@@ -1,11 +1,8 @@
package main
func main() {
//dict := map[string]string{}
dict := map[string]string{"bidule": "machin", "truc": "bidule"}
//println(dict)
//dict["truc"] = "machin"
//println(dict)
dict := map[string]string{}
dict["truc"] = "machin"
println(dict["truc"])
}

View File

@@ -690,11 +690,18 @@ func (interp *Interpreter) Cfg(root *Node) []*Node {
case Ident:
if isKey(n) {
// Skip symbol creation/lookup for idents used as key
} else if isFuncArg(n) || isNewDefine(n) {
// Create a new symbol in current scope, type to be set by parent node
} else if isFuncArg(n) {
n.findex = scope.inc(interp)
scope.sym[n.ident] = &Symbol{index: scope.size, kind: Var, global: scope.global}
n.sym = scope.sym[n.ident]
} else if isNewDefine(n) {
// Create a new symbol in current scope, type to be set by parent node
// Note that global symbol should already be defined (gta)
if _, _, ok := scope.lookup(n.ident); !ok && !scope.global {
n.findex = scope.inc(interp)
scope.sym[n.ident] = &Symbol{index: scope.size, kind: Var, global: scope.global}
n.sym = scope.sym[n.ident]
}
} else if sym, level, ok := scope.lookup(n.ident); ok {
// Found symbol, populate node info
n.typ, n.findex, n.level = sym.typ, sym.index, level

View File

@@ -1549,11 +1549,8 @@ func Example_map3() {
package main
func main() {
//dict := map[string]string{}
dict := map[string]string{"bidule": "machin", "truc": "bidule"}
//println(dict)
//dict["truc"] = "machin"
//println(dict)
dict := map[string]string{}
dict["truc"] = "machin"
println(dict["truc"])
}
`
@@ -2437,6 +2434,24 @@ func main() {
// 1 2
}
func Example_ret4() {
src := `
package main
func r() int { return 1 }
func main() {
a := r()
println(a)
}
`
i := NewInterpreter(Opt{Entry: "main"}, "ret4.go")
i.Eval(src)
// Output:
// 1
}
func Example_run0() {
src := `
package main

View File

@@ -61,6 +61,7 @@ func (interp *Interpreter) run(n *Node, cf *Frame) {
f.data[i] = reflect.New(t).Elem()
}
}
//log.Println(n.index, "run", n.start.index)
runCfg(n.start, f)
}