Commit d1524b18

d1524b18bfbeMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Integer#step with a block, and case/when range conditions

Integer#step(limit[, step]) { [|i|] ... } now emits a stepping loop (direction picked from the step's sign), including the paramless block form. A when lo..hi clause now tests range membership via sp_range_include instead of comparing the subject to the range value, in both the statement and expression case forms (the expression form also gains the regex when-clause arm).

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

Details

Commit
d1524b18bfbe
Parent
7ff5723b542b

src/codegen.cmodifiedView file

@@ -2830,6 +2830,27 @@
28302830 return 1;
28312831 }
28322832
2833+ /* int.step(limit[, step]) { [|i|] ... } -- integer stepping loop */
2834+ if (!strcmp(name, "step") && rt == TY_INT) {
2835+ int args = nt_ref(nt, id, "arguments");
2836+ int sargc = 0;
2837+ const int *sargv = args >= 0 ? nt_arr(nt, args, "arguments", &sargc) : NULL;
2838+ if (sargc < 1) return 0;
2839+ int t = ++g_tmp, tl = ++g_tmp, ts = ++g_tmp;
2840+ emit_indent(b, indent); buf_printf(b, "mrb_int _t%d = ", tl); emit_expr(c, sargv[0], b); buf_puts(b, ";\n");
2841+ emit_indent(b, indent); buf_printf(b, "mrb_int _t%d = ", ts);
2842+ if (sargc >= 2) emit_expr(c, sargv[1], b); else buf_puts(b, "1");
2843+ buf_puts(b, ";\n");
2844+ emit_indent(b, indent);
2845+ buf_printf(b, "for (mrb_int _t%d = ", t); emit_expr(c, recv, b);
2846+ buf_printf(b, "; _t%d >= 0 ? _t%d <= _t%d : _t%d >= _t%d; _t%d += _t%d) {\n",
2847+ ts, t, tl, t, tl, t, ts);
2848+ if (p0) { emit_indent(b, indent + 1); buf_printf(b, "lv_%s = _t%d;\n", p0, t); }
2849+ emit_stmts(c, body, b, indent + 1);
2850+ emit_indent(b, indent); buf_puts(b, "}\n");
2851+ return 1;
2852+ }
2853+
28332854 /* hash.each / each_pair { |k, v| ... } */
28342855 if ((!strcmp(name, "each") || !strcmp(name, "each_pair")) && ty_is_hash(rt)) {
28352856 const char *hn = ty_hash_cname(rt);
@@ -3656,6 +3677,12 @@
36563677 if (reidx >= 0 && pt == TY_STRING) {
36573678 buf_printf(b, "sp_re_match_p(sp_re_pat_%d, _t%d)", reidx, t);
36583679 }
3680+ else if (comp_ntype(c, conds[j]) == TY_RANGE && pt != TY_STRING) {
3681+ /* `when lo..hi` is range membership, not equality */
3682+ int tr = ++g_tmp;
3683+ buf_printf(b, "({ sp_Range _t%d = ", tr); emit_expr(c, conds[j], b);
3684+ buf_printf(b, "; sp_range_include(&_t%d, _t%d); })", tr, t);
3685+ }
36593686 else if (pt == TY_STRING) {
36603687 buf_printf(b, "sp_str_eq(_t%d, ", t); emit_expr(c, conds[j], b); buf_puts(b, ")");
36613688 }
@@ -3723,7 +3750,14 @@
37233750 for (int j = 0; j < wc; j++) {
37243751 if (j) buf_puts(b, " || ");
37253752 if (pred >= 0) {
3726- if (pt == TY_STRING) { buf_printf(b, "sp_str_eq(_t%d, ", t); emit_expr(c, conds[j], b); buf_puts(b, ")"); }
3753+ int reidx = re_lit_index(c, conds[j]);
3754+ if (reidx >= 0 && pt == TY_STRING) { buf_printf(b, "sp_re_match_p(sp_re_pat_%d, _t%d)", reidx, t); }
3755+ else if (comp_ntype(c, conds[j]) == TY_RANGE && pt != TY_STRING) {
3756+ int tr = ++g_tmp;
3757+ buf_printf(b, "({ sp_Range _t%d = ", tr); emit_expr(c, conds[j], b);
3758+ buf_printf(b, "; sp_range_include(&_t%d, _t%d); })", tr, t);
3759+ }
3760+ else if (pt == TY_STRING) { buf_printf(b, "sp_str_eq(_t%d, ", t); emit_expr(c, conds[j], b); buf_puts(b, ")"); }
37273761 else { buf_printf(b, "(_t%d == ", t); emit_expr(c, conds[j], b); buf_puts(b, ")"); }
37283762 }
37293763 else { buf_puts(b, "("); emit_expr(c, conds[j], b); buf_puts(b, ")"); }