Commit 27b32882
27b32882a2deMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Support map/select/reject over poly arrays (and poly result arrays)
emit_collect_expr now accepts a poly_array receiver (elements iterated as poly) and a poly_array result (mapped values boxed). Enables mapping a mixed array to objects/value-types.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 27b32882a2de
- Parent
- 8be0a442c750
src/codegen.cmodifiedView file
@@ -293,7 +293,7 @@
293293
TyKind rt = comp_ntype(c, recv);294294
int range_recv = (rt == TY_RANGE);295295
if (!ty_is_array(rt) && !range_recv) return 0;296-
const char *k = range_recv ? "Int" : array_kind(rt);296+
const char *k = range_recv ? "Int" : (rt == TY_POLY_ARRAY ? "Poly" : array_kind(rt));297297
if (!k) return 0;298298
299299
int is_map = !strcmp(name, "map") || !strcmp(name, "collect");@@ -302,7 +302,8 @@
302302
if (!is_map && !is_sel && !is_rej) return 0;303303
304304
TyKind restype = comp_ntype(c, id);305-
const char *rk = array_kind(restype);305+
int res_poly = (restype == TY_POLY_ARRAY);306+
const char *rk = res_poly ? "Poly" : array_kind(restype);306307
if (!rk) return 0;307308
308309
const char *p0 = block_param_name(c, block, 0); if (p0) p0 = rename_local(p0);@@ -354,7 +355,13 @@
354355
if (is_map) {355356
emit_indent(g_pre, bodyIndent);356357
buf_printf(g_pre, "sp_%sArray_push(_t%d, ", rk, tres);357-
buf_puts(g_pre, vb.p ? vb.p : ""); buf_puts(g_pre, ");\n");358+
/* a poly result array stores boxed values */359+
if (res_poly && comp_ntype(c, bb[bn - 1]) != TY_POLY) {360+
Buf bx; memset(&bx, 0, sizeof bx); emit_boxed_text(c, comp_ntype(c, bb[bn - 1]), vb.p ? vb.p : "", &bx);361+
buf_puts(g_pre, bx.p ? bx.p : ""); free(bx.p);362+
}363+
else buf_puts(g_pre, vb.p ? vb.p : "");364+
buf_puts(g_pre, ");\n");358365
}359366
else {360367
emit_indent(g_pre, bodyIndent);