From 80e05a9acfdd580d06645a3d9ad712ebbb5a1990 Mon Sep 17 00:00:00 2001 From: laentropia Date: Wed, 13 May 2026 10:02:55 -0600 Subject: [PATCH] refactor: changed parser.h, added Node So just added node back but now clearly separated by tokens and nodes of the AST as it should be, now real rework the mess that is the parser --- include/parser.h | 56 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/include/parser.h b/include/parser.h index 75bc70c..6dcc4fd 100644 --- a/include/parser.h +++ b/include/parser.h @@ -4,11 +4,30 @@ #include "lexer.h" #include "arena.h" #include "arraylist.h" +#include #include +typedef enum { + NODE_INT, + NODE_BINARY_OP, + NODE_UNARY_OP, +} NodeType; + typedef struct { - ASTNode *head; -} AST; + NodeType type; + union { + int64_t num; + struct { + Operator op; + struct Node *left; + struct Node *right; + }binary; + struct { + Operator op; + struct Node *to; + }unary; + }; +} Node; typedef enum { PARSER_OK = 0, @@ -24,20 +43,33 @@ typedef struct { ParserErr err; struct { Arena arena; - ASTNode *tree; + Node *tree; }; }; -} ParseResult; +} ParserResult; -ASTNode *nud(ArraySlice *slice); -ASTNode *led(ArraySlice *slice, size_t right_precedence); +typedef struct { + bool is_valid; + union { + ParserErr err; + Node *node; + }; +} NodeResult; -uint8_t prefix_rbp(ASTNode node); -uint8_t postfix_lbp(ASTNode node); -uint8_t infix_lbp(ASTNode node); -uint8_t infix_rbp(ASTNode node); +typedef struct { + bool is_valid; + union { + ParserErr err; + uint8_t num; + }; +} ParserU8Result; -ParseResult parse(TokenizeResult tokens); -ASTNode *parse_expr(ArraySlice *slice, Arena *arena, uint8_t min_bp); +ParserU8Result prefix_rbp(Node node); +ParserU8Result postfix_lbp(Node node); +ParserU8Result infix_lbp(Node node); +ParserU8Result infix_rbp(Node node); + +ParserResult parse(TokenizeResult tokens); +NodeResult parse_expr(ArraySlice *slice, Arena *arena, uint8_t min_bp); #endif // !PARSER_H