Skip to content

Instantly share code, notes, and snippets.

@0racle
Last active June 12, 2023 23:03
Show Gist options
  • Save 0racle/85e93cd10d677521776c722f519011da to your computer and use it in GitHub Desktop.
Save 0racle/85e93cd10d677521776c722f519011da to your computer and use it in GitHub Desktop.
Inline::BQN
use NativeCall;
constant LIB = './libcbqn.so';
sub bqn_init() is native(LIB) { * }
sub bqn_evalCStr(Str) returns int64 is native(LIB) { * }
sub bqn_type(int64) returns int8 is native(LIB) { * }
sub bqn_bound(int64) returns size_t is native(LIB) { * }
sub bqn_rank(int64) returns size_t is native(LIB) { * }
sub bqn_shape(int64, CArray[size_t]) is native(LIB) { * }
sub bqn_readC16Arr(int64, int64) is native(LIB) { * }
sub bqn_call1(int64, int64) returns int64 is native(LIB) { * }
my enum Type <Array Number Character Function 1-Modifier 2-Modifier Namespace>;
bqn_init();
my $a = bqn_evalCStr('2‿3‿4⥊↕20');
my $type = Type(bqn_type($a));
my $elems = bqn_bound($a);
my $rank = bqn_rank($a);
bqn_shape($a, my $shape-buf = CArray[size_t].allocate($rank));
my $shape = $shape-buf[^$rank];
say {:$type, :$elems, :$rank, :$shape};
my $fmt = bqn_evalCStr('•fmt');
my $out = bqn_call1($fmt, $a);
bqn_readC16Arr($out, nativecast(Pointer, my $buf = utf16.allocate(bqn_bound($out))));
say $buf.decode;
{elems => 24, rank => 3, shape => (2 3 4), type => Array}
┌─
╎ 0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
16 17 18 19
0 1 2 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment