From e01baf43c60d804d7177ba11ca94dbb0c8a855f0 Mon Sep 17 00:00:00 2001 From: Fredrik Robertsen Date: Wed, 1 Apr 2026 21:16:11 +0200 Subject: [PATCH] ps5: errata for simple-print to work --- ps5/src/generator.c | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/ps5/src/generator.c b/ps5/src/generator.c index d3732fe..1b70e14 100644 --- a/ps5/src/generator.c +++ b/ps5/src/generator.c @@ -73,7 +73,7 @@ static void generate_stringtable(void) // TODO (Task 1): Print all strings in the program here, with labels you can refer to later // You have access to the global variables string_list and string_list_len from symbols.c for (long int i = 0; i < string_list_len; i++) { - DIRECTIVE("string%ld:\t.asciz \"%s\"", i, string_list[i]); + DIRECTIVE("string%ld:\t.asciz %s", i, string_list[i]); } } @@ -151,7 +151,7 @@ static void generate_function(symbol_t* function) } // TODO (Task 4): the function body can be sent to generate_statement() - generate_statement(function->node); + generate_statement(function->node->children[2]); // TODO (Task 3.2): Emit the epilogue, including a label and a default return value (0) CLEAR(RAX); @@ -354,27 +354,24 @@ static void generate_statement(node_t* node) // The statements you must handle are BLOCK, ASSIGNMENT_STATEMENT, // PRINT_STATEMENT, RETURN_STATEMENT and FUNCTION_CALL. // Statements of type LOCAL_VARIABLE should be ignored. - for (int i = 0; i < node->n_children; i++) { - node_t *child = node->children[i]; - switch (child->type) { - case BLOCK: - node_t *list = child->children[0]; - for (int j = 0; j < list->n_children; j++) - generate_statement(list->children[j]); - break; - case ASSIGNMENT_STATEMENT: - generate_assignment_statement(child); - break; - case PRINT_STATEMENT: - generate_print_statement(child); - break; - case RETURN_STATEMENT: - generate_return_statement(child); - break; - case FUNCTION_CALL: - generate_function_call(child); - break; - } + switch (node->type) { + case BLOCK: + node_t *list = node->children[0]; + for (int j = 0; j < list->n_children; j++) + generate_statement(list->children[j]); + break; + case ASSIGNMENT_STATEMENT: + generate_assignment_statement(node); + break; + case PRINT_STATEMENT: + generate_print_statement(node); + break; + case RETURN_STATEMENT: + generate_return_statement(node); + break; + case FUNCTION_CALL: + generate_function_call(node); + break; } }