// === Includes #include #include #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" // === Error codes #define SUCCESS 0 #define ERR_INPUT_PROCESSING 2 #define ERR_MALLOC 3 #define ERR_GENERIC 4 // === Functions /* @brief: frees the hash map. * @return: always ERR_INPUT_PROCESSING. */ static int err_input(struct hash_map **vars) { hash_map_free(vars); return ERR_INPUT_PROCESSING; } static int main_loop(struct lexer_context *ctx, struct args_options *options, struct hash_map *vars) { int return_code = SUCCESS; // Retrieve and build first AST struct ast *command_ast = get_ast(ctx); if (options->pretty_print) { ast_print_dot(command_ast); } // Main parse-execute loop while (command_ast != NULL && command_ast->type != AST_END) { if (command_ast->type != AST_VOID) { // Execute AST return_code = execution(command_ast, vars); // set $? variable set_var_int(vars, "?", return_code); } ast_free(&command_ast); // Retrieve and build next AST command_ast = get_ast(ctx); } if (command_ast == NULL) return err_input(&vars); ast_free(&command_ast); return return_code; } int main(int argc, char **argv) { struct hash_map *vars = vars_init(); if (vars == NULL) { fprintf(stderr, "Error: Failed to initialize variables hash map\n"); return ERR_MALLOC; } // Create the options struct (with argument handler) struct args_options options; int return_code = args_handler(argc, argv, &options, vars); if (return_code != 0) { print_usage(stderr, argv[0]); return err_input(&vars); } // args_print(&options); // Initialize variables hash map // Create the IO-Backend context struct struct iob_context io_context = { 0 }; // Convert args_options to iob_context return_code = iob_config_from_args(&options, &io_context); if (return_code != 0) { fprintf(stderr, "Error: Failed to configure IO Backend from arguments\n"); return err_input(&vars); } // Init IO Backend (with the context struct) return_code = iob_init(&io_context); if (return_code != 0) { fprintf(stderr, "Error: IO Backend initialization failed with code %d\n", return_code); return err_input(&vars); } // init lexer context struct lexer_context ctx = { 0 }; return_code = main_loop(&ctx, &options,vars); // === free hash_map_free(&vars); return return_code; }