7 #include "common/err.h"
8 #include "common/str.h"
9 #include "common/file.h"
10 #include "common/dl.h"
18 static char *dirname_wrapper(const char *name)
20 char *copy = strdup(name);
21 char *result = dirname(copy);
22 char *result_copy = strdup(result);
38 char *path; // path without file extension
39 char *filename; // path with file extension
40 char *environment; // directory path
42 UwUVMModuleType type; // native (.so) or plain (.uwu)
44 FunctionLink *functions; // required functions
45 size_t num_functions; // number of required functions
46 size_t loaded_functions; // number of loaded functions (<= num_functions)
50 AbstractSyntaxTree ast; // abstract syntax tree generated by parser (for plain modules)
51 void *lib; // dlopen() shared object handle (for native modules)
57 Module **modules; // loaded modules
58 size_t num_modules; // count for modules
60 char *std_path; // path to standard library
62 Program program; // the result program
67 // returns mallocated string
68 static inline char *get_filename(const char *module_path)
70 const char *try_names[3] = {
78 for (int i = 0; i < 3; i++) {
79 filename = asprintf_wrapper(try_names[i], module_path);
81 if (file_exists(filename))
90 // module_path is a mallocated string
91 static Module *require_module(LoadState *state, char *module_path)
93 for (size_t i = 0; i < state->num_modules; i++) {
94 Module *module = state->modules[i];
96 if (strcmp(module_path, module->path) == 0) {
102 char *filename = get_filename(module_path);
105 error("module error: module %s not found\n", module_path);
107 size_t filename_len = strlen(filename);
108 UwUVMModuleType type = (filename_len >= 3 && strcmp(filename + filename_len - 3, ".so") == 0) ? MODULE_NATIVE : MODULE_PLAIN;
110 state->modules = realloc(state->modules, sizeof *state->modules * ++state->num_modules);
111 Module *module = state->modules[state->num_modules - 1] = malloc(sizeof *module);
115 .filename = filename,
116 .environment = dirname_wrapper(module_path),
122 .loaded_functions = 0,
125 if (type == MODULE_PLAIN) {
126 module->handle.ast = parse_file(filename);
128 state->program.libraries = realloc(state->program.libraries, sizeof(void *) * ++state->program.num_libraries);
129 state->program.libraries[state->program.num_libraries - 1] = module->handle.lib = dlopen(filename, RTLD_LAZY);
137 static UwUVMFunction *require_function(LoadState *state, Module *module, const char *name)
139 for (size_t i = 0; i < module->num_functions; i++) {
140 FunctionLink *link = &module->functions[i];
142 if (strcmp(link->name, name) == 0)
146 UwUVMFunction *ref = malloc(sizeof *ref);
147 ref->type = module->type;
149 state->program.functions = realloc(state->program.functions, sizeof *state->program.functions * ++state->program.num_functions);
150 state->program.functions[state->program.num_functions - 1] = ref;
152 module->functions = realloc(module->functions, sizeof *module->functions * ++module->num_functions);
153 module->functions[module->num_functions - 1] = (FunctionLink) {
154 .name = strdup(name),
161 static UwUVMFunction *resolve_function(LoadState *state, Module *caller_module, const char *full_name)
163 size_t len = strlen(full_name);
166 for (fnname = &full_name[len - 1]; *fnname != ':' && fnname > full_name; fnname--)
173 error("module error: empty function name referenced/called by module %s\n", caller_module->filename);
175 Module *callee_module;
177 if (fnname == full_name) {
178 callee_module = caller_module;
180 const char *caller_path = caller_module->environment;
181 const char *callee_name = full_name;
183 if (*callee_name == ':') {
184 caller_path = state->std_path;
188 size_t path_len = fnname - callee_name;
189 char callee_path[path_len];
191 for (size_t i = 0; i < path_len; i++)
192 callee_path[i] = (i == path_len - 1) ? '\0'
193 : (callee_name[i] == ':') ? '/'
196 callee_module = require_module(state, asprintf_wrapper("%s/%s", caller_path, callee_path));
199 return require_function(state, callee_module, fnname);
202 static void translate_expression(LoadState *state, Module *module, UwUVMExpression *vm_expr, ParseExpression *parse_expr)
204 UwUVMFunction *vm_function;
206 if (parse_expr->type == EX_FNNAME || parse_expr->type == EX_FNCALL) {
207 vm_function = resolve_function(state, module, parse_expr->value.str_value);
208 free(parse_expr->value.str_value);
211 switch (vm_expr->type = parse_expr->type) {
214 vm_expr->value.int_value = parse_expr->value.int_value;
218 vm_expr->value.str_value = parse_expr->value.str_value;
222 vm_expr->value.ref_value = vm_function;
226 vm_expr->value.cll_value.function = vm_function;
227 vm_expr->value.cll_value.args = malloc(sizeof(UwUVMExpression) * parse_expr->num_children);
228 vm_expr->value.cll_value.num_args = parse_expr->num_children;
230 for (size_t i = 0; i < parse_expr->num_children; i++)
231 translate_expression(state, module, &vm_expr->value.cll_value.args[i], parse_expr->children[i]);
233 if (parse_expr->children)
234 free(parse_expr->children);
244 static void load_functions(LoadState *state, Module *module)
246 for (; module->loaded_functions < module->num_functions; module->loaded_functions++) {
247 FunctionLink *link = &module->functions[module->loaded_functions];
249 if (module->type == MODULE_PLAIN) {
250 ParseFunction **function = NULL;
252 for (size_t i = 0; i < module->handle.ast.num_functions; i++) {
253 ParseFunction **fn = &module->handle.ast.functions[i];
255 if (*fn && strcmp((*fn)->name, link->name) == 0) {
262 translate_expression(state, module, link->ref->value.plain = malloc(sizeof(UwUVMExpression)), (*function)->expression);
263 free((*function)->name);
268 error("module error: no function %s in module %s\n", link->name, module->filename);
271 char *symbol = asprintf_wrapper("uwu_%s", link->name);
272 link->ref->value.native = dlsym(module->handle.lib, symbol);
280 static void free_expression(ParseExpression *expr)
282 if (expr->type == EX_FNCALL) {
283 for (size_t i = 0; i < expr->num_children; i++)
284 free_expression(expr->children[i]);
287 free(expr->children);
290 if (expr->type != EX_INTLIT && expr->type != EX_ARGNUM)
291 free(expr->value.str_value);
296 Program load_program(const char *progname, const char *modname)
298 char *prog_dirname = dirname_wrapper(progname);
299 char *api_path = asprintf_wrapper("%s/api/api.so", prog_dirname);
304 .std_path = asprintf_wrapper("%s/std", prog_dirname),
306 .api_library = dlopen(api_path, RTLD_NOW | RTLD_GLOBAL),
307 .main_function = NULL,
318 state.program.main_function = require_function(&state, require_module(&state, strdup(modname)), "main");
321 bool fully_loaded = true;
323 for (size_t i = 0; i < state.num_modules; i++) {
324 Module *module = state.modules[i];
327 printf("%s %lu/%lu\n", module->filename, module->loaded_functions, module->num_functions);
330 if (module->loaded_functions < module->num_functions) {
331 fully_loaded = false;
332 load_functions(&state, module);
340 free(state.std_path);
342 for (size_t i = 0; i < state.num_modules; i++) {
343 Module *module = state.modules[i];
346 free(module->filename);
347 free(module->environment);
349 for (size_t f = 0; f < module->num_functions; f++)
350 free(module->functions[f].name);
352 free(module->functions);
354 if (module->type == MODULE_PLAIN) {
355 for (size_t f = 0; f < module->handle.ast.num_functions; f++) {
356 ParseFunction *function = module->handle.ast.functions[f];
359 free_expression(function->expression);
360 free(function->name);
365 if (module->handle.ast.functions)
366 free(module->handle.ast.functions);
374 return state.program;