fix: new lexer is linked to everything

This commit is contained in:
Matteo Flebus 2026-01-20 20:32:59 +01:00
parent d5a1ec3ca6
commit 71e58e38b8
6 changed files with 26 additions and 22 deletions

View file

@ -41,8 +41,8 @@ static void save_state(char *stream, ssize_t i, struct lexer_context *ctx)
ctx->remaining_chars -= i; ctx->remaining_chars -= i;
ctx->end_previous_token = stream + i; ctx->end_previous_token = stream + i;
update_previous_token(ctx->current_token); update_previous_token(ctx->current_token, ctx);
update_current_token(NULL); update_current_token(NULL, ctx);
} }
/* @return: true if a special character from the grammar was found, /* @return: true if a special character from the grammar was found,
@ -182,12 +182,10 @@ void destroy_lexer_context(struct lexer_context **ctx)
{ {
if (ctx == NULL || *ctx == NULL) if (ctx == NULL || *ctx == NULL)
return; return;
if (ctx->end_previous_token != NULL) if ((*ctx)->previous_token != NULL)
free(ctx->end_previous_token); free((*ctx)->previous_token);
if (ctx->previous_token != NULL) if ((*ctx)->current_token != NULL)
free(ctx->previous_token); free((*ctx)->current_token);
if (ctx->current_token != NULL)
free(ctx->current_token);
free(*ctx); free(*ctx);
*ctx = NULL; *ctx = NULL;
} }
@ -278,7 +276,7 @@ struct token *peek_token(struct lexer_context *ctx)
return ctx->current_token; return ctx->current_token;
} }
char *stream = stream_init(); char *stream = stream_init(ctx);
ssize_t i = 0; ssize_t i = 0;
// Usefull to know if we are inside a quote or double quote // Usefull to know if we are inside a quote or double quote
@ -313,7 +311,7 @@ struct token *peek_token(struct lexer_context *ctx)
} }
struct token *tok = new_token(stream, i); struct token *tok = new_token(stream, i);
update_current_token(tok); update_current_token(tok, ctx);
return tok; return tok;
} }
@ -326,7 +324,7 @@ struct token *pop_token(struct lexer_context *ctx)
free_token(&ctx->current_token); free_token(&ctx->current_token);
return NULL; return NULL;
} }
char *stream = stream_init(); char *stream = stream_init(ctx);
ssize_t i = 0; ssize_t i = 0;
// Usefull to know if we are inside a quote or double quote // Usefull to know if we are inside a quote or double quote
@ -366,7 +364,7 @@ struct token *pop_token(struct lexer_context *ctx)
{ {
ctx->current_token = new_token(stream, i); ctx->current_token = new_token(stream, i);
} }
save_state(stream, i); save_state(stream, i, ctx);
return ctx->previous_token; return ctx->previous_token;
} }

View file

@ -4,6 +4,7 @@
#include "execution/execution.h" #include "execution/execution.h"
#include "io_backend/io_backend.h" #include "io_backend/io_backend.h"
#include "lexer/lexer.h"
#include "parser/parser.h" #include "parser/parser.h"
#include "utils/args/args.h" #include "utils/args/args.h"
#include "utils/vars/vars.h" #include "utils/vars/vars.h"
@ -69,8 +70,11 @@ int main(int argc, char **argv)
free(io_context); free(io_context);
// init lexer context
struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context));
// Retrieve and build first AST // Retrieve and build first AST
struct ast *command_ast = get_ast(); struct ast *command_ast = get_ast(ctx);
// Main parse-execute loop // Main parse-execute loop
while (command_ast != NULL && command_ast->type != AST_END) while (command_ast != NULL && command_ast->type != AST_END)
@ -81,9 +85,11 @@ int main(int argc, char **argv)
ast_free(&command_ast); ast_free(&command_ast);
// Retrieve and build next AST // Retrieve and build next AST
command_ast = get_ast(); command_ast = get_ast(ctx);
} }
destroy_lexer_context(&ctx);
ast_free(&command_ast); ast_free(&command_ast);
if (command_ast == NULL) if (command_ast == NULL)

View file

@ -15,9 +15,8 @@
// === Functions // === Functions
struct ast *get_ast() struct ast *get_ast(struct lexer_context *ctx)
{ {
struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context));
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();
struct ast *res; struct ast *res;
@ -44,8 +43,6 @@ struct ast *get_ast()
return NULL; return NULL;
} }
*/ */
destroy_lexer_context(&ctx);
return res; return res;
} }

View file

@ -1,6 +1,7 @@
#ifndef PARSER_H #ifndef PARSER_H
#define PARSER_H #define PARSER_H
#include "lexer/lexer.h"
#include "utils/ast/ast.h" #include "utils/ast/ast.h"
/* @brief Builds the AST representation of the next command to execute. /* @brief Builds the AST representation of the next command to execute.
@ -10,7 +11,7 @@
* *
* @warning NOT IMPLEMENTED * @warning NOT IMPLEMENTED
*/ */
struct ast *get_ast(void); struct ast *get_ast(struct lexer_context *ctx);
/* @brief Builds the AST representation of the given command string. /* @brief Builds the AST representation of the given command string.
* *

View file

@ -67,7 +67,7 @@ struct ast *parse_list(struct lexer_context *ctx)
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();
// and_or // and_or
current_node = parse_and_or(); current_node = parse_and_or(ctx);
if (current_node == NULL) if (current_node == NULL)
return NULL; return NULL;
result_list = list_append(result_list, current_node); result_list = list_append(result_list, current_node);
@ -90,7 +90,7 @@ struct ast *parse_list(struct lexer_context *ctx)
token = PEEK_TOKEN(); token = PEEK_TOKEN();
} }
} }
result_list = list_append(result_list, current_node); // result_list = list_append(result_list, current_node);
return ast_create_list(result_list); return ast_create_list(result_list);
} }
@ -337,7 +337,7 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
} }
if (result == NULL) if (result == NULL)
result = ast_create_void(ctx); result = ast_create_void();
return result; return result;
} }

View file

@ -1,6 +1,8 @@
#ifndef PARSING_UTILS_H #ifndef PARSING_UTILS_H
#define PARSING_UTILS_H #define PARSING_UTILS_H
#include "../lexer/lexer.h"
// === Macros // === Macros
#define PEEK_TOKEN() \ #define PEEK_TOKEN() \