From 6ad3e61a2b5c0629012d6db2c3bb56e55a3a567b Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Tue, 3 Dec 2024 04:13:40 +0000 Subject: Fix errors due to no loops --- assembler.c | 15 +++++++++------ parser.c | 33 ++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/assembler.c b/assembler.c index 36cd2c0..df92f1e 100644 --- a/assembler.c +++ b/assembler.c @@ -120,12 +120,15 @@ void asm_translate_nodes(buffer_t **asm_buffer, struct PResult nodes, const char *src_name) { // First pass: Setup the ASM label array - struct Label labels[nodes.labels * 2]; - u64 label_ptr = 0; - for (size_t i = 0; i < nodes.size; ++i) - if (nodes.nodes[i].type == LIN || nodes.nodes[i].type == LOUT) - labels[label_ptr++] = - (struct Label){.cur = i, .next = nodes.nodes[i].loop_ref}; + struct Label labels[nodes.labels ? nodes.labels * 2 : 1]; + if (nodes.labels) + { + u64 label_ptr = 0; + for (size_t i = 0; i < nodes.size; ++i) + if (nodes.nodes[i].type == LIN || nodes.nodes[i].type == LOUT) + labels[label_ptr++] = + (struct Label){.cur = i, .next = nodes.nodes[i].loop_ref}; + } // Second pass: Translating to assembly for (size_t i = 0; i < nodes.size; ++i) diff --git a/parser.c b/parser.c index 101f3e3..c7765ad 100644 --- a/parser.c +++ b/parser.c @@ -121,26 +121,29 @@ struct PResult parse_buffer(buffer_t *buffer) } // Third pass: setup loop references - node_t *stack[loops]; - memset(stack, 0, loops); size_t stackptr = 0; - for (size_t i = 0; i < usable; ++i) + if (loops) { - node_t *node = nodes + i; - if (node->type == LIN) - stack[stackptr++] = node; - else if (node->type == LOUT) + node_t *stack[loops]; + memset(stack, 0, loops); + for (size_t i = 0; i < usable; ++i) { - if (stackptr == 0) + node_t *node = nodes + i; + if (node->type == LIN) + stack[stackptr++] = node; + else if (node->type == LOUT) { - print_error(buffer->name, node->row, node->col, - "ERROR: Unbalanced square brackets!"); - goto error; + if (stackptr == 0) + { + print_error(buffer->name, node->row, node->col, + "ERROR: Unbalanced square brackets!"); + goto error; + } + // Access last IN loop + --stackptr; + node->loop_ref = stack[stackptr] - nodes; + stack[stackptr]->loop_ref = i; } - // Access last IN loop - --stackptr; - node->loop_ref = stack[stackptr] - nodes; - stack[stackptr]->loop_ref = i; } } -- cgit v1.2.3-13-gbd6f