From 44722bfc6530b0440cf783ee99f5f98beeae0dd6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 8 Sep 2024 11:52:10 +0200 Subject: [PATCH] finished interpreter --- package/src/ExpressionProcessing.jl | 5 +++-- package/test/ExpressionProcessingTests.jl | 4 +++- package/test/InterpreterTests.jl | 6 +++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/package/src/ExpressionProcessing.jl b/package/src/ExpressionProcessing.jl index 469ccd6..ad7f391 100644 --- a/package/src/ExpressionProcessing.jl +++ b/package/src/ExpressionProcessing.jl @@ -23,8 +23,8 @@ function expr_to_postfix(expr::Expr)::PostfixType for j in 2:length(expr.args) arg = expr.args[j] + if typeof(arg) === Expr - # exprElement = convert_to_ExpressionElement() append!(postfix, expr_to_postfix(arg)) elseif typeof(arg) === Symbol # variables/parameters exprElement = convert_to_ExpressionElement(convert_var_to_int(arg)) @@ -34,7 +34,8 @@ function expr_to_postfix(expr::Expr)::PostfixType push!(postfix, exprElement) end - if length(postfix) >= 2 + # only add operator if at least 2 values are added. For the case where another expression is added first, we check if we are at the first iteration or not ( j != 2) + if length(postfix) >= 2 && j != 2 exprElement = convert_to_ExpressionElement(operator) push!(postfix, exprElement) end diff --git a/package/test/ExpressionProcessingTests.jl b/package/test/ExpressionProcessingTests.jl index b40248c..c78bed2 100644 --- a/package/test/ExpressionProcessingTests.jl +++ b/package/test/ExpressionProcessingTests.jl @@ -23,10 +23,12 @@ end @testset "Test conversion to postfix" begin reference = PostfixType() - + append!(reference, [ExpressionProcessing.convert_to_ExpressionElement(1), ExpressionProcessing.convert_to_ExpressionElement(1.0), ExpressionProcessing.convert_to_ExpressionElement(2), ExpressionProcessing.convert_to_ExpressionElement(MULTIPLY), ExpressionProcessing.convert_to_ExpressionElement(ADD), ExpressionProcessing.convert_to_ExpressionElement(-1), ExpressionProcessing.convert_to_ExpressionElement(ADD)]) postfix = expr_to_postfix(expressions[1]) @test isequal(reference, postfix) + + # TODO: Do more complex expressions because these have led to errors in the past end \ No newline at end of file diff --git a/package/test/InterpreterTests.jl b/package/test/InterpreterTests.jl index 113e32b..05bba4d 100644 --- a/package/test/InterpreterTests.jl +++ b/package/test/InterpreterTests.jl @@ -145,8 +145,8 @@ end # var set 1 @test isapprox(result[1,1], 37.32, atol=0.01) # expr1 - @test isequal(result[1,2], 64.74, atol=0.01) # expr2 + @test isapprox(result[1,2], 64.74, atol=0.01) # expr2 # var set 2 - @test isequal(result[2,1], 37.32, atol=0.01) # expr1 - @test isequal(result[2,2], -83.65, atol=0.01) # expr2 + @test isapprox(result[2,1], 37.32, atol=0.01) # expr1 + @test isapprox(result[2,2], -83.65, atol=0.01) # expr2 end