diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 16c8752..77e9f92 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -41,8 +41,8 @@ static void save_state(char *stream, ssize_t i, struct lexer_context *ctx) ctx->remaining_chars -= i; ctx->end_previous_token = stream + i; - update_previous_token(ctx->current_token); - update_current_token(NULL); + update_previous_token(ctx->current_token, ctx); + update_current_token(NULL, ctx); } /* @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) return; - if (ctx->end_previous_token != NULL) - free(ctx->end_previous_token); - if (ctx->previous_token != NULL) - free(ctx->previous_token); - if (ctx->current_token != NULL) - free(ctx->current_token); + if ((*ctx)->previous_token != NULL) + free((*ctx)->previous_token); + if ((*ctx)->current_token != NULL) + free((*ctx)->current_token); free(*ctx); *ctx = NULL; } @@ -278,7 +276,7 @@ struct token *peek_token(struct lexer_context *ctx) return ctx->current_token; } - char *stream = stream_init(); + char *stream = stream_init(ctx); ssize_t i = 0; // 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); - update_current_token(tok); + update_current_token(tok, ctx); return tok; } @@ -326,7 +324,7 @@ struct token *pop_token(struct lexer_context *ctx) free_token(&ctx->current_token); return NULL; } - char *stream = stream_init(); + char *stream = stream_init(ctx); ssize_t i = 0; // 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); } - save_state(stream, i); + save_state(stream, i, ctx); return ctx->previous_token; } diff --git a/src/main.c b/src/main.c index ee2f36a..09b2c45 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include "execution/execution.h" #include "io_backend/io_backend.h" +#include "lexer/lexer.h" #include "parser/parser.h" #include "utils/args/args.h" #include "utils/vars/vars.h" @@ -69,8 +70,11 @@ int main(int argc, char **argv) free(io_context); + // init lexer context + struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context)); + // Retrieve and build first AST - struct ast *command_ast = get_ast(); + struct ast *command_ast = get_ast(ctx); // Main parse-execute loop while (command_ast != NULL && command_ast->type != AST_END) @@ -81,9 +85,11 @@ int main(int argc, char **argv) ast_free(&command_ast); // Retrieve and build next AST - command_ast = get_ast(); + command_ast = get_ast(ctx); } + destroy_lexer_context(&ctx); + ast_free(&command_ast); if (command_ast == NULL) diff --git a/src/parser/parser.c b/src/parser/parser.c index d1fd70a..885a274 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -15,9 +15,8 @@ // === 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 ast *res; @@ -44,8 +43,6 @@ struct ast *get_ast() return NULL; } */ - destroy_lexer_context(&ctx); - return res; } diff --git a/src/parser/parser.h b/src/parser/parser.h index 5089212..4e06511 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -1,6 +1,7 @@ #ifndef PARSER_H #define PARSER_H +#include "lexer/lexer.h" #include "utils/ast/ast.h" /* @brief Builds the AST representation of the next command to execute. @@ -10,7 +11,7 @@ * * @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. * diff --git a/src/parser/parsing_utils.c b/src/parser/parsing_utils.c index 08ef540..8511930 100644 --- a/src/parser/parsing_utils.c +++ b/src/parser/parsing_utils.c @@ -67,7 +67,7 @@ struct ast *parse_list(struct lexer_context *ctx) struct token *token = PEEK_TOKEN(); // and_or - current_node = parse_and_or(); + current_node = parse_and_or(ctx); if (current_node == NULL) return NULL; result_list = list_append(result_list, current_node); @@ -90,7 +90,7 @@ struct ast *parse_list(struct lexer_context *ctx) 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); } @@ -337,7 +337,7 @@ struct ast *parse_else_clause(struct lexer_context *ctx) } if (result == NULL) - result = ast_create_void(ctx); + result = ast_create_void(); return result; } diff --git a/src/parser/parsing_utils.h b/src/parser/parsing_utils.h index 5b32490..89eb4bf 100644 --- a/src/parser/parsing_utils.h +++ b/src/parser/parsing_utils.h @@ -1,6 +1,8 @@ #ifndef PARSING_UTILS_H #define PARSING_UTILS_H +#include "../lexer/lexer.h" + // === Macros #define PEEK_TOKEN() \