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(); }