From 93caa0398395f3ba6d6dcc556111c1cebde747b1 Mon Sep 17 00:00:00 2001 From: "Jesus Galan Lopez (yy)" Date: Sat, 4 Sep 2021 19:16:12 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=A2rand?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/sysfn.c | 11 +++++++++++ src/h.h | 3 +++ 2 files changed, 14 insertions(+) diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index e84afb62..575eb424 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -419,6 +419,16 @@ B makeRand_c1(B t, B x) { ns_set(r, rand_subsetName, m_nfn(rand_subsetDesc, inc(r))); return r; } +static B randNS; +B getRandNS() { + if (randNS.u == 0) { + B fn = bqn_exec(m_str32(U"•MakeRand"), emptyCVec(), emptySVec()); + randNS = c1(fn,m_f64(RANDSEED)); + gc_add(randNS); + dec(fn); + } + return inc(randNS); +} extern B replPath; // defined in main.c static NFnDesc* reBQNDesc; B reBQN_c1(B t, B x) { @@ -679,6 +689,7 @@ B sys_c1(B t, B x) { else if (eqStr(c, U"fmt")) r.a[i] = inc(bi_fmt); else if (eqStr(c, U"glyph")) r.a[i] = inc(bi_glyph); else if (eqStr(c, U"makerand")) r.a[i] = inc(bi_makeRand); + else if (eqStr(c, U"rand")) r.a[i] = getRandNS(); else if (eqStr(c, U"rebqn")) r.a[i] = inc(bi_reBQN); else if (eqStr(c, U"fromutf8")) r.a[i] = inc(bi_fromUtf8); else if (eqStr(c, U"path")) r.a[i] = inc(REQ_PATH); diff --git a/src/h.h b/src/h.h index 4e590910..79f68220 100644 --- a/src/h.h +++ b/src/h.h @@ -36,6 +36,9 @@ #ifndef FORMATTER #define FORMATTER 1 // use self-hosted formatter for output #endif +#ifndef RANDSEED + #define RANDSEED 1 // random seed used to make •rand +#endif // #define HEAP_VERIFY // enable usage of heapVerify() // #define ALLOC_STAT // store basic allocation statistics