Skip to content

Commit

Permalink
Merge pull request #38 from FluidNumerics/feature/finalize-routines
Browse files Browse the repository at this point in the history
Add finalize methods for tokens and equationparser
  • Loading branch information
fluidnumerics-joe committed Jun 15, 2024
2 parents c55beb0 + 65be66e commit 05e5528
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/FEQParse.F90
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ module FEQParse
type(TokenStack) :: inFix
type(TokenStack) :: postFix
contains
final :: Finalize_EquationParser
procedure :: CleanEquation
procedure :: Tokenize
procedure :: ConvertToPostfix
Expand Down Expand Up @@ -128,6 +129,26 @@ function Construct_EquationParser(equation,indepVars) result(parser)

endfunction Construct_EquationParser

subroutine Finalize_EquationParser(parser)
type(EquationParser),intent(inout) :: parser
! Local
integer :: i

if(allocated(parser%inFixFormula)) deallocate(parser%inFixFormula)
if(allocated(parser%equation)) deallocate(parser%equation)
if(allocated(parser%variableName)) deallocate(parser%variableName)
if(allocated(parser%indepVars)) then
do i = 1,parser%nIndepVars
deallocate(parser%indepVars(i)%value)
enddo
deallocate(parser%indepVars)
endif

call parser%infix%Finalize()
call parser%postfix%Finalize()

endsubroutine Finalize_EquationParser

subroutine CleanEquation(parser,equationCleaned)
class(EquationParser),intent(inout) :: parser
logical,intent(out) :: equationCleaned
Expand Down Expand Up @@ -323,8 +344,6 @@ subroutine ConvertToPostFix(parser)
type(Token) :: tok
integer :: i

!success = .FALSE.

call parser%postfix%Construct(Stack_Length)
call operator_stack%Construct(Stack_Length)

Expand Down Expand Up @@ -489,6 +508,7 @@ function Evaluate_sfp32(parser,x) result(f)

call stack%Pop(a)
f = a

endfunction Evaluate_sfp32

function Evaluate_sfp64(parser,x) result(f)
Expand Down
80 changes: 80 additions & 0 deletions src/FEQParse_FloatStacks.F90
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module FEQParse_FloatStacks
contains

procedure :: Construct => Construct_sfp32Stack
final :: Finalize_sfp32Stack
procedure :: Push => Push_sfp32Stack
procedure :: Pop => Pop_sfp32Stack

Expand All @@ -38,6 +39,7 @@ module FEQParse_FloatStacks
contains

procedure :: Construct => Construct_sfp64Stack
final :: Finalize_sfp64Stack
procedure :: Push => Push_sfp64Stack
procedure :: Pop => Pop_sfp64Stack

Expand All @@ -49,6 +51,7 @@ module FEQParse_FloatStacks
contains

procedure :: Construct => Construct_r1fp32Stack
final :: Finalize_r1fp32Stack
procedure :: Push => Push_r1fp32Stack
procedure :: Pop => Pop_r1fp32Stack

Expand All @@ -60,6 +63,7 @@ module FEQParse_FloatStacks
contains

procedure :: Construct => Construct_r1fp64Stack
final :: Finalize_r1fp64Stack
procedure :: Push => Push_r1fp64Stack
procedure :: Pop => Pop_r1fp64Stack

Expand All @@ -71,6 +75,7 @@ module FEQParse_FloatStacks
contains

procedure :: Construct => Construct_r2fp32Stack
final :: Finalize_r2fp32Stack
procedure :: Push => Push_r2fp32Stack
procedure :: Pop => Pop_r2fp32Stack

Expand All @@ -82,6 +87,7 @@ module FEQParse_FloatStacks
contains

procedure :: Construct => Construct_r2fp64Stack
final :: Finalize_r2fp64Stack
procedure :: Push => Push_r2fp64Stack
procedure :: Pop => Pop_r2fp64Stack

Expand All @@ -93,6 +99,7 @@ module FEQParse_FloatStacks
contains

procedure :: Construct => Construct_r3fp32Stack
final :: Finalize_r3fp32Stack
procedure :: Push => Push_r3fp32Stack
procedure :: Pop => Pop_r3fp32Stack

Expand All @@ -104,6 +111,7 @@ module FEQParse_FloatStacks
contains

procedure :: Construct => Construct_r3fp64Stack
final :: Finalize_r3fp64Stack
procedure :: Push => Push_r3fp64Stack
procedure :: Pop => Pop_r3fp64Stack

Expand All @@ -115,6 +123,7 @@ module FEQParse_FloatStacks
contains

procedure :: Construct => Construct_r4fp32Stack
final :: Finalize_r4fp32Stack
procedure :: Push => Push_r4fp32Stack
procedure :: Pop => Pop_r4fp32Stack

Expand All @@ -126,6 +135,7 @@ module FEQParse_FloatStacks
contains

procedure :: Construct => Construct_r4fp64Stack
final :: Finalize_r4fp64Stack
procedure :: Push => Push_r4fp64Stack
procedure :: Pop => Pop_r4fp64Stack

Expand All @@ -142,6 +152,13 @@ subroutine Construct_sfp32Stack(stack,N)

endsubroutine Construct_sfp32Stack

subroutine Finalize_sfp32Stack(stack)
type(sfp32Stack),intent(inout) :: stack

if(allocated(stack%tokens)) deallocate(stack%tokens)

endsubroutine Finalize_sfp32Stack

subroutine Push_sfp32Stack(stack,tok)
class(sfp32Stack),intent(inout) :: stack
real(real32),intent(in) :: tok
Expand Down Expand Up @@ -173,6 +190,13 @@ subroutine Construct_sfp64Stack(stack,N)

endsubroutine Construct_sfp64Stack

subroutine Finalize_sfp64Stack(stack)
type(sfp64Stack),intent(inout) :: stack

if(allocated(stack%tokens)) deallocate(stack%tokens)

endsubroutine Finalize_sfp64Stack

subroutine Push_sfp64Stack(stack,tok)
class(sfp64Stack),intent(inout) :: stack
real(real64),intent(in) :: tok
Expand Down Expand Up @@ -214,6 +238,13 @@ subroutine Construct_r1fp32Stack(stack,N,mold)

endsubroutine Construct_r1fp32Stack

subroutine Finalize_r1fp32Stack(stack)
type(r1fp32Stack),intent(inout) :: stack

if(allocated(stack%tokens)) deallocate(stack%tokens)

endsubroutine Finalize_r1fp32Stack

subroutine Push_r1fp32Stack(stack,tok)
class(r1fp32Stack),intent(inout) :: stack
real(real32),intent(in) :: tok(:)
Expand Down Expand Up @@ -252,6 +283,13 @@ subroutine Construct_r1fp64Stack(stack,N,mold)

endsubroutine Construct_r1fp64Stack

subroutine Finalize_r1fp64Stack(stack)
type(r1fp64Stack),intent(inout) :: stack

if(allocated(stack%tokens)) deallocate(stack%tokens)

endsubroutine Finalize_r1fp64Stack

subroutine Push_r1fp64Stack(stack,tok)
class(r1fp64Stack),intent(inout) :: stack
real(real64),intent(in) :: tok(:)
Expand Down Expand Up @@ -291,6 +329,13 @@ subroutine Construct_r2fp32Stack(stack,N,mold)

endsubroutine Construct_r2fp32Stack

subroutine Finalize_r2fp32Stack(stack)
type(r2fp32Stack),intent(inout) :: stack

if(allocated(stack%tokens)) deallocate(stack%tokens)

endsubroutine Finalize_r2fp32Stack

subroutine Push_r2fp32Stack(stack,tok)
class(r2fp32Stack),intent(inout) :: stack
real(real32),intent(in) :: tok(:,:)
Expand Down Expand Up @@ -330,6 +375,13 @@ subroutine Construct_r2fp64Stack(stack,N,mold)

endsubroutine Construct_r2fp64Stack

subroutine Finalize_r2fp64Stack(stack)
type(r2fp64Stack),intent(inout) :: stack

if(allocated(stack%tokens)) deallocate(stack%tokens)

endsubroutine Finalize_r2fp64Stack

subroutine Push_r2fp64Stack(stack,tok)
class(r2fp64Stack),intent(inout) :: stack
real(real64),intent(in) :: tok(:,:)
Expand Down Expand Up @@ -370,6 +422,13 @@ subroutine Construct_r3fp32Stack(stack,N,mold)

endsubroutine Construct_r3fp32Stack

subroutine Finalize_r3fp32Stack(stack)
type(r3fp32Stack),intent(inout) :: stack

if(allocated(stack%tokens)) deallocate(stack%tokens)

endsubroutine Finalize_r3fp32Stack

subroutine Push_r3fp32Stack(stack,tok)
class(r3fp32Stack),intent(inout) :: stack
real(real32),intent(in) :: tok(:,:,:)
Expand Down Expand Up @@ -410,6 +469,13 @@ subroutine Construct_r3fp64Stack(stack,N,mold)

endsubroutine Construct_r3fp64Stack

subroutine Finalize_r3fp64Stack(stack)
type(r3fp64Stack),intent(inout) :: stack

if(allocated(stack%tokens)) deallocate(stack%tokens)

endsubroutine Finalize_r3fp64Stack

subroutine Push_r3fp64Stack(stack,tok)
class(r3fp64Stack),intent(inout) :: stack
real(real64),intent(in) :: tok(:,:,:)
Expand Down Expand Up @@ -451,6 +517,13 @@ subroutine Construct_r4fp32Stack(stack,N,mold)

endsubroutine Construct_r4fp32Stack

subroutine Finalize_r4fp32Stack(stack)
type(r4fp32Stack),intent(inout) :: stack

if(allocated(stack%tokens)) deallocate(stack%tokens)

endsubroutine Finalize_r4fp32Stack

subroutine Push_r4fp32Stack(stack,tok)
class(r4fp32Stack),intent(inout) :: stack
real(real32),intent(in) :: tok(:,:,:,:)
Expand Down Expand Up @@ -492,6 +565,13 @@ subroutine Construct_r4fp64Stack(stack,N,mold)

endsubroutine Construct_r4fp64Stack

subroutine Finalize_r4fp64Stack(stack)
type(r4fp64Stack),intent(inout) :: stack

if(allocated(stack%tokens)) deallocate(stack%tokens)

endsubroutine Finalize_r4fp64Stack

subroutine Push_r4fp64Stack(stack,tok)
class(r4fp64Stack),intent(inout) :: stack
real(real64),intent(in) :: tok(:,:,:,:)
Expand Down
8 changes: 8 additions & 0 deletions src/FEQParse_TokenStack.F90
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module FEQParse_TokenStack
integer :: top_index = 0
contains
procedure :: Construct => Construct_TokenStack
procedure :: Finalize => Finalize_TokenStack
procedure :: Push => Push_TokenStack
procedure :: Pop => Pop_TokenStack
procedure :: IsEmpty => IsEmpty_TokenStack
Expand All @@ -46,6 +47,13 @@ subroutine Construct_TokenStack(stack,N)

endsubroutine Construct_TokenStack

subroutine Finalize_TokenStack(stack)
class(TokenStack),intent(inout) :: stack

if(allocated(stack%tokens)) deallocate(stack%tokens)

endsubroutine Finalize_TokenStack

subroutine Push_TokenStack(stack,tok)
class(TokenStack),intent(inout) :: stack
type(Token),intent(in) :: tok
Expand Down

0 comments on commit 05e5528

Please sign in to comment.