diff --git a/docs/system.md b/docs/system.md index cafd51bd..96357427 100644 --- a/docs/system.md +++ b/docs/system.md @@ -44,6 +44,10 @@ See [the BQN specification](https://mlochbaum.github.io/BQN/spec/system.html) fo `•term.OutRaw` and `•term.ErrRaw` output the given bytes directly to the specific stream, without any trailing newline. May be removed once a proper interface for stream I/O has been made. +## `•ns` + +`ns •ns.Get "abc"`, `ns •ns.Has "abc"`, and `•ns.Keys ns` for reading namespace information. Order of `•ns.Keys` result is undefined; `Get` & `Has` currently expect the key name to be normalized (i.e. lowercase & without underscores). + ## `•_while_` While `𝕨𝔾𝕩`, execute `𝕩↩𝕨𝔽𝕩`. Equivalent to `{𝕨𝔾𝕩? 𝕨 𝕊 𝕨𝔽𝕩; 𝕩}`. diff --git a/src/builtins.h b/src/builtins.h index e2c55cd4..ec9ed96a 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -12,6 +12,7 @@ /* sysfn.c*/M(type,"•Type") M(decp,"•Decompose") M(repr,"•Repr") M(parseFloat,"•ParseFloat") M(fmt,"•Fmt") A(asrt,"!") A(casrt,"!") M(out,"•Out") M(show,"•Show") A(bqn,"•BQN") \ /* sysfn.c*/A(sh,"•SH") M(fromUtf8,"•FromUTF8") M(toUtf8,"•ToUTF8") M(currentError,"•CurrentError") D(cmp,"•Cmp") A(hash,"•Hash") M(unixTime,"•UnixTime")\ /* sysfn.c*/M(monoTime,"•MonoTime") M(delay,"•Delay") M(makeRand,"•MakeRand") M(reBQN,"•ReBQN") M(exit,"•Exit") M(getLine,"•GetLine") \ +/* sysfn.c*/D(nGet,"•ns.Get") D(nHas,"•ns.Has") M(nKeys,"•ns.Keys") \ /* sysfn.c*/M(fName,"•file.Name") M(fParent,"•file.Parent") \ /* sysfn.c*/M(tRawMode,"•term.RawMode") M(tFlush,"•term.Flush") M(tCharB,"•term.CharB") M(tCharN,"•term.CharN") M(tOutRaw,"•term.OutRaw") M(tErrRaw,"•term.ErrRaw") \ /* inverse.c*/M(setInvReg,"(SetInvReg)") M(setInvSwap,"(SetInvSwap)") M(nativeInvReg,"(NativeInvReg)") M(nativeInvSwap,"(NativeInvSwap)") \ diff --git a/src/builtins/slash.c b/src/builtins/slash.c index a87f4de9..c1123bdd 100644 --- a/src/builtins/slash.c +++ b/src/builtins/slash.c @@ -687,8 +687,8 @@ B slash_c2(B t, B w, B x) { usz csz = arr_csz(x); MAKE_MUT(r0, s*csz) mut_init(r0, TI(x,elType)); MUTG_INIT(r0); SGetU(w) - if (csz==1) { SGetU(x) usz ri=0; for (ux i=0; idesc; + ux am = desc->varAm; + HArr_p r = m_harr0v(am); + for (ux i = 0; i < am; i++) r.a[i] = incG(gid2str(desc->expGIDs[i])); + decG(x); + return r.b; +} +B nGet_c2(B t, B w, B x) { + if (!isNsp(w)) thrM("•ns.Has: 𝕨 must be a namespace"); + vfyStr(x, "•ns.Get", "𝕩"); + B r = ns_getNU(w, x, true); + decG(w); decG(x); + return inc(r); +} +B nHas_c2(B t, B w, B x) { + if (!isNsp(w)) thrM("•ns.Has: 𝕨 must be a namespace"); + vfyStr(x, "•ns.Get", "𝕩"); + B r = ns_getNU(w, x, false); + decG(w); decG(x); + return m_i32(!q_N(r)); +} +static B nsNS; +B getNsNS(void) { + if (nsNS.u == 0) { + #define F(X) incG(bi_##X), + Body* d = m_nnsDesc("get", "has", "keys"); + nsNS = m_nns(d,F(nGet)F(nHas)F(nKeys)); + #undef F + gc_add(nsNS); + } + return incG(nsNS); +} + + typedef struct CastType { usz s; bool c; } CastType; static bool isCharType(u8 t) { @@ -1473,7 +1509,8 @@ static Body* file_nsGen; F("state", U"•state", tag(15,VAR_TAG)) \ F("args", U"•args", tag(16,VAR_TAG)) \ F("listsys", U"•listsys", tag(17,VAR_TAG)) \ - OPTSYS(NATIVE_COMPILER)(F("compobj", U"•CompObj", tag(18,VAR_TAG))) + OPTSYS(NATIVE_COMPILER)(F("compobj", U"•CompObj", tag(18,VAR_TAG))) \ + F("ns", U"•ns", tag(19,VAR_TAG)) NFnDesc* ffiloadDesc; B ffiload_c2(B t, B w, B x); @@ -1577,6 +1614,7 @@ B sys_c1(B t, B x) { } case 17: cr = incG(curr_ns); break; // •listsys case 18: cr = incG(bi_compObj); break; // •CompObj + case 19: cr = getNsNS(); break; // •ns } HARR_ADD(r, i, cr); } @@ -1609,6 +1647,7 @@ u32* dsv_text[] = { U"•internal.ClearRefs",U"•internal.DeepSqueeze",U"•internal.EEqual",U"•internal.ElType",U"•internal.HeapDump",U"•internal.Info",U"•internal.IsPure",U"•internal.ListVariations",U"•internal.Refc",U"•internal.Squeeze",U"•internal.Temp",U"•internal.Type",U"•internal.Unshare",U"•internal.Variation", U"•math.Acos",U"•math.Acosh",U"•math.Asin",U"•math.Asinh",U"•math.Atan",U"•math.Atan2",U"•math.Atanh",U"•math.Cbrt",U"•math.Comb",U"•math.Cos",U"•math.Cosh",U"•math.Erf",U"•math.ErfC",U"•math.Expm1",U"•math.Fact",U"•math.GCD",U"•math.Hypot",U"•math.LCM",U"•math.Log10",U"•math.Log1p",U"•math.Log2",U"•math.LogFact",U"•math.Sin",U"•math.Sinh",U"•math.Sum",U"•math.Tan",U"•math.Tanh", + U"•ns.Get",U"•ns.Has",U"•ns.Keys", U"•rand.Deal",U"•rand.Range",U"•rand.Subset", U"•term.CharB",U"•term.CharN",U"•term.ErrRaw",U"•term.Flush",U"•term.OutRaw",U"•term.RawMode", NULL