From 5e0f74c5b9586d4ec5a3d1cf4617a398b5999e18 Mon Sep 17 00:00:00 2001 From: Fredrik Robertsen Date: Sat, 14 Feb 2026 20:24:22 +0100 Subject: [PATCH] ps2: final touches --- ps2/src/parser.y | 14 ++++++++++---- ps2/src/scanner.l | 7 +++---- ps2/src/tree.c | 12 +++++++++--- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/ps2/src/parser.y b/ps2/src/parser.y index f84f3e4..abc538d 100644 --- a/ps2/src/parser.y +++ b/ps2/src/parser.y @@ -44,7 +44,8 @@ global_list : | global_list global { $$ = append_to_list_node($1, $2); } ; global : - global_variable_declaration { $$ = $1; } + function { $$ = $1; } + | global_variable_declaration { $$ = $1; } ; global_variable_declaration : VAR global_variable_list { $$ = node_create(GLOBAL_VARIABLE_DECLARATION, 1, $2); } @@ -55,6 +56,7 @@ global_variable_list : ; global_variable : identifier { $$ = $1; } + | array_indexing { $$ = $1; } ; array_indexing : identifier '[' expression ']' { $$ = node_create(ARRAY_INDEXING, 2, $1, $3); } @@ -133,7 +135,7 @@ while_statement : ; expression : expression '?' expression ':' expression { - $$ = node_create(OPERATOR, 3, $1, $2, $3); + $$ = node_create(OPERATOR, 3, $1, $3, $5); $$->data.operator = "?:"; } | expression OR expression { @@ -192,7 +194,7 @@ expression : $$ = node_create(OPERATOR, 1, $2); $$->data.operator = "!"; } %prec UNARY_OPERATORS - | '(' expression ')' { $$ = $1; } + | '(' expression ')' { $$ = $2; } | number { $$ = $1; } | identifier { $$ = $1; } | array_indexing { $$ = $1; } @@ -221,7 +223,11 @@ identifier : number : NUMBER_TOKEN { $$ = node_create(NUMBER_LITERAL, 0); - $$->data.number_literal = strtol(yytext, &yytext[0] + strlen(yytext), 10); + char *t; + $$->data.number_literal = strtol(yytext, &t, 10); + if (yytext == t) { + fprintf(stderr, "failed to parse number literal: %s", yytext); + } } ; string : diff --git a/ps2/src/scanner.l b/ps2/src/scanner.l index d932e1a..dad6d4b 100644 --- a/ps2/src/scanner.l +++ b/ps2/src/scanner.l @@ -15,14 +15,13 @@ WHITESPACE [ \v\t\n\r] COMMENT \/\/[^\n]+ QUOTED \"([^\"\n]|\\\")*\" -NUMERIC ^[0-9]+$ -BINDING ^[a-zA-Z]+[a-zA-Z0-9_]*$ +NUMERIC [0-9]+ +BINDING [a-zA-Z][a-zA-Z_0-9]* %% -{COMMENT} { /* Eliminate comments */ } {WHITESPACE}+ { /* Eliminate whitespace */ } +{COMMENT} { /* Eliminate comments */ } {QUOTED} { return STRING_TOKEN; } - func { return FUNC; } var { return VAR; } return { return RETURN; } diff --git a/ps2/src/tree.c b/ps2/src/tree.c index 9b08857..1ecf7e6 100644 --- a/ps2/src/tree.c +++ b/ps2/src/tree.c @@ -18,7 +18,7 @@ node_t* node_create(node_type_t type, size_t n_children, ...) node_t *node = malloc(sizeof(node_t)); node->type = type; node->n_children = n_children; - node->children = malloc(sizeof(node_t) * n_children); + node->children = malloc(sizeof(node_t *) * n_children); va_list args; va_start(args, n_children); @@ -118,8 +118,14 @@ static void node_print(node_t* node, int nesting) // Frees the memory owned by the given node, but does not touch its children static void node_finalize(node_t* discard) { - if (discard->type == IDENTIFIER || discard->type == STRING_LITERAL) - free(&discard->data); + switch (discard->type) { + case IDENTIFIER: + free(discard->data.identifier); + break; + case STRING_LITERAL: + free(discard->data.string_literal); + break; + } free(discard->children); free(discard); }