Skip to content

Commit

Permalink
Merge pull request #34 from FluidNumerics/bugfix/monad-parse-error
Browse files Browse the repository at this point in the history
Fix monadic token priority to resolve issue #31
  • Loading branch information
fluidnumerics-joe authored Jun 12, 2024
2 parents 99c5862 + f166899 commit 2162a72
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
30 changes: 20 additions & 10 deletions src/FEQParse.F90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1670,21 +1668,33 @@ 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

else

Priority = 1
Priority = 0

endif

elseif(toke%tokenType == Monadic_Token) then

Priority = 5

else

Expand Down
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

51 changes: 51 additions & 0 deletions test/parsing_vanderpol_sfp64.f90
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2162a72

Please sign in to comment.