Add builtin resolve; change Repr of BuiltinFn.

This commit is contained in:
Qi Xiao 2016-04-05 06:20:18 +02:00
parent 713b98fd0d
commit 425f19165f
3 changed files with 12 additions and 4 deletions

View File

@ -36,7 +36,7 @@ func (*BuiltinFn) Kind() string {
}
func (b *BuiltinFn) Repr(int) string {
return "$" + FnPrefix + b.Name
return "<builtin " + b.Name + ">"
}
// Call calls a builtin function.
@ -103,6 +103,8 @@ func init() {
&BuiltinFn{"!=", WrapFn(noteq)},
&BuiltinFn{"deepeq", deepeq},
&BuiltinFn{"resolve", WrapFn(resolveFn)},
&BuiltinFn{"take", WrapFn(take)},
&BuiltinFn{"count", count},
@ -713,6 +715,11 @@ func deepeq(ec *EvalCtx, args []Value) {
out <- Bool(true)
}
func resolveFn(ec *EvalCtx, cmd String) {
out := ec.ports[1].Chan
out <- resolve(string(cmd), ec)
}
func take(ec *EvalCtx, n int, iterate func(func(Value))) {
out := ec.ports[1].Chan

View File

@ -6,6 +6,7 @@ import (
"os"
"syscall"
"github.com/elves/elvish/parse"
"github.com/elves/elvish/util"
)
@ -25,7 +26,7 @@ func (ExternalCmd) Kind() string {
}
func (e ExternalCmd) Repr(int) string {
return "<external " + e.Name + " >"
return "<external " + parse.Quote(e.Name) + ">"
}
// Call calls an external command.

View File

@ -27,12 +27,12 @@ func (s String) Call(ec *EvalCtx, args []Value) {
resolve(string(s), ec).Call(ec, args)
}
func resolve(s string, ec *EvalCtx) Fn {
func resolve(s string, ec *EvalCtx) FnValue {
// Try variable
splice, ns, name := ParseVariable(string(s))
if !splice {
if v := ec.ResolveVar(ns, FnPrefix+name); v != nil {
if caller, ok := v.Get().(Fn); ok {
if caller, ok := v.Get().(FnValue); ok {
return caller
}
}