Commit b6b3e206

b6b3e2069774Merged

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

Description

First-class Proc: support ->(){} lambda literals

Lower the ->(x) { ... } LambdaNode through the same path as proc {} / lambda {}: infer it to TY_PROC, type its required params (in the enclosing scope, like block params), and emit it via emit_proc_literal with lambda_p set.

LambdaNode carries its ParametersNode/body directly (one level shallower than a block's BlockParametersNode), so proc_param_name / proc_body_node abstract over both shapes. #call / .() / [] already dispatch on TY_PROC receivers.

Still capture-free + int-return only; closures and non-int returns abort loudly until their slices.

make test: 163 pass / 5 fail / 676 error (was 162 / 5 / 677); multi_arg_lambda newly green, no regressions.

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

Details

Commit
b6b3e2069774
Parent
d2144617bf92

src/analyze.cmodifiedView file

@@ -93,6 +93,14 @@
9393 return 0;
9494 }
9595
96+/* 1 if `id` is any proc-creating literal: a proc/lambda/Proc.new call (above)
97+ or a `->(){}` LambdaNode. */
98+static int is_proc_create(Compiler *c, int id) {
99+ const char *ty = nt_type(c->nt, id);
100+ if (ty && !strcmp(ty, "LambdaNode")) return 1;
101+ return is_proc_literal(c, id);
102+}
103+
96104 /* The body return type of a proc-creating node (proc/lambda CallNode literal,
97105 or a LambdaNode). The last statement of the block/lambda body is the value. */
98106 static TyKind proc_node_ret(Compiler *c, int create) {
@@ -555,6 +563,7 @@
555563 if (!strcmp(ty, "FalseNode")) return TY_BOOL;
556564 if (!strcmp(ty, "NilNode")) return TY_NIL;
557565 if (!strcmp(ty, "RangeNode")) return TY_RANGE;
566+ if (!strcmp(ty, "LambdaNode")) return TY_PROC;
558567
559568 if (!strcmp(ty, "LocalVariableReadNode")) {
560569 const char *nm = nt_str(nt, id, "name");
@@ -1176,7 +1185,7 @@
11761185 a later slice. */
11771186 if (lv->type == TY_PROC && !strcmp(ty, "LocalVariableWriteNode")) {
11781187 int vnode = nt_ref(nt, id, "value");
1179- if (vnode >= 0 && is_proc_literal(c, vnode)) {
1188+ if (vnode >= 0 && is_proc_create(c, vnode)) {
11801189 TyKind pr = proc_node_ret(c, vnode);
11811190 if (pr != TY_UNKNOWN && (TyKind)lv->proc_ret != pr) { lv->proc_ret = (int)pr; changed = 1; }
11821191 }
@@ -1459,6 +1468,25 @@
14591468 static int infer_block_params(Compiler *c) {
14601469 const NodeTable *nt = c->nt;
14611470 int changed = 0;
1471+
1472+ /* `->(x, ...) {}` (LambdaNode): its params live in the enclosing scope (no
1473+ separate scope), like block params. Register and type them; default to
1474+ int (the proc-literal slice default) until call-site arg inference lands. */
1475+ for (int id = 0; id < nt->count; id++) {
1476+ const char *ty = nt_type(nt, id);
1477+ if (!ty || strcmp(ty, "LambdaNode")) continue;
1478+ int pn = nt_ref(nt, id, "parameters"); /* ParametersNode (1 level, unlike blocks) */
1479+ if (pn < 0) continue;
1480+ int rn = 0; const int *reqs = nt_arr(nt, pn, "requireds", &rn);
1481+ Scope *bs = comp_scope_of(c, id);
1482+ for (int k = 0; k < rn; k++) {
1483+ const char *p = nt_str(nt, reqs[k], "name");
1484+ if (!p) continue;
1485+ LocalVar *lv = scope_local_intern(bs, p); lv->is_block_param = 1;
1486+ if (lv->type == TY_UNKNOWN) { lv->type = TY_INT; changed = 1; }
1487+ }
1488+ }
1489+
14621490 for (int id = 0; id < nt->count; id++) {
14631491 const char *ty = nt_type(nt, id);
14641492 if (!ty || strcmp(ty, "CallNode")) continue;