From dbde01ccb5b57fced58bd38e92b9888632f8e979 Mon Sep 17 00:00:00 2001 From: Scott Scoble Date: Mon, 11 May 2026 21:30:38 -0700 Subject: [PATCH] Iterative ternary if/else rewrite, Raft partial fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- compiler/src/generators/typescript-state-machine.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/compiler/src/generators/typescript-state-machine.ts b/compiler/src/generators/typescript-state-machine.ts index b4cfeaa..f7aa068 100644 --- a/compiler/src/generators/typescript-state-machine.ts +++ b/compiler/src/generators/typescript-state-machine.ts @@ -542,10 +542,14 @@ function rewriteExpr(expr: string, stateVarNames: Set, 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 : 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 : 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(); }