Fix callstack bug with returning to previous function

This commit is contained in:
Konstantin Nazarov 2024-08-18 12:56:54 +01:00
parent a11b2bfa7e
commit 7d88d483df
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 6 additions and 2 deletions

View file

@ -402,10 +402,13 @@ Result<Expression> Compiler::compile_list(Context& context, Pair& expr) {
return compile_if(context, sym, expr); return compile_if(context, sym, expr);
} else if (TRY(sym.cmp("lambda")) == 0) { } else if (TRY(sym.cmp("lambda")) == 0) {
return compile_lambda(context, sym, expr); return compile_lambda(context, sym, expr);
} else {
return compile_function_call(context, expr);
} }
} else if (first.is<Pair>()) { } else if (first.is<Pair>()) {
return compile_function_call(context, expr); return compile_function_call(context, expr);
} }
return ERROR(TypeMismatch); return ERROR(TypeMismatch);
} }

View file

@ -11,7 +11,8 @@ StaticArena<64 * 1024 * 1024> arena;
Result<void> run() { Result<void> run() {
// auto code_str = TRY(String::create("(* (+ 1 2 3) (/ 4 2))")); // auto code_str = TRY(String::create("(* (+ 1 2 3) (/ 4 2))"));
auto code_str = TRY(String::create("((lambda (x) (* x x)) 2)")); auto code_str =
TRY(String::create("((lambda (f y) (f y)) (lambda (x) (* x x)) 2)"));
auto parsed = TRY(read_one(code_str)); auto parsed = TRY(read_one(code_str));
auto code_str_written = TRY(write_one(parsed)); auto code_str_written = TRY(write_one(parsed));

View file

@ -87,7 +87,7 @@ Result<void> VM::vm_call(Opcode& oc) {
uint64_t old_base = _base; uint64_t old_base = _base;
Value fun_val = TRY(fun.copy()); Value fun_val = TRY(_fun.copy());
Value oldbase_val = TRY(Int64::create(old_base)); Value oldbase_val = TRY(Int64::create(old_base));
Value pc_val = TRY(Int64::create(_pc)); Value pc_val = TRY(Int64::create(_pc));
_callstack.set(_callstack.gettop(), fun_val); _callstack.set(_callstack.gettop(), fun_val);