]> git.lizzy.rs Git - uwu-lang.git/blob - src/vm.c
Initial commit
[uwu-lang.git] / src / vm.c
1 #include <dlfcn.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "collect.h"
5 #include "vm.h"
6
7 static void free_expression(UwUVMExpression *expr)
8 {
9         if (expr->type == EX_FNCALL) {
10                 for (size_t i = 0; i < expr->value.cll_value.num_args; i++)
11                         free_expression(&expr->value.cll_value.args[i]);
12         
13                 free(expr->value.cll_value.args);
14         }
15
16         if (expr->type == EX_STRLIT)
17                 free(expr->value.str_value);
18 }
19
20 void vm_run_file(const char *progname, const char *modname)
21 {
22         UwUVMProgram program = create_program(progname, modname);
23         UwUVMValue result = ((UwUVMValue (*)(UwUVMFunction *, UwUVMArgs args)) dlsym(program.api_library, "uwuvm_run_function"))(program.main_function, (UwUVMArgs) {.num = 0, .evaluated = NULL, .unevaluated = NULL, .super = NULL});
24
25         char *str = ((char *(*)(UwUVMValue)) dlsym(program.api_library, "uwustr_get"))(result);
26
27         printf("%s\n", str);
28         free(str);
29
30         ((void (*)(UwUVMValue)) dlsym(program.api_library, "uwuvm_free_value"))(result);
31
32         for (size_t i = 0; i < program.num_functions; i++) {
33                 UwUVMFunction *function = program.functions[i];
34
35                 if (function->type == MODULE_PLAIN) {
36                         free_expression(function->value.plain);
37                         free(function->value.plain);
38                 }
39                         
40                 free(function);
41         }
42
43         free(program.functions);
44
45         for (size_t i = 0; i < program.num_libraries; i++)
46                 dlclose(program.libraries[i]);
47
48         if (program.libraries)
49                 free(program.libraries);
50
51         dlclose(program.api_library);
52 }