From adaf5c012fcebbbcd7152937b9d8835e7f0dcbbf Mon Sep 17 00:00:00 2001 From: LaEntropiaa Date: Wed, 4 Mar 2026 18:54:46 -0600 Subject: [PATCH] Redesigned everithing so that everything is cleaner and not making everything all messy to fix later, still, ther may be redesigns and shit but should be fine --- include/lexer.h | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lexer.c | 8 ++++++ 2 files changed, 76 insertions(+) diff --git a/include/lexer.h b/include/lexer.h index e69de29..2056711 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -0,0 +1,68 @@ +#ifndef LEXER_H +#define LEXER_H + +#include +#include + +// For identifing +typedef enum { + NODE_NUMBER, + NODE_OPERATOR, +} ASTNodeType; + +// For classify operators +typedef enum { + OP_ADD, + OP_SUB, + OP_MUL, + OP_DIV +} Operator; + +typedef enum { + ARRAY_OK = 0, + ARRAY_EMPTY, + ARRAY_OUT_OF_BOUNDS, + ARRAY_NULL_ARG, +} ASTNodeArrayErr; + +typedef enum { + LEXER_OK = 0, + LEXER_FAILED_NUMBER_CONVERSION, + LEXER_NOT_RECOGNIZED_SYMBOL, + LEXER_EMPTY_INPUT, + LEXER_NULL_ARG, + LEXER_WRONG_SYNTAX, +} LexerErr; + +// Can be thought as tokens, they will be used by the parser. +typedef struct ASTNode { + ASTNodeType type; + union { + double number; + struct ASTNode *left; + struct ASTNode *right; + Operator op; + } operator; +} ASTNode; + +// I prefer ot have a dynamic array for storing the "tokens" +typedef struct { + size_t len; + size_t cap; + ASTNode *data; +} ASTNodeArray; + +// Basic array functionality +ASTNodeArray ASTNodeArray_init(size_t size); +void ASTNodeArray_free(ASTNodeArray *arr); +ASTNodeArrayErr ASTNodeArray_push(ASTNodeArray arr, ASTNode node); +ASTNodeArrayErr ASTNodeArray_get(const ASTNodeArray arr, size_t index, ASTNode *out); +// Out in pop can be NULL so it doesn't return anything +ASTNodeArrayErr ASTNodeArray_pop(ASTNodeArray arr, size_t index, ASTNode *out); + +// Lexer funtions as well as few functionality +LexerErr tokenize(const char* input, ASTNodeArray *out); +LexerErr tokenize_number(const char* input, size_t *offset, ASTNode *out); +LexerErr string_to_number(const char* str, double *number); + +#endif // !LEXER_H diff --git a/src/lexer.c b/src/lexer.c index e69de29..7ab73c9 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -0,0 +1,8 @@ +#include "lexer.h" + +// Helps state machine for the lexer :) +typedef enum { + WAIT_FOR_NUMBER, + WAIT_FOR_OPERATOR, +} LexerState; +