diff --git a/include/bqnffi.h b/include/bqnffi.h index 9759ee96..a88ef998 100644 --- a/include/bqnffi.h +++ b/include/bqnffi.h @@ -15,6 +15,8 @@ uint32_t bqn_toChar(BQNV v); // includes bqn_free(v) double bqn_readF64 (BQNV v); // doesn't include bqn_free(v) uint32_t bqn_readChar(BQNV v); // doesn't include bqn_free(v) +int bqn_type(BQNV v); // equivalent of BQN •Type + // invoke BQN function BQNV bqn_call1(BQNV f, BQNV x); BQNV bqn_call2(BQNV f, BQNV w, BQNV x); @@ -73,7 +75,7 @@ BQNV bqn_makeBoundFn2(bqn_boundFn2 f, BQNV obj); // direct (zero copy) array item access typedef enum { elt_unk, elt_i8, elt_i16, elt_i32, elt_f64, elt_c8, elt_c16, elt_c32 } BQNElType; // note that more types may be added in the future -BQNElType bqn_directType(BQNV a); +BQNElType bqn_directArrType(BQNV a); // can only use the functions below if bqn_elType returns the corresponding type // a valid implementation of bqn_elType would be to always return elt_unk, thus disallowing the use of direct access entirely int8_t* bqn_directI8 (BQNV a); diff --git a/include/syms b/include/syms index 5babdd56..88b22b4f 100644 --- a/include/syms +++ b/include/syms @@ -4,6 +4,7 @@ bqn_toChar; bqn_readF64; bqn_readChar; + bqn_type; bqn_call1; bqn_call2; bqn_eval; @@ -41,7 +42,7 @@ bqn_makeUTF8Str; bqn_makeBoundFn1; bqn_makeBoundFn2; - bqn_directType; + bqn_directArrType; bqn_directI8; bqn_directI16; bqn_directI32; diff --git a/src/ffi.c b/src/ffi.c index a8ad2878..73015c01 100644 --- a/src/ffi.c +++ b/src/ffi.c @@ -30,6 +30,11 @@ uint32_t bqn_toChar(BQNV v) { uint32_t r = o2cu(getB(v)); freeTagged(v); return double bqn_readF64 (BQNV v) { return o2fu(getB(v)); } uint32_t bqn_readChar(BQNV v) { return o2cu(getB(v)); } +B type_c1(B t, B x); +int bqn_type(BQNV v) { + return o2i(type_c1(bi_N, inc(getB(v)))); +} + BQNV bqn_call1(BQNV f, BQNV x) { return makeX(c1(getB(f), inc(getB(x)))); } @@ -159,7 +164,7 @@ const static u8 typeMap[] = { [el_i32] = elt_i32, [el_c32] = elt_c32, [el_f64] = elt_f64, }; -BQNElType bqn_directType(BQNV a) { +BQNElType bqn_directArrType(BQNV a) { B b = getB(a); if (!isArr(b)) return elt_unk; return typeMap[TI(b,elType)]; diff --git a/test/ffi/ffiTest.c b/test/ffi/ffiTest.c index 698166f1..bd156e09 100644 --- a/test/ffi/ffiTest.c +++ b/test/ffi/ffiTest.c @@ -4,6 +4,12 @@ void do_nothing() { } +int32_t getType(BQNV v) { + int32_t i = bqn_type(v); + bqn_free(v); + return i; +} + BQNV timesTen(BQNV v) { size_t len = bqn_bound(v); int32_t* buf = malloc(len*4); @@ -93,7 +99,7 @@ BQNV makeArrays() { } int32_t directAccess(BQNV x) { - BQNElType e = bqn_directType(x); + BQNElType e = bqn_directArrType(x); uint32_t res = 0; if (e==elt_i32) { size_t bound = bqn_bound(x); diff --git a/test/ffi/test.bqn b/test/ffi/test.bqn index dd87fcd2..7104d158 100644 --- a/test/ffi/test.bqn +++ b/test/ffi/test.bqn @@ -4,6 +4,7 @@ Section ← {•Out 𝕩∾˜@+10} f ↩ "lib.so" •FFI ""‿"do_nothing" ⋄ •Show F ⟨⟩ Section "# ""a""" +f ↩ "lib.so" •FFI "i32"‿"getType"‿">a" ⋄ •Show F¨ ⟨⟨⟩, 1, '𝕩', +, ¨, ∘, {⇐}⟩ f ↩ "lib.so" •FFI "a"‿"timesTen"‿"a" ⋄ •Show F ⟨↕10⟩ f ↩ "lib.so" •FFI "a"‿"readAtoms"‿"a"‿"a" ⋄ •Show F ⟨÷5 ⋄ '𝕨'⟩ f ↩ "lib.so" •FFI "a"‿"readTyped"‿">a" ⋄ •Show F (10×↕7) + (4⥊<↕10)∾3⥊<@+↕10 diff --git a/test/ffi/test.expected b/test/ffi/test.expected index 2c31d13a..9f23dbe9 100644 --- a/test/ffi/test.expected +++ b/test/ffi/test.expected @@ -1,6 +1,7 @@ @ # "a" +⟨ 0 1 2 3 4 5 6 ⟩ ⟨ 0 10 20 30 40 50 60 70 80 90 ⟩ 120168 120168 0.200000000000000011 0.200000000000000011 '𝕩'