fix / with negative numbers in indexes

This commit is contained in:
dzaima 2021-12-16 12:20:42 +02:00
parent c68858ba90
commit 4af4b45ab7
6 changed files with 32 additions and 19 deletions

View File

@ -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

View File

@ -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)

View File

@ -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");
}

View File

@ -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");

View File

@ -9,3 +9,4 @@ void gc_maybeGC() { }
void gc_forceGC() { }
void gc_visitRoots() { }
void mm_forHeap(V2v f) { }
u64 mm_heapUsed() { return 123; } // idk

View File

@ -79,7 +79,7 @@ B file_lines(B path) { // consumes
usz spos = pos;
while(pos<ia && p[pos]!='\n' && p[pos]!='\r') pos++;
r.a[i++] = fromUTF8((char*)p+spos, pos-spos);
if (p[pos]=='\r' && pos+1<ia && p[pos+1]=='\n') pos+= 2;
if (pos<ia && p[pos]=='\r' && pos+1<ia && p[pos+1]=='\n') pos+= 2;
else pos++;
}
ptr_dec(tf);