fix: updated main to a parse-execute loop and changed error codes from an enum to macros

This commit is contained in:
Guillem George 2026-01-16 20:05:49 +01:00 committed by william.valenduc
parent a4fcce530c
commit f475f4d4ec

View file

@ -1,4 +1,5 @@
// All the includes
// === Includes
#include <cstddef>
#include <stdio.h>
#include <stdlib.h>
@ -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;
}