Commit cc9752a1

cc9752a1353bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 3 files · +1 added · ~2 modified

Description

fix(codegen): distinguish .. and ... (exclude_end) Range across slicing, iteration, case/when, and Range#to_a

Closes #37.

The Prism PM_RANGE_FLAGS_EXCLUDE_END bit was dropped during AST serialization, so 1...3 was indistinguishable from 1..3 in spinel_codegen. That bug was latent in:

  • string slicing: s[1...3] returned s[1..3]
  • IntArray / StrArray / FloatArray slicing (introduced in the prior three commits): same off-by-one
  • for i in 1...3: iterated through 3
  • case n; when 0...10: matched 10
  • (1...3).to_a: produced [1, 2, 3]

spinel_parse.c now emits the prism flags field for RangeNode. spinel_codegen.rb adds range_excl_end(rid) which tests bit 2 (PM_RANGE_FLAGS_EXCLUDE_END = 4), and the seven Range-consuming sites consult it to pick <= vs <, + 1 vs nothing, or shave the upper bound for from_range.

The runtime sp_Range struct still doesn't carry exclude_end, so non-literal Range values held in locals lose the distinction at runtime. That's a follow-up if it ever bites; the literal-Range cases above cover everything I could find in the test corpus.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
cc9752a1353b
Parent
63849dd18466

spinel_codegen.rbmodifiedView file

@@ -3279,6 +3279,14 @@
32793279 c_default_val(t)
32803280 end
32813281
3282+ # PM_RANGE_FLAGS_EXCLUDE_END = 4: bit 2 set means `...` (exclusive).
3283+ def range_excl_end(rid)
3284+ if (@nd_flags[rid] & 4) != 0
3285+ return 1
3286+ end
3287+ return 0
3288+ end
3289+
32823290 def sanitize_name(name)
32833291 if name == "<=>"
32843292 return "_cmp"
@@ -13764,10 +13772,11 @@
1376413772 a = get_args(args_id)
1376513773 if a.length >= 1
1376613774 if @nd_type[a[0]] == "RangeNode"
13767- # s[1..3]
13775+ # s[1..3] inclusive, s[1...3] exclusive
1376813776 left = compile_expr(@nd_left[a[0]])
1376913777 right = compile_expr(@nd_right[a[0]])
13770- return fn + "(" + lprefix + ", " + left + ", " + right + " - " + left + " + 1)"
13778+ adj = range_excl_end(a[0]) == 1 ? "" : " + 1"
13779+ return fn + "(" + lprefix + ", " + left + ", " + right + " - " + left + adj + ")"
1377113780 end
1377213781 if a.length >= 2
1377313782 # s[0, 2]
@@ -13911,6 +13920,31 @@
1391113920 if mname == "to_a"
1391213921 @needs_int_array = 1
1391313922 @needs_gc = 1
13923+ # Honour `...` exclusive Range when the receiver is a literal
13924+ # RangeNode (or wrapped in parens). For non-literal Range values
13925+ # held in sp_Range structs, exclude_end is not tracked at runtime
13926+ # and the inclusive form is used.
13927+ recv = @nd_receiver[nid]
13928+ range_nid = -1
13929+ if recv >= 0 && @nd_type[recv] == "RangeNode"
13930+ range_nid = recv
13931+ end
13932+ if recv >= 0 && @nd_type[recv] == "ParenthesesNode"
13933+ pb = @nd_body[recv]
13934+ if pb >= 0
13935+ ps = get_stmts(pb)
13936+ if ps.length > 0 && @nd_type[ps.first] == "RangeNode"
13937+ range_nid = ps.first
13938+ end
13939+ end
13940+ end
13941+ if range_nid >= 0
13942+ rright = compile_expr(@nd_right[range_nid])
13943+ if range_excl_end(range_nid) == 1
13944+ rright = "(" + rright + ") - 1"
13945+ end
13946+ return "sp_IntArray_from_range(" + compile_expr(@nd_left[range_nid]) + ", " + rright + ")"
13947+ end
1391413948 return "sp_IntArray_from_range(" + rc + ".first, " + rc + ".last)"
1391513949 end
1391613950 if mname == "length"
@@ -14441,7 +14475,8 @@
1444114475 if a.length >= 1 && @nd_type[a[0]] == "RangeNode"
1444214476 left = compile_expr(@nd_left[a[0]])
1444314477 right = compile_expr(@nd_right[a[0]])
14444- return "sp_IntArray_slice(" + rc + ", " + left + ", " + right + " - " + left + " + 1)"
14478+ adj = range_excl_end(a[0]) == 1 ? "" : " + 1"
14479+ return "sp_IntArray_slice(" + rc + ", " + left + ", " + right + " - " + left + adj + ")"
1444514480 end
1444614481 if a.length >= 2
1444714482 return "sp_IntArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
@@ -14634,7 +14669,8 @@
1463414669 if a.length >= 1 && @nd_type[a[0]] == "RangeNode"
1463514670 left = compile_expr(@nd_left[a[0]])
1463614671 right = compile_expr(@nd_right[a[0]])
14637- return "sp_FloatArray_slice(" + rc + ", " + left + ", " + right + " - " + left + " + 1)"
14672+ adj = range_excl_end(a[0]) == 1 ? "" : " + 1"
14673+ return "sp_FloatArray_slice(" + rc + ", " + left + ", " + right + " - " + left + adj + ")"
1463814674 end
1463914675 if a.length >= 2
1464014676 return "sp_FloatArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
@@ -14710,7 +14746,8 @@
1471014746 if a.length >= 1 && @nd_type[a[0]] == "RangeNode"
1471114747 left = compile_expr(@nd_left[a[0]])
1471214748 right = compile_expr(@nd_right[a[0]])
14713- return "sp_StrArray_slice(" + rc + ", " + left + ", " + right + " - " + left + " + 1)"
14749+ adj = range_excl_end(a[0]) == 1 ? "" : " + 1"
14750+ return "sp_StrArray_slice(" + rc + ", " + left + ", " + right + " - " + left + adj + ")"
1471414751 end
1471514752 if a.length >= 2
1471614753 return "sp_StrArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
@@ -15360,7 +15397,12 @@
1536015397 if range_nid >= 0
1536115398 @needs_int_array = 1
1536215399 @needs_gc = 1
15363- return "sp_IntArray_from_range(" + compile_expr(@nd_left[range_nid]) + ", " + compile_expr(@nd_right[range_nid]) + ")"
15400+ right_expr = compile_expr(@nd_right[range_nid])
15401+ # sp_IntArray_from_range is inclusive; for `1...3` shave the upper end.
15402+ if range_excl_end(range_nid) == 1
15403+ right_expr = "(" + right_expr + ") - 1"
15404+ end
15405+ return "sp_IntArray_from_range(" + compile_expr(@nd_left[range_nid]) + ", " + right_expr + ")"
1536415406 end
1536515407 ""
1536615408 end
@@ -17106,7 +17148,8 @@
1710617148 if @nd_type[coll] == "RangeNode"
1710717149 left = compile_expr(@nd_left[coll])
1710817150 right = compile_expr(@nd_right[coll])
17109- emit(" for (lv_" + vname + " = " + left + "; lv_" + vname + " <= " + right + "; lv_" + vname + "++) {")
17151+ cmp = range_excl_end(coll) == 1 ? "<" : "<="
17152+ emit(" for (lv_" + vname + " = " + left + "; lv_" + vname + " " + cmp + " " + right + "; lv_" + vname + "++) {")
1711017153 @indent = @indent + 1
1711117154 compile_stmts_body(@nd_body[nid])
1711217155 @indent = @indent - 1
@@ -17214,7 +17257,8 @@
1721417257 if @nd_type[cid] == "RangeNode"
1721517258 left = compile_expr(@nd_left[cid])
1721617259 right = compile_expr(@nd_right[cid])
17217- result = result + "(" + tmp + " >= " + left + " && " + tmp + " <= " + right + ")"
17260+ cmp = range_excl_end(cid) == 1 ? "<" : "<="
17261+ result = result + "(" + tmp + " >= " + left + " && " + tmp + " " + cmp + " " + right + ")"
1721817262 else
1721917263 if pred_type == "string"
1722017264 result = result + "strcmp(" + tmp + ", " + compile_expr(cid) + ") == 0"