diff --git a/Makefile b/Makefile index 3c15ac90..1e3ab523 100644 --- a/Makefile +++ b/Makefile @@ -24,15 +24,16 @@ PREFIX?=/usr/local BINDIR=$(PREFIX)/bin -VERSION=\"0.0.0-beta\" +VERSION=0.0.0-beta -CFLAGS=-std=c99 -Wall -Wextra -I./include -I./libs -g -DDST_VERSION=$(VERSION) -CLIBS=-lm +CFLAGS=-std=c99 -Wall -Wextra -I./include -I./libs -g -DDST_VERSION=\"$(VERSION)\" +CLIBS=-lm -ldl PREFIX=/usr/local DST_TARGET=dst DEBUGGER=lldb DST_INTERNAL_HEADERS=$(addprefix core/,symcache.h opcodes.h strtod.h compile.h gc.h sourcemap.h util.h) DST_HEADERS=$(addprefix include/dst/,dst.h dstconfig.h dsttypes.h dststate.h dststl.h) +DST_C_LIBS=$(addprefix libs/,testlib.so) ############################# ##### Generated headers ##### @@ -58,6 +59,14 @@ DST_CLIENT_SOURCES=$(addprefix client/,\ $(DST_TARGET): $(DST_CORE_SOURCES) $(DST_CLIENT_SOURCES) $(DST_ALL_HEADERS) $(CC) $(CFLAGS) -o $(DST_TARGET) $(DST_CORE_SOURCES) $(DST_CLIENT_SOURCES) $(CLIBS) +####################### +##### C Libraries ##### +####################### + +%.so: %.c $(DST_HEADERS) + $(CC) $(CFLAGS) -shared -undefined dynamic_lookup -o $@ $< + + ################### ##### Testing ##### ################### diff --git a/core/stl.c b/core/stl.c index 1ddddc5f..4e001333 100644 --- a/core/stl.c +++ b/core/stl.c @@ -23,6 +23,31 @@ #include #include +#include +int dst_stl_loadc(int32_t argn, Dst *argv, Dst *ret) { + void *lib; + DstCFunction init; + if (argn < 1) { + *ret = dst_cstringv("expected at least on argument"); + return 1; + } + if (!dst_checktype(argv[0], DST_STRING)) { + *ret = dst_cstringv("expected string"); + return 1; + } + lib = dlopen((const char *)dst_unwrap_string(argv[0]), RTLD_NOW); + if (NULL == lib) { + *ret = dst_cstringv(dlerror()); + return 1; + } + init = dlsym(lib, "_dst_init"); + if (NULL == init) { + *ret = dst_cstringv("could not find lib init function"); + return 1; + } + return init(argn, argv, ret); +} + int dst_stl_parse(int32_t argn, Dst *argv, Dst *ret) { const uint8_t *src; int32_t len; @@ -385,6 +410,7 @@ DST_DEFINE_COMPARATOR(notdescending, > 0) DST_DEFINE_COMPARATOR(notascending, < 0) static DstReg stl[] = { + {"load-native", dst_stl_loadc}, {"parse", dst_stl_parse}, {"compile", dst_stl_compile}, {"int", dst_int}, diff --git a/libs/bootstrap.dst b/junkyard/bootstrap.dst similarity index 100% rename from libs/bootstrap.dst rename to junkyard/bootstrap.dst diff --git a/libs/xxd.c b/junkyard/xxd.c similarity index 100% rename from libs/xxd.c rename to junkyard/xxd.c diff --git a/libs/stl.dst b/libs/stl.dst deleted file mode 100644 index 0ad16e2f..00000000 --- a/libs/stl.dst +++ /dev/null @@ -1,11 +0,0 @@ -(print "Hello, World!") - -(do (+ 1 2 3)) - -(print (+ 1 2 3)) - -# Comment -(var i 0) -(while (< i 1000) - (print i) - (varset! i (+ i 1))) diff --git a/libs/testlib.c b/libs/testlib.c new file mode 100644 index 00000000..fda28bde --- /dev/null +++ b/libs/testlib.c @@ -0,0 +1,19 @@ +#include + +static void additem(DstTable *t, const char *name, Dst val) { + DstTable *subt = dst_table(1); + dst_table_put(subt, dst_csymbolv("value"), val); + dst_table_put(t, dst_csymbolv(name), dst_wrap_table(subt)); +} + +int _dst_init(int32_t argn, Dst *argv, Dst *ret) { + DstTable *table; + if (argn >= 2 && dst_checktype(argv[1], DST_TABLE)) { + table = dst_unwrap_table(argv[1]); + } else { + table = dst_table(0); + } + additem(table, "pi", dst_wrap_real(M_PI)); + *ret = dst_wrap_table(table); + return 0; +}