From 6ccf0572469dfc8cd8fa7b8537b2ac6c265d2df6 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Tue, 9 Jan 2024 12:58:54 -0800 Subject: done FLI in theory --- builtins.c | 27 +++++++++++++++++++++++++++ main.c | 5 +---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/builtins.c b/builtins.c index 288eec9..b8d3322 100644 --- a/builtins.c +++ b/builtins.c @@ -1,6 +1,7 @@ #include "builtins.h" #include "parser.h" #include +#include #include #include #include @@ -731,6 +732,31 @@ void ltequals(value_t *v) { value_free(v1); value_free(v2); } +void clib(value_t *v) { + value_t *v1 = array_pop(STACK); + void *handle = dlopen(v1->str_word->value, RTLD_LAZY); + if (!handle) { + array_append(STACK, v1); + eval_error(); + return; + } + dlerror(); + void (*af)(void); + void (*aobjs)(void); + char *error; + *(void **)(&af) = dlsym(handle, "add_funcs"); + *(void **)(&aobjs) = dlsym(handle, "add_objs"); + if ((error = dlerror()) != NULL) { + value_free(v1); + fprintf(stderr, "%s\n", error); + eval_error(); + return; + } else { + (*af)(); + (*aobjs)(); + value_free(v1); + } +} void gthan(value_t *v) { value_t *v2 = array_pop(STACK); @@ -1096,4 +1122,5 @@ void add_funcs() { add_func(FLIT, swap, "swap"); add_func(FLIT, isdef, "isdef"); add_func(FLIT, dsc, "dsc"); + add_func(FLIT, clib, "clib"); } diff --git a/main.c b/main.c index fe3bb2a..6fc41b5 100644 --- a/main.c +++ b/main.c @@ -47,10 +47,7 @@ int main(int argc, char **argv) { } ssize_t bytes_read = getdelim(&INBUF, &len, '\0', FP); - if (FP != NULL) { - fflush(FP); - fclose(FP); - } + fclose(FP); PARSER = init_parser(INBUF); STACK = init_array(10); -- cgit