Commit f4c2fc34

f4c2fc342beeMerged

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

Description

Proc composition: proc << proc / proc >> proc

Lower f<>g to sp_proc_compose with the right outer/inner order; type the result TY_PROC and (f<<g).call's value as the outer proc's return (unwrapping the parenthesized receiver). Fixes i920.

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

Details

Commit
f4c2fc342bee
Parent
bcefdb4d2c38

src/analyze.cmodifiedView file

@@ -337,6 +337,12 @@
337337 const NodeTable *nt = c->nt;
338338 const char *ty = nt_type(nt, node);
339339 if (!ty) return TY_UNKNOWN;
340+ /* unwrap `(expr)` so `(f << g).call` sees the composition node */
341+ if (!strcmp(ty, "ParenthesesNode")) {
342+ int body = nt_ref(nt, node, "body");
343+ int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
344+ return bn == 1 ? proc_ret_of(c, bb[0]) : TY_UNKNOWN;
345+ }
340346 if (!strcmp(ty, "LambdaNode") || is_proc_literal(c, node)) return proc_node_ret(c, node);
341347 if (!strcmp(ty, "LocalVariableReadNode")) {
342348 Scope *s = comp_scope_of(c, node);
@@ -347,6 +353,16 @@
347353 /* a method call that returns a proc -> the callee's recorded proc return */
348354 int recv = nt_ref(nt, node, "receiver");
349355 const char *name = nt_str(nt, node, "name");
356+ /* proc << proc / proc >> proc: the composed call returns the OUTER proc's
357+ value (f<<g outer=f; f>>g outer=g). */
358+ if (recv >= 0 && name && infer_type(c, recv) == TY_PROC) {
359+ int args = nt_ref(nt, node, "arguments");
360+ int an = 0; const int *av = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;
361+ if (an == 1 && infer_type(c, av[0]) == TY_PROC) {
362+ if (!strcmp(name, "<<")) return proc_ret_of(c, recv);
363+ if (!strcmp(name, ">>")) return proc_ret_of(c, av[0]);
364+ }
365+ }
350366 int mi = -1;
351367 if (recv < 0) {
352368 mi = comp_method_index(c, name);
@@ -580,6 +596,12 @@
580596 (!strcmp(name, "call") || !strcmp(name, "()") || !strcmp(name, "[]")))
581597 return proc_call_ret(c, recv);
582598
599+ /* Proc composition: proc << proc / proc >> proc -> a new Proc. */
600+ if (recv >= 0 && rt == TY_PROC && argc == 1 &&
601+ (!strcmp(name, "<<") || !strcmp(name, ">>")) &&
602+ infer_type(c, argv[0]) == TY_PROC)
603+ return TY_PROC;
604+
583605 /* Proc introspection */
584606 if (recv >= 0 && rt == TY_PROC && argc == 0) {
585607 if (!strcmp(name, "arity")) return TY_INT;