From 604839d324e1c24974c93feeff14de4b8b024932 Mon Sep 17 00:00:00 2001 From: William Bell Date: Tue, 3 Jun 2025 13:57:49 +0100 Subject: [PATCH] add else if, else, and comments --- src/lexer/lex.l | 3 + src/lexer/token.h | 1 + src/parser/assign/assign.c | 8 +++ src/parser/assign/assign.h | 1 + src/parser/if/if.c | 133 +++++++++++++++++++++++++++---------- src/parser/if/if.h | 10 +-- src/parser/parser.c | 7 +- test.ar | 3 +- 8 files changed, 122 insertions(+), 44 deletions(-) diff --git a/src/lexer/lex.l b/src/lexer/lex.l index 345a282..48d4015 100644 --- a/src/lexer/lex.l +++ b/src/lexer/lex.l @@ -47,6 +47,7 @@ int yywrap(void *) { "if" { return TOKEN_IF; } +"else if" { return TOKEN_ELSE_IF; } "else" { return TOKEN_ELSE; } "while" { return TOKEN_WHILE; } "forever" { return TOKEN_FOREVER; } @@ -102,6 +103,8 @@ int yywrap(void *) { COLUMN_NO += yyleng; } +#[^\n]* {} + . { GET_STATE fprintf(stderr, "%s:%u:%u error: unexpected character '%s'\n", state->path, yylineno, COLUMN_NO+1, yytext); diff --git a/src/lexer/token.h b/src/lexer/token.h index 9172bd9..3312fc4 100644 --- a/src/lexer/token.h +++ b/src/lexer/token.h @@ -39,6 +39,7 @@ typedef enum { // Keywords TOKEN_IF, + TOKEN_ELSE_IF, TOKEN_ELSE, TOKEN_WHILE, TOKEN_FOREVER, diff --git a/src/parser/assign/assign.c b/src/parser/assign/assign.c index a3eaa79..96d1d36 100644 --- a/src/parser/assign/assign.c +++ b/src/parser/assign/assign.c @@ -8,7 +8,14 @@ ParsedValue *parse_assign(char *file, DArray *parsed, DArray *tokens, ParsedValue *assign_to, size_t *index) { + bool islet = false; Token *token = darray_get(tokens, *index); + if (token->type == TOKEN_LET) { + islet = true; + (*index)++; + error_if_finished(file,tokens,index); + token = darray_get(tokens, *index); + } switch (assign_to->type) { case AST_IDENTIFIER: case AST_ASSIGN: @@ -25,6 +32,7 @@ ParsedValue *parse_assign(char *file, DArray *parsed, DArray *tokens, error_if_finished(file,tokens,index); token = darray_get(tokens, *index); assign->from = parse_token(file, parsed, tokens, index, true); + assign->let = islet; ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue)); parsedValue->type = AST_ASSIGN; parsedValue->data = assign; diff --git a/src/parser/assign/assign.h b/src/parser/assign/assign.h index 9596c51..3e006b9 100644 --- a/src/parser/assign/assign.h +++ b/src/parser/assign/assign.h @@ -4,6 +4,7 @@ #include "../../lexer/token.h" typedef struct { + bool let; ParsedValue * to; TokenType type; ParsedValue * from; diff --git a/src/parser/if/if.c b/src/parser/if/if.c index 30f50e2..ea0a5ef 100644 --- a/src/parser/if/if.c +++ b/src/parser/if/if.c @@ -10,54 +10,115 @@ ParsedValue *parse_if(char *file, DArray *parsed, DArray *tokens, size_t *index) { (*index)++; error_if_finished(file, tokens, index); - Token *token = darray_get(tokens, *index); - if (token->type != TOKEN_LPAREN) { - fprintf(stderr, - "%s:%u:%u error: if statement requires paren for the condition\n", - file, token->line, token->column); - exit(EXIT_FAILURE); - } - (*index)++; - error_if_finished(file, tokens, index); + DArray *parsed_if = checked_malloc(sizeof(DArray)); darray_init(parsed_if, sizeof(ParsedConditional)); - DArray *condition = checked_malloc(sizeof(DArray)); - darray_init(condition, sizeof(ParsedValue)); - while ((*index) < tokens->size) { - ParsedValue *parsed_code = parse_token(file, parsed, tokens, index, true); - if (parsed_code) { - darray_push(condition, parsed_code); - free(parsed_code); + + bool expect_conditional = true; + + while (*index < tokens->size) { + Token *token = darray_get(tokens, *index); + + // Handle TOKEN_ELSE or TOKEN_ELSE_IF for subsequent branches + if (!expect_conditional) { + if (token->type != TOKEN_NEW_LINE) + break; // no more branches + (*index)++; + error_if_finished(file, tokens, index); + token = darray_get(tokens, *index); + + if (token->type == TOKEN_ELSE || token->type == TOKEN_ELSE_IF) { + (*index)++; + error_if_finished(file, tokens, index); + } else { + break; // no more branches + } } - token = darray_get(tokens, *index); - if (token->type == TOKEN_RPAREN) - break; + + DArray *condition = NULL; + + if (token->type != TOKEN_ELSE) { + // Parse ( condition ) + token = darray_get(tokens, *index); + if (token->type != TOKEN_LPAREN) { + fprintf(stderr, + "%s:%u:%u error: expected '(' after if\n", + file, token->line, token->column); + exit(EXIT_FAILURE); + } + + (*index)++; + error_if_finished(file, tokens, index); + + condition = checked_malloc(sizeof(DArray)); + darray_init(condition, sizeof(ParsedValue)); + + while (*index < tokens->size) { + ParsedValue *parsed_code = parse_token(file, parsed, tokens, index, true); + if (parsed_code) { + darray_push(condition, parsed_code); + free(parsed_code); + } + + token = darray_get(tokens, *index); + if (token->type == TOKEN_RPAREN) + break; + } + + if (token->type != TOKEN_RPAREN) { + fprintf(stderr, + "%s:%u:%u error: missing closing ')' in condition\n", + file, token->line, token->column); + exit(EXIT_FAILURE); + } + + (*index)++; + error_if_finished(file, tokens, index); + } + + // Parse the body + ParsedValue *parsed_content = + parse_token(file, parsed, tokens, index, false); + + if (!parsed_content) { + fprintf(stderr, + "%s:%u:%u error: expected body after condition\n", + file, token->line, token->column); + exit(EXIT_FAILURE); + } + + ParsedConditional conditional = {condition, parsed_content}; + darray_push(parsed_if, &conditional); + + expect_conditional = false; // After first iteration, expect newline + else/else if } - if (token->type != TOKEN_RPAREN) { - fprintf(stderr, - "%s:%u:%u error: missing closing parenthesis in if condition\n", - file, token->line, token->column); - exit(EXIT_FAILURE); - } - (*index)++; - error_if_finished(file, tokens, index); - ParsedValue *parsed_content = parse_token(file, parsed, tokens, index, true); - if (!parsed_content) { - fprintf(stderr, "%s:%u:%u error: expected body after if condition\n", file, - token->line, token->column); - exit(EXIT_FAILURE); - } - ParsedConditional output_conditional = {condition, parsed_content}; - darray_push(parsed_if, &output_conditional); + ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue)); parsedValue->type = AST_IF; parsedValue->data = parsed_if; + + // printf("Parsed if chain:\n"); + // for (size_t i = 0; i < parsed_if->size; i++) { + // ParsedConditional *cond = darray_get(parsed_if, i); + // if (cond->condition) { + // printf(" if/else if condition:\n"); + // for (size_t j = 0; j < cond->condition->size; j++) { + // ParsedValue *v = darray_get(cond->condition, j); + // printf(" - condition value type: %d\n", v->type); + // } + // } else { + // printf(" else:\n"); + // } + // printf(" - content value type: %d\n", cond->content->type); + // } + return parsedValue; } void free_conditional(void *ptr) { ParsedConditional *conditional = ptr; - darray_free(conditional->condition, free_parsed); + if (conditional->condition) + darray_free(conditional->condition, free_parsed); free_parsed(conditional->content); } diff --git a/src/parser/if/if.h b/src/parser/if/if.h index 391b314..22d19c3 100644 --- a/src/parser/if/if.h +++ b/src/parser/if/if.h @@ -2,17 +2,17 @@ #ifndef iF_H #define iF_H +#include "../../lexer/token.h" // for Token #include "../parser.h" -#include "../../lexer/token.h" // for Token typedef struct { - DArray * condition; - ParsedValue * content; + DArray *condition; // NULL for 'else' + ParsedValue *content; } ParsedConditional; -ParsedValue *parse_if(char *file, DArray *parsed, DArray *tokens, size_t *index); +ParsedValue *parse_if(char *file, DArray *parsed, DArray *tokens, + size_t *index); void free_parsed_if(void *ptr); - #endif // iF_H \ No newline at end of file diff --git a/src/parser/parser.c b/src/parser/parser.c index bd0ef43..ecf3f4a 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -12,7 +12,8 @@ #include #include -const char *ValueTypeNames[] = {"string", "assign", "identifier", "number", "if statement"}; +const char *ValueTypeNames[] = {"string", "assign", "identifier", "number", + "if statement"}; void error_if_finished(char *file, DArray *tokens, size_t *index) { if ((*index) >= tokens->size) { @@ -49,6 +50,7 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens, fprintf(stderr, "%s:%u:%u error: invalid indentation\n", file, token->line, token->column); exit(EXIT_FAILURE); + case TOKEN_LET: case TOKEN_IDENTIFIER:; ParsedValue *assign_to = parse_identifier(token); (*index)++; @@ -90,7 +92,8 @@ ParsedValue *parse_token(char *file, DArray *parsed, DArray *tokens, (*index)++; return parse_number(token); default: - fprintf(stderr, "Panic: unreachable\n"); + fprintf(stderr, "%s:%u:%u error: syntax error\n", file, token->line, + token->column); exit(EXIT_FAILURE); } } diff --git a/test.ar b/test.ar index cade759..a2517d9 100644 --- a/test.ar +++ b/test.ar @@ -1 +1,2 @@ -if (x=10) "hello world" \ No newline at end of file +let x = 10 # hello world +if (x = 10) "hello world" \ No newline at end of file