From 84ce5c40b3710dd16047ad06c1aafa38b1d6861a Mon Sep 17 00:00:00 2001 From: Joe Schoonover Date: Wed, 12 Jun 2024 19:10:07 +0000 Subject: [PATCH 1/2] Fix monadic token priority to resolve issue #31 --- src/FEQParse.F90 | 26 +++++++++------- test/CMakeLists.txt | 3 +- test/parsing_vanderpol_sfp64.f90 | 51 ++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 test/parsing_vanderpol_sfp64.f90 diff --git a/src/FEQParse.F90 b/src/FEQParse.F90 index 1674796..e89cb4f 100644 --- a/src/FEQParse.F90 +++ b/src/FEQParse.F90 @@ -572,11 +572,9 @@ function Evaluate_sfp64(parser,x) result(f) case(Monadic_Token) if(trim(t%tokenString) == '-') then - call stack%Pop(a) a = -a call stack%Push(a) - endif case default @@ -1670,21 +1668,29 @@ integer function Priority(parser,toke) Priority = 5 - elseif(toke%tokenString(1:1) == '^') then + elseif(toke%tokenType == Operator_Token) then + + if(toke%tokenString(1:1) == '^') then + + Priority = 4 - Priority = 4 + elseif(toke%tokenString(1:1) == '/') then - elseif(toke%tokenString(1:1) == '/') then + Priority = 3 - Priority = 3 + elseif(toke%tokenString(1:1) == '*') then - elseif(toke%tokenString(1:1) == '*') then + Priority = 2 - Priority = 2 + elseif(toke%tokenString(1:1) == '+' .or. toke%tokenString(1:1) == '-') then - elseif(toke%tokenString(1:1) == '+' .or. toke%tokenString(1:1) == '-') then + Priority = 1 - Priority = 1 + endif + + elseif(toke%tokenType == Monadic_Token) then + + Priority = 5 else diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 53ccb26..4be87dd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -189,5 +189,6 @@ add_fortran_tests ( "tan_sfp64.f90" "print_tokens.f90" "custom_r1fp64.f90" - "parsing_difficult_r1fp64.f90") + "parsing_difficult_r1fp64.f90" + "parsing_vanderpol_sfp64.f90") \ No newline at end of file diff --git a/test/parsing_vanderpol_sfp64.f90 b/test/parsing_vanderpol_sfp64.f90 new file mode 100644 index 0000000..a8b0843 --- /dev/null +++ b/test/parsing_vanderpol_sfp64.f90 @@ -0,0 +1,51 @@ +program test + + implicit none + integer :: exit_code + + exit_code = vanderpol_sfp64() + stop exit_code + +contains + + integer function vanderpol_sfp64() result(r) + use FEQParse + use iso_fortran_env + implicit none + integer,parameter :: N = 10 + type(EquationParser) :: f + character(len=128) :: fun_buf + real(real64) :: t,x,y,u,v + real(real64) :: feval + real(real64) :: fexact + + fun_buf = '-8.53*(1-x*x)*v-y' + f = equationparser('f = '//fun_buf,['t','x','y','u','v']) + + print*,"--------- Infix -------------------" + call f%Print_InFixTokens() + print*,"-----------------------------------" + print*,"--------- Postfix -------------------" + call f%Print_PostFixTokens() + print*,"-----------------------------------" + + t = 0.0_real64 + x = 1.0_real64 + y = 3.0_real64 + u = 12.0_real64 + v = 0.0_real64 + + feval = f%evaluate([t,x,y,u,v]) + + fexact = -8.53_real64*(1.0_real64-x*x)*v-y + + if((abs(feval-fexact)) <= epsilon(1.0_real64)) then + r = 0 + print*,feval,fexact + else + r = 1 + print*,feval,fexact + endif + + endfunction vanderpol_sfp64 +endprogram test From f1668992ddefdb1c613cc88c97d58d39b581b539 Mon Sep 17 00:00:00 2001 From: Joe Schoonover Date: Wed, 12 Jun 2024 19:57:18 +0000 Subject: [PATCH 2/2] Add edge case operator priority back Intel ifort compiler builds fail on parsing_* tests; errors suggest priority value was unset. This patch preserves previous behavior for operators in addition to increasing monad priority --- src/FEQParse.F90 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/FEQParse.F90 b/src/FEQParse.F90 index e89cb4f..886e85d 100644 --- a/src/FEQParse.F90 +++ b/src/FEQParse.F90 @@ -1686,6 +1686,10 @@ integer function Priority(parser,toke) Priority = 1 + else + + Priority = 0 + endif elseif(toke%tokenType == Monadic_Token) then