Commit 5e9fea38
5e9fea3869bbMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
String ranges and Range#===
Range#=== now dispatches as cover? for integer ranges. Literal string ranges ("a".."z"), which can't fit the int-only sp_Range struct, get an inlined path: include?/cover?/member?/=== compare via strcmp (a non-string argument is statically false), and to_a iterates the single-char ASCII span. RangeNode inference now also types its bounds so codegen can tell an int range from a string range.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 5e9fea3869bb
- Parent
- f8386c73d210
src/analyze.cmodifiedView file
@@ -534,6 +534,20 @@
534534
535535
/* range receiver methods */536536
if (recv >= 0 && rt == TY_RANGE) {537+
/* a literal string range ("a".."z") yields strings, not ints */538+
if (!strcmp(name, "to_a")) {539+
int rn = recv;540+
while (rn >= 0 && nt_type(nt, rn) && !strcmp(nt_type(nt, rn), "ParenthesesNode")) {541+
int body = nt_ref(nt, rn, "body"); int bn = 0;542+
const int *bd = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;543+
rn = bn == 1 ? bd[0] : -1;544+
}545+
if (rn >= 0 && nt_type(nt, rn) && !strcmp(nt_type(nt, rn), "RangeNode")) {546+
int lo = nt_ref(nt, rn, "left"), hi = nt_ref(nt, rn, "right");547+
if (lo >= 0 && hi >= 0 && infer_type(c, lo) == TY_STRING && infer_type(c, hi) == TY_STRING)548+
return TY_STR_ARRAY;549+
}550+
}537551
if (!strcmp(name, "to_a") || !strcmp(name, "minmax")) return TY_INT_ARRAY;538552
if (!strcmp(name, "include?") || !strcmp(name, "member?") ||539553
!strcmp(name, "cover?") || !strcmp(name, "exclude_end?") ||@@ -705,7 +719,13 @@
705719
if (!strcmp(ty, "TrueNode")) return TY_BOOL;706720
if (!strcmp(ty, "FalseNode")) return TY_BOOL;707721
if (!strcmp(ty, "NilNode")) return TY_NIL;708-
if (!strcmp(ty, "RangeNode")) return TY_RANGE;722+
if (!strcmp(ty, "RangeNode")) {723+
/* infer the bounds so codegen can tell an int range from a string range */724+
int lo = nt_ref(nt, id, "left"), hi = nt_ref(nt, id, "right");725+
if (lo >= 0) infer_type(c, lo);726+
if (hi >= 0) infer_type(c, hi);727+
return TY_RANGE;728+
}709729
if (!strcmp(ty, "LambdaNode")) return TY_PROC;710730
/* an assignment expression evaluates to the assigned value */711731
if (!strcmp(ty, "LocalVariableWriteNode")) return infer_type(c, nt_ref(nt, id, "value"));src/codegen.cmodifiedView file
Loading diff…