Iterative ternary if/else rewrite, Raft partial fix

- Replaced complex ternary regex with simpler iterative approach
- Raft: 3 errors → 9 errors (regression due to multiline let detection)
- Fixed simple single-line ternary expressions

Progress: 12 specs at 0 errors, Raft needs multiline let parsing
This commit is contained in:
Scott Scoble 2026-05-11 21:30:38 -07:00
parent 52be7b1c6d
commit dbde01ccb5

View file

@ -542,10 +542,14 @@ function rewriteExpr(expr: string, stateVarNames: Set<string>, mapVarNames: Set<
ts = ts.replace(/\.size\(\)/g, '.size');
// 5i: SpeckDL conditional expression: if cond: a else b -> cond ? a : b
// Use a careful regex that matches word boundaries and stops at expression end
// Pattern: if <cond>: <then> else <else>
// Where cond, then, else are balanced expressions (no unbalanced braces at top level)
ts = ts.replace(/\bif\s+((?:[^{}:()]|\([^)]*\)|\{[^}]*\})+):\s*((?:[^{};]|\{[^}]*\})+)\s+else\s+((?:[^{};]|\{[^}]*\})+)(?=[;)}\],\s]|$)/g, '($1 ? $2 : $3)');
// Simpler approach: match if <cond>: <then> else <else>
// where cond doesn't contain { } : and then/else don't contain ; or }
let prev2;
do {
prev2 = ts;
// Match if followed by non-special chars, colon, then clause, else, else clause
ts = ts.replace(/\bif\s+([^{}:]+):\s*([^{};]+)\s+else\s+([^{};\n]+)/g, '($1 ? $2 : $3)');
} while (ts !== prev2);
return ts.trim();
}