Skip to content

Render expressions via AST rewrites - #50

Merged
kwabenantim merged 2 commits into
developfrom
49-ast-driven-rendering
Aug 20, 2026
Merged

Render expressions via AST rewrites#50
kwabenantim merged 2 commits into
developfrom
49-ast-driven-rendering

Conversation

@kwabenantim

Copy link
Copy Markdown
Member

Fixes #49

formula_to_string ran formulaToL3String and then patched the rendered
string with regexes and a token loop to fix integer division and swap
function/constant names. Matching names by spelling had no
variable-vs-function guard (that guard existed only for constants), so a
model variable spelled like a C++ function (min, max, sin, ...) was
silently rewritten into that function, producing wrong C++.

Keep formulaToL3String as the sole renderer (precedence, parenthesisation
and number formatting are untouched) and map the required changes onto the
AST instead, by node kind:

- resolve_constants renames symbol and non-finite nodes to their C++ text
- map_functions handles power/root/log, degenerate plus/times, n-ary
  relationals, logical operators (renamed in place to keep libsbml's
  infix-vs-call choice), and the std::/sm:: name maps

Delete the token loop, rewrite_power, rewrite_nary_relational and the
placeholder machinery; only the integer->double regex remains.
formula_to_string's signature is unchanged.

Output is byte-identical across all reference models and the full SBML
test suite (1821 cases); the one behaviour change is fixing the latent
variable-vs-function-name collision above.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (1dc0b0e) to head (cc9b09d).
⚠️ Report is 2 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

Flag Coverage Δ
cpp 100.00% <ø> (ø)
python 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
chaste_sbml/_expressions.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors SBML expression rendering to rewrite typed AST nodes before libSBML generates C++.

Changes:

  • Maps constants, functions, powers, roots, logs, and operators through AST rewrites.
  • Adds broad rendering regression and edge-case coverage.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
chaste_sbml/_expressions.py Implements AST-driven C++ expression mapping.
chaste_sbml/tests/test_expressions.py Expands expression-rendering coverage.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread chaste_sbml/_expressions.py Outdated
A call to a model's own function definition is a generic AST_FUNCTION node
that also reports isFunction(), so the name-map branch was rerouting a model
function spelled like a built-in (sin, min, log) to std::/sm::, calling the
wrong implementation. Restrict the name maps to libsbml's typed built-in
function nodes and leave generic AST_FUNCTION calls unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@kwabenantim
kwabenantim marked this pull request as ready for review August 19, 2026 19:42
@kwabenantim
kwabenantim merged commit 2d93949 into develop Aug 20, 2026
15 checks passed
@kwabenantim
kwabenantim deleted the 49-ast-driven-rendering branch August 20, 2026 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Robust AST-driven expression rendering

2 participants