Commit dd1b4bb3

dd1b4bb30e27Merged

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

Description

Add value-returning yield methods (statement-expression inline)

A yielding/block-aware method called in expression position inlines as a GCC statement-expression ({ ... }) whose value is the method body's last statement (its implicit return). emit_inline_call_x parameterizes the statement vs expression form; emit_inline_expr guards on a scalar return type and is tried first in emit_call.

Covers the common accumulate-and-return iterators (build an array / count via an internal each-with-block, return the result). make test: see count above.

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

Details

Commit
dd1b4bb30e27
Parent
71b7aab4f0f3

src/codegen.cmodifiedView file

@@ -170,6 +170,7 @@
170170 static int emit_output_call(Compiler *c, int id, Buf *b, int indent);
171171 static int emit_iteration_stmt(Compiler *c, int id, Buf *b, int indent);
172172 static int emit_inline_call(Compiler *c, int id, Buf *b, int indent);
173+static int emit_inline_expr(Compiler *c, int id, Buf *b);
173174 static int needs_root(TyKind t);
174175 static void emit_index_op_write(Compiler *c, int id, Buf *b, int indent);
175176 static void emit_super(Compiler *c, int id, Buf *b);
@@ -400,6 +401,7 @@
400401 static void emit_call(Compiler *c, int id, Buf *b) {
401402 const NodeTable *nt = c->nt;
402403 if (emit_collect_expr(c, id, b)) return;
404+ if (emit_inline_expr(c, id, b)) return; /* value-returning yield method */
403405 const char *name = nt_str(nt, id, "name");
404406 int recv = nt_ref(nt, id, "receiver");
405407 int args = nt_ref(nt, id, "arguments");
@@ -1049,7 +1051,7 @@
10491051 declare the method's locals (renamed to avoid clashing with the call
10501052 site), bind params to args, then emit the method body with yield
10511053 expanding to the block. Returns 1 if handled. */
1052-static int emit_inline_call(Compiler *c, int id, Buf *b, int indent) {
1054+static int emit_inline_call_x(Compiler *c, int id, Buf *b, int indent, int as_expr) {
10531055 const NodeTable *nt = c->nt;
10541056 const char *name = nt_str(nt, id, "name");
10551057 int recv = nt_ref(nt, id, "receiver");
@@ -1083,7 +1085,8 @@
10831085 static char selfbuf[64];
10841086 g_block_id = block;
10851087
1086- emit_indent(b, indent); buf_puts(b, "{\n");
1088+ if (as_expr) buf_puts(b, "({\n");
1089+ else { emit_indent(b, indent); buf_puts(b, "{\n"); }
10871090 /* instance method: bind self to the receiver */
10881091 if (recv >= 0) {
10891092 int st = ++g_tmp;
@@ -1135,7 +1138,8 @@
11351138 }
11361139
11371140 emit_stmts(c, m->body, b, din);
1138- emit_indent(b, indent); buf_puts(b, "}\n");
1141+ if (as_expr) { emit_indent(b, indent); buf_puts(b, "})"); }
1142+ else { emit_indent(b, indent); buf_puts(b, "}\n"); }
11391143
11401144 g_nren = saved_nren;
11411145 g_block_id = saved_block;
@@ -1143,6 +1147,19 @@
11431147 return 1;
11441148 }
11451149
1150+static int emit_inline_call(Compiler *c, int id, Buf *b, int indent) {
1151+ return emit_inline_call_x(c, id, b, indent, 0);
1152+}
1153+
1154+/* Inline a yielding method call in expression position: ({ ...; value; }).
1155+ The method must return a usable value (its body's last statement). */
1156+static int emit_inline_expr(Compiler *c, int id, Buf *b) {
1157+ /* only when a value is actually produced (scalar return) */
1158+ TyKind rt = comp_ntype(c, id);
1159+ if (!is_scalar_ret(rt)) return 0;
1160+ return emit_inline_call_x(c, id, b, g_indent + 1, 1);
1161+}
1162+
11461163 /* Block iteration lowered to an inline C for-loop. Handles n.times,
11471164 array.each, range.each, n.upto/downto. Returns 1 if handled. */
11481165 static int emit_iteration_stmt(Compiler *c, int id, Buf *b, int indent) {