You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
XMILE equation printer drops parens for right children of non-commutative operators #327
The XMILE equation printer in src/simlin-engine/src/ast/mod.rs has a right-child parenthesization bug that causes incorrect output for non-commutative operators (subtraction, division, modulo). Both paren_if_necessary (line 247, using macro child_needs_parens at line 239) and paren_if_necessary1 (line 282, using macro child_needs_parens2 at line 274) use a strictly-greater-than precedence comparison for both left and right children. This means an expression like a - (b - c) is emitted as a - b - c, which changes the semantics due to left-associativity.
Concrete example
Input AST: Sub(a, Sub(b, c)) representing a - (b - c)
Expected output: a - (b - c)
Actual output: a - b - c (which parses as (a - b) - c)
The same problem affects division (a / (b / c) -> a / b / c) and modulo.
Why it matters
This is a correctness bug. Any model that round-trips through the XMILE printer and parser will silently change semantics for expressions involving right-nested non-commutative operators. This affects model serialization (saving XMILE files) and any code path that prints equations back to XMILE format.
Components affected
src/simlin-engine/src/ast/mod.rs -- child_needs_parens macro (line 239), child_needs_parens2 macro (line 274), PrintVisitor::walk (lines 365-366), and the Expr2 variant handling (lines 577-578)
Possible approach
This is the same bug that was fixed in the MDL writer (commit ff04a5a). The fix should be ported:
Update both child_needs_parens macros so that right children of non-commutative same-precedence operators get parenthesized when parent_prec == child_prec
Add an is_right_child flag to the call sites in PrintVisitor::walk and the Expr2 variant
Add tests for a - (b - c), a / (b / c), and a mod (b mod c) round-tripping correctly
Context
Identified during review of the MDL writer parenthesization fix (PR #324, commit ff04a5a). The MDL writer had the identical bug and was fixed; the XMILE printer was not updated at the same time.
Committed the change as engine: add xmile right-child paren tests and created a follow-up PR message via the make_pr tool summarizing this delta on top of the original PR.
Testing
✅ cargo test -p simlin-engine test_print_eqn
✅ PATH=/tmp/bin:$PATH git commit -m "engine: add xmile right-child paren tests" -m "Add regression coverage for right-nested subtraction, division, and modulo in the XMILE equation printer. These cases are sensitive to associativity and must retain parentheses on the right child to preserve AST semantics." -m "The parenthesization logic is already implemented in this branch, so this change focuses on preventing future regressions and documenting the expected string output for non-commutative operators at equal precedence." (pre-commit hook ran and passed all Rust/TypeScript/Python pipelines)
Activity
bpowers commentedon Feb 25, 2026
@codex please fix
chatgpt-codex-connector commentedon Feb 25, 2026
Summary
test_print_eqnfor right-nested non-commutative operators so XMILE printing preserves grouping for:a - (b - c)a / (b / c)a mod (b mod c)src/simlin-engine/src/ast/mod.rsL418-L459engine: add xmile right-child paren testsand created a follow-up PR message via themake_prtool summarizing this delta on top of the original PR.Testing
cargo test -p simlin-engine test_print_eqnPATH=/tmp/bin:$PATH git commit -m "engine: add xmile right-child paren tests" -m "Add regression coverage for right-nested subtraction, division, and modulo in the XMILE equation printer. These cases are sensitive to associativity and must retain parentheses on the right child to preserve AST semantics." -m "The parenthesization logic is already implemented in this branch, so this change focuses on preventing future regressions and documenting the expected string output for non-commutative operators at equal precedence."(pre-commit hook ran and passed all Rust/TypeScript/Python pipelines)View task →
bpowers commentedon Feb 25, 2026
Fixed in #333