imports nitial impl finish

This commit is contained in:
ProgramSnail 2024-12-01 01:03:09 +03:00
parent 009fdd6bfe
commit 9ad694a439
5 changed files with 26 additions and 12 deletions

View file

@ -4,7 +4,7 @@
void run_init(size_t *stack); void run_init(size_t *stack);
void run_init_mod_rec(uint mod_id); void run_mod_rec(uint mod_id, int argc, char **argv);
void run_prepare_exec(int argc, char **argv); void run_prepare_exec(int argc, char **argv);

View file

@ -14,6 +14,8 @@ void mod_add_search_path(const char *path);
bytefile *mod_get(uint32_t id); bytefile *mod_get(uint32_t id);
int32_t find_mod_loaded(const char *name); // < 0 => not found
int32_t mod_load(const char *name); // < 0 => not found int32_t mod_load(const char *name); // < 0 => not found
uint32_t mod_add(bytefile *module); uint32_t mod_add(bytefile *module);

View file

@ -13,15 +13,12 @@ int main(int argc, char** argv) {
bytefile *f = read_file(argv[2]); bytefile *f = read_file(argv[2]);
if (strcmp(argv[1], "-i") == 0) { if (strcmp(argv[1], "-i") == 0) {
uint main_mod_id = mod_add(f);
size_t stack[STACK_SIZE]; size_t stack[STACK_SIZE];
run_init(stack); run_init(stack);
run_init_mod_rec(main_mod_id); uint main_mod_id = mod_add(f);
run_prepare_exec(argc - 1, argv + 1);
// TODO: run all included modules before run_mod_rec(main_mod_id, argc - 1, argv + 1);
run_mod(main_mod_id, argc - 1, argv + 1);
} else if (strcmp(argv[1], "-p") == 0) { } else if (strcmp(argv[1], "-p") == 0) {
dump_file (stdout, f); dump_file (stdout, f);
} else { } else {

View file

@ -56,10 +56,23 @@ void run_prepare_exec(int argc, char **argv) {
} }
} }
void run_init_mod_rec(uint mod_id) { void run_mod_rec(uint mod_id, int argc, char **argv) {
bytefile* mod = mod_get(mod_id); // TODO: pass as param ??
for (size_t i = 0; i < mod->imports_number; ++i) {
if (find_mod_loaded(get_import(mod, i)) < 0 && strcmp(get_import(mod, i), "Std") != 0) { // not loaded
int32_t import_mod = mod_load(get_import(mod, i));
if (import_mod < 0) {
failure("module %s not found\n", get_import(mod, i));
}
run_mod_rec(mod_id, argc, argv);
}
}
init_mod_state(mod_id, &s); init_mod_state(mod_id, &s);
init_mod_state_globals(&s); init_mod_state_globals(&s);
// TODO: recursive for imports, check if visited
run_prepare_exec(argc, argv); // args for module main
run_mod(mod_id, argc, argv);
} }
void run_mod(uint mod_id, int argc, char **argv) { void run_mod(uint mod_id, int argc, char **argv) {

View file

@ -73,6 +73,8 @@ int32_t find_mod_loaded(const char *name) {
} }
int32_t mod_load(const char *name) { int32_t mod_load(const char *name) {
std::string full_name = std::string{name} + ".bc";
auto it = manager.loaded_modules.find(name); auto it = manager.loaded_modules.find(name);
// module already loaded // module already loaded
@ -80,11 +82,11 @@ int32_t mod_load(const char *name) {
return it->second; return it->second;
} }
if (std::filesystem::exists(name)) { if (std::filesystem::exists(full_name)) {
return path_mod_load(name, name); return path_mod_load(name, full_name);
} }
for (const auto &dir_path : manager.search_paths) { for (const auto &dir_path : manager.search_paths) {
auto path = dir_path / name; auto path = dir_path / full_name;
if (std::filesystem::exists(path)) { if (std::filesystem::exists(path)) {
return path_mod_load(name, std::move(path)); return path_mod_load(name, std::move(path));
} }