From 74f9badefe95c070c0294d90cbf70965d8f68b7a Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sat, 23 Oct 2021 13:11:10 +0200 Subject: Fix buggy arithmetic list compilation --- src/bytecode/compiler.h | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h index 7ec1ca9..201dca6 100755 --- a/src/bytecode/compiler.h +++ b/src/bytecode/compiler.h @@ -62,36 +62,29 @@ compile_list_primitive(Chunk *chunk, Visitor *vs, Token op_tok) { Ops op; switch (op_tok.type) { case TOKEN_ADD: { - emit_constant(chunk, op_tok, 0); op = OP_SUM; } break; case TOKEN_SUB: { - // TODO: fetch first element. - emit_constant(chunk, op_tok, 0); op = OP_SUB; } break; case TOKEN_MUL: { - emit_constant(chunk, op_tok, 1); op = OP_MUL; } break; case TOKEN_DIV: { - // TODO: fetch first element. - emit_constant(chunk, op_tok, 1); op = OP_DIV; } break; case TOKEN_MOD: { - // TODO: fetch first element. - emit_constant(chunk, op_tok, 1); op = OP_MOD; } break; default: { } break; } + size_t n = 0; while (has_next_token(vs)) { Token tok = peek_token(vs); if (tok.type == TOKEN_EOF) { error_push((Error){ - .type = ERR_TYPE_PARSER, + .type = ERR_TYPE_COMPILER, .value = ERR_UNBALANCED_PAREN, .line = op_tok.line, .col = op_tok.column, @@ -103,7 +96,18 @@ compile_list_primitive(Chunk *chunk, Visitor *vs, Token op_tok) { break; } parse_tree(chunk, vs); - add_code(chunk, op, tok.line, tok.column); + n++; + if (n > 1) { + add_code(chunk, op, tok.line, tok.column); + } + } + if (n == 0) { + error_push((Error){ + .type = ERR_TYPE_COMPILER, + .value = ERR_NOT_ENOUGH_ARGS, + .line = op_tok.line, + .col = op_tok.column, + }); } } @@ -151,7 +155,7 @@ parse_tree(Chunk *chunk, Visitor *vs) { } break; case TOKEN_RPAREN: { error_push((Error){ - .type = ERR_TYPE_PARSER, + .type = ERR_TYPE_COMPILER, .value = ERR_UNBALANCED_PAREN, .line = tok.line, .col = tok.column, @@ -175,7 +179,7 @@ parse_tree(Chunk *chunk, Visitor *vs) { // Object *obj = parse_list(vs); // if (obj == obj_err) { // error_push((Error){ - // .type = ERR_TYPE_PARSER, + // .type = ERR_TYPE_COMPILER, // .value = ERR_UNBALANCED_PAREN, // .line = tok.line, // .col = tok.column, @@ -206,7 +210,7 @@ parse_tree(Chunk *chunk, Visitor *vs) { } break; } error_push((Error){ - .type = ERR_TYPE_PARSER, + .type = ERR_TYPE_COMPILER, .value = ERR_EOF_REACHED, .line = tok.line, .col = tok.column, -- cgit v1.2.1