dyadic •FBytes

This commit is contained in:
dzaima 2021-05-20 23:10:06 +03:00
parent 8ec316ec94
commit 2cee9260f7
6 changed files with 38 additions and 16 deletions

View File

@ -1,9 +0,0 @@
#!/usr/bin/env bash
if [[ $# -ne 1 ]]; then
echo "Usage: ./genRuntimeSrc path/to/mlochbaum/BQN"
exit
fi
./BQN cc.bqn $1 -i r0 > src/runtime0
./BQN cc.bqn $1 -i r1 > src/runtime1
./BQN cc.bqn $1 -i c > src/compiler
./BQN cc.bqn $1 -i f > src/formatter

9
genRuntimeSrc.bqn Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bqn
args•args
"Usage: ./getRuntime.bqn path/to/mlochbaum/BQN"!1args
path•args
CC {𝕨 •FChars 1,path,"-i",𝕩 •Import "cc.bqn"}
"src/runtime0"CC"r0"
"src/runtime1"CC"r1"
"src/compiler"CC"c"
"src/formatter"CC"f"

View File

@ -3,13 +3,18 @@ typedef struct TmpFile { // to be turned into a proper I8Arr
i8 a[]; i8 a[];
} TmpFile; } TmpFile;
TmpFile* file_bytes(B path) { // consumes; may throw FILE* file_open(B path, char* desc, char* mode) { // consumes path; can error
u64 plen = utf8lenB(path); u64 plen = utf8lenB(path);
char p[plen+1]; char p[plen+1];
toUTF8(path, p); toUTF8(path, p);
p[plen] = 0; p[plen] = 0;
FILE* f = fopen(p, "r"); FILE* f = fopen(p, mode);
if (f==NULL) thrF("Couldn't read file \"%R\"", path); if (f==NULL) thrF("Couldn't %S file \"%R\"", desc, path);
return f;
}
TmpFile* file_bytes(B path) { // consumes; may throw
FILE* f = file_open(path, "read", "r");
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
u64 len = ftell(f); u64 len = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
@ -17,7 +22,6 @@ TmpFile* file_bytes(B path) { // consumes; may throw
arr_shVec(tag(src,ARR_TAG), len); arr_shVec(tag(src,ARR_TAG), len);
fread((char*)src->a, 1, len, f); fread((char*)src->a, 1, len, f);
fclose(f); fclose(f);
dec(path);
return src; return src;
} }
B file_chars(B path) { // consumes; may throw B file_chars(B path) { // consumes; may throw
@ -65,4 +69,16 @@ B path_dir(B path) { // consumes; returns directory part of file path, with trai
dec(path); dec(path);
u32* rp; B r = m_c32arrv(&rp, 2); rp[0] = '.'; rp[1] = '/'; u32* rp; B r = m_c32arrv(&rp, 2); rp[0] = '.'; rp[1] = '/';
return r; return r;
} }
void file_write(B path, B x) { // consumes path
FILE* f = file_open(path, "write to", "w");
u64 len = utf8lenB(x);
char val[len];
toUTF8(x, val);
fwrite(val, 1, len, f);
fclose(f);
}

View File

@ -252,6 +252,12 @@ B import_c2(B d, B w, B x) { B f = c(Md1D,d)->f;
return bqn_execFile(path_resolve(f, x), w); return bqn_execFile(path_resolve(f, x), w);
} }
B fchars_c2(B d, B w, B x) { B f = c(Md1D,d)->f;
file_write(path_resolve(f, w), x);
return x;
}
B rt_cell; B rt_cell;
B cell_c1(B d, B x) { B f = c(Md1D,d)->f; B cell_c1(B d, B x) { B f = c(Md1D,d)->f;
if (isAtm(x) || rnk(x)==0) { if (isAtm(x) || rnk(x)==0) {
@ -295,7 +301,7 @@ B cell_c2(B d, B w, B x) { B f = c(Md1D,d)->f;
void print_md1BI(B x) { printf("%s", format_pm1(c(Md1,x)->extra)); } void print_md1BI(B x) { printf("%s", format_pm1(c(Md1,x)->extra)); }
B bi_tbl, bi_each, bi_fold, bi_scan, bi_const, bi_swap, bi_cell, bi_timed, bi_fchars, bi_fbytes, bi_flines, bi_import; B bi_tbl, bi_each, bi_fold, bi_scan, bi_const, bi_swap, bi_cell, bi_timed, bi_fchars, bi_fbytes, bi_flines, bi_import;
static inline void md1_init() { ba(tbl) ba(each) ba(fold) ba(scan) ba(const) ba(swap) ba(cell) ba(timed) bm(fchars) bm(fbytes) bm(flines) ba(import) static inline void md1_init() { ba(tbl) ba(each) ba(fold) ba(scan) ba(const) ba(swap) ba(cell) ba(timed) ba(fchars) bm(fbytes) bm(flines) ba(import)
ti[t_md1BI].print = print_md1BI; ti[t_md1BI].print = print_md1BI;
} }

View File

@ -64,7 +64,7 @@ u64 utf8lenB(B x) { // doesn't consume; may error as it verifies whether is all
} }
return res; return res;
} }
void toUTF8(B x, char* p) { // doesn't consume; doesn't verify anything; p must have utf8_lenB(x) bytes void toUTF8(B x, char* p) { // doesn't consume; doesn't verify anything; p must have utf8lenB(x) bytes
BS2B xgetU = TI(x).getU; BS2B xgetU = TI(x).getU;
usz ia = a(x)->ia; usz ia = a(x)->ia;
for (usz i = 0; i < ia; i++) { for (usz i = 0; i < ia; i++) {