diff --git a/genRuntime b/genRuntime.bqn similarity index 100% rename from genRuntime rename to genRuntime.bqn diff --git a/genRuntimeSrc b/genRuntimeSrc deleted file mode 100755 index 02387c29..00000000 --- a/genRuntimeSrc +++ /dev/null @@ -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 diff --git a/genRuntimeSrc.bqn b/genRuntimeSrc.bqn new file mode 100755 index 00000000..2cde278a --- /dev/null +++ b/genRuntimeSrc.bqn @@ -0,0 +1,9 @@ +#!/usr/bin/env bqn +args←•args +"Usage: ./getRuntime.bqn path/to/mlochbaum/BQN"!1≤≠args +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" diff --git a/src/file.c b/src/file.c index c2a1d234..6376f796 100644 --- a/src/file.c +++ b/src/file.c @@ -3,13 +3,18 @@ typedef struct TmpFile { // to be turned into a proper I8Arr i8 a[]; } 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); char p[plen+1]; toUTF8(path, p); p[plen] = 0; - FILE* f = fopen(p, "r"); - if (f==NULL) thrF("Couldn't read file \"%R\"", path); + FILE* f = fopen(p, mode); + 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); u64 len = ftell(f); fseek(f, 0, SEEK_SET); @@ -17,7 +22,6 @@ TmpFile* file_bytes(B path) { // consumes; may throw arr_shVec(tag(src,ARR_TAG), len); fread((char*)src->a, 1, len, f); fclose(f); - dec(path); return src; } 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); u32* rp; B r = m_c32arrv(&rp, 2); rp[0] = '.'; rp[1] = '/'; return r; -} \ No newline at end of file +} + + +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); +} diff --git a/src/md1.c b/src/md1.c index 519c41b1..74afe7b4 100644 --- a/src/md1.c +++ b/src/md1.c @@ -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); } + +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 cell_c1(B d, B x) { B f = c(Md1D,d)->f; 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)); } 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; } diff --git a/src/utf.c b/src/utf.c index 1019a5ba..878a2438 100644 --- a/src/utf.c +++ b/src/utf.c @@ -64,7 +64,7 @@ u64 utf8lenB(B x) { // doesn't consume; may error as it verifies whether is all } 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; usz ia = a(x)->ia; for (usz i = 0; i < ia; i++) {