From b3d44317b03f73a0db68e65af79f3d00788a370e Mon Sep 17 00:00:00 2001 From: Guillem George Date: Fri, 16 Jan 2026 20:05:49 +0100 Subject: [PATCH] fix: updated main to a parse-execute loop and changed error codes from an enum to macros --- src/main.c | 58 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/src/main.c b/src/main.c index b664786..399b874 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ -// All the includes +// === Includes +#include #include #include @@ -7,22 +8,24 @@ #include "parser/parser.h" #include "utils/args/args.h" -enum return_codes -{ - SUCCESS = 0, - ERR_IO_BACKEND = 2, - ERR_MALLOC = 3 -}; +// === Error codes + +#define SUCCESS 0 +#define ERR_INPUT_PROCESSING 2 +#define ERR_MALLOC 3 +#define ERR_GENERIC 4 + +// === Functions int main(int argc, char **argv) { // Create the options struct (with argument handler) struct args_options options; - int r = args_handler(argc, argv, &options); - if (r != 0) + int return_code = args_handler(argc, argv, &options); + if (return_code != 0) { print_usage(stderr, argv[0]); - return ERR_IO_BACKEND; + return ERR_INPUT_PROCESSING; } // args_print(&options); @@ -36,33 +39,42 @@ int main(int argc, char **argv) } // Convert args_options to iob_context - r = iob_config_from_args(&options, io_context); - if (r != 0) + return_code = iob_config_from_args(&options, io_context); + if (return_code != 0) { fprintf(stderr, "Error: Failed to configure IO Backend from arguments\n"); free(io_context); - return ERR_IO_BACKEND; + return ERR_INPUT_PROCESSING; } // Init IO Backend (with the context struct) - r = iob_init(io_context); - if (r != 0) + return_code = iob_init(io_context); + if (return_code != 0) { fprintf(stderr, - "Error: IO Backend initialization failed with code %d\n", r); + "Error: IO Backend initialization failed with code %d\n", + return_code); free(io_context); - return ERR_IO_BACKEND; + return ERR_INPUT_PROCESSING; } free(io_context); - // Call the parser to get the AST - struct ast *command_ast = get_ast(); // We'll pass the options later + // Retrieve and build first AST + struct ast *command_ast = get_ast(); - // Call the executor with the AST - r = execution(command_ast); + // Main parse-execute loop + while (command_ast != NULL && command_ast->type != AST_END) + { + // Execute AST + return_code = execution(command_ast); - // Return the execution return code (last command executed) - return r; + // Retrieve and build next AST + command_ast = get_ast(); + } + if (command_ast == NULL) + return ERR_INPUT_PROCESSING; + + return return_code; }