bh_snprintf(folder, 511, "%s", include->name);
}
+ bh_path_convert_separators(folder);
// This does not take into account #load_path'd folders...
+
+ bh_arr(char *) folders_to_process = NULL;
+ bh_arr_new(global_heap_allocator, folders_to_process, 2);
- bh_path_convert_separators(folder);
- bh_dir dir = bh_dir_open(folder);
- if (dir == NULL) {
- onyx_report_error(include->token->pos, Error_Critical, "Could not find folder '%s'.", folder);
- return 0;
- }
+ bh_arr_push(folders_to_process, bh_strdup(global_scratch_allocator, folder));
+
+ while (bh_arr_length(folders_to_process) > 0) {
+ char *folder = bh_arr_pop(folders_to_process);
+ bh_dir dir = bh_dir_open(folder);
+ if (dir == NULL) {
+ onyx_report_error(include->token->pos, Error_Critical, "Could not find or open folder '%s'.", folder);
+ return 0;
+ }
+
+ bh_dirent entry;
+ char fullpath[512];
+ while (bh_dir_read(dir, &entry)) {
+ if (entry.type == BH_DIRENT_FILE && bh_str_ends_with(entry.name, ".onyx")) {
+ bh_snprintf(fullpath, 511, "%s/%s", folder, entry.name);
+ bh_path_convert_separators(fullpath);
- bh_dirent entry;
- b32 success = 1;
- char fullpath[512];
- while (bh_dir_read(dir, &entry)) {
- if (entry.type == BH_DIRENT_FILE && bh_str_ends_with(entry.name, ".onyx")) {
- bh_snprintf(fullpath, 511, "%s/%s", folder, entry.name);
- bh_path_convert_separators(fullpath);
- u8* formatted_name = bh_path_get_full_name(fullpath, global_heap_allocator);
- success = process_source_file(formatted_name, include->token->pos);
- if (!success) break;
+ u8* formatted_name = bh_path_get_full_name(fullpath, global_heap_allocator);
+
+ AstInclude* new_include = onyx_ast_node_new(context.ast_alloc, sizeof(AstInclude), Ast_Kind_Load_File);
+ new_include->token = include->token;
+ new_include->name = formatted_name;
+ add_entities_for_node(NULL, (AstNode *) new_include, include->entity->scope, include->entity->package);
+ }
+
+ if (entry.type == BH_DIRENT_DIRECTORY && include->recursive) {
+ if (!strcmp(entry.name, ".") || !strcmp(entry.name, "..")) continue;
+
+ bh_snprintf(fullpath, 511, "%s/%s", folder, entry.name);
+ u8* formatted_name = bh_path_get_full_name(fullpath, global_scratch_allocator); // Could this overflow the scratch allocator?
+
+ bh_arr_push(folders_to_process, formatted_name);
+ }
}
+
+ bh_dir_close(dir);
}
- bh_dir_close(dir);
- return success;
+ return 1;
} else if (include->kind == Ast_Kind_Load_Path) {
bh_arr_push(context.options->included_folders, include->name);
ENTITY_SUBMIT(include);
return;
}
+ else if (parse_possible_directive(parser, "load_all_recursive")) {
+ AstInclude* include = make_node(AstInclude, Ast_Kind_Load_All);
+ include->token = dir_token;
+ include->name_node = parse_expression(parser, 0);
+ include->recursive = 1;
+
+ ENTITY_SUBMIT(include);
+ return;
+ }
else if (parse_possible_directive(parser, "load_path")) {
AstInclude* include = make_node(AstInclude, Ast_Kind_Load_Path);
include->token = dir_token;