From 5c2a921e5601957896587a628fa82c9409e1db2a Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Wed, 15 Dec 2021 20:19:47 -0500 Subject: [PATCH 1/6] Switch from fixed-primitives compiler (c) to variable (cc) --- genRuntime | 2 +- makefile | 6 +++--- src/load.c | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/genRuntime b/genRuntime index 16c98b92..1591902b 100755 --- a/genRuntime +++ b/genRuntime @@ -5,7 +5,7 @@ args←•args path←⊑•args CC ← {𝕨 •FChars ⟨1,path,𝕩⟩ •Import "cc.bqn"} "src/gen/src" •FChars "#define RT_SRC 0"∾@+10 - "src/gen/compiler"CC"c" + "src/gen/compiles"CC"cc" "src/gen/runtime0"CC"r0" "src/gen/runtime1"CC"r1" "src/gen/formatter"CC"f" diff --git a/makefile b/makefile index 8a802ed2..48c30683 100644 --- a/makefile +++ b/makefile @@ -124,9 +124,9 @@ ${bd}/%.o: src/builtins/%.c src/gen/customRuntime: @echo "Copying precompiled bytecode from the bytecode branch" - git checkout remotes/origin/bytecode src/gen/{compiler,formatter,runtime0,runtime1,src} - git reset src/gen/{compiler,formatter,runtime0,runtime1,src} -${bd}/load.o: src/gen/customRuntime + git checkout remotes/origin/bytecode src/gen/{compiles,formatter,runtime0,runtime1,src} + git reset src/gen/{compiles,formatter,runtime0,runtime1,src} +${bd}/load.o: src/gen/compiles diff --git a/src/load.c b/src/load.c index f507df23..520aa765 100644 --- a/src/load.c +++ b/src/load.c @@ -356,10 +356,11 @@ void load_init() { // very last init function B prevAsrt = runtime[n_asrt]; runtime[n_asrt] = bi_casrt; // horrible but GC is off so it's fiiiiiine Block* comp_b = load_compImport( - #include "gen/compiler" + #include "gen/compiles" ); runtime[n_asrt] = prevAsrt; - load_comp = m_funBlock(comp_b, 0); ptr_dec(comp_b); + B glyphs = m_hVec3(m_str32(U"+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍⋈↑↓↕«»⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔!"), m_str32(U"˙˜˘¨⌜⁼´˝`"), m_str32(U"∘○⊸⟜⌾⊘◶⎉⚇⍟⎊")); + load_comp = c1(m_funBlock(comp_b, 0), glyphs); ptr_dec(comp_b); gc_add(load_comp); From 4d394ea441fe2bf9ccfbc4a8430aff8f35893df6 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Wed, 15 Dec 2021 21:38:48 -0500 Subject: [PATCH 2/6] =?UTF-8?q?Store=20compiler=20and=20runtime=20in=20?= =?UTF-8?q?=E2=80=A2ReBQN=20result?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/sysfn.c | 15 ++++++++++----- src/load.c | 15 +++++++++++++-- src/vm.h | 4 +++- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 2b76ce0e..d68489ff 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -520,13 +520,16 @@ B getRandNS() { return incG(randNS); } static NFnDesc* reBQNDesc; +static B ns_getNUf(B ns, B field) { + B r = ns_getNU(ns, field, false); dec(field); return r; +} B reBQN_c1(B t, B x) { if (!isNsp(x)) thrM("•ReBQN: Argument must be a namespace"); - B replStr = m_str8l("repl"); - B repl = ns_getNU(x, replStr, false); dec(replStr); + B repl = ns_getNUf(x, m_str8l("repl")); + B prim = ns_getNUf(x, m_str8l("primitives")); + dec(x); i32 replVal = q_N(repl) || eqStr(repl,U"none")? 0 : eqStr(repl,U"strict")? 1 : eqStr(repl,U"loose")? 2 : 3; if (replVal==3) thrM("•ReBQN: Invalid repl value"); - dec(x); Block* initBlock = bqn_comp(m_str8l("\"(REPL initializer)\""), inc(cdPath), m_f64(0)); B scVal; if (replVal==0) { @@ -536,7 +539,9 @@ B reBQN_c1(B t, B x) { scVal = tag(sc,OBJ_TAG); } ptr_dec(initBlock); - return m_nfn(reBQNDesc, m_hVec2(m_f64(replVal), scVal)); + B dat = m_hVec4(m_f64(replVal), scVal, bi_N, bi_N); + init_comp(harr_ptr(dat)+2, prim); + return m_nfn(reBQNDesc, dat); } B repl_c2(B t, B w, B x) { vfyStr(x, "REPL", "𝕩"); @@ -550,7 +555,7 @@ B repl_c2(B t, B w, B x) { B res; if (replMode>0) { - Block* block = bqn_compSc(x, fullpath, args, sc, replMode==2); + Block* block = bqn_compScc(x, fullpath, args, sc, op[2], op[3], replMode==2); ptr_dec(sc->body); sc->body = ptr_inc(block->bodies[0]); res = execBlockInline(block, sc); diff --git a/src/load.c b/src/load.c index 520aa765..f58cb497 100644 --- a/src/load.c +++ b/src/load.c @@ -164,7 +164,7 @@ NOINLINE Block* bqn_comp(B str, B path, B args) { // consumes all comp_currEnvPos = prevEnvPos; return r; } -NOINLINE Block* bqn_compSc(B str, B path, B args, Scope* sc, bool repl) { // consumes str,path,args +Block* bqn_compScc(B str, B path, B args, Scope* sc, B comp, B rt, bool repl) { // consumes str,path,args B prevPath = comp_currPath ; comp_currPath = path; B prevArgs = comp_currArgs ; comp_currArgs = args; B prevSrc = comp_currSrc ; comp_currSrc = str; @@ -191,7 +191,7 @@ NOINLINE Block* bqn_compSc(B str, B path, B args, Scope* sc, bool repl) { // con csc = csc->psc; depth++; } - Block* r = load_compObj(c2(load_comp, m_hVec4(incG(load_rtObj), incG(bi_sys), vName, vDepth), inc(str)), str, path, sc); + Block* r = load_compObj(c2(comp, m_hVec4(incG(rt), incG(bi_sys), vName, vDepth), inc(str)), str, path, sc); dec(path); dec(args); comp_currPath = prevPath; comp_currArgs = prevArgs; @@ -199,6 +199,17 @@ NOINLINE Block* bqn_compSc(B str, B path, B args, Scope* sc, bool repl) { // con comp_currEnvPos = prevEnvPos; return r; } +NOINLINE Block* bqn_compSc(B str, B path, B args, Scope* sc, bool repl) { // consumes str,path,args + return bqn_compScc(str, path, args, sc, load_comp, load_rtObj, repl); +} +void init_comp(B* set, B prim) { // doesn't consume + if (q_N(prim)) { + set[0] = inc(load_comp); + set[1] = inc(load_rtObj); + } else { + thrM("•ReBQN: primitives⇐ unimplemented"); + } +} B bqn_exec(B str, B path, B args) { // consumes all Block* block = bqn_comp(str, path, args); diff --git a/src/vm.h b/src/vm.h index a0d76ed3..37ee3af7 100644 --- a/src/vm.h +++ b/src/vm.h @@ -142,10 +142,12 @@ struct Scope { }; Block* bqn_comp(B str, B path, B args); -Block* bqn_compSc(B str, B path, B args, Scope* sc, bool repl); +Block* bqn_compSc (B str, B path, B args, Scope* sc, bool repl); +Block* bqn_compScc(B str, B path, B args, Scope* sc, B comp, B rt, bool repl); Block* compile(B bcq, B objs, B blocks, B bodies, B indices, B tokenInfo, B src, B path, Scope* sc); Scope* m_scope(Body* body, Scope* psc, u16 varAm, i32 initVarAm, B* initVars); Body* m_body(i32 vam, i32 pos, u32 maxStack, u16 maxPSC); // leaves varIDs and nsDesc uninitialized +void init_comp(B* set, B prim); typedef struct Nvm_res { u8* p; B refs; } Nvm_res; Nvm_res m_nvm(Body* b); From 9e5f4886420f8992c57b0ebd2996e841b45717ff Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Thu, 16 Dec 2021 09:50:22 -0500 Subject: [PATCH 3/6] =?UTF-8?q?Allow=20custom=20compiler/runtime=20for=20n?= =?UTF-8?q?on-REPL=20=E2=80=A2ReBQN=20too?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/sysfn.c | 2 +- src/load.c | 21 +++++++++++++++++---- src/vm.h | 1 + 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index d68489ff..30f21cbc 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -561,7 +561,7 @@ B repl_c2(B t, B w, B x) { res = execBlockInline(block, sc); ptr_dec(block); } else { - res = bqn_exec(x, fullpath, args); + res = rebqn_exec(x, fullpath, args, op[2], op[3]); } return res; diff --git a/src/load.c b/src/load.c index f58cb497..da7b269c 100644 --- a/src/load.c +++ b/src/load.c @@ -124,6 +124,7 @@ Block* load_compImport(B bc, B objs, B blocks, B bodies) { // consumes all #endif B load_comp; +B load_compgen; B load_rtObj; B load_compArg; @@ -151,12 +152,12 @@ void load_gcFn() { mm_visit(rt_invFnReg); mm_visit(rt_invFnSwap); } -NOINLINE Block* bqn_comp(B str, B path, B args) { // consumes all +static Block* bqn_compc(B str, B path, B args, B comp, B compArg) { // consumes str,path,args B prevPath = comp_currPath ; comp_currPath = path; B prevArgs = comp_currArgs ; comp_currArgs = args; B prevSrc = comp_currSrc ; comp_currSrc = str; i64 prevEnvPos = comp_currEnvPos; comp_currEnvPos = envCurr-envStart; - Block* r = load_compObj(c2(load_comp, incG(load_compArg), inc(str)), str, path, NULL); + Block* r = load_compObj(c2(comp, incG(compArg), inc(str)), str, path, NULL); dec(path); dec(args); comp_currPath = prevPath; comp_currArgs = prevArgs; @@ -164,6 +165,9 @@ NOINLINE Block* bqn_comp(B str, B path, B args) { // consumes all comp_currEnvPos = prevEnvPos; return r; } +NOINLINE Block* bqn_comp(B str, B path, B args) { // consumes all + return bqn_compc(str, path, args, load_comp, load_compArg); +} Block* bqn_compScc(B str, B path, B args, Scope* sc, B comp, B rt, bool repl) { // consumes str,path,args B prevPath = comp_currPath ; comp_currPath = path; B prevArgs = comp_currArgs ; comp_currArgs = args; @@ -217,6 +221,14 @@ B bqn_exec(B str, B path, B args) { // consumes all ptr_dec(block); return res; } +B rebqn_exec(B str, B path, B args, B comp, B rt) { // consumes str,path,args + B rtsys = m_hVec2(rt, incG(bi_sys)); + Block* block = bqn_compc(str, path, args, comp, rtsys); + dec(rtsys); + B res = m_funBlock(block, 0); + ptr_dec(block); + return res; +} void bqn_setComp(B comp) { // consumes; doesn't unload old comp, but whatever load_comp = comp; gc_add(load_comp); @@ -371,8 +383,9 @@ void load_init() { // very last init function ); runtime[n_asrt] = prevAsrt; B glyphs = m_hVec3(m_str32(U"+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍⋈↑↓↕«»⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔!"), m_str32(U"˙˜˘¨⌜⁼´˝`"), m_str32(U"∘○⊸⟜⌾⊘◶⎉⚇⍟⎊")); - load_comp = c1(m_funBlock(comp_b, 0), glyphs); ptr_dec(comp_b); - gc_add(load_comp); + load_compgen = m_funBlock(comp_b, 0); + load_comp = c1(load_compgen, glyphs); ptr_dec(comp_b); + gc_add(load_compgen); gc_add(load_comp); #if FORMATTER diff --git a/src/vm.h b/src/vm.h index 37ee3af7..c6579edc 100644 --- a/src/vm.h +++ b/src/vm.h @@ -148,6 +148,7 @@ Block* compile(B bcq, B objs, B blocks, B bodies, B indices, B tokenInfo, B src, Scope* m_scope(Body* body, Scope* psc, u16 varAm, i32 initVarAm, B* initVars); Body* m_body(i32 vam, i32 pos, u32 maxStack, u16 maxPSC); // leaves varIDs and nsDesc uninitialized void init_comp(B* set, B prim); +B rebqn_exec(B str, B path, B args, B comp, B rt); typedef struct Nvm_res { u8* p; B refs; } Nvm_res; Nvm_res m_nvm(Body* b); From 89e8cf5a6c2cf1432884670e3e7df9e56bf05ec5 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Thu, 16 Dec 2021 14:27:20 -0500 Subject: [PATCH 4/6] =?UTF-8?q?Implement=20=E2=80=A2ReBQN=20=F0=9D=95=A9.p?= =?UTF-8?q?rimitive=20processing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/sysfn.c | 2 +- src/load.c | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 30f21cbc..d3faf5cf 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -527,7 +527,6 @@ B reBQN_c1(B t, B x) { if (!isNsp(x)) thrM("•ReBQN: Argument must be a namespace"); B repl = ns_getNUf(x, m_str8l("repl")); B prim = ns_getNUf(x, m_str8l("primitives")); - dec(x); i32 replVal = q_N(repl) || eqStr(repl,U"none")? 0 : eqStr(repl,U"strict")? 1 : eqStr(repl,U"loose")? 2 : 3; if (replVal==3) thrM("•ReBQN: Invalid repl value"); Block* initBlock = bqn_comp(m_str8l("\"(REPL initializer)\""), inc(cdPath), m_f64(0)); @@ -541,6 +540,7 @@ B reBQN_c1(B t, B x) { ptr_dec(initBlock); B dat = m_hVec4(m_f64(replVal), scVal, bi_N, bi_N); init_comp(harr_ptr(dat)+2, prim); + dec(x); return m_nfn(reBQNDesc, dat); } B repl_c2(B t, B w, B x) { diff --git a/src/load.c b/src/load.c index da7b269c..0bc31e19 100644 --- a/src/load.c +++ b/src/load.c @@ -211,7 +211,39 @@ void init_comp(B* set, B prim) { // doesn't consume set[0] = inc(load_comp); set[1] = inc(load_rtObj); } else { - thrM("•ReBQN: primitives⇐ unimplemented"); + if (!isArr(prim)||rnk(prim)!=1) thrM("•ReBQN: 𝕩.primitives must be a list"); + usz pia = a(prim)->ia; + usz np[3] = {0}; // number of functions, 1-modifiers, and 2-modifiers + SGetU(prim); + for (usz i = 0; i < pia; i++) { // check and count + B p = GetU(prim, i); + if (!isArr(p)||rnk(p)!=1||a(p)->ia!=2) thrM("•ReBQN: 𝕩.primitives must consist of glyph-primitive pairs"); + if (!isC32(IGet(p, 0))) thrM("•ReBQN 𝕩.primitives: Glyphs must be characters"); + B v = IGetU(p, 1); + i32 t = isFun(v) ? 0 : isMd1(v) ? 1 : isMd2(v) ? 2 : 3; + if (t==3) thrM("•ReBQN 𝕩.primitives: Primitives must be operations"); + np[t] += 1; + } + HArr_p r = m_harrUv(3); + u32* gl[3]; + usz sum = 0; + for (usz i = 0; i < 3; i++) { + usz l = np[i]; + r.a[i] = m_c32arrv(gl+i, l); + np[i] = sum; + sum += l; + } + HArr_p prh = m_harrUv(pia); + B *rt = prh.a; + for (usz i = 0; i < pia; i++) { + B gv = GetU(prim, i); + B v = IGet(gv, 1); + i32 t = isFun(v) ? 0 : isMd1(v) ? 1 : isMd2(v) ? 2 : 3; + *(gl[t]++) = o2cu(IGet(gv, 0)); + rt[np[t]++] = v; + } + set[1] = prh.b; + set[0] = c1(load_compgen, r.b); } } @@ -222,7 +254,7 @@ B bqn_exec(B str, B path, B args) { // consumes all return res; } B rebqn_exec(B str, B path, B args, B comp, B rt) { // consumes str,path,args - B rtsys = m_hVec2(rt, incG(bi_sys)); + B rtsys = m_hVec2(inc(rt), incG(bi_sys)); Block* block = bqn_compc(str, path, args, comp, rtsys); dec(rtsys); B res = m_funBlock(block, 0); @@ -383,8 +415,8 @@ void load_init() { // very last init function ); runtime[n_asrt] = prevAsrt; B glyphs = m_hVec3(m_str32(U"+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍⋈↑↓↕«»⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔!"), m_str32(U"˙˜˘¨⌜⁼´˝`"), m_str32(U"∘○⊸⟜⌾⊘◶⎉⚇⍟⎊")); - load_compgen = m_funBlock(comp_b, 0); - load_comp = c1(load_compgen, glyphs); ptr_dec(comp_b); + load_compgen = m_funBlock(comp_b, 0); ptr_dec(comp_b); + load_comp = c1(load_compgen, glyphs); gc_add(load_compgen); gc_add(load_comp); From 7b2c79fb87ca79db2da63931893e33f6ce11d383 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Thu, 16 Dec 2021 17:57:05 -0500 Subject: [PATCH 5/6] =?UTF-8?q?Implement=20=E2=80=A2primitives?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/sysfn.c | 28 ++++----------- src/load.c | 85 +++++++++++++++++++++++++++++++++----------- src/vm.h | 5 ++- 3 files changed, 73 insertions(+), 45 deletions(-) diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index d3faf5cf..488b1774 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -538,33 +538,17 @@ B reBQN_c1(B t, B x) { scVal = tag(sc,OBJ_TAG); } ptr_dec(initBlock); - B dat = m_hVec4(m_f64(replVal), scVal, bi_N, bi_N); - init_comp(harr_ptr(dat)+2, prim); + HArr_p d = m_harrUv(5); d.a[0] = m_f64(replVal); d.a[1] = scVal; + d.a[2]=d.a[3]=d.a[4]=bi_N; + init_comp(d.a+2, prim); dec(x); - return m_nfn(reBQNDesc, dat); + return m_nfn(reBQNDesc, d.b); } B repl_c2(B t, B w, B x) { vfyStr(x, "REPL", "𝕩"); - B o = nfn_objU(t); - B* op = harr_ptr(o); - i32 replMode = o2iu(op[0]); - Scope* sc = c(Scope, op[1]); - B fullpath; B args = args_path(&fullpath, w, "REPL"); - - B res; - if (replMode>0) { - Block* block = bqn_compScc(x, fullpath, args, sc, op[2], op[3], replMode==2); - ptr_dec(sc->body); - sc->body = ptr_inc(block->bodies[0]); - res = execBlockInline(block, sc); - ptr_dec(block); - } else { - res = rebqn_exec(x, fullpath, args, op[2], op[3]); - } - - return res; + return rebqn_exec(x, fullpath, args, nfn_objU(t)); } B repl_c1(B t, B x) { return repl_c2(t, emptyHVec(), x); @@ -812,6 +796,7 @@ B sh_c2(B t, B w, B x) { B getInternalNS(void); B getMathNS(void); +B getPrimitives(void); static Body* file_nsGen; B sys_c1(B t, B x) { @@ -864,6 +849,7 @@ B sys_c1(B t, B x) { else if (eqStr(c, U"makerand")) r.a[i] = incG(bi_makeRand); else if (eqStr(c, U"rand")) r.a[i] = getRandNS(); else if (eqStr(c, U"rebqn")) r.a[i] = incG(bi_reBQN); + else if (eqStr(c, U"primitives")) r.a[i] = getPrimitives(); else if (eqStr(c, U"fromutf8")) r.a[i] = incG(bi_fromUtf8); else if (eqStr(c, U"path")) r.a[i] = inc(REQ_PATH); else if (eqStr(c, U"name")) r.a[i] = inc(REQ_NAME); diff --git a/src/load.c b/src/load.c index 0bc31e19..f88b23a3 100644 --- a/src/load.c +++ b/src/load.c @@ -100,6 +100,7 @@ i64 comp_currEnvPos; B comp_currPath; B comp_currArgs; B comp_currSrc; +B comp_currRe; B rt_merge, rt_undo, rt_select, rt_slash, rt_join, rt_ud, rt_pick,rt_take, rt_drop, rt_group, rt_under, rt_reverse, rt_indexOf, rt_count, rt_memberOf, rt_find, rt_cell; @@ -127,6 +128,7 @@ B load_comp; B load_compgen; B load_rtObj; B load_compArg; +B load_glyphs; #if FORMATTER B load_fmt, load_repr; @@ -149,6 +151,7 @@ void load_gcFn() { mm_visit(comp_currPath); mm_visit(comp_currArgs); mm_visit(comp_currSrc); + mm_visit(comp_currRe); mm_visit(rt_invFnReg); mm_visit(rt_invFnSwap); } @@ -206,10 +209,23 @@ Block* bqn_compScc(B str, B path, B args, Scope* sc, B comp, B rt, bool repl) { NOINLINE Block* bqn_compSc(B str, B path, B args, Scope* sc, bool repl) { // consumes str,path,args return bqn_compScc(str, path, args, sc, load_comp, load_rtObj, repl); } + +B bqn_exec(B str, B path, B args) { // consumes all + Block* block = bqn_comp(str, path, args); + B res = m_funBlock(block, 0); + ptr_dec(block); + return res; +} +void bqn_setComp(B comp) { // consumes; doesn't unload old comp, but whatever + load_comp = comp; + gc_add(load_comp); +} + void init_comp(B* set, B prim) { // doesn't consume if (q_N(prim)) { set[0] = inc(load_comp); set[1] = inc(load_rtObj); + set[2] = inc(load_glyphs); } else { if (!isArr(prim)||rnk(prim)!=1) thrM("•ReBQN: 𝕩.primitives must be a list"); usz pia = a(prim)->ia; @@ -243,29 +259,55 @@ void init_comp(B* set, B prim) { // doesn't consume rt[np[t]++] = v; } set[1] = prh.b; + set[2] = inc(r.b); set[0] = c1(load_compgen, r.b); } } - -B bqn_exec(B str, B path, B args) { // consumes all - Block* block = bqn_comp(str, path, args); - B res = m_funBlock(block, 0); - ptr_dec(block); - return res; -} -B rebqn_exec(B str, B path, B args, B comp, B rt) { // consumes str,path,args - B rtsys = m_hVec2(inc(rt), incG(bi_sys)); - Block* block = bqn_compc(str, path, args, comp, rtsys); - dec(rtsys); - B res = m_funBlock(block, 0); - ptr_dec(block); - return res; -} -void bqn_setComp(B comp) { // consumes; doesn't unload old comp, but whatever - load_comp = comp; - gc_add(load_comp); +B getPrimitives(void) { + B g, r; + if (q_N(comp_currRe)) { + g = load_glyphs; r = load_rtObj; + } else { + B* o = harr_ptr(comp_currRe); g = o[4]; r = o[3]; + } + B* pr = harr_ptr(r); + B* gg = harr_ptr(g); + HArr_p ph = m_harrUv(a(r)->ia); B* p = ph.a; + for (usz gi = 0; gi < 3; gi++) { + usz l = a(gg[gi])->ia; + u32 *gp = c32arr_ptr(gg[gi]); + for (usz i = 0; i < l; i++) { + p[i] = m_hVec2(m_c32(gp[i]), inc(pr[i])); + } + p += l; pr += l; + } + return ph.b; } +B rebqn_exec(B str, B path, B args, B o) { + B prevRe = comp_currRe; comp_currRe = inc(o); + B* op = harr_ptr(o); + i32 replMode = o2iu(op[0]); + Scope* sc = c(Scope, op[1]); + B res; + if (replMode>0) { + Block* block = bqn_compScc(str, path, args, sc, op[2], op[3], replMode==2); + comp_currRe = prevRe; + ptr_dec(sc->body); + sc->body = ptr_inc(block->bodies[0]); + res = execBlockInline(block, sc); + ptr_dec(block); + } else { + B rtsys = m_hVec2(inc(op[3]), incG(bi_sys)); + Block* block = bqn_compc(str, path, args, op[2], rtsys); + dec(rtsys); + comp_currRe = prevRe; + res = m_funBlock(block, 0); + ptr_dec(block); + } + dec(o); + return res; +} static NOINLINE B m_lvB_0( ) { return emptyHVec(); } static NOINLINE B m_lvB_1(B a ) { return m_hVec1(a); } @@ -282,6 +324,7 @@ void load_init() { // very last init function comp_currPath = bi_N; comp_currArgs = bi_N; comp_currSrc = bi_N; + comp_currRe = bi_N; gc_addFn(load_gcFn); B fruntime[] = { /* +-×÷⋆√⌊⌈|¬ */ bi_add , bi_sub , bi_mul , bi_div , bi_pow , bi_root , bi_floor , bi_ceil , bi_stile , bi_not, @@ -414,10 +457,10 @@ void load_init() { // very last init function #include "gen/compiles" ); runtime[n_asrt] = prevAsrt; - B glyphs = m_hVec3(m_str32(U"+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍⋈↑↓↕«»⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔!"), m_str32(U"˙˜˘¨⌜⁼´˝`"), m_str32(U"∘○⊸⟜⌾⊘◶⎉⚇⍟⎊")); + load_glyphs = m_hVec3(m_str32(U"+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍⋈↑↓↕«»⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔!"), m_str32(U"˙˜˘¨⌜⁼´˝`"), m_str32(U"∘○⊸⟜⌾⊘◶⎉⚇⍟⎊")); load_compgen = m_funBlock(comp_b, 0); ptr_dec(comp_b); - load_comp = c1(load_compgen, glyphs); - gc_add(load_compgen); gc_add(load_comp); + load_comp = c1(load_compgen, inc(load_glyphs)); + gc_add(load_compgen); gc_add(load_comp); gc_add(load_glyphs); #if FORMATTER diff --git a/src/vm.h b/src/vm.h index c6579edc..921f7637 100644 --- a/src/vm.h +++ b/src/vm.h @@ -142,13 +142,12 @@ struct Scope { }; Block* bqn_comp(B str, B path, B args); -Block* bqn_compSc (B str, B path, B args, Scope* sc, bool repl); -Block* bqn_compScc(B str, B path, B args, Scope* sc, B comp, B rt, bool repl); +Block* bqn_compSc(B str, B path, B args, Scope* sc, bool repl); Block* compile(B bcq, B objs, B blocks, B bodies, B indices, B tokenInfo, B src, B path, Scope* sc); Scope* m_scope(Body* body, Scope* psc, u16 varAm, i32 initVarAm, B* initVars); Body* m_body(i32 vam, i32 pos, u32 maxStack, u16 maxPSC); // leaves varIDs and nsDesc uninitialized void init_comp(B* set, B prim); -B rebqn_exec(B str, B path, B args, B comp, B rt); +B rebqn_exec(B str, B path, B args, B o); typedef struct Nvm_res { u8* p; B refs; } Nvm_res; Nvm_res m_nvm(Body* b); From 12250ef202b5fa76f434b1a0a63fbcfcaee8789a Mon Sep 17 00:00:00 2001 From: dzaima Date: Fri, 17 Dec 2021 14:39:42 +0200 Subject: [PATCH 6/6] =?UTF-8?q?changes=20to=20=E2=80=A2ReBQN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- makefile | 2 +- src/core/harr.h | 1 + src/load.c | 52 ++++++++++++++++++++++++++----------------------- src/vm.h | 8 ++++---- 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/makefile b/makefile index 48c30683..85cf1036 100644 --- a/makefile +++ b/makefile @@ -126,7 +126,7 @@ src/gen/customRuntime: @echo "Copying precompiled bytecode from the bytecode branch" git checkout remotes/origin/bytecode src/gen/{compiles,formatter,runtime0,runtime1,src} git reset src/gen/{compiles,formatter,runtime0,runtime1,src} -${bd}/load.o: src/gen/compiles +${bd}/load.o: src/gen/customRuntime diff --git a/src/core/harr.h b/src/core/harr.h index d8c885e1..5f0cd824 100644 --- a/src/core/harr.h +++ b/src/core/harr.h @@ -63,6 +63,7 @@ static void harr_abandon(HArr_p p) { VTY(p.b, t_harrPartial); value_free((Value*)p.c); } +// unsafe-ish things - don't allocate/GC anything before having written to all items static HArr_p m_harrUv(usz ia) { HArr* r = m_arr(fsizeof(HArr,a,B,ia), t_harr, ia); arr_shVec((Arr*)r); diff --git a/src/load.c b/src/load.c index f88b23a3..e61faa46 100644 --- a/src/load.c +++ b/src/load.c @@ -155,7 +155,7 @@ void load_gcFn() { mm_visit(rt_invFnReg); mm_visit(rt_invFnSwap); } -static Block* bqn_compc(B str, B path, B args, B comp, B compArg) { // consumes str,path,args +static NOINLINE Block* bqn_compc(B str, B path, B args, B comp, B compArg) { // consumes str,path,args B prevPath = comp_currPath ; comp_currPath = path; B prevArgs = comp_currArgs ; comp_currArgs = args; B prevSrc = comp_currSrc ; comp_currSrc = str; @@ -168,7 +168,7 @@ static Block* bqn_compc(B str, B path, B args, B comp, B compArg) { // consumes comp_currEnvPos = prevEnvPos; return r; } -NOINLINE Block* bqn_comp(B str, B path, B args) { // consumes all +Block* bqn_comp(B str, B path, B args) { // consumes all return bqn_compc(str, path, args, load_comp, load_compArg); } Block* bqn_compScc(B str, B path, B args, Scope* sc, B comp, B rt, bool repl) { // consumes str,path,args @@ -216,54 +216,56 @@ B bqn_exec(B str, B path, B args) { // consumes all ptr_dec(block); return res; } -void bqn_setComp(B comp) { // consumes; doesn't unload old comp, but whatever - load_comp = comp; - gc_add(load_comp); -} -void init_comp(B* set, B prim) { // doesn't consume +void init_comp(B* set, B prim) { if (q_N(prim)) { set[0] = inc(load_comp); set[1] = inc(load_rtObj); set[2] = inc(load_glyphs); } else { - if (!isArr(prim)||rnk(prim)!=1) thrM("•ReBQN: 𝕩.primitives must be a list"); + if (!isArr(prim) || rnk(prim)!=1) thrM("•ReBQN: 𝕩.primitives must be a list"); usz pia = a(prim)->ia; usz np[3] = {0}; // number of functions, 1-modifiers, and 2-modifiers SGetU(prim); for (usz i = 0; i < pia; i++) { // check and count B p = GetU(prim, i); - if (!isArr(p)||rnk(p)!=1||a(p)->ia!=2) thrM("•ReBQN: 𝕩.primitives must consist of glyph-primitive pairs"); + if (!isArr(p) || rnk(p)!=1 || a(p)->ia!=2) thrM("•ReBQN: 𝕩.primitives must consist of glyph-primitive pairs"); if (!isC32(IGet(p, 0))) thrM("•ReBQN 𝕩.primitives: Glyphs must be characters"); B v = IGetU(p, 1); - i32 t = isFun(v) ? 0 : isMd1(v) ? 1 : isMd2(v) ? 2 : 3; + i32 t = isFun(v)? 0 : isMd1(v) ? 1 : isMd2(v) ? 2 : 3; if (t==3) thrM("•ReBQN 𝕩.primitives: Primitives must be operations"); - np[t] += 1; + np[t]+= 1; } - HArr_p r = m_harrUv(3); + + usz i = 0; + HArr_p r = m_harrs(3, &i); u32* gl[3]; usz sum = 0; - for (usz i = 0; i < 3; i++) { + for (; i < 3; i++) { usz l = np[i]; r.a[i] = m_c32arrv(gl+i, l); np[i] = sum; - sum += l; + sum+= l; } - HArr_p prh = m_harrUv(pia); - B *rt = prh.a; - for (usz i = 0; i < pia; i++) { + harr_fv(r); + + i = 0; + HArr_p prh = m_harrs(pia, &i); + B* rt = prh.a; + for (; i < pia; i++) { B gv = GetU(prim, i); B v = IGet(gv, 1); i32 t = isFun(v) ? 0 : isMd1(v) ? 1 : isMd2(v) ? 2 : 3; *(gl[t]++) = o2cu(IGet(gv, 0)); rt[np[t]++] = v; } - set[1] = prh.b; + + set[1] = harr_fv(prh); set[2] = inc(r.b); set[0] = c1(load_compgen, r.b); } } -B getPrimitives(void) { +B getPrimitives() { B g, r; if (q_N(comp_currRe)) { g = load_glyphs; r = load_rtObj; @@ -272,16 +274,18 @@ B getPrimitives(void) { } B* pr = harr_ptr(r); B* gg = harr_ptr(g); - HArr_p ph = m_harrUv(a(r)->ia); B* p = ph.a; + usz pi = 0; + HArr_p ph = m_harrs(a(r)->ia, &pi); for (usz gi = 0; gi < 3; gi++) { usz l = a(gg[gi])->ia; - u32 *gp = c32arr_ptr(gg[gi]); + u32* gp = c32arr_ptr(gg[gi]); for (usz i = 0; i < l; i++) { - p[i] = m_hVec2(m_c32(gp[i]), inc(pr[i])); + ph.a[pi] = m_hVec2(m_c32(gp[i]), inc(pr[i])); + pi++; } - p += l; pr += l; + pr+= l; } - return ph.b; + return harr_fv(ph); } B rebqn_exec(B str, B path, B args, B o) { diff --git a/src/vm.h b/src/vm.h index 921f7637..36dc6152 100644 --- a/src/vm.h +++ b/src/vm.h @@ -141,13 +141,13 @@ struct Scope { B vars[]; }; -Block* bqn_comp(B str, B path, B args); -Block* bqn_compSc(B str, B path, B args, Scope* sc, bool repl); +Block* bqn_comp(B str, B path, B args); // consumes all +Block* bqn_compSc(B str, B path, B args, Scope* sc, bool repl); // consumes str,path,args Block* compile(B bcq, B objs, B blocks, B bodies, B indices, B tokenInfo, B src, B path, Scope* sc); Scope* m_scope(Body* body, Scope* psc, u16 varAm, i32 initVarAm, B* initVars); Body* m_body(i32 vam, i32 pos, u32 maxStack, u16 maxPSC); // leaves varIDs and nsDesc uninitialized -void init_comp(B* set, B prim); -B rebqn_exec(B str, B path, B args, B o); +void init_comp(B* set, B prim); // doesn't consume; writes into first 3 elements of set +B rebqn_exec(B str, B path, B args, B o); // consumes str,path,args typedef struct Nvm_res { u8* p; B refs; } Nvm_res; Nvm_res m_nvm(Body* b);