aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-28 13:34:18 +0200
committerBad Diode <bd@badd10de.dev>2021-10-28 13:34:18 +0200
commit0ff1716f45c9494b1aa02b8ddb2541821480c5ad (patch)
treef08bbe92d279a00d451a6f1c7360a52085b23b86 /src
parenta8807776e6795dccfe8d2f381ae01fdb4cfb07e6 (diff)
downloadbdl-0ff1716f45c9494b1aa02b8ddb2541821480c5ad.tar.gz
bdl-0ff1716f45c9494b1aa02b8ddb2541821480c5ad.zip
Fix difference between lambda and named func calls
Diffstat (limited to 'src')
-rwxr-xr-xsrc/bytecode/compiler.h14
-rwxr-xr-xsrc/bytecode/vm.h5
2 files changed, 11 insertions, 8 deletions
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) {
520 520
521void 521void
522compile_call_op(Chunk *chunk, Compiler *compiler, Token start, Token name) { 522compile_call_op(Chunk *chunk, Compiler *compiler, Token start, Token name) {
523 if (name.type == TOKEN_SYMBOL) {
524 parse_symbol(chunk, compiler, name);
525 }
526
523 // Compile body. 527 // Compile body.
524 size_t n = 0; 528 size_t n = 0;
525 while (has_next_token(compiler)) { 529 while (has_next_token(compiler)) {
@@ -540,11 +544,6 @@ compile_call_op(Chunk *chunk, Compiler *compiler, Token start, Token name) {
540 parse_tree(chunk, compiler); 544 parse_tree(chunk, compiler);
541 n++; 545 n++;
542 } 546 }
543 if (name.type == TOKEN_SYMBOL) {
544 Object obj = make_symbol(name.value);
545 emit_constant(chunk, start, obj);
546 add_code(chunk, OP_GET, start.line, start.column);
547 }
548 emit_constant(chunk, start, FIXNUM_VAL(n)); 547 emit_constant(chunk, start, FIXNUM_VAL(n));
549 add_code(chunk, OP_CALL, start.line, start.column); 548 add_code(chunk, OP_CALL, start.line, start.column);
550} 549}
@@ -635,7 +634,10 @@ parse_list(Chunk *chunk, Compiler *compiler, Token start) {
635 case TOKEN_GREATER_EQUAL: { compile_list_binary_op(chunk, compiler, start, OP_GREATER_EQUAL); } break; 634 case TOKEN_GREATER_EQUAL: { compile_list_binary_op(chunk, compiler, start, OP_GREATER_EQUAL); } break;
636 case TOKEN_PRINT: { compile_list_unary_op(chunk, compiler, start, OP_PRINT); } break; 635 case TOKEN_PRINT: { compile_list_unary_op(chunk, compiler, start, OP_PRINT); } break;
637 case TOKEN_DISPLAY: { compile_list_unary_op(chunk, compiler, start, OP_DISPLAY); } break; 636 case TOKEN_DISPLAY: { compile_list_unary_op(chunk, compiler, start, OP_DISPLAY); } break;
638 case TOKEN_NEWLINE: { compile_list_simple_op(chunk, compiler, start, OP_NEWLINE); } break; 637 case TOKEN_NEWLINE: {
638 compile_list_simple_op(chunk, compiler, start, OP_NEWLINE);
639 emit_constant(chunk, start, NIL_VAL);
640 } break;
639 case TOKEN_DEF: { compile_declare_op(chunk, compiler, start, OP_DEF); } break; 641 case TOKEN_DEF: { compile_declare_op(chunk, compiler, start, OP_DEF); } break;
640 case TOKEN_SET: { compile_declare_op(chunk, compiler, start, OP_SET); } break; 642 case TOKEN_SET: { compile_declare_op(chunk, compiler, start, OP_SET); } break;
641 case TOKEN_FUN: { compile_fun_op(chunk, compiler, start); } break; 643 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) {
193 // of it that lives on the heap. 193 // of it that lives on the heap.
194 Object proc = array_pop(vm->stack); 194 Object proc = array_pop(vm->stack);
195 proc = make_lambda(proc.closure->chunk); 195 proc = make_lambda(proc.closure->chunk);
196
196 ssize_t n_captured = AS_FIXNUM(array_pop(vm->stack)); 197 ssize_t n_captured = AS_FIXNUM(array_pop(vm->stack));
197 for (ssize_t i = 0; i < n_captured; i++) { 198 for (ssize_t i = 0; i < n_captured; i++) {
198 Object value = array_pop(vm->stack); 199 Object value = array_pop(vm->stack);
@@ -303,7 +304,7 @@ vm_interpret(VM *vm) {
303 } break; 304 } break;
304 case OP_CALL: { 305 case OP_CALL: {
305 ssize_t n_args = AS_FIXNUM(array_pop(vm->stack)); 306 ssize_t n_args = AS_FIXNUM(array_pop(vm->stack));
306 Object proc = array_pop(vm->stack); 307 Object proc = vm->stack[array_size(vm->stack) - 1 - n_args];
307 308
308 // Check the number of arguments is correct. 309 // Check the number of arguments is correct.
309 // NOTE: This is probably better handled at compilation, but for 310 // NOTE: This is probably better handled at compilation, but for
@@ -363,7 +364,7 @@ vm_interpret(VM *vm) {
363 vm->pc = frame->rp; 364 vm->pc = frame->rp;
364 array_head(vm->frames)->size--; 365 array_head(vm->frames)->size--;
365 Object ret = array_pop(vm->stack); 366 Object ret = array_pop(vm->stack);
366 array_head(vm->stack)->size = frame->stack_offset; 367 array_head(vm->stack)->size = frame->stack_offset - 1;
367 array_push(vm->stack, ret); 368 array_push(vm->stack, ret);
368 frame = &vm->frames[array_size(vm->frames) - 1]; 369 frame = &vm->frames[array_size(vm->frames) - 1];
369 chunk = frame->closure->chunk; 370 chunk = frame->closure->chunk;