From 4af4b45ab7956aaa394b7dd632fa8b765a5f2202 Mon Sep 17 00:00:00 2001 From: dzaima Date: Thu, 16 Dec 2021 12:20:42 +0200 Subject: [PATCH] fix / with negative numbers in indexes --- makefile | 2 +- src/builtins/sfns.c | 10 ++++------ src/core/numarr.h | 27 +++++++++++++++++++-------- src/main.c | 9 ++++++--- src/opt/mm_malloc.c | 1 + src/utils/file.c | 2 +- 6 files changed, 32 insertions(+), 19 deletions(-) diff --git a/makefile b/makefile index f10000e4..8a802ed2 100644 --- a/makefile +++ b/makefile @@ -93,7 +93,7 @@ endif gen: builtins core base jit utils # build the final binary - @$(CC) ${PIE} -o BQN ${bd}/*.o -lm + @$(CC) ${lf} ${PIE} -o BQN ${bd}/*.o -lm @echo ${postmsg} # build individual object files diff --git a/src/builtins/sfns.c b/src/builtins/sfns.c index 27df0871..8aff9d83 100644 --- a/src/builtins/sfns.c +++ b/src/builtins/sfns.c @@ -437,8 +437,8 @@ B select_c2(B t, B w, B x) { extern B rt_slash; B slash_c1(B t, B x) { if (RARE(isAtm(x)) || RARE(rnk(x)!=1)) thrF("/: Argument must have rank 1 (%H ≡ ≢𝕩)", x); - i64 s = isum(x); - if(s<0) thrM("/: Argument must consist of natural numbers"); + u64 s = usum(x); + if (s>=USZ_MAX) thrOOM(); usz xia = a(x)->ia; if (RARE(xia>=I32_MAX)) { usz xia = a(x)->ia; @@ -469,7 +469,6 @@ B slash_c1(B t, B x) { *rp = i; rp+= c; } else { - if (RARE(c)<0) thrF("/: Argument must consist of natural numbers (contained %i)", c); for (i32 j = 0; j < c; j++) *rp++ = i; } } @@ -482,7 +481,6 @@ B slash_c1(B t, B x) { *rp = i; rp+= c; } else { - if (RARE(c)<0) thrF("/: Argument must consist of natural numbers (contained %i)", c); for (i32 j = 0; j < c; j++) *rp++ = i; } } @@ -615,8 +613,8 @@ B slash_c2(B t, B w, B x) { #undef TYPED #undef CASE SLOW2("𝕨/𝕩", w, x); - i64 ria = isum(w); - if (ria>USZ_MAX) thrOOM(); + u64 ria = usum(w); + if (ria>=USZ_MAX) thrOOM(); HArr_p r = m_harrs(ria, &ri); SGetU(w) SGetU(x) diff --git a/src/core/numarr.h b/src/core/numarr.h index 0b9a7268..ba709bf1 100644 --- a/src/core/numarr.h +++ b/src/core/numarr.h @@ -104,22 +104,33 @@ static i64 bit_sum(u64* x, u64 am) { return r; } -static i64 isum(B x) { // doesn't consume; may error +static u64 usum(B x) { // doesn't consume; may error assert(isArr(x)); - i64 r = 0; + u64 r = 0; usz xia = a(x)->ia; u8 xe = TI(x,elType); if (xe==el_bit) return bit_sum(bitarr_ptr(x), xia); - else if (xe==el_i8 ) { i8* p = i8any_ptr (x); for (usz i = 0; i < xia; i++) r+= p[i]; } - else if (xe==el_i16) { i16* p = i16any_ptr(x); for (usz i = 0; i < xia; i++) if (addOn(r,p[i])) goto err; } - else if (xe==el_i32) { i32* p = i32any_ptr(x); for (usz i = 0; i < xia; i++) if (addOn(r,p[i])) goto err; } + else if (xe==el_i8 ) { i8* p = i8any_ptr (x); for (usz i = 0; i < xia; i++) { if (RARE(p[i]<0)) goto neg; r+= p[i]; } } + else if (xe==el_i16) { i16* p = i16any_ptr(x); for (usz i = 0; i < xia; i++) { if (RARE(p[i]<0)) goto neg; if (addOn(r,p[i])) goto overflow; } } + else if (xe==el_i32) { i32* p = i32any_ptr(x); for (usz i = 0; i < xia; i++) { if (RARE(p[i]<0)) goto neg; if (addOn(r,p[i])) goto overflow; } } else if (xe==el_f64) { f64* p = f64any_ptr(x); - for (usz i = 0; i < xia; i++) { if(p[i]!=(i64)p[i] || addOn(r,(i64)p[i])) goto err; } + for (usz i = 0; i < xia; i++) { + f64 c = p[i]; + u64 ci = (u64)c; + if (c!=ci) thrM("Expected integer"); + if (ci<0) goto neg; + if (addOn(r,ci)) goto overflow; + } } else { SGetU(x) - for (usz i = 0; i < xia; i++) r+= o2i64(GetU(x,i)); + for (usz i = 0; i < xia; i++) { + u64 c = o2u64(GetU(x,i)); + if (c<0) thrM("Didn't expect negative integer"); + if (addOn(r,c)) goto overflow; + } } return r; - err: thrM("Expected integer"); + overflow: thrM("Sum too big"); + neg: thrM("Didn't expect negative integer"); } diff --git a/src/main.c b/src/main.c index 184f2322..6daf7bc4 100644 --- a/src/main.c +++ b/src/main.c @@ -203,9 +203,12 @@ int main(int argc, char* argv[]) { heap_printInfo(sizes, types); goto cont; } else if (isCmd(cmdS, &cmdE, "gc ")) { - if (gc_depth!=0) printf("Cannot GC currently\n"); - else if (ENABLE_GC) gc_forceGC(); - else printf("Macro ENABLE_GC was false at compile-time, cannot GC\n"); + #if ENABLE_GC + if (gc_depth!=0) printf("Cannot GC currently\n"); + else gc_forceGC(); + #else + printf("Macro ENABLE_GC was false at compile-time, cannot GC\n"); + #endif goto cont; } else { printf("Unknown REPL command\n"); diff --git a/src/opt/mm_malloc.c b/src/opt/mm_malloc.c index e633dd7f..ff141425 100644 --- a/src/opt/mm_malloc.c +++ b/src/opt/mm_malloc.c @@ -9,3 +9,4 @@ void gc_maybeGC() { } void gc_forceGC() { } void gc_visitRoots() { } void mm_forHeap(V2v f) { } +u64 mm_heapUsed() { return 123; } // idk diff --git a/src/utils/file.c b/src/utils/file.c index b6cc0df3..895932d2 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -79,7 +79,7 @@ B file_lines(B path) { // consumes usz spos = pos; while(pos