Skip to content

Instantly share code, notes, and snippets.

@Ansarina
Created August 11, 2018 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ansarina/c887ac2f95c8073d48ef72a618a281fc to your computer and use it in GitHub Desktop.
Save Ansarina/c887ac2f95c8073d48ef72a618a281fc to your computer and use it in GitHub Desktop.
program main
use function_parser
use iso_fortran_env, only: wp => real64, output_unit
implicit none
character(len=50):: func ='sin(x)'
integer,parameter :: nvar = 1 !! number of variables
character(len=*),dimension(nvar),parameter :: var = [ 'x' ]
real(wp),dimension(nvar) :: val = [ 2.0_wp ]
type(fparser) :: parser
real(wp) :: res
real(wp) :: x
real:: A
write(*,*) 'Please enter your function f(x):'
read(*,*) func
write(*,*) 'Please enter value for x:'
read(*,*) A
val = [A]
call parser%parse(func, var, .false.) ! parse and bytecompile function string
if (parser%error()) then
call parser%print_errors(output_unit)
else
call parser%evaluate(val,res) ! interprete bytecode representation of function
if (parser%error()) then
call parser%print_errors(output_unit)
else
write(*,*) 'f(x)=',func
write(*,'(/X,A2,F5.2,A2,F15.8/)') 'f(',A,')=',res
end if
end if
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment