Implement tail calls for continuation calling
This commit is contained in:
parent
d017884c5b
commit
0ce71beb62
1 changed files with 10 additions and 4 deletions
14
src/vm.cpp
14
src/vm.cpp
|
@ -132,11 +132,17 @@ Result<void> VM::vm_call_cont(Opcode& oc, Continuation& fun, bool tail) {
|
|||
if (reg_end - reg_start != 2) return ERROR(ArgumentCountMismatch);
|
||||
|
||||
auto param = TRY(_stack.get(reg_start + 1));
|
||||
_stack = TRY(_stack.settop(reg_start));
|
||||
|
||||
auto cont_stack = TRY(fun.frame());
|
||||
_stack = TRY(_stack.incpc());
|
||||
_stack = TRY(cont_stack.attach(_stack));
|
||||
|
||||
if (!tail) {
|
||||
_stack = TRY(_stack.settop(reg_start));
|
||||
_stack = TRY(_stack.incpc());
|
||||
_stack = TRY(cont_stack.attach(_stack));
|
||||
} else {
|
||||
auto par = TRY(_stack.parent());
|
||||
if (!par.is<StackFrame>()) return ERROR(TypeMismatch);
|
||||
_stack = TRY(cont_stack.attach(*par.to<StackFrame>()));
|
||||
}
|
||||
_stack = TRY(_stack.set(TRY(_stack.size()), param));
|
||||
|
||||
return Result<void>();
|
||||
|
|
Loading…
Reference in a new issue