Fix map check expression

This commit is contained in:
Marc Vertes
2018-06-26 18:27:14 +02:00
parent 19ca75ae2a
commit a062e34ba8
5 changed files with 56 additions and 4 deletions

View File

@@ -34,7 +34,6 @@ When developping/debugging, I'm running `gi` on a single script, using `-a` and
- [ ] diagnostics and proper error handling
- [ ] named output variables (test/ret1.gi)
- [ ] defer
- [ ] blank
- [ ] ellipsis
- [ ] method calls from runtime to interpreter
- [ ] forward declarations

12
_test/map5.go Normal file
View File

@@ -0,0 +1,12 @@
package main
import "fmt"
func main() {
dict := map[string]string{"bidule": "machin", "truc": "bidule"}
r, ok := dict["xxx"]
fmt.Println(r, ok)
}
// Output:
// false

View File

@@ -242,7 +242,8 @@ func (interp *Interpreter) Cfg(root *Node, sdef *NodeMap) []*Node {
if n.kind == DefineX {
// retrieve assigned value types from call signature
var types []*Type
if n.child[l].kind == CallExpr {
switch n.child[l].kind {
case CallExpr:
if funtype := n.child[l].child[0].typ; funtype.cat == ValueT {
// Handle functions imported from runtime
for i := 0; i < funtype.rtype.NumOut(); i++ {
@@ -251,6 +252,10 @@ func (interp *Interpreter) Cfg(root *Node, sdef *NodeMap) []*Node {
} else {
types = funtype.ret
}
case TypeAssertExpr, IndexExpr:
types = append(types, n.child[l].child[1].typ, defaultTypes["error"])
default:
log.Fatalln(n.index, "Assign expression unsupported:", n.child[l].kind)
}
// Force definition of assigned idents in current scope
for i, c := range n.child[:l] {
@@ -273,6 +278,7 @@ func (interp *Interpreter) Cfg(root *Node, sdef *NodeMap) []*Node {
n.typ = n.child[0].typ.val
n.recv = n
if n.child[0].typ.cat == MapT {
frameIndex.max++ // Reserve an entry for getIndexMap 2nd return value
n.run = getIndexMap
}
@@ -834,6 +840,18 @@ func getDefault(n *Node) int {
return -1
}
// isType returns true if node refers to a type definition, false otherwise
func (n *Node) isType() bool {
switch n.kind {
case ArrayType, ChanType, FuncType, MapType, StructType:
return true
case Ident:
_, ok := n.interp.types[n.ident]
return ok
}
return false
}
// Wire AST nodes for CFG in subtree
func wireChild(n *Node) {
// Set start node, in subtree (propagated to ancestors by post-order processing)

View File

@@ -1075,6 +1075,26 @@ func main() {
// bonjour
}
func Example_map5() {
src := `
package main
import "fmt"
func main() {
dict := map[string]string{"bidule": "machin", "truc": "bidule"}
r, ok := dict["xxx"]
fmt.Println(r, ok)
}
`
i := NewInterpreter(Opt{Entry: "main"})
i.ImportBin(export.Pkg)
i.Eval(src)
// Output:
// false
}
func Example_math0() {
src := `
package main

View File

@@ -401,7 +401,7 @@ func callBinMethod(n *Node, f *Frame) {
in[i+1] = reflect.ValueOf(value(c, f))
}
}
log.Println(n.index, "in callBinMethod", n.ident, in, in[0].MethodByName(n.child[0].child[1].ident).Type())
//log.Println(n.index, "in callBinMethod", n.ident, in, in[0].MethodByName(n.child[0].child[1].ident).Type())
if !fun.IsValid() {
fun = in[0].MethodByName(n.child[0].child[1].ident)
in = in[1:]
@@ -464,7 +464,10 @@ func getIndex(n *Node, f *Frame) {
func getIndexMap(n *Node, f *Frame) {
m := value(n.child[0], f).(map[interface{}]interface{})
f.data[n.findex] = m[value(n.child[1], f)]
if f.data[n.findex], f.data[n.findex+1] = m[value(n.child[1], f)]; !f.data[n.findex+1].(bool) {
// Force a zero value if key is not present in map
f.data[n.findex] = n.child[0].typ.val.zero()
}
}
func getMap(n *Node, f *Frame) {