Skip to content

Commit

Permalink
Simplify compileFn with argsWalker.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaq committed Feb 18, 2017
1 parent 59d2bd5 commit 9cb2978
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions eval/builtin-special.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,14 @@ func makeFnOp(op Op) Op {
//
// fn f []{foobar} is a shorthand for set '&'f = []{foobar}.
func compileFn(cp *compiler, fn *parse.Form) OpFunc {
if len(fn.Args) == 0 {
end := fn.End()
cp.errorpf(end, end, "should be followed by function name")
}
fnName := mustString(cp, fn.Args[0], "must be a literal string")
varName := FnPrefix + fnName

if len(fn.Args) == 1 {
end := fn.Args[0].End()
cp.errorpf(end, end, "should be followed by a lambda")
}
pn := mustPrimary(cp, fn.Args[1], "should be a lambda")
if pn.Type != parse.Lambda {
cp.compiling(pn)
cp.errorf("should be a lambda")
}
if len(fn.Args) > 2 {
cp.errorpf(fn.Args[2].Begin(), fn.Args[len(fn.Args)-1].End(), "superfluous argument(s)")
}
args := cp.walkArgs(fn)
nameNode := args.next()
varName := FnPrefix + mustString(cp, nameNode, "must be a literal string")
bodyNode := args.nextMustLambda()
args.mustEnd()

cp.registerVariableSet(":" + varName)
op := cp.lambda(pn)
op := cp.lambda(bodyNode)

return func(ec *EvalCtx) {
// Initialize the function variable with the builtin nop
Expand Down

0 comments on commit 9cb2978

Please sign in to comment.