From 46356365270b71be94097b3c408d5f35a9ebd6ed Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 26 Oct 2021 08:40:59 +0200 Subject: Add initial function call procedure --- src/bytecode/compiler.h | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) (limited to 'src/bytecode/compiler.h') diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h index 2c7827f..6dab1fe 100755 --- a/src/bytecode/compiler.h +++ b/src/bytecode/compiler.h @@ -290,6 +290,43 @@ compile_fun_op(Chunk *chunk, Compiler *compiler, Token start) { add_code(chunk, OP_DEF, start.line, start.column); } +void +compile_call_op(Chunk *chunk, Compiler *compiler, Token start, Token name) { + if (name.type != TOKEN_SYMBOL) { + error_push((Error){ + .type = ERR_TYPE_COMPILER, + .value = ERR_WRONG_ARG_TYPE, + .line = start.line, + .col = start.column, + }); + return; + } + + // FIXME: skipping arguments for now. Assuming nil. + + // Compile body. + while (has_next_token(compiler)) { + Token tok = peek_token(compiler); + if (tok.type == TOKEN_EOF) { + error_push((Error){ + .type = ERR_TYPE_COMPILER, + .value = ERR_UNBALANCED_PAREN, + .line = start.line, + .col = start.column, + }); + return; + } + if (tok.type == TOKEN_RPAREN) { + next_token(compiler); + break; + } + } + Object obj = make_symbol(name.value); + emit_constant(chunk, start, obj); + add_code(chunk, OP_GET, start.line, start.column); + add_code(chunk, OP_CALL, start.line, start.column); +} + void compile_if_op(Chunk *chunk, Compiler *compiler, Token start) { Token tok = peek_token(compiler); @@ -378,12 +415,9 @@ parse_list(Chunk *chunk, Compiler *compiler, Token start) { case TOKEN_FUN: { compile_fun_op(chunk, compiler, start); } break; case TOKEN_IF: { compile_if_op(chunk, compiler, start); } break; default: { - error_push((Error){ - .type = ERR_TYPE_COMPILER, - .value = ERR_OBJ_NOT_CALLABLE, - .line = tok.line, - .col = tok.column, - }); + // TODO: Emit error if compiler knows there hasn't been + // a function delcared with that name. + compile_call_op(chunk, compiler, start, tok); } break; } } -- cgit v1.2.1