aboutsummaryrefslogtreecommitdiff
path: root/src/vm.c
diff options
context:
space:
mode:
authorElias Fleckenstein <eliasfleckenstein@web.de>2021-12-30 14:18:15 +0100
committerElias Fleckenstein <eliasfleckenstein@web.de>2021-12-30 14:18:15 +0100
commitecc06c082036aa93f6810ec21e73610c55f5a57b (patch)
treeaff479c3bc5b39ead9f65dffb01d399b341fa4ba /src/vm.c
downloaduwu-lang-ecc06c082036aa93f6810ec21e73610c55f5a57b.tar.xz
Initial commit
Diffstat (limited to 'src/vm.c')
-rw-r--r--src/vm.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/vm.c b/src/vm.c
new file mode 100644
index 0000000..7f0db99
--- /dev/null
+++ b/src/vm.c
@@ -0,0 +1,52 @@
+#include <dlfcn.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "collect.h"
+#include "vm.h"
+
+static void free_expression(UwUVMExpression *expr)
+{
+ if (expr->type == EX_FNCALL) {
+ for (size_t i = 0; i < expr->value.cll_value.num_args; i++)
+ free_expression(&expr->value.cll_value.args[i]);
+
+ free(expr->value.cll_value.args);
+ }
+
+ if (expr->type == EX_STRLIT)
+ free(expr->value.str_value);
+}
+
+void vm_run_file(const char *progname, const char *modname)
+{
+ UwUVMProgram program = create_program(progname, modname);
+ 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});
+
+ char *str = ((char *(*)(UwUVMValue)) dlsym(program.api_library, "uwustr_get"))(result);
+
+ printf("%s\n", str);
+ free(str);
+
+ ((void (*)(UwUVMValue)) dlsym(program.api_library, "uwuvm_free_value"))(result);
+
+ for (size_t i = 0; i < program.num_functions; i++) {
+ UwUVMFunction *function = program.functions[i];
+
+ if (function->type == MODULE_PLAIN) {
+ free_expression(function->value.plain);
+ free(function->value.plain);
+ }
+
+ free(function);
+ }
+
+ free(program.functions);
+
+ for (size_t i = 0; i < program.num_libraries; i++)
+ dlclose(program.libraries[i]);
+
+ if (program.libraries)
+ free(program.libraries);
+
+ dlclose(program.api_library);
+}