From ee2b02d44dae246e7e405852a4e2e726e7c8b08b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 21 Feb 2025 15:51:22 -0500 Subject: [PATCH] Integer range iteration over fixed width integers should iterate over that type --- environment.c | 16 ++++++++-------- stdlib/integers.c | 36 ++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/environment.c b/environment.c index 44e74ed..350743c 100644 --- a/environment.c +++ b/environment.c @@ -149,8 +149,8 @@ env_t *new_compilation_unit(CORD libname) {"modulo", "Int64$modulo", "func(x,y:Int64 -> Int64)"}, {"modulo1", "Int64$modulo1", "func(x,y:Int64 -> Int64)"}, {"octal", "Int64$octal", "func(i:Int64, digits=0, prefix=yes -> Text)"}, - {"onward", "Int64$onward", "func(first:Int64,step=Int64(1) -> func(->Int?))"}, - {"to", "Int64$to", "func(first:Int64,last:Int64,step=none:Int64 -> func(->Int?))"}, + {"onward", "Int64$onward", "func(first:Int64,step=Int64(1) -> func(->Int64?))"}, + {"to", "Int64$to", "func(first:Int64,last:Int64,step=none:Int64 -> func(->Int64?))"}, {"unsigned_left_shifted", "Int64$unsigned_left_shifted", "func(x:Int64,y:Int64 -> Int64)"}, {"unsigned_right_shifted", "Int64$unsigned_right_shifted", "func(x:Int64,y:Int64 -> Int64)"}, {"wrapping_minus", "Int64$wrapping_minus", "func(x:Int64,y:Int64 -> Int64)"}, @@ -170,8 +170,8 @@ env_t *new_compilation_unit(CORD libname) {"modulo", "Int32$modulo", "func(x,y:Int32 -> Int32)"}, {"modulo1", "Int32$modulo1", "func(x,y:Int32 -> Int32)"}, {"octal", "Int32$octal", "func(i:Int32, digits=0, prefix=yes -> Text)"}, - {"onward", "Int32$onward", "func(first:Int32,step=Int32(1) -> func(->Int?))"}, - {"to", "Int32$to", "func(first:Int32,last:Int32,step=none:Int32 -> func(->Int?))"}, + {"onward", "Int32$onward", "func(first:Int32,step=Int32(1) -> func(->Int32?))"}, + {"to", "Int32$to", "func(first:Int32,last:Int32,step=none:Int32 -> func(->Int32?))"}, {"unsigned_left_shifted", "Int32$unsigned_left_shifted", "func(x:Int32,y:Int32 -> Int32)"}, {"unsigned_right_shifted", "Int32$unsigned_right_shifted", "func(x:Int32,y:Int32 -> Int32)"}, {"wrapping_minus", "Int32$wrapping_minus", "func(x:Int32,y:Int32 -> Int32)"}, @@ -191,8 +191,8 @@ env_t *new_compilation_unit(CORD libname) {"modulo", "Int16$modulo", "func(x,y:Int16 -> Int16)"}, {"modulo1", "Int16$modulo1", "func(x,y:Int16 -> Int16)"}, {"octal", "Int16$octal", "func(i:Int16, digits=0, prefix=yes -> Text)"}, - {"onward", "Int16$onward", "func(first:Int16,step=Int16(1) -> func(->Int?))"}, - {"to", "Int16$to", "func(first:Int16,last:Int16,step=none:Int16 -> func(->Int?))"}, + {"onward", "Int16$onward", "func(first:Int16,step=Int16(1) -> func(->Int16?))"}, + {"to", "Int16$to", "func(first:Int16,last:Int16,step=none:Int16 -> func(->Int16?))"}, {"unsigned_left_shifted", "Int16$unsigned_left_shifted", "func(x:Int16,y:Int16 -> Int16)"}, {"unsigned_right_shifted", "Int16$unsigned_right_shifted", "func(x:Int16,y:Int16 -> Int16)"}, {"wrapping_minus", "Int16$wrapping_minus", "func(x:Int16,y:Int16 -> Int16)"}, @@ -212,8 +212,8 @@ env_t *new_compilation_unit(CORD libname) {"modulo", "Int8$modulo", "func(x,y:Int8 -> Int8)"}, {"modulo1", "Int8$modulo1", "func(x,y:Int8 -> Int8)"}, {"octal", "Int8$octal", "func(i:Int8, digits=0, prefix=yes -> Text)"}, - {"onward", "Int8$onward", "func(first:Int8,step=Int8(1) -> func(->Int?))"}, - {"to", "Int8$to", "func(first:Int8,last:Int8,step=none:Int8 -> func(->Int?))"}, + {"onward", "Int8$onward", "func(first:Int8,step=Int8(1) -> func(->Int8?))"}, + {"to", "Int8$to", "func(first:Int8,last:Int8,step=none:Int8 -> func(->Int8?))"}, {"unsigned_left_shifted", "Int8$unsigned_left_shifted", "func(x:Int8,y:Int8 -> Int8)"}, {"unsigned_right_shifted", "Int8$unsigned_right_shifted", "func(x:Int8,y:Int8 -> Int8)"}, {"wrapping_minus", "Int8$wrapping_minus", "func(x:Int8,y:Int8 -> Int8)"}, diff --git a/stdlib/integers.c b/stdlib/integers.c index 835da97..c60f237 100644 --- a/stdlib/integers.c +++ b/stdlib/integers.c @@ -576,19 +576,35 @@ public void Int32$deserialize(FILE *in, void *outval, Array_t*, const TypeInfo_t } \ return bit_array; \ } \ + typedef struct { \ + Optional##KindOfInt##_t current, last; \ + KindOfInt##_t step; \ + } KindOfInt##Range_t; \ + static Optional##KindOfInt##_t _next_##KindOfInt(KindOfInt##Range_t *info) \ + { \ + Optional##KindOfInt##_t i = info->current; \ + if (!i.is_none) { \ + KindOfInt##_t next; bool overflow = __builtin_add_overflow(i.i, info->step, &next); \ + if (overflow || (!info->last.is_none && (info->step >= 0 ? next > info->last.i : next < info->last.i))) \ + info->current = (Optional##KindOfInt##_t){.is_none=true}; \ + else \ + info->current = (Optional##KindOfInt##_t){.i=next}; \ + } \ + return i; \ + } \ public to_attr Closure_t KindOfInt ## $to(c_type first, c_type last, Optional ## KindOfInt ## _t step) { \ - IntRange_t *range = GC_MALLOC(sizeof(IntRange_t)); \ - range->current = KindOfInt##_to_Int(first); \ - range->last = KindOfInt##_to_Int(last); \ - range->step = step.is_none ? (last >= first ? I_small(1) : I_small(-1)) : KindOfInt##_to_Int(step.i); \ - return (Closure_t){.fn=_next_int, .userdata=range}; \ + KindOfInt##Range_t *range = GC_MALLOC(sizeof(KindOfInt##Range_t)); \ + range->current = (Optional##KindOfInt##_t){.i=first}; \ + range->last = (Optional##KindOfInt##_t){.i=last}; \ + range->step = step.is_none ? (last >= first ? 1 : -1) : step.i; \ + return (Closure_t){.fn=_next_##KindOfInt, .userdata=range}; \ } \ public to_attr Closure_t KindOfInt ## $onward(c_type first, c_type step) { \ - IntRange_t *range = GC_MALLOC(sizeof(IntRange_t)); \ - range->current = KindOfInt##_to_Int(first); \ - range->last = NONE_INT; \ - range->step = KindOfInt##_to_Int(step); \ - return (Closure_t){.fn=_next_int, .userdata=range}; \ + KindOfInt##Range_t *range = GC_MALLOC(sizeof(KindOfInt##Range_t)); \ + range->current = (Optional##KindOfInt##_t){.i=first}; \ + range->last = (Optional##KindOfInt##_t){.is_none=true}; \ + range->step = step; \ + return (Closure_t){.fn=_next_##KindOfInt, .userdata=range}; \ } \ public PUREFUNC Optional ## KindOfInt ## _t KindOfInt ## $parse(Text_t text) { \ OptionalInt_t full_int = Int$parse(text); \