Add support for type conversion (in progress)
This commit is contained in:
13
_test/type3.go
Normal file
13
_test/type3.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type S1 string
|
||||
|
||||
func main() {
|
||||
s := S1("Hello")
|
||||
fmt.Println(s)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// Hello
|
||||
@@ -194,9 +194,7 @@ func (interp *Interpreter) Cfg(root *Node, sdef *NodeMap) []*Node {
|
||||
n.findex = n.child[0].findex
|
||||
// Propagate type
|
||||
// TODO: Check that existing destination type matches source type
|
||||
//log.Println(n.index, "Assign child1:", n.child[1].index, n.child[1].typ)
|
||||
n.typ = n.child[0].typ
|
||||
//n.run = setInt // Temporary, debug
|
||||
if sym, level, ok := scope.lookup(n.child[0].ident); ok {
|
||||
sym.typ = n.typ
|
||||
n.level = level
|
||||
@@ -312,9 +310,11 @@ func (interp *Interpreter) Cfg(root *Node, sdef *NodeMap) []*Node {
|
||||
n.child[1].val = n.typ
|
||||
n.child[1].kind = BasicLit
|
||||
}
|
||||
}
|
||||
// TODO: Should process according to child type, not kind.
|
||||
if n.child[0].kind == SelectorImport {
|
||||
} else if n.child[0].isType() {
|
||||
n.typ = n.child[0].typ
|
||||
n.run = convert
|
||||
} else if n.child[0].kind == SelectorImport {
|
||||
// TODO: Should process according to child type, not kind.
|
||||
n.fsize = n.child[0].fsize
|
||||
rtype := n.child[0].val.(reflect.Value).Type()
|
||||
if rtype.NumOut() > 0 {
|
||||
@@ -408,7 +408,6 @@ func (interp *Interpreter) Cfg(root *Node, sdef *NodeMap) []*Node {
|
||||
// Trigger frame indirection to handle nested functions
|
||||
n.action = CallF
|
||||
}
|
||||
//log.Println(n.index, "callExpr:", n.child[0].ident, "frame index:", n.findex)
|
||||
|
||||
case CaseClause:
|
||||
frameIndex.max++
|
||||
|
||||
@@ -2781,6 +2781,46 @@ func main() {
|
||||
// 2009-11-10 23:00:00 +0000 UTC
|
||||
}
|
||||
|
||||
func Example_type3() {
|
||||
src := `
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type S1 string
|
||||
|
||||
func main() {
|
||||
s := S1("Hello")
|
||||
fmt.Println(s)
|
||||
}
|
||||
`
|
||||
i := NewInterpreter(Opt{Entry: "main"})
|
||||
i.ImportBin(export.Pkg)
|
||||
i.Eval(src)
|
||||
|
||||
// Output:
|
||||
// Hello
|
||||
}
|
||||
|
||||
func Example_type4() {
|
||||
src := `
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a := int32(12)
|
||||
fmt.Println(reflect.TypeOf(a))
|
||||
}`
|
||||
i := NewInterpreter(Opt{Entry: "main"})
|
||||
i.ImportBin(export.Pkg)
|
||||
i.Eval(src)
|
||||
|
||||
}
|
||||
|
||||
func Example_var() {
|
||||
src := `
|
||||
package main
|
||||
|
||||
@@ -2,7 +2,6 @@ package interp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
@@ -161,15 +160,14 @@ func runCfg(n *Node, f *Frame) {
|
||||
}
|
||||
}
|
||||
|
||||
func setInt(n *Node, f *Frame) {
|
||||
log.Println(n.index, "setInt", value(n.child[0], f))
|
||||
f.data[n.child[0].findex].(reflect.Value).SetInt(int64(value(n.child[1], f).(int)))
|
||||
}
|
||||
|
||||
func typeAssert(n *Node, f *Frame) {
|
||||
f.data[n.findex] = value(n.child[0], f)
|
||||
}
|
||||
|
||||
func convert(n *Node, f *Frame) {
|
||||
f.data[n.findex] = value(n.child[1], f)
|
||||
}
|
||||
|
||||
// assignX implements multiple value assignement
|
||||
func assignX(n *Node, f *Frame) {
|
||||
l := len(n.child) - 1
|
||||
@@ -279,7 +277,6 @@ func (n *Node) wrapNode(in []reflect.Value) []reflect.Value {
|
||||
}
|
||||
|
||||
func call(n *Node, f *Frame) {
|
||||
//log.Println(n.index, "call", n.child[0].child[1].ident, n.child[0].typ.cat)
|
||||
// TODO: method detection should be done at CFG, and handled in a separate callMethod()
|
||||
var recv *Node
|
||||
var rseq []int
|
||||
|
||||
Reference in New Issue
Block a user