7 #include "common/err.h"
8 #include "common/str.h"
9 #include "common/file.h"
10 #include "common/dl.h"
18 static char *wrap_name_func(const char *name, char *(*fn)(char *))
20 char *copy = strdup(name);
21 char *result = fn(copy);
22 char *result_copy = strdup(result);
28 static char *basename_wrapper(const char *name)
30 return wrap_name_func(name, &basename);
33 static char *dirname_wrapper(const char *name)
35 return wrap_name_func(name, &dirname);
48 char *path; // path without file extension
49 char *filename; // path with file extension
50 char *environment; // directory path
52 UwUVMModuleType type; // native (.so) or plain (.uwu)
54 FunctionLink *functions; // required functions
55 size_t num_functions; // number of required functions
56 size_t loaded_functions; // number of loaded functions (<= num_functions)
60 AbstractSyntaxTree ast; // abstract syntax tree generated by parser (for plain modules)
61 void *lib; // dlopen() shared object handle (for native modules)
67 Module **modules; // loaded modules
68 size_t num_modules; // count for modules
70 char *std_path; // path to standard library
72 Program program; // the result program
77 // returns mallocated string
78 static inline char *get_filename(const char *module_path)
80 const char *try_names[3] = {
88 for (int i = 0; i < 3; i++) {
89 filename = asprintf_wrapper(try_names[i], module_path);
91 if (file_exists(filename))
100 // module_path is a mallocated string
101 static Module *require_module(LoadState *state, char *module_path)
103 for (size_t i = 0; i < state->num_modules; i++) {
104 Module *module = state->modules[i];
106 if (strcmp(module_path, module->path) == 0) {
112 char *filename = get_filename(module_path);
115 error("module error: module %s not found\n", module_path);
117 size_t filename_len = strlen(filename);
118 UwUVMModuleType type = (filename_len >= 3 && strcmp(filename + filename_len - 3, ".so") == 0) ? MODULE_NATIVE : MODULE_PLAIN;
120 state->modules = realloc(state->modules, sizeof *state->modules * ++state->num_modules);
121 Module *module = state->modules[state->num_modules - 1] = malloc(sizeof *module);
125 .filename = filename,
126 .environment = dirname_wrapper(module_path),
132 .loaded_functions = 0,
135 if (type == MODULE_PLAIN) {
136 module->handle.ast = parse_file(filename);
138 state->program.libraries = realloc(state->program.libraries, sizeof(void *) * ++state->program.num_libraries);
139 state->program.libraries[state->program.num_libraries - 1] = module->handle.lib = dlopen(filename, RTLD_LAZY);
147 static UwUVMFunction *require_function(LoadState *state, Module *module, const char *name)
149 for (size_t i = 0; i < module->num_functions; i++) {
150 FunctionLink *link = &module->functions[i];
152 if (strcmp(link->name, name) == 0)
156 UwUVMFunction *ref = malloc(sizeof *ref);
157 ref->type = module->type;
159 state->program.functions = realloc(state->program.functions, sizeof *state->program.functions * ++state->program.num_functions);
160 state->program.functions[state->program.num_functions - 1] = ref;
162 module->functions = realloc(module->functions, sizeof *module->functions * ++module->num_functions);
163 module->functions[module->num_functions - 1] = (FunctionLink) {
164 .name = strdup(name),
171 static UwUVMFunction *resolve_function(LoadState *state, Module *caller_module, const char *full_name)
173 size_t len = strlen(full_name);
176 for (fnname = &full_name[len - 1]; *fnname != ':' && fnname > full_name; fnname--)
183 error("module error: empty function name referenced/called by module %s\n", caller_module->filename);
185 Module *callee_module;
187 if (fnname == full_name) {
188 callee_module = caller_module;
190 const char *caller_path = caller_module->environment;
191 const char *callee_name = full_name;
193 if (*callee_name == ':') {
194 caller_path = state->std_path;
198 size_t path_len = fnname - callee_name;
199 char callee_path[path_len];
201 for (size_t i = 0; i < path_len; i++)
202 callee_path[i] = (i == path_len - 1) ? '\0'
203 : (callee_name[i] == ':') ? '/'
206 callee_module = require_module(state, asprintf_wrapper("%s/%s", caller_path, callee_path));
209 return require_function(state, callee_module, fnname);
212 static void translate_expression(LoadState *state, Module *module, UwUVMExpression *vm_expr, ParseExpression *parse_expr)
214 UwUVMFunction *vm_function;
216 if (parse_expr->type == EX_FNNAME || parse_expr->type == EX_FNCALL) {
217 vm_function = resolve_function(state, module, parse_expr->value.str_value);
218 free(parse_expr->value.str_value);
221 switch (vm_expr->type = parse_expr->type) {
224 vm_expr->value.int_value = parse_expr->value.int_value;
228 vm_expr->value.str_value = parse_expr->value.str_value;
232 vm_expr->value.ref_value = vm_function;
236 vm_expr->value.cll_value.function = vm_function;
237 vm_expr->value.cll_value.args = malloc(sizeof(UwUVMExpression) * parse_expr->num_children);
238 vm_expr->value.cll_value.num_args = parse_expr->num_children;
240 for (size_t i = 0; i < parse_expr->num_children; i++)
241 translate_expression(state, module, &vm_expr->value.cll_value.args[i], parse_expr->children[i]);
243 if (parse_expr->children)
244 free(parse_expr->children);
254 static void load_functions(LoadState *state, Module *module)
256 for (; module->loaded_functions < module->num_functions; module->loaded_functions++) {
257 FunctionLink *link = &module->functions[module->loaded_functions];
259 if (module->type == MODULE_PLAIN) {
260 ParseFunction **function = NULL;
262 for (size_t i = 0; i < module->handle.ast.num_functions; i++) {
263 ParseFunction **fn = &module->handle.ast.functions[i];
265 if (*fn && strcmp((*fn)->name, link->name) == 0) {
272 translate_expression(state, module, link->ref->value.plain = malloc(sizeof(UwUVMExpression)), (*function)->expression);
273 free((*function)->name);
278 error("module error: no function %s in module %s\n", link->name, module->filename);
281 char *symbol = asprintf_wrapper("uwu_%s", link->name);
282 link->ref->value.native = dlsym(module->handle.lib, symbol);
290 static void free_expression(ParseExpression *expr)
292 if (expr->type == EX_FNCALL) {
293 for (size_t i = 0; i < expr->num_children; i++)
294 free_expression(expr->children[i]);
297 free(expr->children);
300 if (expr->type != EX_INTLIT && expr->type != EX_ARGNUM)
301 free(expr->value.str_value);
306 Program load_program(const char *progname, const char *modname)
308 char *prog_dirname = dirname_wrapper(progname);
309 char *api_path = asprintf_wrapper("%s/api/api.so", prog_dirname);
314 .std_path = asprintf_wrapper("%s/std", prog_dirname),
316 .api_library = dlopen(api_path, RTLD_NOW | RTLD_GLOBAL),
317 .main_function = NULL,
328 state.program.main_function = require_function(&state, require_module(&state, strdup(modname)), "main");
331 bool fully_loaded = true;
333 for (size_t i = 0; i < state.num_modules; i++) {
334 Module *module = state.modules[i];
337 printf("%s %lu/%lu\n", module->filename, module->loaded_functions, module->num_functions);
340 if (module->loaded_functions < module->num_functions) {
341 fully_loaded = false;
342 load_functions(&state, module);
350 free(state.std_path);
352 for (size_t i = 0; i < state.num_modules; i++) {
353 Module *module = state.modules[i];
356 free(module->filename);
357 free(module->environment);
359 for (size_t f = 0; f < module->num_functions; f++)
360 free(module->functions[f].name);
362 free(module->functions);
364 if (module->type == MODULE_PLAIN) {
365 for (size_t f = 0; f < module->handle.ast.num_functions; f++) {
366 ParseFunction *function = module->handle.ast.functions[f];
369 free_expression(function->expression);
370 free(function->name);
375 if (module->handle.ast.functions)
376 free(module->handle.ast.functions);
384 return state.program;