Commit a7380f41

a7380f4183b2Merged

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

Description

flat_map, Complex op-assign, poly arithmetic typing for optcarrot

optcarrot now lowers fully to C; these close inference/codegen gaps the C compile then exposed. Gate green (test 845, bench 57):

  • flat_map / collect_concat: inference returns the block's array type (flatten one level), and a codegen emitter maps each element through the block and concatenates the returned arrays, with single- and destructured-param binding.
  • Complex +/* op-assign on a local (iq += Complex.polar(...)) via sp_complex_add/mul -- needed by the NTSC palette generator.
  • A poly operand makes +,-,*,/ poly: codegen already lowers these to sp_poly_ (boxed result), so inference must agree rather than leaving the result unknown, which silently stranded a poly value in a float/int slot.
  • Coerce a poly RHS feeding an int/float/bool slot in local and ivar op-assigns and plain local assignments (sp_poly_to_i / sp_poly_to_f).

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

Details

Commit
a7380f4183b2
Parent
01243a919961

src/analyze_infer.cmodifiedView file

@@ -1121,6 +1121,15 @@
11211121 const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
11221122 return ty_array_of(bn > 0 ? infer_type(c, bb[bn - 1]) : TY_UNKNOWN);
11231123 }
1124+ if (!strcmp(name, "flat_map") || !strcmp(name, "collect_concat")) {
1125+ int body = nt_ref(nt, block, "body");
1126+ int bn = 0;
1127+ const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
1128+ TyKind bret = bn > 0 ? infer_type(c, bb[bn - 1]) : TY_UNKNOWN;
1129+ /* block returns an array -> flatten one level keeps its element type;
1130+ a scalar block return behaves like map (each wrapped element). */
1131+ return ty_is_array(bret) ? bret : ty_array_of(bret);
1132+ }
11241133 if (!strcmp(name, "select") || !strcmp(name, "reject") ||
11251134 !strcmp(name, "filter") || !strcmp(name, "sort_by") ||
11261135 !strcmp(name, "take_while") || !strcmp(name, "drop_while"))
@@ -1774,6 +1783,11 @@
17741783 if (rt == TY_BIGINT || a0 == TY_BIGINT) return TY_BIGINT;
17751784 return TY_INT;
17761785 }
1786+ /* a poly operand makes the +,-,*,/ result poly: codegen lowers these to
1787+ sp_poly_<op>, which returns a (boxed) poly, so the static type must agree. */
1788+ if ((rt == TY_POLY || a0 == TY_POLY) &&
1789+ (!strcmp(name, "+") || !strcmp(name, "-") || !strcmp(name, "*") || !strcmp(name, "/")))
1790+ return TY_POLY;
17771791 return TY_UNKNOWN;
17781792 }
17791793 if (recv >= 0 && argc == 1 && !strcmp(name, "<=>")) return TY_INT;