From 20496865279b66f969b4ca3882e45bcedfa604e1 Mon Sep 17 00:00:00 2001 From: dzaima Date: Wed, 16 Feb 2022 01:41:46 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=A2term?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins.h | 1 + src/builtins/sysfn.c | 59 ++++++++++++++++++++++++++++++++++++++++---- src/h.h | 2 +- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/builtins.h b/src/builtins.h index 619e29de..f3061005 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -12,6 +12,7 @@ /* sysfn.c*/M(repr,"•Repr") M(fmt,"•Fmt") A(asrt,"!") A(casrt,"!") M(out,"•Out") M(show,"•Show") A(bqn,"•BQN") A(sh,"•SH") M(fromUtf8,"•FromUTF8") M(currentError,"•CurrentError") \ /* sysfn.c*/D(cmp,"•Cmp") A(hash,"•Hash") M(unixTime,"•UnixTime") M(monoTime,"•MonoTime") M(delay,"•Delay") M(makeRand,"•MakeRand") M(reBQN,"•ReBQN") M(exit,"•Exit") M(getLine,"•GetLine") \ /* sysfn.c*/M(fName,"•file.Name") \ +/* sysfn.c*/M(tRaw,"•term.Raw") M(tFlush,"•term.Flush") M(tCharB,"•term.CharB") M(tCharN,"•term.CharN") \ /* inverse.c*/M(setInvReg, "(SetInvReg)") M(setInvSwap, "(SetInvSwap)") M(nativeInvReg, "(NativeInvReg)") M(nativeInvSwap, "(NativeInvSwap)") \ /*internal.c*/M(itype,"•internal.Type") M(elType,"•internal.ElType") M(refc,"•internal.Refc") M(isPure,"•internal.IsPure") A(info,"•internal.Info") \ /*internal.c*/M(squeeze,"•internal.Squeeze") M(deepSqueeze,"•internal.DeepSqueeze") \ diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index be3ebdc5..38fa5df6 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -858,13 +858,49 @@ B sh_c2(B t, B w, B x) { return m_hVec3(m_i32(WEXITSTATUS(status)), s_out, s_err); } #else -B sh_c2(B t, B w, B x) { - thrM("•SH: CBQN was built without "); -} +B sh_c2(B t, B w, B x) { thrM("•SH: CBQN was built without "); } #endif -B sh_c1(B t, B x) { - return sh_c2(t, bi_N, x); +B sh_c1(B t, B x) { return sh_c2(t, bi_N, x); } + + + +#if __has_include() +#include +#include +B tFlush_c1(B t, B x) { + fflush(stdout); + fflush(stderr); + return x; } +B tRaw_c1(B t, B x) { + struct termios term; + tcgetattr(STDIN_FILENO, &term); + if (o2b(x)) term.c_lflag&= ~(ICANON | ECHO); + else term.c_lflag|= ICANON | ECHO; + tcsetattr(STDIN_FILENO, TCSAFLUSH, &term); + return x; +} +B tCharB_c1(B t, B x) { + dec(x); + int n = fgetc(stdin); + return n>=0? m_c32(n) : m_f64(0); +} +B tCharN_c1(B t, B x) { + dec(x); + fcntl(0, F_SETFL, O_NONBLOCK); + int n = fgetc(stdin); + fcntl(0, F_SETFL, 0); + return n>=0? m_c32(n) : m_f64(0); +} +#else +NORETURN B noTerm() { thrM("•term not available"); } +B tFlush_c1(B t, B x) { noTerm(); } +B tRaw_c1(B t, B x) { noTerm(); } +B tCharB_c1(B t, B x) { noTerm(); } +B tCharN_c1(B t, B x) { noTerm(); } +#endif + + typedef struct CastType { usz s; bool c; } CastType; static bool isCharType(u8 t) { @@ -981,6 +1017,18 @@ B getBitNS() { } +static B termNS; +B getTermNS() { + if (termNS.u == 0) { + #define F(X) inc(bi_##X), + Body* d = m_nnsDesc("flush", "raw", "charb", "charn"); + termNS = m_nns(d,F(tFlush)F(tRaw)F(tCharB)F(tCharN)); + #undef F + gc_add(termNS); + } + return inc(termNS); +} + B getInternalNS(void); B getMathNS(void); B getPrimitives(void); @@ -1015,6 +1063,7 @@ B sys_c1(B t, B x) { if (!wdpath.u) wdpath = path_abs(inc(cdPath)); cr = inc(wdpath); } + else if (eqStr(c, U"term")) cr = getTermNS(); else if (eqStr(c, U"internal")) cr = getInternalNS(); else if (eqStr(c, U"math")) cr = getMathNS(); else if (eqStr(c, U"bit")) cr = getBitNS(); diff --git a/src/h.h b/src/h.h index 72e099d4..32ab6a74 100644 --- a/src/h.h +++ b/src/h.h @@ -408,7 +408,7 @@ static u32 o2cu (B x) { return (u32)x.u; } static usz o2su (B x) { return (usz)x.f; } static f64 o2fu (B x) { return x.f; } static i64 o2i64u(B x) { return (i64)x.f; } -static bool o2b (B x) { usz t=o2s(x); if(t!=0&t!=1)thrM("Expected boolean"); return t; } +static bool o2b (B x) { i32 t=(i32)x.f; if(t!=x.f || t!=0&t!=1)thrM("Expected boolean"); return t; } static bool o2bu (B x) { return o2s(x); } static bool q_bit(B x) { return isNum(x) & (x.f==0 | x.f==1); } static bool q_c8 (B x) { return isC32(x) && ((u32)x.u) == ((u8 )x.u); }