Skip to content

Conversation

@ChrisRackauckas-Claude
Copy link
Contributor

Summary

This PR adds support for unary minus operations (negative variables) in BaseModelica expressions.

Problem

The parser could not handle negative variables like -'x' in equations such as der('x') = -'x'. The issue was that the create_arithmetic_expression function expected a left operand when encountering a minus operator, but with unary minus there is no left operand.

Solution

  1. Added new AST node: BaseModelicaUnaryMinus(operand) to represent unary minus operations
  2. Updated parser logic: Modified create_arithmetic_expression to detect when the first element is a minus operator and handle it as unary minus
  3. Added evaluation support: Added evaluation logic for BaseModelicaUnaryMinus in the evaluator (-f(operand))

Changes

  • src/parser.jl:
    • Added BaseModelicaUnaryMinus(operand) AST node
    • Updated create_arithmetic_expression function to handle unary minus case
  • src/evaluator.jl:
    • Added evaluation case for BaseModelicaUnaryMinus(operand) => -f(operand)
  • test/runtests.jl:
    • Added unit test for simple unary minus parsing (-5)
    • Added integration test for negative variables in full model
  • test/testfiles/NegativeVariable.mo:
    • Added test file with the example from the issue

Test plan

  • Added unit test for parsing -5 as BaseModelicaUnaryMinus
  • Added integration test parsing the full model from issue Add support for negative variables #35
  • Tests verify both parsing and evaluation work correctly
  • Existing tests continue to pass

Fixes

Closes #35

🤖 Generated with Claude Code

- Add BaseModelicaUnaryMinus AST node for unary minus operations
- Update create_arithmetic_expression to handle unary minus (-x)
- Add evaluation logic for BaseModelicaUnaryMinus in evaluator
- Add test cases for both simple unary minus (-5) and negative variables
- Fixes SciML#35: der('x') = -'x' now parses and evaluates correctly

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ChrisRackauckas ChrisRackauckas merged commit 861a0e4 into SciML:main Sep 9, 2025
9 of 10 checks passed
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.

Add support for negative variables

2 participants