Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/evaluator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function eval_AST(expr::BaseModelicaExpr)
BaseModelicaFactor(base, exp) => (f(base))^f(exp)
BaseModelicaSum(left, right) => (f(left)) + (f(right))
BaseModelicaMinus(left, right) => f(left) - f(right)
BaseModelicaUnaryMinus(operand) => -f(operand)
BaseModelicaProd(left, right) => f(left) * f(right)
BaseModelicaDivide(left, right) => f(left) / f(right)
BaseModelicaNot(relation) => !(f(relation))
Expand Down
6 changes: 6 additions & 0 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ end
BaseModelicaIdentifier(name)
BaseModelicaSum(left, right)
BaseModelicaMinus(left, right)
BaseModelicaUnaryMinus(operand)
BaseModelicaProd(left, right)
BaseModelicaFactor(base, exp)
BaseModelicaElementWiseFactor(base, exp)
Expand Down Expand Up @@ -126,6 +127,11 @@ function create_term(input_list)
end

function create_arithmetic_expression(input_list)
# Handle unary minus case - if first element is a minus operator
if length(input_list) >= 2 && input_list[1] isa BMSubtract
return BaseModelicaUnaryMinus(input_list[2])
end

left_el = input_list[1]
for (i, element) in enumerate(input_list)
left_el = @match element begin
Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ if GROUP == "All" || GROUP == "Core"
arith_test = only(PC.parse_one("5 + 6*(45 + 9^2)^2", BM.arithmetic_expression))
@test arith_test isa BM.BaseModelicaSum
@test BM.eval_AST(arith_test) == 95261.0

# Test unary minus parsing (issue #35)
unary_minus_test = only(PC.parse_one("-5", BM.arithmetic_expression))
@test unary_minus_test isa BM.BaseModelicaUnaryMinus
@test BM.eval_AST(unary_minus_test) == -5.0

newton_path = joinpath(
dirname(dirname(pathof(BM))), "test", "testfiles", "NewtonCoolingBase.mo")
Expand All @@ -27,6 +32,15 @@ if GROUP == "All" || GROUP == "Core"
newton_system = BM.baseModelica_to_ModelingToolkit(newton_cooling)
@test newton_system isa ODESystem
@test parse_basemodelica("testfiles/NewtonCoolingBase.mo") isa ODESystem

# Test parsing with negative variables (issue #35)
negative_path = joinpath(
dirname(dirname(pathof(BM))), "test", "testfiles", "NegativeVariable.mo")
negative_package = BM.parse_file(negative_path)
@test negative_package isa BM.BaseModelicaPackage
negative_system = BM.baseModelica_to_ModelingToolkit(negative_package)
@test negative_system isa ODESystem
@test parse_basemodelica("testfiles/NegativeVariable.mo") isa ODESystem
end
end
end
7 changes: 7 additions & 0 deletions test/testfiles/NegativeVariable.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package 'Negate'
model 'Negate'
Real 'x';
equation
der('x') = -'x';
end 'Negate';
end 'Negate';
Loading