From 038b27072f393d001c053ffdb855ea4e3be67ecb Mon Sep 17 00:00:00 2001 From: dzaima Date: Fri, 28 May 2021 21:57:45 +0300 Subject: [PATCH] =?UTF-8?q?=E2=80=A2internal=20namespace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- obj/subMakefile | 2 +- src/builtins/internal.c | 64 +++++++++++++++++++++++++++++++++++++++++ src/builtins/sysfn.c | 28 ++---------------- src/h.h | 17 +++++------ src/opt/single.c | 1 + 5 files changed, 78 insertions(+), 34 deletions(-) create mode 100644 src/builtins/internal.c diff --git a/obj/subMakefile b/obj/subMakefile index 6e5ea457..5955d969 100644 --- a/obj/subMakefile +++ b/obj/subMakefile @@ -38,7 +38,7 @@ utils: utf.o hash.o file.o @echo $< | cut -c 11- @$(CMD) $@.d -c $< -builtins: arithm.o arithd.o cmp.o sfns.o sort.o md1.o md2.o fns.o sysfn.o +builtins: arithm.o arithd.o cmp.o sfns.o sort.o md1.o md2.o fns.o sysfn.o internal.o %.o: ../../src/builtins/%.c @echo $< | cut -c 11- @$(CMD) $@.d -c $< diff --git a/src/builtins/internal.c b/src/builtins/internal.c new file mode 100644 index 00000000..f3bd9027 --- /dev/null +++ b/src/builtins/internal.c @@ -0,0 +1,64 @@ +#include "../core.h" +#include "../utils/mut.h" + +B itype_c1(B t, B x) { + B r; + if(isVal(x)) { + r = m_str8l(format_type(v(x)->type)); + } else { + if (isF64(x)) r = m_str32(U"tagged f64"); + else if (isI32(x)) r = m_str32(U"tagged i32"); + else if (isC32(x)) r = m_str32(U"tagged c32"); + else if (isTag(x)) r = m_str32(U"tagged tag"); + else if (isVar(x)) r = m_str32(U"tagged var"); + else if (isExt(x)) r = m_str32(U"tagged extvar"); + else r = m_str32(U"tagged unknown"); + } + dec(x); + return r; +} +B refc_c1(B t, B x) { + B r = isVal(x)? m_i32(v(x)->refc) : m_str32(U"(not heap-allocated)"); + dec(x); + return r; +} +B squeeze_c1(B t, B x) { + return bqn_squeeze(x); +} +B isPure_c1(B t, B x) { + B r = m_f64(isPureFn(x)); + dec(x); + return r; +} + +B info_c1(B t, B x) { + B s = inc(bi_emptyCVec); + AFMT("%xl: ", x.u); + if (isVal(x)) { + Value* xv = v(x); + AFMT("refc:%i ", xv->refc); + AFMT("mmInfo:%i ", xv->mmInfo); + AFMT("flags:%i ", xv->flags); + AFMT("extra:%i ", xv->extra); + AFMT("type:%i=%S ", xv->type, format_type(xv->type)); + AFMT("alloc:%l", mm_size(xv)); + dec(x); + } else { + A8("not heap-allocated"); + } + return s; +} + +static B internalNS; +B getInternalNS() { + if (internalNS.u == 0) { + #define F(X) inc(bi_##X), + B fn = bqn_exec(m_str32(U"{ Type, Refc, Squeeze, IsPure, Info⟩⇐𝕩}"), inc(bi_emptyHVec), inc(bi_emptyHVec)); + B arg = m_caB(7, (B[]){F(itype)F(refc)F(squeeze)F(isPure)F(info)}); + #undef F + internalNS = c1(fn,arg); + gc_add(internalNS); + dec(fn); + } + return inc(internalNS); +} diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 7d1fe9a7..d42fa36d 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -119,30 +119,6 @@ B asrt_c2(B t, B w, B x) { thr(w); } -bool isPureFn(B x); -B internal_c2(B t, B w, B x) { - B r; - i32 id = o2i(w); - if(id==0) { - if(isVal(x)) { char* c = format_type(v(x)->type); r = m_str8(strlen(c), c); } - else { - if (isF64(x)) r = m_str32(U"tagged f64"); - else if (isI32(x)) r = m_str32(U"tagged i32"); - else if (isC32(x)) r = m_str32(U"tagged c32"); - else if (isTag(x)) r = m_str32(U"tagged tag"); - else if (isVar(x)) r = m_str32(U"tagged var"); - else if (isExt(x)) r = m_str32(U"tagged extvar"); - else r = m_str32(U"tagged unknown"); - } - } else if(id==1) { r = isVal(x)? m_i32(v(x)->mmInfo & 0x7f) : m_str32(U"(not heap-allocated)"); } - else if(id==2) { r = isVal(x)? m_i32(v(x)->refc) : m_str32(U"(not heap-allocated)"); } - else if(id==3) { printf("%p\n", (void*)x.u); r = inc(x); } - else if(id==4) { r = m_f64(isPureFn(x)); } - else if(id==5) { r = bqn_squeeze(inc(x)); } - else { dec(x); thrF("•Internal: 𝕨≡%i is invalid", id); } - dec(x); - return r; -} B sys_c1(B t, B x); B out_c1(B t, B x) { printRaw(x); putchar('\n'); return x; } B show_c1(B t, B x) { @@ -195,6 +171,8 @@ static B makeRel(B md) { // doesn't consume return m1_d(inc(md), path_dir(inc(comp_currPath))); } +B getInternalNS(); + B sys_c1(B t, B x) { assert(isArr(x)); usz i = 0; @@ -204,7 +182,7 @@ B sys_c1(B t, B x) { B c = xgetU(x,i); if (eqStr(c, U"out")) r.a[i] = inc(bi_out); else if (eqStr(c, U"show")) r.a[i] = inc(bi_show); - else if (eqStr(c, U"internal")) r.a[i] = inc(bi_internal); + else if (eqStr(c, U"internal")) r.a[i] = getInternalNS(); else if (eqStr(c, U"type")) r.a[i] = inc(bi_type); else if (eqStr(c, U"decompose")) r.a[i] = inc(bi_decp); else if (eqStr(c, U"primind")) r.a[i] = inc(bi_primInd); diff --git a/src/h.h b/src/h.h index cd73864b..10a785c2 100644 --- a/src/h.h +++ b/src/h.h @@ -155,17 +155,18 @@ enum ElType { // a⌈b shall return the type that can store both, if possible; a char* format_type(u8 u); #define FOR_PFN(A,M,D) \ - /*arith.c*/ A(add,"+") A(sub,"-") A(mul,"×") A(div,"÷") A(pow,"⋆") A(floor,"⌊") A(ceil,"⌈") A(stile,"|") A(eq,"=") \ - /*arith.c*/ A(ne,"≠") D(le,"≤") D(ge,"≥") A(lt,"<") A(gt,">") A(and,"∧") A(or,"∨") A(not,"¬") A(log,"⋆⁼") \ - /*fns.c*/ A(ud,"↕") A(fne,"≢") A(feq,"≡") A(ltack,"⊣") A(rtack,"⊢") M(fmtF,"•FmtF") A(indexOf,"⊐") A(memberOf,"∊") A(find,"⍷") A(count,"⊒") \ - /*sfns.c*/ A(shape,"⥊") A(pick,"⊑") A(pair,"{𝕨‿𝕩}") A(select,"⊏") A(slash,"/") A(join,"∾") A(couple,"≍") A(shiftb,"»") A(shifta,"«") A(take,"↑") A(drop,"↓") A(group,"⊔") A(reverse,"⌽") \ - /*sort.c*/ A(gradeUp,"⍋") A(gradeDown,"⍒") \ - /*sysfn.c*/ M(type,"•Type") M(decp,"•Decompose") M(primInd,"•PrimInd") M(glyph,"•Glyph") M(repr,"•Repr") A(fill,"•FillFn") \ - /*sysfn.c*/ A(grLen,"•GroupLen") D(grOrd,"•groupOrd") A(asrt,"!") M(out,"•Out") M(show,"•Show") M(sys,"•getsys") M(bqn,"•BQN") D(cmp,"•Cmp") D(internal,"•Internal") A(hash,"•Hash") \ +/* arith.c*/A(add,"+") A(sub,"-") A(mul,"×") A(div,"÷") A(pow,"⋆") A(floor,"⌊") A(ceil,"⌈") A(stile,"|") A(eq,"=") \ +/* arith.c*/A(ne,"≠") D(le,"≤") D(ge,"≥") A(lt,"<") A(gt,">") A(and,"∧") A(or,"∨") A(not,"¬") A(log,"⋆⁼") \ +/* fns.c*/A(ud,"↕") A(fne,"≢") A(feq,"≡") A(ltack,"⊣") A(rtack,"⊢") M(fmtF,"•FmtF") A(indexOf,"⊐") A(memberOf,"∊") A(find,"⍷") A(count,"⊒") \ +/* sfns.c*/A(shape,"⥊") A(pick,"⊑") A(pair,"{𝕨‿𝕩}") A(select,"⊏") A(slash,"/") A(join,"∾") A(couple,"≍") A(shiftb,"»") A(shifta,"«") A(take,"↑") A(drop,"↓") A(group,"⊔") A(reverse,"⌽") \ +/* sort.c*/A(gradeUp,"⍋") A(gradeDown,"⍒") \ +/* sysfn.c*/M(type,"•Type") M(decp,"•Decompose") M(primInd,"•PrimInd") M(glyph,"•Glyph") A(fill,"•FillFn") M(sys,"•getsys") A(grLen,"•GroupLen") D(grOrd,"•groupOrd") \ +/* sysfn.c*/M(repr,"•Repr") A(asrt,"!") M(out,"•Out") M(show,"•Show") M(bqn,"•BQN") D(cmp,"•Cmp") A(hash,"•Hash") \ +/*internal.c*/M(itype,"•internal.Type") M(refc,"•internal.Refc") M(squeeze,"•internal.Squeeze") M(isPure,"•internal.IsPure") M(info,"•internal.Info") #define FOR_PM1(A,M,D) \ /*md1.c*/ A(tbl,"⌜") A(each,"¨") A(fold,"´") A(scan,"`") A(const,"˙") A(swap,"˜") A(cell,"˘") \ - /*md1.c*/ A(timed,"•_timed") A(fchars,"•FChars") M(fbytes,"•FBytes") M(flines,"•FLines") A(import,"•Import") + /*md1.c*/ A(timed,"•_timed") A(fchars,"•FChars") M(fbytes,"•FBytes") M(flines,"•FLines") A(import,"•Import") \ #define FOR_PM2(A,M,D) \ /*md2.c*/ A(val,"⊘") A(repeat,"⍟") A(fillBy,"•_fillBy_") A(catch,"⎊") \ diff --git a/src/opt/single.c b/src/opt/single.c index 06776a21..a8078cec 100644 --- a/src/opt/single.c +++ b/src/opt/single.c @@ -20,6 +20,7 @@ #include "../builtins/cmp.c" #include "../builtins/md1.c" #include "../builtins/md2.c" +#include "../builtins/internal.c" #include "../vm.c" #include "../ns.c" #include "../rtwrap.c"