diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-01-21 13:37:07 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-01-21 13:37:07 -0500 |
commit | 18740f2f8502adf410ac3d4fa853730bedade787 (patch) | |
tree | 1081733a1f5bb56d81bc98e2386eb828435e1d48 | |
parent | 10dbaa3391e8ad4e076a6fcd6f9790d7f625552a (diff) |
int literal bug fix
-rwxr-xr-x | build.sh | 8 | ||||
-rw-r--r-- | eval.c | 21 | ||||
-rw-r--r-- | foreign.c | 30 | ||||
-rw-r--r-- | main.c | 3 | ||||
-rw-r--r-- | test.toc | 8 |
5 files changed, 57 insertions, 13 deletions
@@ -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 @@ -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 */ @@ -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 @@ -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) @@ -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 |