From 2a73f5f9d66063ad9cd808982bce23a11eabe252 Mon Sep 17 00:00:00 2001 From: laentropia Date: Wed, 13 May 2026 09:37:15 -0600 Subject: [PATCH] refactor: delete ASTNode, add Token to lexer So, total refactor, now we serious. I feel ASTNode was feeling very bloated so we need to rewrite and adapt everything, by now lets get the lexer working again, is already well written for me at least. --- include/lexer.h | 39 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/include/lexer.h b/include/lexer.h index 4af0f9d..b997eb8 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -8,12 +8,9 @@ // For identifing typedef enum { - NODE_INTEGER, - NODE_BINARY_OP, - NODE_UNARY_OP, - NODE_PARENTHESIS, -} ASTNodeType; - + TOKEN_INTEGER, + TOKEN_OPERATOR, +} TokenType; // For classify operators typedef enum { OP_ADD, @@ -36,25 +33,13 @@ typedef enum { } LexerErr; // Can be thought as tokens, they will be used by the parser. -typedef struct ASTNode { - ASTNodeType type; +typedef struct { + TokenType type; union { - int64_t integer; - struct { - struct ASTNode *left; - struct ASTNode *right; - Operator op; - } binary; - struct { - struct ASTNode *val; - Operator op; - } unary; - struct { - struct ASTNode *val; - Operator op; - } parenthesis; - } data; -} ASTNode; + int64_t num; + Operator op; + }; +} Token; typedef struct { bool is_valid; @@ -68,9 +53,9 @@ typedef struct { bool is_valid; union { LexerErr err; - ASTNode node; + Token token; }; -} ASTNodeResult; +} TokenResult; typedef struct { bool is_valid; @@ -82,7 +67,7 @@ typedef struct { // Lexer funtions as well as few functionality TokenizeResult tokenize(const char* input); -ASTNodeResult tokenize_number(const char* input, size_t *offset); +TokenResult tokenize_number(const char* input, size_t *offset); LexerI64Result string_to_integer(const char buf[]); bool isoperator(int c); Operator char_to_operator(int c);