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]returneds[1..3] - IntArray / StrArray / FloatArray slicing (introduced in the prior three commits): same off-by-one
for i in 1...3: iterated through 3case 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
c_default_val(t) end # PM_RANGE_FLAGS_EXCLUDE_END = 4: bit 2 set means `...` (exclusive). def range_excl_end(rid) if (@nd_flags[rid] & 4) != 0 return 1 end return 0 end def sanitize_name(name) if name == "<=>" return "_cmp" a = get_args(args_id) if a.length >= 1 if @nd_type[a[0]] == "RangeNode" # s[1..3] # s[1..3] inclusive, s[1...3] exclusive left = compile_expr(@nd_left[a[0]]) right = compile_expr(@nd_right[a[0]]) return fn + "(" + lprefix + ", " + left + ", " + right + " - " + left + " + 1)" adj = range_excl_end(a[0]) == 1 ? "" : " + 1" return fn + "(" + lprefix + ", " + left + ", " + right + " - " + left + adj + ")" end if a.length >= 2 # s[0, 2] if mname == "to_a" @needs_int_array = 1 @needs_gc = 1 # Honour `...` exclusive Range when the receiver is a literal # RangeNode (or wrapped in parens). For non-literal Range values # held in sp_Range structs, exclude_end is not tracked at runtime # and the inclusive form is used. recv = @nd_receiver[nid] range_nid = -1 if recv >= 0 && @nd_type[recv] == "RangeNode" range_nid = recv end if recv >= 0 && @nd_type[recv] == "ParenthesesNode" pb = @nd_body[recv] if pb >= 0 ps = get_stmts(pb) if ps.length > 0 && @nd_type[ps.first] == "RangeNode" range_nid = ps.first end end end if range_nid >= 0 rright = compile_expr(@nd_right[range_nid]) if range_excl_end(range_nid) == 1 rright = "(" + rright + ") - 1" end return "sp_IntArray_from_range(" + compile_expr(@nd_left[range_nid]) + ", " + rright + ")" end return "sp_IntArray_from_range(" + rc + ".first, " + rc + ".last)" end if mname == "length" if a.length >= 1 && @nd_type[a[0]] == "RangeNode" left = compile_expr(@nd_left[a[0]]) right = compile_expr(@nd_right[a[0]]) return "sp_IntArray_slice(" + rc + ", " + left + ", " + right + " - " + left + " + 1)" adj = range_excl_end(a[0]) == 1 ? "" : " + 1" return "sp_IntArray_slice(" + rc + ", " + left + ", " + right + " - " + left + adj + ")" end if a.length >= 2 return "sp_IntArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")" if a.length >= 1 && @nd_type[a[0]] == "RangeNode" left = compile_expr(@nd_left[a[0]]) right = compile_expr(@nd_right[a[0]]) return "sp_FloatArray_slice(" + rc + ", " + left + ", " + right + " - " + left + " + 1)" adj = range_excl_end(a[0]) == 1 ? "" : " + 1" return "sp_FloatArray_slice(" + rc + ", " + left + ", " + right + " - " + left + adj + ")" end if a.length >= 2 return "sp_FloatArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")" if a.length >= 1 && @nd_type[a[0]] == "RangeNode" left = compile_expr(@nd_left[a[0]]) right = compile_expr(@nd_right[a[0]]) return "sp_StrArray_slice(" + rc + ", " + left + ", " + right + " - " + left + " + 1)" adj = range_excl_end(a[0]) == 1 ? "" : " + 1" return "sp_StrArray_slice(" + rc + ", " + left + ", " + right + " - " + left + adj + ")" end if a.length >= 2 return "sp_StrArray_slice(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")" if range_nid >= 0 @needs_int_array = 1 @needs_gc = 1 return "sp_IntArray_from_range(" + compile_expr(@nd_left[range_nid]) + ", " + compile_expr(@nd_right[range_nid]) + ")" right_expr = compile_expr(@nd_right[range_nid]) # sp_IntArray_from_range is inclusive; for `1...3` shave the upper end. if range_excl_end(range_nid) == 1 right_expr = "(" + right_expr + ") - 1" end return "sp_IntArray_from_range(" + compile_expr(@nd_left[range_nid]) + ", " + right_expr + ")" end "" end if @nd_type[coll] == "RangeNode" left = compile_expr(@nd_left[coll]) right = compile_expr(@nd_right[coll]) emit(" for (lv_" + vname + " = " + left + "; lv_" + vname + " <= " + right + "; lv_" + vname + "++) {") cmp = range_excl_end(coll) == 1 ? "<" : "<=" emit(" for (lv_" + vname + " = " + left + "; lv_" + vname + " " + cmp + " " + right + "; lv_" + vname + "++) {") @indent = @indent + 1 compile_stmts_body(@nd_body[nid]) @indent = @indent - 1 if @nd_type[cid] == "RangeNode" left = compile_expr(@nd_left[cid]) right = compile_expr(@nd_right[cid]) result = result + "(" + tmp + " >= " + left + " && " + tmp + " <= " + right + ")" cmp = range_excl_end(cid) == 1 ? "<" : "<=" result = result + "(" + tmp + " >= " + left + " && " + tmp + " " + cmp + " " + right + ")" else if pred_type == "string" result = result + "strcmp(" + tmp + ", " + compile_expr(cid) + ") == 0"spinel_parse.cmodifiedView file
Loading diff…
test/bm_range_exclusive.rbaddedView file
Loading diff…