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
return 0;}/* 1 if `id` is any proc-creating literal: a proc/lambda/Proc.new call (above) or a `->(){}` LambdaNode. */static int is_proc_create(Compiler *c, int id) { const char *ty = nt_type(c->nt, id); if (ty && !strcmp(ty, "LambdaNode")) return 1; return is_proc_literal(c, id);}/* The body return type of a proc-creating node (proc/lambda CallNode literal, or a LambdaNode). The last statement of the block/lambda body is the value. */static TyKind proc_node_ret(Compiler *c, int create) { if (!strcmp(ty, "FalseNode")) return TY_BOOL; if (!strcmp(ty, "NilNode")) return TY_NIL; if (!strcmp(ty, "RangeNode")) return TY_RANGE; if (!strcmp(ty, "LambdaNode")) return TY_PROC; if (!strcmp(ty, "LocalVariableReadNode")) { const char *nm = nt_str(nt, id, "name"); a later slice. */ if (lv->type == TY_PROC && !strcmp(ty, "LocalVariableWriteNode")) { int vnode = nt_ref(nt, id, "value"); if (vnode >= 0 && is_proc_literal(c, vnode)) { if (vnode >= 0 && is_proc_create(c, vnode)) { TyKind pr = proc_node_ret(c, vnode); if (pr != TY_UNKNOWN && (TyKind)lv->proc_ret != pr) { lv->proc_ret = (int)pr; changed = 1; } }static int infer_block_params(Compiler *c) { const NodeTable *nt = c->nt; int changed = 0; /* `->(x, ...) {}` (LambdaNode): its params live in the enclosing scope (no separate scope), like block params. Register and type them; default to int (the proc-literal slice default) until call-site arg inference lands. */ for (int id = 0; id < nt->count; id++) { const char *ty = nt_type(nt, id); if (!ty || strcmp(ty, "LambdaNode")) continue; int pn = nt_ref(nt, id, "parameters"); /* ParametersNode (1 level, unlike blocks) */ if (pn < 0) continue; int rn = 0; const int *reqs = nt_arr(nt, pn, "requireds", &rn); Scope *bs = comp_scope_of(c, id); for (int k = 0; k < rn; k++) { const char *p = nt_str(nt, reqs[k], "name"); if (!p) continue; LocalVar *lv = scope_local_intern(bs, p); lv->is_block_param = 1; if (lv->type == TY_UNKNOWN) { lv->type = TY_INT; changed = 1; } } } for (int id = 0; id < nt->count; id++) { const char *ty = nt_type(nt, id); if (!ty || strcmp(ty, "CallNode")) continue;src/codegen.cmodifiedView file
Loading diff…