summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbuild.sh8
-rw-r--r--eval.c21
-rw-r--r--foreign.c30
-rw-r--r--main.c3
-rw-r--r--test.toc8
5 files changed, 57 insertions, 13 deletions
diff --git a/build.sh b/build.sh
index 4b69315..92c94e3 100755
--- a/build.sh
+++ b/build.sh
@@ -21,7 +21,13 @@ else
WARNINGS=''
fi
-DEBUG_FLAGS="-O0 -g3 $WARNINGS -std=c11 -DTOC_DEBUG -DCOMPILE_TIME_FOREIGN_FN_SUPPORT=1 -lffcall -ldl"
+if [ "$1" = "" ]; then
+ if [ "$COMPILE_TIME_FOREIGN_FN_SUPPORT" != "no" ]; then
+ ADDITIONAL_FLAGS="$ADDITIONAL_FLAGS -DCOMPILE_TIME_FOREIGN_FN_SUPPORT=1 -lffcall -ldl"
+ fi
+fi
+
+DEBUG_FLAGS="-O0 -g3 $WARNINGS -std=c11 -DTOC_DEBUG"
RELEASE_FLAGS="-O3 -s -DNDEBUG $WARNINGS -std=c11"
if [ "$1" = "release" ]; then
diff --git a/eval.c b/eval.c
index 0866ba6..49a2d32 100644
--- a/eval.c
+++ b/eval.c
@@ -18,7 +18,7 @@ static void evalr_create(Evaluator *ev, Typer *tr, Allocator *allocr) {
ev->typer = tr;
ev->enabled = true;
ev->allocr = allocr;
- ffmgr_create(&ev->ffmgr, allocr);
+ ffmgr_create(&ev->ffmgr);
}
static void evalr_free(Evaluator *ev) {
@@ -27,6 +27,7 @@ static void evalr_free(Evaluator *ev) {
free(*f);
}
arr_clear(&ev->to_free);
+ ffmgr_free(&ev->ffmgr);
}
static inline void *evalr_malloc(Evaluator *ev, size_t bytes) {
@@ -230,15 +231,29 @@ static void i64_to_val(Value *v, BuiltinType v_type, I64 x) {
v->u32 = (U32)x; break;
case BUILTIN_U64:
v->u64 = (U64)x; break;
+ case BUILTIN_F32:
+ v->f32 = (F32)x; break;
+ case BUILTIN_F64:
+ v->f64 = (F64)x; break;
default: assert(0); break;
}
}
static void u64_to_val(Value *v, BuiltinType v_type, U64 x) {
- if (v_type == BUILTIN_U64)
+ switch (v_type) {
+ case BUILTIN_U64:
v->u64 = x;
- else
+ break;
+ case BUILTIN_F32:
+ v->f32 = (F32)x;
+ break;
+ case BUILTIN_F64:
+ v->f64 = (F64)x;
+ break;
+ default:
i64_to_val(v, v_type, (I64)x);
+ break;
+ }
}
/* rerturns a pointer to the underlying data of v, e.g. an I64 * if t is the builtin BUILTIN_I64 */
diff --git a/foreign.c b/foreign.c
index 6e7bc98..ff8ab03 100644
--- a/foreign.c
+++ b/foreign.c
@@ -222,8 +222,8 @@ static bool arg_list_add(av_alist *arg_list, Value *val, Type *type, Location wh
return true;
}
-static void ffmgr_create(ForeignFnManager *ffmgr, Allocator *allocr) {
- str_hash_table_create(&ffmgr->libs_loaded, sizeof(Library), allocr);
+static void ffmgr_create(ForeignFnManager *ffmgr) {
+ str_hash_table_create(&ffmgr->libs_loaded, sizeof(Library), NULL);
}
static bool foreign_call(ForeignFnManager *ffmgr, FnExpr *fn, Type *fn_type, Value *args, Location call_where, Value *ret) {
@@ -235,9 +235,8 @@ static bool foreign_call(ForeignFnManager *ffmgr, FnExpr *fn, Type *fn_type, Val
if (!lib) {
/* TODO: IMPORTANT: only open libraries once */
void *handle = dlopen(libname, RTLD_LAZY);
- printf("Load %s\n",libname);
if (!handle) {
- err_print(call_where, "Could not open dynamic library: %s.", lib);
+ err_print(call_where, "Could not open dynamic library: %s.", libname);
return false;
}
lib = str_hash_table_insert(&ffmgr->libs_loaded, libname, strlen(libname));
@@ -275,10 +274,29 @@ static bool foreign_call(ForeignFnManager *ffmgr, FnExpr *fn, Type *fn_type, Val
return true;
}
+
+static void ffmgr_free(ForeignFnManager *ffmgr) {
+ arr_foreach(ffmgr->libs_loaded.slots, StrHashTableSlotPtr, slotp) {
+ if (*slotp) {
+ Library lib = *(Library *)((*slotp)->data);
+ dlclose(lib.handle);
+ }
+ }
+ str_hash_table_free(&ffmgr->libs_loaded);
+}
+
#else
-static bool foreign_call(FnExpr *fn, Type *fn_type, Value *args, Location call_where, Value *ret) {
- (void)fn; (void)fn_type; (void)args; (void)ret;
+static void ffmgr_create(ForeignFnManager *ffmgr) {
+ (void)ffmgr;
+}
+
+static bool foreign_call(ForeignFnManager *ffmgr, FnExpr *fn, Type *fn_type, Value *args, Location call_where, Value *ret) {
+ (void)ffmgr; (void)fn; (void)fn_type; (void)args; (void)ret;
err_print(call_where, "You have not compiled toc with compile time foreign function support.");
return false;
}
+
+static void ffmgr_free(ForeignFnManager *ffmgr) {
+ (void)ffmgr;
+}
#endif
diff --git a/main.c b/main.c
index a5c886f..3d7f527 100644
--- a/main.c
+++ b/main.c
@@ -18,11 +18,12 @@
/*
TODO:
-C functions (#foreign)
+run time foreign functions
foreign non-functions
no foreign parameter declarations
variadic fns
#include
+constants in structs
---
X ::= newtype(int); or something
don't allow while {3; 5} (once break is added)
diff --git a/test.toc b/test.toc
index 4bcb48e..05f94ec 100644
--- a/test.toc
+++ b/test.toc
@@ -26,10 +26,14 @@ foo ::= fn() i32 {
malloc :: fn(u64) &u8 = #foreign "malloc", "libc.so.6";
-sqrt :: fn(f64) f64 = #foreign "sqrt", "libm.so";
+sqrt :: fn(f64) f64 = #foreign "sqrt", "libm.so.6";
+cos :: fn(f64) f64 = #foreign "cos", "libm.so.6";
+sin :: fn(f64) f64 = #foreign "sin", "libm.so.6";
main ::= fn() {
x ::= foo();
- y ::= malloc(10);
+ // y ::= malloc(10);
sq2 ::= sqrt(2);
+ cospi ::= cos(3.14159);
+ sinpi ::= sin(3.14159);
}; \ No newline at end of file