Skip to content

Commit

Permalink
interp: implement conversion for interpreter function types
Browse files Browse the repository at this point in the history
Interpreter function types are represented internally by the AST node
of their definition. The conversion operation creates a new node with
the type field pointing to the target type.

Fixes #936.
  • Loading branch information
mvertes committed Nov 3, 2020
1 parent 9880738 commit 0ed4b36
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions _test/convert0.go
@@ -0,0 +1,21 @@
package main

type T struct {
v int
}

type comparator func(T, T) bool

func sort(items []T, comp comparator) {
println("in sort")
}

func compT(t0, t1 T) bool { return t0.v < t1.v }

func main() {
a := []T{}
sort(a, comparator(compT))
}

// Output:
// in sort
15 changes: 15 additions & 0 deletions interp/run.go
Expand Up @@ -390,6 +390,21 @@ func convert(n *node) {
return
}

if n.child[0].typ.cat == funcT && c.typ.cat == funcT {
value := genValue(c)
n.exec = func(f *frame) bltn {
n, ok := value(f).Interface().(*node)
if !ok || !n.typ.convertibleTo(c.typ) {
panic("cannot convert")
}
n1 := *n
n1.typ = c.typ
dest(f).Set(reflect.ValueOf(&n1))
return next
}
return
}

var value func(*frame) reflect.Value
if c.typ.cat == funcT {
value = genFunctionWrapper(c)
Expand Down

0 comments on commit 0ed4b36

Please sign in to comment.