Commit 3d10dc76

3d10dc76125bMerged

Ryan Seys <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

analyze: fold parenthesized and chained alias-modifier conditions

Follow-up to the merged alias-under-a-statement-modifier support. Extend the compile-time predicate folding in register_aliases so more statically-known guards resolve, rather than being treated as runtime (and so leaving the alias unregistered):

  • a parenthesized condition (alias a b if (true)) is unwrapped to its inner expression; an empty () is nil (falsy), multiple statements stay runtime;
  • array / hash / regexp literals fold as truthy (they are never nil or false);
  • a full if/elsif/else chain is followed, so an alias in a statically-selected elsif branch is registered (previously only a direct else was handled).

A non-constant guard anywhere along the chain still selects nothing.

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

Details

Commit
3d10dc76125b
Parent
5f96b1460568

src/analyze_scope.cmodifiedView file

@@ -999,11 +999,26 @@
999999 literals and false / nil fold; anything else (a call, constant, variable) is
10001000 runtime. */
10011001 static int alias_pred_const(const NodeTable *nt, int pred) {
1002- const char *t = pred >= 0 ? nt_type(nt, pred) : NULL;
1002+ if (pred < 0) return -1;
1003+ const char *t = nt_type(nt, pred);
10031004 if (!t) return -1;
1005+ /* unwrap parentheses: `if (cond)`. An empty `()` is nil (falsy); multiple
1006+ statements are not folded (conservative). */
1007+ while (!strcmp(t, "ParenthesesNode")) {
1008+ int stmts = nt_ref(nt, pred, "body"); /* ParenthesesNode -> StatementsNode */
1009+ int n = 0;
1010+ const int *body = stmts >= 0 ? nt_arr(nt, stmts, "body", &n) : NULL;
1011+ if (n == 0 || !body) return 0;
1012+ if (n != 1) return -1;
1013+ pred = body[0];
1014+ if (pred < 0) return -1;
1015+ t = nt_type(nt, pred);
1016+ if (!t) return -1;
1017+ }
10041018 if (!strcmp(t, "FalseNode") || !strcmp(t, "NilNode")) return 0;
10051019 if (!strcmp(t, "TrueNode") || !strcmp(t, "IntegerNode") || !strcmp(t, "FloatNode") ||
1006- !strcmp(t, "StringNode") || !strcmp(t, "SymbolNode"))
1020+ !strcmp(t, "StringNode") || !strcmp(t, "SymbolNode") || !strcmp(t, "ArrayNode") ||
1021+ !strcmp(t, "HashNode") || !strcmp(t, "RegularExpressionNode"))
10071022 return 1;
10081023 return -1;
10091024 }
@@ -1036,19 +1051,29 @@
10361051 comp_add_alias(cls, nt_str(nt, argv[0], "value"), nt_str(nt, argv[1], "value"));
10371052 }
10381053 else if (!strcmp(sty, "IfNode") || !strcmp(sty, "UnlessNode")) {
1039- /* A statement modifier (`alias a b if cond`) wraps the alias in an IfNode.
1040- An alias resolves at compile time, so only register the branch the
1041- condition statically selects. A non-constant guard registers nothing:
1042- the alias cannot be created conditionally with static method tables, so
1043- the name is left unresolved and codegen rejects it loudly if reached. */
1044- int is_unless = !strcmp(sty, "UnlessNode");
1045- int pc = alias_pred_const(nt, nt_ref(nt, s, "predicate"));
1046- int then_runs = is_unless ? (pc == 0) : (pc == 1);
1047- int else_runs = is_unless ? (pc == 1) : (pc == 0);
1048- if (then_runs) register_aliases_body(c, cls, nt_ref(nt, s, "statements"));
1049- int els = nt_ref(nt, s, is_unless ? "else_clause" : "subsequent");
1050- if (else_runs && els >= 0 && nt_type(nt, els) && !strcmp(nt_type(nt, els), "ElseNode"))
1051- register_aliases_body(c, cls, nt_ref(nt, els, "statements"));
1054+ /* A statement modifier (`alias a b if cond`) wraps the alias in an IfNode;
1055+ a full if/elsif/else chains through `subsequent`. An alias resolves at
1056+ compile time, so register only the branch the conditions statically
1057+ select, following the chain. A non-constant guard selects nothing: the
1058+ alias cannot be created conditionally with static method tables, so the
1059+ name is left unresolved and rejects loudly if used. */
1060+ int curr = s;
1061+ while (curr >= 0) {
1062+ const char *cty = nt_type(nt, curr);
1063+ if (!cty) break;
1064+ if (!strcmp(cty, "ElseNode")) {
1065+ register_aliases_body(c, cls, nt_ref(nt, curr, "statements"));
1066+ break;
1067+ }
1068+ if (strcmp(cty, "IfNode") && strcmp(cty, "UnlessNode")) break;
1069+ int is_unless = !strcmp(cty, "UnlessNode");
1070+ int pc = alias_pred_const(nt, nt_ref(nt, curr, "predicate"));
1071+ int then_runs = is_unless ? (pc == 0) : (pc == 1);
1072+ int else_runs = is_unless ? (pc == 1) : (pc == 0);
1073+ if (then_runs) { register_aliases_body(c, cls, nt_ref(nt, curr, "statements")); break; }
1074+ if (else_runs) curr = nt_ref(nt, curr, is_unless ? "else_clause" : "subsequent");
1075+ else break; /* non-constant: select nothing */
1076+ }
10521077 }
10531078 }
10541079 }