diff --git a/src/main.c b/src/main.c index 1c802d7..1d9f7b2 100644 --- a/src/main.c +++ b/src/main.c @@ -18,6 +18,44 @@ // === Functions +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_PROCESSING; + + ast_free(&command_ast); + + return return_code; +} + int main(int argc, char **argv) { struct hash_map *vars = vars_init(); @@ -40,74 +78,36 @@ int main(int argc, char **argv) // Initialize variables hash map // Create the IO-Backend context struct - struct iob_context *io_context = malloc(sizeof(struct iob_context)); - if (io_context == NULL) - { - fprintf(stderr, - "Error: Memory allocation failed for IO Backend context\n"); - return ERR_MALLOC; - } + struct iob_context io_context = { 0 }; // Convert args_options to iob_context - return_code = iob_config_from_args(&options, io_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"); - free(io_context); return ERR_INPUT_PROCESSING; } // Init IO Backend (with the context struct) - return_code = iob_init(io_context); + return_code = iob_init(&io_context); if (return_code != 0) { fprintf(stderr, "Error: IO Backend initialization failed with code %d\n", return_code); - free(io_context); return ERR_INPUT_PROCESSING; } - free(io_context); // init lexer context - struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context)); + struct lexer_context ctx = { 0 }; - // Retrieve and build first AST - struct ast *command_ast = get_ast(ctx); + return_code = main_loop(&ctx, &options,vars); - 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); - } - - destroy_lexer_context(&ctx); + // === free hash_map_free(&vars); - if (command_ast == NULL) - return ERR_INPUT_PROCESSING; - - ast_free(&command_ast); - return return_code; } diff --git a/src/utils/lists/lists.c b/src/utils/lists/lists.c index be97835..ba0ec50 100644 --- a/src/utils/lists/lists.c +++ b/src/utils/lists/lists.c @@ -4,63 +4,6 @@ #include #include -struct list *list_prepend(struct list *list, void *value) -{ - struct list *new_elt = malloc(sizeof(struct list)); - if (new_elt == NULL) - { - return NULL; - } - new_elt->next = list; - new_elt->data = value; - - return new_elt; -} - -size_t list_length(struct list *list) -{ - size_t len = 0; - while (list != NULL) - { - len++; - list = list->next; - } - return len; -} - -void list_print(struct list *list) -{ - if (list == NULL) - { - return; - } - - while (list != NULL) - { - if (list->next != NULL) - { - printf("%p ", list->data); - } - else - { - printf("%p\n", list->data); - } - list = list->next; - } -} - -void list_destroy(struct list *list) -{ - struct list *elt = list; - struct list *next_elt; - while (elt != NULL) - { - next_elt = elt->next; - free(elt); - elt = next_elt; - } -} - struct list *list_append(struct list *list, void *value) { if (list == NULL) @@ -93,36 +36,6 @@ struct list *list_append(struct list *list, void *value) ******************* * */ - -struct list *list_insert(struct list *list, void *value, size_t index) -{ - if (list == NULL || index == 0) - { - struct list *new_elt = malloc(sizeof(struct list)); - new_elt->data = value; - new_elt->next = list; - return new_elt; - } - - struct list *elt = list; - - for (size_t i = 0; i < index - 1; i++) - { - if (elt->next == NULL) - { - break; - } - elt = elt->next; - } - - struct list *new_elt = malloc(sizeof(struct list)); - new_elt->data = value; - new_elt->next = elt->next; - elt->next = new_elt; - - return list; -} - struct list *list_remove(struct list *list, size_t index) { struct list *elt = list; @@ -204,40 +117,6 @@ static void swap_next(struct list *elt) elt->next->data = elt->data; elt->data = c; } -struct list *list_sort(struct list *list) -{ - // Bubble sort go ! - if (list == NULL) - { - return list; - } - struct list *elt = list; - int len = 0; - while (elt->next != NULL) - { - if (elt->data > elt->next->data) - { - swap_next(elt); - } - elt = elt->next; - len++; - } - - for (int i = 1; i < len; i++) - { - elt = list; - while (elt->next != NULL) - { - if (elt->data > elt->next->data) - { - swap_next(elt); - } - elt = elt->next; - } - } - - return list; -} // Old proto // WARNING no malloc/free allowed (moulinette issue)