diff --git a/_test/map3.go b/_test/map3.go index c8856eb3..4d0552d0 100644 --- a/_test/map3.go +++ b/_test/map3.go @@ -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"]) } diff --git a/interp/cfg.go b/interp/cfg.go index 82675dc2..bae640bd 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -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 diff --git a/interp/interp_test.go b/interp/interp_test.go index d048d220..7a3ea6d5 100644 --- a/interp/interp_test.go +++ b/interp/interp_test.go @@ -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 diff --git a/interp/run.go b/interp/run.go index 8a2a900f..0451b6d6 100644 --- a/interp/run.go +++ b/interp/run.go @@ -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) }