Support defining recursive functions with fn.

This commit is contained in:
Qi Xiao 2016-02-16 14:49:22 +01:00
parent 64c9bf842d
commit c19a48ea8a

View File

@ -199,8 +199,12 @@ func compileFn(cp *compiler, fn *parse.Form) Op {
op := cp.lambda(pn)
return func(ec *EvalCtx) {
// Initialize the function variable with the builtin nop
// function. This step allows the definition of recursive
// functions; the actual function will never be called.
ec.local[varName] = NewPtrVariable(&BuiltinFn{"<shouldn't be called>", nop})
closure := op(ec)[0].(*Closure)
closure.Op = makeFnOp(closure.Op)
ec.local[varName] = NewPtrVariable(closure)
ec.local[varName].Set(closure)
}
}