From 8ed4019edf22c9079741b40cb4d49bbe13eef157 Mon Sep 17 00:00:00 2001 From: dzaima Date: Sat, 8 May 2021 15:47:04 +0300 Subject: [PATCH] bi_emptyHVec, actually fix !GS_REALLOC --- src/h.h | 2 +- src/harr.c | 2 ++ src/sysfn.c | 3 +-- src/vm.c | 32 ++++++++++++++++++++++++++------ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/h.h b/src/h.h index 8f2c6c25..bc2dfe29 100644 --- a/src/h.h +++ b/src/h.h @@ -393,7 +393,7 @@ TypeInfo ti[t_COUNT]; #define TI(x) (ti[v(x)->type]) -B bi_N, bi_noVar, bi_badHdr, bi_optOut, bi_noFill; +B bi_N, bi_noVar, bi_badHdr, bi_optOut, bi_noFill, bi_emptyHVec; void do_nothing(B x) { } bool isNothing(B b) { return b.u==bi_N.u; } diff --git a/src/harr.c b/src/harr.c index 6524c620..eff6f5ca 100644 --- a/src/harr.c +++ b/src/harr.c @@ -174,4 +174,6 @@ static inline void harr_init() { ti[t_harr].print = arr_print; ti[t_hslice].print = arr_print; ti[t_harrPartial].print = harrP_print; ti[t_harr].isArr = true; ti[t_hslice].isArr = true; ti[t_harr].canStore = harr_canStore; + bi_emptyHVec = m_harrUv(0).b; + gc_add(bi_emptyHVec); } diff --git a/src/sysfn.c b/src/sysfn.c index 9eb11dbf..82a6c417 100644 --- a/src/sysfn.c +++ b/src/sysfn.c @@ -167,13 +167,12 @@ B sys_c1(B t, B x) { for (; i < a(x)->ia; i++) { B c = xgetU(x,i); if (eqStr(c, U"internal")) r.a[i] = inc(bi_internal); - else if (eqStr(c, U"eq")) r.a[i] = inc(bi_feq); else if (eqStr(c, U"decompose")) r.a[i] = inc(bi_decp); else if (eqStr(c, U"primind")) r.a[i] = inc(bi_primInd); else if (eqStr(c, U"type")) r.a[i] = inc(bi_type); else if (eqStr(c, U"out")) r.a[i] = inc(bi_out); else if (eqStr(c, U"show")) r.a[i] = inc(bi_show); - else thrM("Unknown system function"); + else { dec(x); thrM("Unknown system function"); } } return harr_fcd(r, x); } diff --git a/src/vm.c b/src/vm.c index 112037a3..08f9ba2b 100644 --- a/src/vm.c +++ b/src/vm.c @@ -290,6 +290,18 @@ void gsAdd(B x) { B gsPop() { return *--gStack; } +void gsPrint() { + B* c = gStackStart; + i32 i = 0; + while (c!=gStack) { + printf("%d: ", i); + print(*c); + printf(", refc=%d\n", v(*c)->refc); + c++; + i++; + } +} + B evalBC(Body* b, Scope* sc) { // doesn't consume #ifdef DEBUG_VM bcDepth+= 2; @@ -359,11 +371,17 @@ B evalBC(Body* b, Scope* sc) { // doesn't consume } case ARRO: case ARRM: { i32 sz = *bc++; - HArr_p r = m_harrUv(sz); - bool allNum = true; - for (i32 i = 0; i < sz; i++) if (!isNum(r.a[sz-i-1] = POP)) allNum = false; - GS_UPD; - ADD(allNum && sz? withFill(r.b, m_f64(0)) : r.b); + if (sz==0) { + ADD(inc(bi_emptyHVec)); + } else { + HArr_p r = m_harrUv(sz); + bool allNum = true; + for (i32 i = 0; i < sz; i++) if (!isNum(r.a[sz-i-1] = POP)) allNum = false; + if (allNum) { + GS_UPD; + ADD(withFill(r.b, m_f64(0))); + } else ADD(r.b); + } break; } case DFND: { @@ -428,7 +446,9 @@ B evalBC(Body* b, Scope* sc) { // doesn't consume #ifdef DEBUG_VM bcDepth-= 2; #endif - return POP; + B r = POP; + GS_UPD; + return r; #undef P #undef ADD #undef POP