Fix using Indexer as Caller.

This commit is contained in:
Qi Xiao 2016-02-15 14:31:59 +01:00
parent 0fdf595633
commit ed2855c595
3 changed files with 26 additions and 4 deletions

View File

@ -26,6 +26,17 @@ type Caller interface {
Call(ec *EvalCtx, args []Value)
}
func getCaller(v Value) Caller {
if caller, ok := v.(Caller); ok {
return caller
}
if indexer, ok := v.(Indexer); ok {
return IndexerCaller{indexer}
}
throw(fmt.Errorf("a %s is not callable", v.Kind()))
panic("unreachable")
}
func (ec *EvalCtx) PCall(f Caller, args []Value) (ex error) {
defer errutil.Catch(&ex)
f.Call(ec, args)

View File

@ -237,10 +237,7 @@ func (cp *compiler) form(n *parse.Form) Op {
headValues := headOp(ec)
headMust := ec.must(headValues, "the head of command", p)
headMust.mustLen(1)
headCaller, ok := headValues[0].(Caller)
if !ok {
headMust.error("a callable", headValues[0].Kind())
}
headCaller := getCaller(headValues[0])
// args
var args []Value

View File

@ -64,3 +64,17 @@ func intIndex(idx Value) int {
}
return i
}
/*
// CallerIndexer adapts a Caller to an Indexer.
type CallerIndexer struct {
Caller
ec *EvalCtx
}
func (ci CallerIndexer) Index(idx Value) Value {
return captureOutput(ci.ec, func(ec *EvalCtx) {
ci.Caller.Call(ec, []Value{idx})
})
}
*/