From bd1480fd2cb80680933b80900c5fb13b5f88ca42 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sat, 23 Oct 2021 14:16:45 +0200 Subject: Fix a bug in signed fixnum compilation --- src/bytecode/compiler.h | 96 ++++++++++++++++++------------------------------- src/bytecode/debug.h | 4 +-- 2 files changed, 36 insertions(+), 64 deletions(-) (limited to 'src/bytecode') diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h index 201dca6..ae8fac1 100755 --- a/src/bytecode/compiler.h +++ b/src/bytecode/compiler.h @@ -52,33 +52,13 @@ parse_fixnum(Chunk *chunk, Token tok) { } num = num * 10 + (c - '0'); } - emit_constant(chunk, tok, num); + emit_constant(chunk, tok, num * sign); } void parse_tree(Chunk *chunk, Visitor *vs); void -compile_list_primitive(Chunk *chunk, Visitor *vs, Token op_tok) { - Ops op; - switch (op_tok.type) { - case TOKEN_ADD: { - op = OP_SUM; - } break; - case TOKEN_SUB: { - op = OP_SUB; - } break; - case TOKEN_MUL: { - op = OP_MUL; - } break; - case TOKEN_DIV: { - op = OP_DIV; - } break; - case TOKEN_MOD: { - op = OP_MOD; - } break; - default: { - } break; - } +compile_list_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { size_t n = 0; while (has_next_token(vs)) { Token tok = peek_token(vs); @@ -86,8 +66,8 @@ compile_list_primitive(Chunk *chunk, Visitor *vs, Token op_tok) { error_push((Error){ .type = ERR_TYPE_COMPILER, .value = ERR_UNBALANCED_PAREN, - .line = op_tok.line, - .col = op_tok.column, + .line = list_start.line, + .col = list_start.column, }); return; } @@ -98,42 +78,44 @@ compile_list_primitive(Chunk *chunk, Visitor *vs, Token op_tok) { parse_tree(chunk, vs); n++; if (n > 1) { - add_code(chunk, op, tok.line, tok.column); + add_code(chunk, op, list_start.line, list_start.column); } } if (n == 0) { error_push((Error){ .type = ERR_TYPE_COMPILER, .value = ERR_NOT_ENOUGH_ARGS, - .line = op_tok.line, - .col = op_tok.column, + .line = list_start.line, + .col = list_start.column, }); } } void -parse_list(Chunk *chunk, Visitor *vs) { - if (has_next_token(vs)) { - Token tok = next_token(vs); - print_token(tok); - // TODO: check if is function call. - switch (tok.type) { - case TOKEN_ADD: - case TOKEN_SUB: - case TOKEN_MUL: - case TOKEN_DIV: - case TOKEN_MOD:{ - compile_list_primitive(chunk, vs, tok); - } break; - default: { - error_push((Error){ - .type = ERR_TYPE_COMPILER, - .value = ERR_OBJ_NOT_CALLABLE, - .line = tok.line, - .line = tok.column, - }); - } break; - } +parse_list(Chunk *chunk, Visitor *vs, Token list_start) { + if (!has_next_token(vs)) { + error_push((Error){ + .type = ERR_TYPE_COMPILER, + .value = ERR_UNBALANCED_PAREN, + .line = list_start.line, + .col = list_start.column, + }); + } + Token tok = next_token(vs); + switch (tok.type) { + case TOKEN_ADD: { compile_list_op(chunk, vs, list_start, OP_SUM); } break; + case TOKEN_SUB: { compile_list_op(chunk, vs, list_start, OP_SUB); } break; + case TOKEN_MUL: { compile_list_op(chunk, vs, list_start, OP_MUL); } break; + case TOKEN_DIV: { compile_list_op(chunk, vs, list_start, OP_DIV); } break; + case TOKEN_MOD: { compile_list_op(chunk, vs, list_start, OP_MOD); } break; + default: { + error_push((Error){ + .type = ERR_TYPE_COMPILER, + .value = ERR_OBJ_NOT_CALLABLE, + .line = tok.line, + .col = tok.column, + }); + } break; } } @@ -175,7 +157,7 @@ parse_tree(Chunk *chunk, Visitor *vs) { return; } break; case TOKEN_LPAREN: { - parse_list(chunk, vs); + parse_list(chunk, vs, tok); // Object *obj = parse_list(vs); // if (obj == obj_err) { // error_push((Error){ @@ -226,21 +208,11 @@ compile(Token *tokens) { .tokens = tokens, .current = 0, }; + Token start_tok = peek_token(&visitor); while (has_next_token(&visitor) && peek_token(&visitor).type != TOKEN_EOF) { parse_tree(chunk, &visitor); } - // error_push((Error){ - // .type = ERR_TYPE_COMPILER, - // .value = ERR_UNKNOWN, - // }); - // size_t const_a = add_constant(chunk, 7); - // add_code(chunk, OP_CONSTANT, 1, 1); - // add_code(chunk, const_a, 1, 1); - // size_t const_b = add_constant(chunk, 2); - // add_code(chunk, OP_CONSTANT, 1, 2); - // add_code(chunk, const_b, 1, 2); - // add_code(chunk, OP_MOD, 1, 3); - add_code(chunk, OP_RETURN, 1, 1); + add_code(chunk, OP_RETURN, start_tok.line, start_tok.column); return chunk; } diff --git a/src/bytecode/debug.h b/src/bytecode/debug.h index 05f9d8e..25ac693 100755 --- a/src/bytecode/debug.h +++ b/src/bytecode/debug.h @@ -38,9 +38,9 @@ disassemble_instruction(Chunk *chunk, size_t offset) { switch (instruction) { case OP_CONSTANT: { u8 constant = chunk->code[offset + 1]; - printf("%-16s %4d (", "OP_CONSTANT", constant); + printf("%-16s %4d -> ", "OP_CONSTANT", constant); display(chunk->constants[constant]); - printf(")\n"); + printf("\n"); return offset + 2; } break; case OP_SUM: { printf("OP_SUM\n"); return offset + 1; } break; -- cgit v1.2.1