1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
package script
import (
"fmt"
"go/ast"
"reflect"
"strings"
"unicode"
)
const GoExclusiveMethodSuffix = "Go"
type selector struct {
x Expr
method string
}
// compiles a selector statement like x.sel
func (w *World) compileSelectorStmt(n *ast.SelectorExpr) Expr {
x := w.compileExpr(n.X)
t := x.Type()
if t == nil {
panic(err(n.Pos(), "void does not have member", n.Sel.Name))
}
sel := strings.ToLower(n.Sel.Name)
N := ""
for i := 0; i < t.NumMethod(); i++ {
name := t.Method(i).Name
if strings.ToLower(name) == sel && unicode.IsUpper(rune(name[0])) && !strings.HasSuffix(name, GoExclusiveMethodSuffix) {
N = t.Method(i).Name
break
}
}
if N == "" {
panic(err(n.Pos(), t, "has no method", n.Sel.Name))
}
return &selector{x, N}
}
func (e *selector) Eval() interface{} {
obj := reflect.ValueOf(e.x.Eval())
meth := obj.MethodByName(e.method)
if meth.Kind() == 0 {
panic(fmt.Sprint(e.x, " has no method ", e.method))
}
return meth.Interface()
}
func (e *selector) Type() reflect.Type {
return reflect.New(e.x.Type()).Elem().MethodByName(e.method).Type()
}
func (e *selector) Child() []Expr {
return []Expr{e.x}
}
func (e *selector) Fix() Expr {
return &selector{x: e.x.Fix(), method: e.method}
}
|