From 8f9a84345c147da5d398331548753d1e350ce846 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sun, 24 Oct 2021 12:04:06 +0200 Subject: Add globals and OP_DEF operation --- src/bytecode/compiler.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'src/bytecode/compiler.h') diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h index ec51942..8f3fa81 100755 --- a/src/bytecode/compiler.h +++ b/src/bytecode/compiler.h @@ -176,6 +176,52 @@ compile_list_simple_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { }); } +void +compile_define_op(Chunk *chunk, Visitor *vs, Token start) { + Token name = peek_token(vs); + if (name.type != TOKEN_SYMBOL) { + error_push((Error){ + .type = ERR_TYPE_COMPILER, + .value = ERR_WRONG_ARG_TYPE, + .line = start.line, + .col = start.column, + }); + return; + } + parse_tree(chunk, vs); + Token expr = peek_token(vs); + if (name.type == TOKEN_EOF || expr.type == TOKEN_EOF) { + error_push((Error){ + .type = ERR_TYPE_COMPILER, + .value = ERR_UNBALANCED_PAREN, + .line = start.line, + .col = start.column, + }); + return; + } + if (name.type == TOKEN_RPAREN || expr.type == TOKEN_RPAREN) { + error_push((Error){ + .type = ERR_TYPE_COMPILER, + .value = ERR_NOT_ENOUGH_ARGS, + .line = start.line, + .col = start.column, + }); + return; + } + parse_tree(chunk, vs); + if (peek_token(vs).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_DEF, start.line, start.column); +} + void parse_list(Chunk *chunk, Visitor *vs, Token start) { if (!has_next_token(vs)) { @@ -205,6 +251,7 @@ parse_list(Chunk *chunk, Visitor *vs, Token start) { 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; + case TOKEN_DEF: { compile_define_op(chunk, vs, start); } break; default: { error_push((Error){ .type = ERR_TYPE_COMPILER, @@ -219,6 +266,9 @@ parse_list(Chunk *chunk, Visitor *vs, Token start) { void parse_tree(Chunk *chunk, Visitor *vs) { Token tok = next_token(vs); + if (errors_n != 0) { + return; + } switch (tok.type) { case TOKEN_FIXNUM: { parse_fixnum(chunk, tok); -- cgit v1.2.1