From b743e03fc6042e3e2d55cfa0387c092824de64c5 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sun, 24 Oct 2021 09:52:09 +0200 Subject: Add print/display/newline ops --- src/bytecode/compiler.h | 103 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 70 insertions(+), 33 deletions(-) (limited to 'src/bytecode/compiler.h') diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h index 02d938f..d404a5a 100755 --- a/src/bytecode/compiler.h +++ b/src/bytecode/compiler.h @@ -58,7 +58,7 @@ parse_fixnum(Chunk *chunk, Token tok) { void parse_tree(Chunk *chunk, Visitor *vs); void -compile_list_binary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { +compile_list_binary_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { size_t n = 0; while (has_next_token(vs)) { Token tok = peek_token(vs); @@ -66,8 +66,8 @@ compile_list_binary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { error_push((Error){ .type = ERR_TYPE_COMPILER, .value = ERR_UNBALANCED_PAREN, - .line = list_start.line, - .col = list_start.column, + .line = start.line, + .col = start.column, }); return; } @@ -77,8 +77,8 @@ compile_list_binary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { error_push((Error){ .type = ERR_TYPE_COMPILER, .value = ERR_NOT_ENOUGH_ARGS, - .line = list_start.line, - .col = list_start.column, + .line = start.line, + .col = start.column, }); return; } @@ -87,12 +87,12 @@ compile_list_binary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { parse_tree(chunk, vs); n++; } - emit_constant(chunk, list_start, FIXNUM_VAL(n)); - add_code(chunk, op, list_start.line, list_start.column); + emit_constant(chunk, start, FIXNUM_VAL(n)); + add_code(chunk, op, start.line, start.column); } void -compile_list_unary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { +compile_list_unary_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { size_t n = 0; while (has_next_token(vs)) { Token tok = peek_token(vs); @@ -100,8 +100,8 @@ compile_list_unary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { error_push((Error){ .type = ERR_TYPE_COMPILER, .value = ERR_UNBALANCED_PAREN, - .line = list_start.line, - .col = list_start.column, + .line = start.line, + .col = start.column, }); return; } @@ -111,57 +111,94 @@ compile_list_unary_op(Chunk *chunk, Visitor *vs, Token list_start, Ops op) { error_push((Error){ .type = ERR_TYPE_COMPILER, .value = ERR_NOT_ENOUGH_ARGS, - .line = list_start.line, - .col = list_start.column, + .line = start.line, + .col = start.column, }); } return; } parse_tree(chunk, vs); - add_code(chunk, op, list_start.line, list_start.column); + add_code(chunk, op, start.line, start.column); n++; if (n > 1) { error_push((Error){ .type = ERR_TYPE_COMPILER, .value = ERR_TOO_MANY_ARGS, - .line = list_start.line, - .col = list_start.column, + .line = start.line, + .col = start.column, }); } } error_push((Error){ .type = ERR_TYPE_COMPILER, .value = ERR_UNBALANCED_PAREN, - .line = list_start.line, - .col = list_start.column, + .line = start.line, + .col = start.column, }); } void -parse_list(Chunk *chunk, Visitor *vs, Token list_start) { +compile_list_simple_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { + if (has_next_token(vs)) { + Token tok = peek_token(vs); + 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) { + error_push((Error){ + .type = ERR_TYPE_COMPILER, + .value = ERR_TOO_MANY_ARGS, + .line = start.line, + .col = start.column, + }); + return; + } + next_token(vs); + add_code(chunk, op, start.line, start.column); + return; + } + error_push((Error){ + .type = ERR_TYPE_COMPILER, + .value = ERR_UNBALANCED_PAREN, + .line = start.line, + .col = start.column, + }); +} + +void +parse_list(Chunk *chunk, Visitor *vs, Token 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, + .line = start.line, + .col = start.column, }); } Token tok = next_token(vs); switch (tok.type) { - case TOKEN_ADD: { compile_list_binary_op(chunk, vs, list_start, OP_SUM); } break; - case TOKEN_SUB: { compile_list_binary_op(chunk, vs, list_start, OP_SUB); } break; - case TOKEN_MUL: { compile_list_binary_op(chunk, vs, list_start, OP_MUL); } break; - case TOKEN_DIV: { compile_list_binary_op(chunk, vs, list_start, OP_DIV); } break; - case TOKEN_MOD: { compile_list_binary_op(chunk, vs, list_start, OP_MOD); } break; - case TOKEN_NOT: { compile_list_unary_op(chunk, vs, list_start, OP_NOT); } break; - case TOKEN_AND: { compile_list_binary_op(chunk, vs, list_start, OP_AND); } break; - case TOKEN_OR: { compile_list_binary_op(chunk, vs, list_start, OP_OR); } break; - case TOKEN_EQUAL: { compile_list_binary_op(chunk, vs, list_start, OP_EQUAL); } break; - case TOKEN_LESS: { compile_list_binary_op(chunk, vs, list_start, OP_LESS); } break; - case TOKEN_GREATER: { compile_list_binary_op(chunk, vs, list_start, OP_GREATER); } break; - case TOKEN_LESS_EQUAL: { compile_list_binary_op(chunk, vs, list_start, OP_LESS_EQUAL); } break; - case TOKEN_GREATER_EQUAL: { compile_list_binary_op(chunk, vs, list_start, OP_GREATER_EQUAL); } break; + case TOKEN_ADD: { compile_list_binary_op(chunk, vs, start, OP_SUM); } break; + case TOKEN_SUB: { compile_list_binary_op(chunk, vs, start, OP_SUB); } break; + case TOKEN_MUL: { compile_list_binary_op(chunk, vs, start, OP_MUL); } break; + case TOKEN_DIV: { compile_list_binary_op(chunk, vs, start, OP_DIV); } break; + case TOKEN_MOD: { compile_list_binary_op(chunk, vs, start, OP_MOD); } break; + case TOKEN_NOT: { compile_list_unary_op(chunk, vs, start, OP_NOT); } break; + case TOKEN_AND: { compile_list_binary_op(chunk, vs, start, OP_AND); } break; + case TOKEN_OR: { compile_list_binary_op(chunk, vs, start, OP_OR); } break; + case TOKEN_EQUAL: { compile_list_binary_op(chunk, vs, start, OP_EQUAL); } break; + case TOKEN_LESS: { compile_list_binary_op(chunk, vs, start, OP_LESS); } break; + case TOKEN_GREATER: { compile_list_binary_op(chunk, vs, start, OP_GREATER); } break; + case TOKEN_LESS_EQUAL: { compile_list_binary_op(chunk, vs, start, OP_LESS_EQUAL); } break; + case TOKEN_GREATER_EQUAL: { compile_list_binary_op(chunk, vs, start, OP_GREATER_EQUAL); } break; + case TOKEN_PRINT: { compile_list_unary_op(chunk, vs, start, OP_PRINT); } break; + case TOKEN_DISPLAY: { compile_list_unary_op(chunk, vs, start, OP_DISPLAY); } break; + case TOKEN_NEWLINE: { compile_list_simple_op(chunk, vs, start, OP_NEWLINE); } break; default: { error_push((Error){ .type = ERR_TYPE_COMPILER, -- cgit v1.2.1