From 0ff1716f45c9494b1aa02b8ddb2541821480c5ad Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Thu, 28 Oct 2021 13:34:18 +0200 Subject: Fix difference between lambda and named func calls --- src/bytecode/compiler.h | 14 ++++++++------ src/bytecode/vm.h | 5 +++-- 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h index 867d194..5f38216 100755 --- a/src/bytecode/compiler.h +++ b/src/bytecode/compiler.h @@ -520,6 +520,10 @@ compile_fun_op(Chunk *chunk, Compiler *compiler, Token start) { void compile_call_op(Chunk *chunk, Compiler *compiler, Token start, Token name) { + if (name.type == TOKEN_SYMBOL) { + parse_symbol(chunk, compiler, name); + } + // Compile body. size_t n = 0; while (has_next_token(compiler)) { @@ -540,11 +544,6 @@ compile_call_op(Chunk *chunk, Compiler *compiler, Token start, Token name) { parse_tree(chunk, compiler); n++; } - if (name.type == TOKEN_SYMBOL) { - Object obj = make_symbol(name.value); - emit_constant(chunk, start, obj); - add_code(chunk, OP_GET, start.line, start.column); - } emit_constant(chunk, start, FIXNUM_VAL(n)); add_code(chunk, OP_CALL, start.line, start.column); } @@ -635,7 +634,10 @@ parse_list(Chunk *chunk, Compiler *compiler, Token start) { case TOKEN_GREATER_EQUAL: { compile_list_binary_op(chunk, compiler, start, OP_GREATER_EQUAL); } break; case TOKEN_PRINT: { compile_list_unary_op(chunk, compiler, start, OP_PRINT); } break; case TOKEN_DISPLAY: { compile_list_unary_op(chunk, compiler, start, OP_DISPLAY); } break; - case TOKEN_NEWLINE: { compile_list_simple_op(chunk, compiler, start, OP_NEWLINE); } break; + case TOKEN_NEWLINE: { + compile_list_simple_op(chunk, compiler, start, OP_NEWLINE); + emit_constant(chunk, start, NIL_VAL); + } break; case TOKEN_DEF: { compile_declare_op(chunk, compiler, start, OP_DEF); } break; case TOKEN_SET: { compile_declare_op(chunk, compiler, start, OP_SET); } break; case TOKEN_FUN: { compile_fun_op(chunk, compiler, start); } break; diff --git a/src/bytecode/vm.h b/src/bytecode/vm.h index a02bdd1..0d1595d 100755 --- a/src/bytecode/vm.h +++ b/src/bytecode/vm.h @@ -193,6 +193,7 @@ vm_interpret(VM *vm) { // of it that lives on the heap. Object proc = array_pop(vm->stack); proc = make_lambda(proc.closure->chunk); + ssize_t n_captured = AS_FIXNUM(array_pop(vm->stack)); for (ssize_t i = 0; i < n_captured; i++) { Object value = array_pop(vm->stack); @@ -303,7 +304,7 @@ vm_interpret(VM *vm) { } break; case OP_CALL: { ssize_t n_args = AS_FIXNUM(array_pop(vm->stack)); - Object proc = array_pop(vm->stack); + Object proc = vm->stack[array_size(vm->stack) - 1 - n_args]; // Check the number of arguments is correct. // NOTE: This is probably better handled at compilation, but for @@ -363,7 +364,7 @@ vm_interpret(VM *vm) { vm->pc = frame->rp; array_head(vm->frames)->size--; Object ret = array_pop(vm->stack); - array_head(vm->stack)->size = frame->stack_offset; + array_head(vm->stack)->size = frame->stack_offset - 1; array_push(vm->stack, ret); frame = &vm->frames[array_size(vm->frames) - 1]; chunk = frame->closure->chunk; -- cgit v1.2.1