From 0c9f97b41db44901bbf1d38677ac8be1ae56e7a4 Mon Sep 17 00:00:00 2001 From: Kyler Date: Thu, 19 Jun 2025 20:48:30 -0600 Subject: [PATCH] implemented parse_expression --- src/syntax.c | 70 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/src/syntax.c b/src/syntax.c index c06756d..f6a1951 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -11,7 +11,6 @@ #include "sync/syntax.h" typedef struct { - const char* filename; TokenArray tokens; size_t pos; } Parser; @@ -106,7 +105,45 @@ static SyntaxResult syntax_error(const char* message, Token token) { }}; } +static SyntaxResult parse_expression(Parser* parser) { +} + static SyntaxResult parse_statement(Parser* parser) { + Child* children = NULL; + + if ( + parser->tokens.tokens[parser->pos].type == TOKEN_EOF || + parser->tokens.tokens[parser->pos].type == TOKEN_RBRACE + ) return syntax_error("Expected statement", parser->tokens.tokens[parser->pos]); + SyntaxResult result = parse_expression(parser); + if (result.type == SYNC_ERROR) return result; + if (result.result.type == SYNC_ERROR) return result; + if (children == NULL) { + children = (Child*)malloc(sizeof(Child)); + if (children == NULL) + return (SyntaxResult){SYNC_ERROR, .error = (GeneralError){"Failed to allocate memory.", 1}}; + children->next = NULL; + children->node = result.result.result; + } else { + GeneralError error = add_child(children, result.result.result); + if (error.message != NULL) return (SyntaxResult){SYNC_ERROR, .error = error}; + } + + if (parser->tokens.tokens[parser->pos].type != TOKEN_SEMICOLON) + return syntax_error("Expected statement", parser->tokens.tokens[parser->pos]); + + SyntaxNode node = { + .type = NODE_SEMICOLON, + .start = parser->tokens.tokens[parser->pos].start, + .length = parser->tokens.tokens[parser->pos].length, + .file_info = parser->tokens.tokens[parser->pos].file_info, + .children = (Children){0, NULL} + }; + GeneralError error = add_child(children, result.result.result); + if (error.message != NULL) return (SyntaxResult){SYNC_ERROR, .error = error}; + parser->pos++; + + return syntax_result(NODE_STATEMENT, children); } static SyntaxResult parse_block(Parser* parser) { @@ -120,10 +157,14 @@ static SyntaxResult parse_block(Parser* parser) { if (result.result.type == SYNC_ERROR) return result; if (children == NULL) { children = (Child*)malloc(sizeof(Child)); - if (children == NULL) return (SyntaxResult){SYNC_ERROR, .error = (GeneralError){"Failed to allocate memory.", 1}}; + if (children == NULL) + return (SyntaxResult){SYNC_ERROR, .error = (GeneralError){"Failed to allocate memory.", 1}}; children->next = NULL; children->node = result.result.result; - } else add_child(children, result.result.result); + } else { + GeneralError error = add_child(children, result.result.result); + if (error.message != NULL) return (SyntaxResult){SYNC_ERROR, .error = error}; + } if (parser->tokens.tokens[parser->pos].type == TOKEN_EOF) return syntax_error("Expected '}'", parser->tokens.tokens[parser->pos]); } @@ -141,26 +182,25 @@ static SyntaxResult parse_block(Parser* parser) { return syntax_result(NODE_BLOCK, children); } -SyntaxResult syntactical_analysis(TokenArray tokens) { - Parser* parser = (Parser*)malloc(sizeof(Parser)); - if (parser == NULL) return (SyntaxResult){SYNC_ERROR, .error = (GeneralError){"Failed to allocate memory.", 1}}; +SyntaxResult syntactical_analysis(const char* filename, TokenArray tokens) { + Parser parser = { tokens, 0 }; Child* children = NULL; - while (parser->tokens.tokens[parser->pos].type != TOKEN_EOF) { - SyntaxResult result = parse_block(parser); - if (result.type == SYNC_ERROR) { free(parser); return result; } - if (result.result.type == SYNC_ERROR) { free(parser); return result; } + while (parser.tokens.tokens[parser.pos].type != TOKEN_EOF) { + SyntaxResult result = parse_block(&parser); + if (result.type == SYNC_ERROR) return result; + if (result.result.type == SYNC_ERROR) return result; if (children == NULL) { children = (Child*)malloc(sizeof(Child)); - if (children == NULL) { - free(parser); + if (children == NULL) return (SyntaxResult){SYNC_ERROR, .error = (GeneralError){"Failed to allocate memory.", 1}}; - } children->next = NULL; children->node = result.result.result; - } else add_child(children, result.result.result); + } else { + GeneralError error = add_child(children, result.result.result); + if (error.message != NULL) return (SyntaxResult){SYNC_ERROR, .error = error}; + } } - free(parser); return syntax_result(NODE_FILE, children); }