From 35f93683d56d8b7f57c3f27fa7085847e2ad4598 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sun, 24 Oct 2021 17:48:42 +0200 Subject: Change Visitor -> Compiler structs --- src/bytecode/compiler.h | 144 ++++++++++++++++++++++++------------------------ 1 file changed, 72 insertions(+), 72 deletions(-) (limited to 'src') diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h index 27c5ead..d7d7920 100755 --- a/src/bytecode/compiler.h +++ b/src/bytecode/compiler.h @@ -4,31 +4,31 @@ #include "chunk.h" #include "lexer.h" -typedef struct Visitor { +typedef struct Compiler { Token *tokens; size_t current; -} Visitor; +} Compiler; // Mimics the functionality in the Scanner functions, but for entire tokens. -Token next_token(Visitor *visitor); -Token peek_token(const Visitor *visitor); -bool has_next_token(const Visitor *visitor); +Token next_token(Compiler *compiler); +Token peek_token(const Compiler *compiler); +bool has_next_token(const Compiler *compiler); Chunk * compile(Token *tokens); Token -peek_token(const Visitor *visitor) { - return visitor->tokens[visitor->current]; +peek_token(const Compiler *compiler) { + return compiler->tokens[compiler->current]; } Token -next_token(Visitor *visitor) { - return visitor->tokens[visitor->current++]; +next_token(Compiler *compiler) { + return compiler->tokens[compiler->current++]; } bool -has_next_token(const Visitor *visitor) { - return visitor->current < array_size(visitor->tokens); +has_next_token(const Compiler *compiler) { + return compiler->current < array_size(compiler->tokens); } void @@ -76,13 +76,13 @@ parse_fixnum(Chunk *chunk, Token tok) { emit_constant(chunk, tok, FIXNUM_VAL(num * sign)); } -void parse_tree(Chunk *chunk, Visitor *vs); +void parse_tree(Chunk *chunk, Compiler *compiler); void -compile_list_binary_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { +compile_list_binary_op(Chunk *chunk, Compiler *compiler, Token start, Ops op) { size_t n = 0; - while (has_next_token(vs)) { - Token tok = peek_token(vs); + while (has_next_token(compiler)) { + Token tok = peek_token(compiler); if (tok.type == TOKEN_EOF) { error_push((Error){ .type = ERR_TYPE_COMPILER, @@ -93,7 +93,7 @@ compile_list_binary_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { return; } if (tok.type == TOKEN_RPAREN) { - next_token(vs); + next_token(compiler); if (n <= 1) { error_push((Error){ .type = ERR_TYPE_COMPILER, @@ -105,7 +105,7 @@ compile_list_binary_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { } break; } - parse_tree(chunk, vs); + parse_tree(chunk, compiler); if (tok.type == TOKEN_SYMBOL) { add_code(chunk, OP_GET, tok.line, tok.column); } @@ -116,10 +116,10 @@ compile_list_binary_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { } void -compile_list_unary_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { +compile_list_unary_op(Chunk *chunk, Compiler *compiler, Token start, Ops op) { size_t n = 0; - while (has_next_token(vs)) { - Token tok = peek_token(vs); + while (has_next_token(compiler)) { + Token tok = peek_token(compiler); if (tok.type == TOKEN_EOF) { error_push((Error){ .type = ERR_TYPE_COMPILER, @@ -130,7 +130,7 @@ compile_list_unary_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { return; } if (tok.type == TOKEN_RPAREN) { - next_token(vs); + next_token(compiler); if (n == 0) { error_push((Error){ .type = ERR_TYPE_COMPILER, @@ -141,7 +141,7 @@ compile_list_unary_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { } return; } - parse_tree(chunk, vs); + parse_tree(chunk, compiler); if (tok.type == TOKEN_SYMBOL) { add_code(chunk, OP_GET, tok.line, tok.column); } @@ -165,9 +165,9 @@ compile_list_unary_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { } void -compile_list_simple_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { - if (has_next_token(vs)) { - Token tok = peek_token(vs); +compile_list_simple_op(Chunk *chunk, Compiler *compiler, Token start, Ops op) { + if (has_next_token(compiler)) { + Token tok = peek_token(compiler); if (tok.type == TOKEN_EOF) { error_push((Error){ .type = ERR_TYPE_COMPILER, @@ -186,7 +186,7 @@ compile_list_simple_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { }); return; } - next_token(vs); + next_token(compiler); add_code(chunk, op, start.line, start.column); return; } @@ -199,8 +199,8 @@ compile_list_simple_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { } void -compile_declare_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { - Token name = peek_token(vs); +compile_declare_op(Chunk *chunk, Compiler *compiler, Token start, Ops op) { + Token name = peek_token(compiler); if (name.type != TOKEN_SYMBOL) { error_push((Error){ .type = ERR_TYPE_COMPILER, @@ -210,8 +210,8 @@ compile_declare_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { }); return; } - parse_tree(chunk, vs); - Token expr = peek_token(vs); + parse_tree(chunk, compiler); + Token expr = peek_token(compiler); if (name.type == TOKEN_EOF || expr.type == TOKEN_EOF) { error_push((Error){ .type = ERR_TYPE_COMPILER, @@ -230,11 +230,11 @@ compile_declare_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { }); return; } - parse_tree(chunk, vs); + parse_tree(chunk, compiler); if (expr.type == TOKEN_SYMBOL) { add_code(chunk, OP_GET, expr.line, expr.column); } - if (peek_token(vs).type != TOKEN_RPAREN) { + if (peek_token(compiler).type != TOKEN_RPAREN) { error_push((Error){ .type = ERR_TYPE_COMPILER, .value = ERR_TOO_MANY_ARGS, @@ -243,13 +243,13 @@ compile_declare_op(Chunk *chunk, Visitor *vs, Token start, Ops op) { }); return; } - next_token(vs); + next_token(compiler); add_code(chunk, op, start.line, start.column); } void -compile_if_op(Chunk *chunk, Visitor *vs, Token start) { - Token tok = peek_token(vs); +compile_if_op(Chunk *chunk, Compiler *compiler, Token start) { + Token tok = peek_token(compiler); if (tok.type == TOKEN_EOF) { error_push((Error){ .type = ERR_TYPE_COMPILER, @@ -270,26 +270,26 @@ compile_if_op(Chunk *chunk, Visitor *vs, Token start) { } // Condition. - parse_tree(chunk, vs); + parse_tree(chunk, compiler); size_t jmp_false = emit_jump(chunk, start, OP_JUMP_IF_FALSE, 0xFFFF); // True expression. - parse_tree(chunk, vs); + parse_tree(chunk, compiler); // No second expression. - if (peek_token(vs).type == TOKEN_RPAREN) { + if (peek_token(compiler).type == TOKEN_RPAREN) { patch_jump(chunk, jmp_false); - next_token(vs); + next_token(compiler); return; } // False expression. size_t jmp_end = emit_jump(chunk, start, OP_JUMP, 0xFFFF); patch_jump(chunk, jmp_false); - parse_tree(chunk, vs); + parse_tree(chunk, compiler); patch_jump(chunk, jmp_end); - if (peek_token(vs).type != TOKEN_RPAREN) { + if (peek_token(compiler).type != TOKEN_RPAREN) { error_push((Error){ .type = ERR_TYPE_COMPILER, .value = ERR_TOO_MANY_ARGS, @@ -298,12 +298,12 @@ compile_if_op(Chunk *chunk, Visitor *vs, Token start) { }); return; } - next_token(vs); + next_token(compiler); } void -parse_list(Chunk *chunk, Visitor *vs, Token start) { - if (!has_next_token(vs)) { +parse_list(Chunk *chunk, Compiler *compiler, Token start) { + if (!has_next_token(compiler)) { error_push((Error){ .type = ERR_TYPE_COMPILER, .value = ERR_UNBALANCED_PAREN, @@ -312,27 +312,27 @@ parse_list(Chunk *chunk, Visitor *vs, Token start) { }); return; } - Token tok = next_token(vs); + Token tok = next_token(compiler); switch (tok.type) { - 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; - case TOKEN_DEF: { compile_declare_op(chunk, vs, start, OP_DEF); } break; - case TOKEN_SET: { compile_declare_op(chunk, vs, start, OP_SET); } break; - case TOKEN_IF: { compile_if_op(chunk, vs, start); } break; + case TOKEN_ADD: { compile_list_binary_op(chunk, compiler, start, OP_SUM); } break; + case TOKEN_SUB: { compile_list_binary_op(chunk, compiler, start, OP_SUB); } break; + case TOKEN_MUL: { compile_list_binary_op(chunk, compiler, start, OP_MUL); } break; + case TOKEN_DIV: { compile_list_binary_op(chunk, compiler, start, OP_DIV); } break; + case TOKEN_MOD: { compile_list_binary_op(chunk, compiler, start, OP_MOD); } break; + case TOKEN_NOT: { compile_list_unary_op(chunk, compiler, start, OP_NOT); } break; + case TOKEN_AND: { compile_list_binary_op(chunk, compiler, start, OP_AND); } break; + case TOKEN_OR: { compile_list_binary_op(chunk, compiler, start, OP_OR); } break; + case TOKEN_EQUAL: { compile_list_binary_op(chunk, compiler, start, OP_EQUAL); } break; + case TOKEN_LESS: { compile_list_binary_op(chunk, compiler, start, OP_LESS); } break; + case TOKEN_GREATER: { compile_list_binary_op(chunk, compiler, start, OP_GREATER); } break; + case TOKEN_LESS_EQUAL: { compile_list_binary_op(chunk, compiler, start, OP_LESS_EQUAL); } break; + case TOKEN_GREATER_EQUAL: { compile_list_binary_op(chunk, compiler, start, OP_GREATER_EQUAL); } break; + case TOKEN_PRINT: { compile_list_unary_op(chunk, compiler, start, OP_PRINT); } break; + case TOKEN_DISPLAY: { compile_list_unary_op(chunk, compiler, start, OP_DISPLAY); } break; + case TOKEN_NEWLINE: { compile_list_simple_op(chunk, compiler, start, OP_NEWLINE); } break; + case TOKEN_DEF: { compile_declare_op(chunk, compiler, start, OP_DEF); } break; + case TOKEN_SET: { compile_declare_op(chunk, compiler, start, OP_SET); } break; + case TOKEN_IF: { compile_if_op(chunk, compiler, start); } break; default: { error_push((Error){ .type = ERR_TYPE_COMPILER, @@ -345,8 +345,8 @@ parse_list(Chunk *chunk, Visitor *vs, Token start) { } void -parse_tree(Chunk *chunk, Visitor *vs) { - Token tok = next_token(vs); +parse_tree(Chunk *chunk, Compiler *compiler) { + Token tok = next_token(compiler); if (errors_n != 0) { return; } @@ -376,7 +376,7 @@ parse_tree(Chunk *chunk, Visitor *vs) { // Object *base = make_pair(obj_quote, obj_nil); // base->cdr = make_pair(obj_nil, obj_nil); // push_root(base); - // Object *next_obj = parse_tree(vs); + // Object *next_obj = parse_tree(compiler); // if (next_obj == obj_err) { // return obj_err; // } @@ -385,7 +385,7 @@ parse_tree(Chunk *chunk, Visitor *vs) { return; } break; case TOKEN_LPAREN: { - parse_list(chunk, vs, tok); + parse_list(chunk, compiler, tok); return; } break; case TOKEN_STRING: { @@ -419,13 +419,13 @@ Chunk * compile(Token *tokens) { Chunk *chunk = NULL; chunk = chunk_init(); - Visitor visitor = (Visitor){ + Compiler compiler = (Compiler){ .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); + Token start_tok = peek_token(&compiler); + while (has_next_token(&compiler) && peek_token(&compiler).type != TOKEN_EOF) { + parse_tree(chunk, &compiler); } add_code(chunk, OP_RETURN, start_tok.line, start_tok.column); return chunk; -- cgit v1.2.1