Fix panic for empty statement block

This commit is contained in:
Marc Vertes
2018-10-01 18:30:41 +02:00
parent 56bbb4d846
commit 81232a1fab
5 changed files with 46 additions and 4 deletions

View File

@@ -2,7 +2,10 @@ package main
import "fmt"
var samples = []int{}
var (
samples = []int{}
b = 1
)
func main() {
samples = append(samples, 1)

11
_test/fun4.go Normal file
View File

@@ -0,0 +1,11 @@
package main
func f() {}
func main() {
f()
println("ok")
}
// Output:
// ok

View File

@@ -9,3 +9,8 @@ func Foo() {
func F1() {
fmt.Println("SomeString:", SomeString)
}
//func F2() {
//var buf [SomeInt]byte
//fmt.Println("buf:", buf)
//}

View File

@@ -331,7 +331,9 @@ func (interp *Interpreter) Cfg(root *Node) []*Node {
case BlockStmt:
wireChild(n)
n.findex = n.child[len(n.child)-1].findex
if len(n.child) > 0 {
n.findex = n.child[len(n.child)-1].findex
}
scope = scope.pop()
case ConstDecl:
@@ -450,7 +452,7 @@ func (interp *Interpreter) Cfg(root *Node) []*Node {
n.fsize = 0
}
} else {
log.Println(n.index, "resolve method")
log.Println(n.index, "unresolve call")
// method must be resolved here due to declaration after use
}
n.child[0].findex = -1 // To force reading value from node instead of frame (methods)

View File

@@ -292,7 +292,10 @@ package main
import "fmt"
var samples = []int{}
var (
samples = []int{}
b = 1
)
func main() {
samples = append(samples, 1)
@@ -916,6 +919,24 @@ func main() {
// 18
}
func Example_fun4() {
src := `
package main
func f() {}
func main() {
f()
println("ok")
}
`
i := NewInterpreter(Opt{Entry: "main"})
i.Eval(src)
// Output:
// ok
}
func Example_goroutine() {
src := `
package main