Skip to content

Commit 861a0e4

Browse files
Merge pull request #42 from ChrisRackauckas-Claude/fix-negative-variables-35
Add support for negative variables (unary minus) (#35)
2 parents d1a8808 + db62d28 commit 861a0e4

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

src/evaluator.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function eval_AST(expr::BaseModelicaExpr)
1919
BaseModelicaFactor(base, exp) => (f(base))^f(exp)
2020
BaseModelicaSum(left, right) => (f(left)) + (f(right))
2121
BaseModelicaMinus(left, right) => f(left) - f(right)
22+
BaseModelicaUnaryMinus(operand) => -f(operand)
2223
BaseModelicaProd(left, right) => f(left) * f(right)
2324
BaseModelicaDivide(left, right) => f(left) / f(right)
2425
BaseModelicaNot(relation) => !(f(relation))

src/parser.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ end
6363
BaseModelicaIdentifier(name)
6464
BaseModelicaSum(left, right)
6565
BaseModelicaMinus(left, right)
66+
BaseModelicaUnaryMinus(operand)
6667
BaseModelicaProd(left, right)
6768
BaseModelicaFactor(base, exp)
6869
BaseModelicaElementWiseFactor(base, exp)
@@ -126,6 +127,11 @@ function create_term(input_list)
126127
end
127128

128129
function create_arithmetic_expression(input_list)
130+
# Handle unary minus case - if first element is a minus operator
131+
if length(input_list) >= 2 && input_list[1] isa BMSubtract
132+
return BaseModelicaUnaryMinus(input_list[2])
133+
end
134+
129135
left_el = input_list[1]
130136
for (i, element) in enumerate(input_list)
131137
left_el = @match element begin

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ if GROUP == "All" || GROUP == "Core"
1919
arith_test = only(PC.parse_one("5 + 6*(45 + 9^2)^2", BM.arithmetic_expression))
2020
@test arith_test isa BM.BaseModelicaSum
2121
@test BM.eval_AST(arith_test) == 95261.0
22+
23+
# Test unary minus parsing (issue #35)
24+
unary_minus_test = only(PC.parse_one("-5", BM.arithmetic_expression))
25+
@test unary_minus_test isa BM.BaseModelicaUnaryMinus
26+
@test BM.eval_AST(unary_minus_test) == -5.0
2227

2328
newton_path = joinpath(
2429
dirname(dirname(pathof(BM))), "test", "testfiles", "NewtonCoolingBase.mo")
@@ -27,6 +32,15 @@ if GROUP == "All" || GROUP == "Core"
2732
newton_system = BM.baseModelica_to_ModelingToolkit(newton_cooling)
2833
@test newton_system isa ODESystem
2934
@test parse_basemodelica("testfiles/NewtonCoolingBase.mo") isa ODESystem
35+
36+
# Test parsing with negative variables (issue #35)
37+
negative_path = joinpath(
38+
dirname(dirname(pathof(BM))), "test", "testfiles", "NegativeVariable.mo")
39+
negative_package = BM.parse_file(negative_path)
40+
@test negative_package isa BM.BaseModelicaPackage
41+
negative_system = BM.baseModelica_to_ModelingToolkit(negative_package)
42+
@test negative_system isa ODESystem
43+
@test parse_basemodelica("testfiles/NegativeVariable.mo") isa ODESystem
3044
end
3145
end
3246
end

test/testfiles/NegativeVariable.mo

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package 'Negate'
2+
model 'Negate'
3+
Real 'x';
4+
equation
5+
der('x') = -'x';
6+
end 'Negate';
7+
end 'Negate';

0 commit comments

Comments
 (0)