Commit 29bceff3
29bceff39538Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Add M16: more array methods (min/max/first/last/include?/index/sort/uniq)
Codegen for array-receiver methods whose return types were already inferred: first (get 0), last (get length-1, array evaluated once into a temp), min/max (int/float arrays), include?/index (int/str arrays, name mapped off the trailing ?), sort/uniq (int arrays).
make test: see count above.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 29bceff39538
- Parent
- 7738084fa9c8
src/codegen.cmodifiedView file
@@ -647,6 +647,33 @@
647647
buf_printf(b, "sp_%sArray_inspect(", k); emit_expr(c, recv, b); buf_puts(b, ")");648648
return;649649
}650+
if (!strcmp(name, "first") && argc == 0) {651+
buf_printf(b, "sp_%sArray_get(", k); emit_expr(c, recv, b); buf_puts(b, ", 0)");652+
return;653+
}654+
if ((!strcmp(name, "min") || !strcmp(name, "max")) && argc == 0 && rt != TY_STR_ARRAY) {655+
buf_printf(b, "sp_%sArray_%s(", k, name); emit_expr(c, recv, b); buf_puts(b, ")");656+
return;657+
}658+
if ((!strcmp(name, "include?") || !strcmp(name, "index")) && argc == 1 && rt != TY_FLOAT_ARRAY) {659+
const char *fn = !strcmp(name, "include?") ? "include" : "index";660+
buf_printf(b, "sp_%sArray_%s(", k, fn);661+
emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b); buf_puts(b, ")");662+
return;663+
}664+
if ((!strcmp(name, "sort") || !strcmp(name, "uniq")) && argc == 0 && rt == TY_INT_ARRAY) {665+
buf_printf(b, "sp_IntArray_%s(", name); emit_expr(c, recv, b); buf_puts(b, ")");666+
return;667+
}668+
if (!strcmp(name, "last") && argc == 0) {669+
int t = ++g_tmp;670+
Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);671+
emit_indent(g_pre, g_indent);672+
buf_printf(g_pre, "%s _t%d = ", c_type_name(rt), t);673+
buf_puts(g_pre, rb.p ? rb.p : ""); buf_puts(g_pre, ";\n"); free(rb.p);674+
buf_printf(b, "sp_%sArray_get(_t%d, sp_%sArray_length(_t%d) - 1)", k, t, k, t);675+
return;676+
}650677
}651678
}652679