diff options
author | 2019-03-13 15:36:46 -0700 | |
---|---|---|
committer | 2019-03-13 15:41:13 -0700 | |
commit | 77a53d41a63f06089ecc83c84fccd7c4a42a89af (patch) | |
tree | 4e7fa36f324dba48dea0aebb158ba7c9ff045ce2 | |
parent | 56104548c321aa1d26f6773091e2801c47626dea (diff) | |
download | WarpX-77a53d41a63f06089ecc83c84fccd7c4a42a89af.tar.gz WarpX-77a53d41a63f06089ecc83c84fccd7c4a42a89af.tar.zst WarpX-77a53d41a63f06089ecc83c84fccd7c4a42a89af.zip |
switch to WarpXParser
-rw-r--r-- | Source/FortranInterface/WarpX_f.H | 7 | ||||
-rw-r--r-- | Source/Initialization/Make.package | 1 | ||||
-rw-r--r-- | Source/Initialization/PlasmaInjector.H | 9 | ||||
-rw-r--r-- | Source/Initialization/PlasmaInjector.cpp | 36 | ||||
-rw-r--r-- | Source/Initialization/WarpX_parser.F90 | 1365 | ||||
-rw-r--r-- | Source/Laser/LaserParticleContainer.H | 3 | ||||
-rw-r--r-- | Source/Laser/LaserParticleContainer.cpp | 76 | ||||
-rw-r--r-- | Source/Laser/WarpX_laser.F90 | 19 | ||||
-rw-r--r-- | Source/Make.WarpX | 1 | ||||
-rw-r--r-- | Source/Parser/GNUmakefile | 4 | ||||
-rw-r--r-- | Source/Parser/Make.package | 9 | ||||
-rw-r--r-- | Source/Parser/WarpXParser.H | 76 | ||||
-rw-r--r-- | Source/Parser/WarpXParser.cpp | 126 | ||||
-rw-r--r-- | Source/Parser/wp_parser.l | 68 | ||||
-rw-r--r-- | Source/Parser/wp_parser.lex.c | 2012 | ||||
-rw-r--r-- | Source/Parser/wp_parser.lex.h | 338 | ||||
-rw-r--r-- | Source/Parser/wp_parser.tab.c | 1561 | ||||
-rw-r--r-- | Source/Parser/wp_parser.tab.h | 85 | ||||
-rw-r--r-- | Source/Parser/wp_parser.y | 83 | ||||
-rw-r--r-- | Source/Parser/wp_parser_c.c | 13 | ||||
-rw-r--r-- | Source/Parser/wp_parser_c.h | 16 | ||||
-rw-r--r-- | Source/Parser/wp_parser_y.c | 1082 | ||||
-rw-r--r-- | Source/Parser/wp_parser_y.h | 157 |
23 files changed, 5689 insertions, 1458 deletions
diff --git a/Source/FortranInterface/WarpX_f.H b/Source/FortranInterface/WarpX_f.H index 1f5256483..ba9c4b044 100644 --- a/Source/FortranInterface/WarpX_f.H +++ b/Source/FortranInterface/WarpX_f.H @@ -178,8 +178,6 @@ extern "C" amrex::Real* Xp, amrex::Real* Yp, amrex::Real* t, amrex::Real* wavelength, amrex::Real* e_max, amrex::Real* waist, amrex::Real* duration, amrex::Real* f, amrex::Real* amplitude ); - void parse_function_laser( const long* np, amrex::Real* Xp, amrex::Real* Yp, amrex::Real* t, amrex::Real* amplitude, const int parser_instance_number ); - void calculate_laser_plane_coordinates( const long* np, amrex::Real* xp, amrex::Real* yp, amrex::Real* zp, amrex::Real* plane_Xp, amrex::Real* plane_Yp, @@ -464,11 +462,6 @@ extern "C" const amrex_real* fin, const int* ilo, const int* ihi, const amrex_real* stencil, const int* nsten); - int parser_initialize_function(const char *str_func, const int len_func, - const char *str_var, const int len_var); - - amrex::Real parser_evaluate_function(const amrex::Real*, const int, const int); - #ifdef WARPX_USE_PSATD void warpx_fft_mpi_init (int fcomm); void warpx_fft_domain_decomp (int* warpx_local_nz, int* warpx_local_z0, diff --git a/Source/Initialization/Make.package b/Source/Initialization/Make.package index 829b9a91c..b825924c6 100644 --- a/Source/Initialization/Make.package +++ b/Source/Initialization/Make.package @@ -3,7 +3,6 @@ CEXE_sources += WarpXInitData.cpp CEXE_sources += CustomMomentumProb.cpp CEXE_sources += PlasmaInjector.cpp CEXE_headers += PlasmaInjector.H -F90EXE_sources += WarpX_parser.F90 INCLUDE_LOCATIONS += $(WARPX_HOME)/Source/Initialization VPATH_LOCATIONS += $(WARPX_HOME)/Source/Initialization diff --git a/Source/Initialization/PlasmaInjector.H b/Source/Initialization/PlasmaInjector.H index a25d1141b..7e61fa5d2 100644 --- a/Source/Initialization/PlasmaInjector.H +++ b/Source/Initialization/PlasmaInjector.H @@ -6,6 +6,7 @@ #include "AMReX_REAL.H" #include <AMReX_Vector.H> #include <WarpXConst.H> +#include <WarpXParser.H> #include "AMReX_ParmParse.H" #include "AMReX_Utility.H" @@ -70,7 +71,7 @@ public: UserConstants my_constants; private: std::string _parse_density_function; - int parser_instance_number=0; + WarpXParser parser_density; }; /// @@ -177,9 +178,9 @@ private: std::string _parse_momentum_function_ux; std::string _parse_momentum_function_uy; std::string _parse_momentum_function_uz; - int parser_instance_number_ux=0; - int parser_instance_number_uy=0; - int parser_instance_number_uz=0; + WarpXParser parser_ux; + WarpXParser parser_uy; + WarpXParser parser_uz; }; diff --git a/Source/Initialization/PlasmaInjector.cpp b/Source/Initialization/PlasmaInjector.cpp index 3c120a7c1..01d122ac6 100644 --- a/Source/Initialization/PlasmaInjector.cpp +++ b/Source/Initialization/PlasmaInjector.cpp @@ -74,17 +74,13 @@ ParseDensityProfile::ParseDensityProfile(std::string parse_density_function) { my_constants.ReadParameters(); parse_density_function = my_constants.replaceStringValue(parse_density_function); - const std::string s_var = "x,y,z"; - parser_instance_number = parser_initialize_function(parse_density_function.c_str(), - parse_density_function.length(), - s_var.c_str(), - s_var.length()); + parser_density.define(parse_density_function); + parser_density.registerVariables({"x","y","z"}); } Real ParseDensityProfile::getDensity(Real x, Real y, Real z) const { - std::array<amrex::Real, 3> list_var = {x,y,z}; - return parser_evaluate_function(list_var.data(), 3, parser_instance_number); + return parser_density.eval(x,y,z); } ConstantMomentumDistribution::ConstantMomentumDistribution(Real ux, @@ -141,31 +137,23 @@ ParseMomentumFunction::ParseMomentumFunction(std::string parse_momentum_function _parse_momentum_function_uy(parse_momentum_function_uy), _parse_momentum_function_uz(parse_momentum_function_uz) { - const std::string s_var = "x,y,z"; my_constants.ReadParameters(); parse_momentum_function_ux = my_constants.replaceStringValue(parse_momentum_function_ux); parse_momentum_function_uy = my_constants.replaceStringValue(parse_momentum_function_uy); parse_momentum_function_uz = my_constants.replaceStringValue(parse_momentum_function_uz); - parser_instance_number_ux = parser_initialize_function(parse_momentum_function_ux.c_str(), - parse_momentum_function_ux.length(), - s_var.c_str(), - s_var.length()); - parser_instance_number_uy = parser_initialize_function(parse_momentum_function_uy.c_str(), - parse_momentum_function_uy.length(), - s_var.c_str(), - s_var.length()); - parser_instance_number_uz = parser_initialize_function(parse_momentum_function_uz.c_str(), - parse_momentum_function_uz.length(), - s_var.c_str(), - s_var.length()); + parser_ux.define(parse_momentum_function_ux); + parser_uy.define(parse_momentum_function_uy); + parser_uz.define(parse_momentum_function_uz); + parser_ux.registerVariables({"x","y","z"}); + parser_uy.registerVariables({"x","y","z"}); + parser_uz.registerVariables({"x","y","z"}); } void ParseMomentumFunction::getMomentum(vec3& u, Real x, Real y, Real z) { - std::array<amrex::Real, 3> list_var = {x,y,z}; - u[0] = parser_evaluate_function(list_var.data(), 3, parser_instance_number_ux); - u[1] = parser_evaluate_function(list_var.data(), 3, parser_instance_number_uy); - u[2] = parser_evaluate_function(list_var.data(), 3, parser_instance_number_uz); + u[0] = parser_ux.eval(x,y,z); + u[1] = parser_uy.eval(x,y,z); + u[2] = parser_uz.eval(x,y,z); } RandomPosition::RandomPosition(int num_particles_per_cell): diff --git a/Source/Initialization/WarpX_parser.F90 b/Source/Initialization/WarpX_parser.F90 deleted file mode 100644 index 4b95c5ac7..000000000 --- a/Source/Initialization/WarpX_parser.F90 +++ /dev/null @@ -1,1365 +0,0 @@ -! MODULE mod_interpret -! Parser to read and evaluate math expressions - -module mod_interpret -use iso_c_binding -use amrex_fort_module, only : amrex_real -use amrex_error_module, only : amrex_error -implicit none -INTEGER, parameter :: plus =1, & - minus =2, & - multiply =3, & - divide =4, & - power =5, & - greaterthan=6, & - lessthan =7, & - exponential=8, & - logarithm =9, & - sine =10, & - cosine =11, & - tangent =12, & - square_root=13, & - arccos =14, & - arcsin =15, & - arctan =16, & - sinhyp =17, & - coshyp =18, & - tanhyp =19, & - logten =20 - -CHARACTER(5), DIMENSION(0:20) :: coper -INTEGER, parameter :: w_parenthesis = 1, & - w_number = 2, & - w_operator = 3, & - w_intrinsic = 4, & - w_variable = 5 -TYPE :: operation_type - INTEGER :: a,b,op -END TYPE operation_type - -TYPE :: res_type - INTEGER :: nb_op - REAL(amrex_real) :: value - TYPE(res_type), DIMENSION(:), POINTER :: res - LOGICAL, DIMENSION(:), POINTER :: l_res, l_res_do - TYPE(operation_type), DIMENSION(:), POINTER :: operation - LOGICAL :: a_res,a_l_res,a_l_res_do,a_operation -END TYPE res_type - -TYPE :: res_type_array - INTEGER :: nb_op - REAL(amrex_real), DIMENSION(:), POINTER :: value - TYPE(res_type_array), DIMENSION(:), POINTER :: res - LOGICAL, DIMENSION(:), POINTER :: l_res, l_res_do - TYPE(operation_type), DIMENSION(:), POINTER :: operation - LOGICAL :: a_res,a_l_res,a_l_res_do,a_operation -END TYPE res_type_array - -TYPE(res_type_array) :: res_array - -INTEGER :: nb_res=0 -TYPE(res_type), DIMENSION(10) :: table_of_res - -contains - -RECURSIVE subroutine del_res(resin) -implicit none -TYPE(res_type), INTENT(INOUT) :: resin -INTEGER :: j - IF(resin%a_res) then - do j = LBOUND(resin%res,1),UBOUND(resin%res,1) - call del_res(resin%res(j)) - end do - DEALLOCATE(resin%res) - resin%a_res=.FALSE. - END if - IF(resin%a_l_res) then - DEALLOCATE(resin%l_res) - resin%a_l_res=.FALSE. - END if - IF(resin%a_l_res_do) then - DEALLOCATE(resin%l_res_do) - resin%a_l_res_do=.FALSE. - END if - IF(resin%a_operation) then - DEALLOCATE(resin%operation) - resin%a_operation=.FALSE. - END if -return -end subroutine del_res - -RECURSIVE subroutine del_res_array(resin) -implicit none -TYPE(res_type_array), INTENT(INOUT) :: resin -INTEGER :: j - IF(resin%a_res) then - do j = LBOUND(resin%res,1),UBOUND(resin%res,1) - call del_res_array(resin%res(j)) - end do - DEALLOCATE(resin%res) - resin%a_res=.FALSE. - END if - IF(resin%a_l_res) then - DEALLOCATE(resin%l_res) - resin%a_l_res=.FALSE. - END if - IF(resin%a_l_res_do) then - DEALLOCATE(resin%l_res_do) - resin%a_l_res_do=.FALSE. - END if - IF(resin%a_operation) then - DEALLOCATE(resin%operation) - resin%a_operation=.FALSE. - END if - if (associated(resin%value)) then - nullify(resin%value) - end if -return -end subroutine del_res_array - -subroutine init_interpret() -implicit none - -coper(0 )='###' -coper(plus )='+' -coper(minus )='-' -coper(multiply )='*' -coper(divide )='/' -coper(power )='**' -coper(square_root)='sqrt' -coper(exponential)='exp' -coper(logarithm )='log' -coper(sine )='sin' -coper(cosine )='cos' -coper(tangent )='tan' -coper(arccos )='acos' -coper(arcsin )='asin' -coper(arctan )='atan' -coper(sinhyp )='sinh' -coper(coshyp )='cosh' -coper(tanhyp )='tanh' -coper(logten )='log10' -coper(greaterthan)='>' -coper(lessthan )='<' - -return -end subroutine init_interpret - -recursive function calc_res(res,list_var,root) RESULT(calc_res_r) -implicit none -REAL(amrex_real) :: calc_res_r -TYPE(res_type), INTENT(IN OUT) :: res -REAL(amrex_real), DIMENSION(:), OPTIONAL :: list_var -LOGICAL, INTENT(IN), OPTIONAL :: root - -INTEGER :: j,ia,ib,op -REAL(amrex_real) :: a,b - -do j = 1, res%nb_op - ia = res%operation(j)%a - ib = res%operation(j)%b - op = res%operation(j)%op - IF(op<0) then - IF(op>-100) res%res(ia)%value=list_var(-op) - else - IF(res%l_res(ia).and.res%l_res_do(ia)) then - IF(PRESENT(list_var)) then - a = calc_res(res=res%res(ia),list_var=list_var,root=.false.) - else - a = calc_res(res=res%res(ia),root=.false.) - END if - res%l_res_do(ia)=.false. - else - a = res%res(ia)%value - END if - IF(res%l_res(ib).and.res%l_res_do(ib)) then - IF(PRESENT(list_var)) then - b = calc_res(res=res%res(ib),list_var=list_var,root=.false.) - else - b = calc_res(res=res%res(ib),root=.false.) - END if - ! This might prevent OMP threading - res%l_res_do(ib)=.false. - else - b = res%res(ib)%value - END if - ! This might prevent OMP threading - res%res(ia)%value=eval(a,b,op) - END if -end do -IF(res%nb_op==0) then - ia=1 - calc_res_r = res%res(ia)%value -else - calc_res_r = res%res(ia)%value -END if - -IF(.not.PRESENT(root)) call calc_res_init(res) - -end function calc_res - -recursive subroutine calc_res_init(res) -implicit none -TYPE(res_type), INTENT(IN OUT) :: res - -INTEGER :: j,ia,ib,op - -do j = 1, res%nb_op - ia = res%operation(j)%a - ib = res%operation(j)%b - op = res%operation(j)%op - IF(op>=0) then - IF(res%l_res(ia).and..not.res%l_res_do(ia)) then - call calc_res_init(res%res(ia)) - res%l_res_do(ia)=.true. - END if - IF(res%l_res(ib).and..not.res%l_res_do(ib)) then - call calc_res_init(res%res(ib)) - res%l_res_do(ib)=.true. - END if - END if -end do - -end subroutine calc_res_init - -recursive function calc_res_array(res,list_values,root) RESULT(calc_res_array_r) -implicit none -REAL(amrex_real), dimension(:), pointer :: calc_res_array_r -TYPE(res_type_array), INTENT(IN OUT) :: res -REAL(amrex_real), DIMENSION(:,:), OPTIONAL :: list_values -LOGICAL, INTENT(IN), OPTIONAL :: root - -INTEGER :: j,ia,ib,op,n -REAL(amrex_real),dimension(:),pointer :: a,b - -do j = 1, res%nb_op - ia = res%operation(j)%a - ib = res%operation(j)%b - op = res%operation(j)%op - IF(op<0) then - IF(op>-100) then - n = size(list_values,2) - allocate(res%res(ia)%value(n)) - res%res(ia)%value=list_values(-op,:) - end if - else - IF(res%l_res(ia).and.res%l_res_do(ia)) then - IF(PRESENT(list_values)) then - a => calc_res_array(res=res%res(ia),list_values=list_values,root=.false.) - else - a => calc_res_array(res=res%res(ia),root=.false.) - END if - res%l_res_do(ia)=.false. - else - a => res%res(ia)%value - END if - IF(res%l_res(ib).and.res%l_res_do(ib)) then - IF(PRESENT(list_values)) then - b => calc_res_array(res=res%res(ib),list_values=list_values,root=.false.) - else - b => calc_res_array(res=res%res(ib),root=.false.) - END if - res%l_res_do(ib)=.false. - else - b => res%res(ib)%value - END if - res%res(ia)%value=>eval_array(a,b,op) - END if -end do - -IF(res%nb_op==0) then - ia=1 - calc_res_array_r => res%res(ia)%value -else - calc_res_array_r => res%res(ia)%value -END if - -IF(.not.PRESENT(root)) call calc_res_array_init(res) - -end function calc_res_array - -recursive subroutine calc_res_array_init(res) -implicit none -TYPE(res_type_array), INTENT(IN OUT) :: res - -INTEGER :: j,ia,ib,op - -do j = 1, res%nb_op - ia = res%operation(j)%a - ib = res%operation(j)%b - op = res%operation(j)%op - IF(op>=0) then - IF(res%l_res(ia).and..not.res%l_res_do(ia)) then - call calc_res_array_init(res%res(ia)) - res%l_res_do(ia)=.true. - END if - IF(res%l_res(ib).and..not.res%l_res_do(ib)) then - call calc_res_array_init(res%res(ib)) - res%l_res_do(ib)=.true. - END if - END if -end do - -end subroutine calc_res_array_init - -recursive subroutine eval_res(res,exprin,int_op,list_var,l_root) -implicit none -TYPE(res_type) :: res -CHARACTER(LEN=*), INTENT(IN) :: exprin -INTEGER, OPTIONAL, INTENT(IN) :: int_op -CHARACTER(*), DIMENSION(:),INTENT(IN), OPTIONAL :: list_var -LOGICAL, OPTIONAL :: l_root - -CHARACTER(LEN=LEN(exprin)) :: expr_old -CHARACTER(LEN=LEN(exprin)+2) :: expr -INTEGER, parameter :: nop_tot=25 -INTEGER :: ln, i, j,ja -INTEGER :: jtot,jres,jop -INTEGER, DIMENSION(0:nop_tot) :: next_l,next_r,op,what -LOGICAL, ALLOCATABLE, DIMENSION(:) :: l_res - -IF(.not.PRESENT(l_root)) call del_res(res) - -res%a_res=.false. -res%a_l_res=.false. -res%a_l_res_do=.false. -res%a_operation=.false. - -op=0 -what=0 - -expr_old = TRIM(ADJUSTL(C_replace(exprin,' ',''))) -IF(expr_old(1:1)=='-') then - expr='0'//expr_old -else - expr='0+'//expr_old -END if -ln = LEN(TRIM(ADJUSTL(expr))) -next_r(0) = 0 -i = 1 -j = 1 -jres = 0 -do WHILE(i<ln+1) - IF(PRESENT(list_var)) then - call what_is_next(expr(i:ln),what(j),next_l(j),next_r(j),op(j),list_var) - else - call what_is_next(expr(i:ln),what(j),next_l(j),next_r(j),op(j)) - END if - next_r(j) = next_r(j)+next_r(j-1) - next_l(j) = next_r(j-1)+1+next_l(j) - i = next_r(j)+1 - IF(what(j)/=w_operator) jres=jres+1 - j = j+1 -end do -jtot = j-1 -IF(PRESENT(int_op)) then - ALLOCATE(res%res(0:jres),res%operation(jres),res%l_res(0:jres),res%l_res_do(0:jres)) - res%a_res=.true. - res%a_l_res=.true. - res%a_l_res_do=.true. - res%a_operation=.true. - res%res(0:jres)%a_res=.false. - res%res(0:jres)%a_l_res=.false. - res%res(0:jres)%a_l_res_do=.false. - res%res(0:jres)%a_operation=.false. - res%l_res(0)=.false. - res%l_res_do=.true. - res%res(0)%value=0. - res%nb_op=jres - res%operation(jres)%op=int_op - res%operation(jres)%a=1 - res%operation(jres)%b=0 - res%l_res(jres)=.false. - nullify(res%res(0)%res) - nullify(res%res(0)%l_res) - nullify(res%res(0)%l_res_do) - nullify(res%res(0)%operation) -else - ALLOCATE(res%res(jres),res%operation(jres-1),res%l_res(jres),res%l_res_do(jres)) - res%a_res=.true. - res%a_l_res=.true. - res%a_l_res_do=.true. - res%a_operation=.true. - res%res(1:jres)%a_res=.false. - res%res(1:jres)%a_l_res=.false. - res%res(1:jres)%a_l_res_do=.false. - res%res(1:jres)%a_operation=.false. - res%nb_op=jres-1 - res%l_res_do=.true. -END if -ALLOCATE(l_res(jres)) -l_res(:) = .true. - -res%res(:)%value=0. - -jres = 0 -do j=1, jtot - select case (what(j)) - case (w_number) - jres = jres+1 - ALLOCATE(res%res(jres)%res(1),res%res(jres)%l_res(1),res%res(jres)%l_res_do(1),res%res(jres)%operation(1)) - res%res(jres)%a_res=.true. - res%res(jres)%a_l_res=.true. - res%res(jres)%a_l_res_do=.true. - res%res(jres)%a_operation=.true. - res%res(jres)%res(1)%a_res=.false. - res%res(jres)%res(1)%a_l_res=.false. - res%res(jres)%res(1)%a_l_res_do=.false. - res%res(jres)%res(1)%a_operation=.false. - res%res(jres)%nb_op=1 - res%res(jres)%l_res(1) = .false. - res%res(jres)%l_res_do(1) = .true. - res%res(jres)%operation(1)%op = -100-op(j) - res%res(jres)%operation(1)%a = 1 - res%res(jres)%operation(1)%b = 0 - res%l_res(jres)=.true. - res%res(jres)%res(1)%value = char2real(expr(next_l(j):next_r(j))) - nullify(res%res(jres)%res(1)%res) - nullify(res%res(jres)%res(1)%l_res) - nullify(res%res(jres)%res(1)%l_res_do) - nullify(res%res(jres)%res(1)%operation) - case (w_parenthesis) - jres = jres+1 - IF(PRESENT(list_var)) then - call eval_res(res%res(jres),expr(next_l(j)+1:next_r(j)-1),list_var=list_var,l_root=.false.) - else - call eval_res(res%res(jres),expr(next_l(j)+1:next_r(j)-1),l_root=.false.) - endif - res%l_res(jres)=.true. - case (w_intrinsic) - jres = jres+1 - IF(PRESENT(list_var)) then - call eval_res(res%res(jres),expr(next_l(j)+1:next_r(j)-1),op(j),list_var,l_root=.false.) - else - call eval_res(res%res(jres),expr(next_l(j)+1:next_r(j)-1),op(j),l_root=.false.) - END if - res%l_res(jres)=.true. - case (w_variable) - jres = jres+1 - ALLOCATE(res%res(jres)%res(1),res%res(jres)%l_res(1),res%res(jres)%l_res_do(1),res%res(jres)%operation(1)) - res%res(jres)%a_res=.true. - res%res(jres)%a_l_res=.true. - res%res(jres)%a_l_res_do=.true. - res%res(jres)%a_operation=.true. - res%res(jres)%res(1)%a_res=.false. - res%res(jres)%res(1)%a_l_res=.false. - res%res(jres)%res(1)%a_l_res_do=.false. - res%res(jres)%res(1)%a_operation=.false. - res%res(jres)%nb_op=1 - res%res(jres)%l_res(1) = .false. - res%res(jres)%l_res_do(1) = .true. - res%res(jres)%operation(1)%op = -op(j) - res%res(jres)%operation(1)%a = 1 - res%res(jres)%operation(1)%b = 0 - res%l_res(jres)=.true. - nullify(res%res(jres)%res(1)%res) - nullify(res%res(jres)%res(1)%l_res) - nullify(res%res(jres)%res(1)%l_res_do) - nullify(res%res(jres)%res(1)%operation) - case (w_operator) - case default - end select -end do -jop = 1 -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==power) then - res%operation(jop)%op = power - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END IF - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==divide) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==multiply) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==plus.or.op(j)==minus) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==greaterthan.or.op(j)==lessthan) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do - -DEALLOCATE(l_res) - -return -end subroutine eval_res - -recursive subroutine eval_res_array(res,exprin,int_op,list_var,l_root) -implicit none -TYPE(res_type_array) :: res -CHARACTER(LEN=*), INTENT(IN) :: exprin -INTEGER, OPTIONAL, INTENT(IN) :: int_op -CHARACTER(*), DIMENSION(:),INTENT(IN), OPTIONAL :: list_var -LOGICAL, OPTIONAL :: l_root - -CHARACTER(LEN=LEN(exprin)) :: expr_old -CHARACTER(LEN=LEN(exprin)+2) :: expr -INTEGER, parameter :: nop_tot=25 -INTEGER :: ln, i, j,ja -INTEGER :: jtot,jres,jop -INTEGER, DIMENSION(0:nop_tot) :: next_l,next_r,op,what -LOGICAL, ALLOCATABLE, DIMENSION(:) :: l_res - -IF(.not.PRESENT(l_root)) call del_res_array(res) - -res%a_res=.false. -res%a_l_res=.false. -res%a_l_res_do=.false. -res%a_operation=.false. - -op=0 -what=0 - -expr_old = TRIM(ADJUSTL(C_replace(exprin,' ',''))) -IF(expr_old(1:1)=='-') then - expr='0'//expr_old -else - expr='0+'//expr_old -END if -ln = LEN(TRIM(ADJUSTL(expr))) -next_r(0) = 0 -i = 1 -j = 1 -jres = 0 -do WHILE(i<ln+1) - IF(PRESENT(list_var)) then - call what_is_next(expr(i:ln),what(j),next_l(j),next_r(j),op(j),list_var) - else - call what_is_next(expr(i:ln),what(j),next_l(j),next_r(j),op(j)) - END if - next_r(j) = next_r(j)+next_r(j-1) - next_l(j) = next_r(j-1)+1+next_l(j) - i = next_r(j)+1 - IF(what(j)/=w_operator) jres=jres+1 - j = j+1 -end do -jtot = j-1 -IF(PRESENT(int_op)) then - ALLOCATE(res%res(0:jres),res%operation(jres),res%l_res(0:jres),res%l_res_do(0:jres)) - res%a_res=.true. - res%a_l_res=.true. - res%a_l_res_do=.true. - res%a_operation=.true. - res%res(0:jres)%a_res=.false. - res%res(0:jres)%a_l_res=.false. - res%res(0:jres)%a_l_res_do=.false. - res%res(0:jres)%a_operation=.false. - res%l_res(0)=.false. - res%l_res_do=.true. - nullify(res%res(0)%value) - res%nb_op=jres - res%operation(jres)%op=int_op - res%operation(jres)%a=1 - res%operation(jres)%b=0 - res%l_res(jres)=.false. - nullify(res%res(0)%res) - nullify(res%res(0)%l_res) - nullify(res%res(0)%l_res_do) - nullify(res%res(0)%operation) -else - ALLOCATE(res%res(jres),res%operation(jres-1),res%l_res(jres),res%l_res_do(jres)) - res%a_res=.true. - res%a_l_res=.true. - res%a_l_res_do=.true. - res%a_operation=.true. - res%res(1:jres)%a_res=.false. - res%res(1:jres)%a_l_res=.false. - res%res(1:jres)%a_l_res_do=.false. - res%res(1:jres)%a_operation=.false. - res%nb_op=jres-1 - res%l_res_do=.true. -END if -ALLOCATE(l_res(jres)) -l_res(:) = .true. - -jres = 0 -do j=1, jtot - select case (what(j)) - case (w_number) - jres = jres+1 - ALLOCATE(res%res(jres)%res(1),res%res(jres)%l_res(1),res%res(jres)%l_res_do(1),res%res(jres)%operation(1)) - res%res(jres)%a_res=.true. - res%res(jres)%a_l_res=.true. - res%res(jres)%a_l_res_do=.true. - res%res(jres)%a_operation=.true. - res%res(jres)%res(1)%a_res=.false. - res%res(jres)%res(1)%a_l_res=.false. - res%res(jres)%res(1)%a_l_res_do=.false. - res%res(jres)%res(1)%a_operation=.false. - res%res(jres)%nb_op=1 - res%res(jres)%l_res(1) = .false. - res%res(jres)%l_res_do(1) = .true. - res%res(jres)%operation(1)%op = -100-op(j) - res%res(jres)%operation(1)%a = 1 - res%res(jres)%operation(1)%b = 0 - res%l_res(jres)=.true. - allocate(res%res(jres)%res(1)%value(1)) - res%res(jres)%res(1)%value = char2real(expr(next_l(j):next_r(j))) - nullify(res%res(jres)%res(1)%res) - nullify(res%res(jres)%res(1)%l_res) - nullify(res%res(jres)%res(1)%l_res_do) - nullify(res%res(jres)%res(1)%operation) - case (w_parenthesis) - jres = jres+1 - IF(PRESENT(list_var)) then - call eval_res_array(res%res(jres),expr(next_l(j)+1:next_r(j)-1),list_var=list_var,l_root=.false.) - else - call eval_res_array(res%res(jres),expr(next_l(j)+1:next_r(j)-1),l_root=.false.) - endif - res%l_res(jres)=.true. - case (w_intrinsic) - jres = jres+1 - IF(PRESENT(list_var)) then - call eval_res_array(res%res(jres),expr(next_l(j)+1:next_r(j)-1),op(j),list_var,l_root=.false.) - else - call eval_res_array(res%res(jres),expr(next_l(j)+1:next_r(j)-1),op(j),l_root=.false.) - END if - res%l_res(jres)=.true. - case (w_variable) - jres = jres+1 - ALLOCATE(res%res(jres)%res(1),res%res(jres)%l_res(1),res%res(jres)%l_res_do(1),res%res(jres)%operation(1)) - res%res(jres)%a_res=.true. - res%res(jres)%a_l_res=.true. - res%res(jres)%a_l_res_do=.true. - res%res(jres)%a_operation=.true. - res%res(jres)%res(1)%a_res=.false. - res%res(jres)%res(1)%a_l_res=.false. - res%res(jres)%res(1)%a_l_res_do=.false. - res%res(jres)%res(1)%a_operation=.false. - res%res(jres)%nb_op=1 - res%res(jres)%l_res(1) = .false. - res%res(jres)%l_res_do(1) = .true. - res%res(jres)%operation(1)%op = -op(j) - res%res(jres)%operation(1)%a = 1 - res%res(jres)%operation(1)%b = 0 - res%l_res(jres)=.true. - nullify(res%res(jres)%res(1)%res) - nullify(res%res(jres)%res(1)%l_res) - nullify(res%res(jres)%res(1)%l_res_do) - nullify(res%res(jres)%res(1)%operation) - case (w_operator) - case default - end select -end do -jop = 1 -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==power) then - res%operation(jop)%op = power - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END IF - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==divide) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==multiply) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==plus.or.op(j)==minus) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do -do j=jtot,1,-1 - IF(what(j)==w_operator) then - IF(op(j)==greaterthan.or.op(j)==lessthan) then - res%operation(jop)%op = op(j) - ja=j/2 - do WHILE(.not.l_res(ja)) - ja=ja-1 - end do - res%operation(jop)%a = ja - res%operation(jop)%b = j/2+1 - l_res(j/2+1)=.false. - jop = jop+1 - END if - END if -end do - -DEALLOCATE(l_res) - -return -end subroutine eval_res_array - -subroutine what_is_next(exprin,what,next_l,next_r,op,list_var) -implicit none -CHARACTER(LEN=*), INTENT(IN) :: exprin -INTEGER, INTENT(OUT) :: what,next_l,next_r,op -CHARACTER(*), DIMENSION(:),INTENT(IN), optional :: list_var - -CHARACTER(LEN=LEN(exprin)) :: expr - -INTEGER :: ln, i, asc, istart,ln2,ierror,jv - -ln = LEN_TRIM(ADJUSTL(exprin)) -expr = TRIM(ADJUSTL(exprin)) -istart = LEN(exprin)-LEN(ADJUSTL(exprin)) -next_l = istart - -i = 1 -asc = IACHAR(expr(1:1)) -select case (asc) - ! parenthesis ( - case (40) - what = w_parenthesis - next_r = find_close_bracket(expr(i:ln)) - ! alphabet - case (65:90,97:122) - ln2 = ln-i+1 - op = find_intrinsic(expr(i:ln),ln2,ierror) - IF(ierror==0) then - what = w_intrinsic - IF( expr(i+ln2:i+ln2) .ne.'(' ) then - WRITE(*,*) 'Error in intrinsic ',exprin,': missing parenthesis.' - stop - END if - next_l = next_l+ln2 - next_r = ln2+find_close_bracket(expr(i+ln2:ln)) - ELSEIF(PRESENT(list_var)) then - what = w_variable - jv = 0 - op = 0 - do WHILE(jv+1<=SIZE(list_var)) - jv=jv+1 - IF(expr(i:i+ln2-1)==TRIM(list_var(jv)(:))) then - op=jv - END if - end do - IF(op==0) then - WRITE(*,*) 'Error: variable ',exprin(i:i+ln2-1),' not allowed.' - stop - END if - next_l = next_l+ln2 - next_r = ln2 - else - WRITE(*,*) 'Error: intrinsic ',exprin(i:i+ln2-1),' does not exists.' - stop - END if - ! number - case (48:57) - what = w_number - next_r = next_l+find_number_end(expr(i:ln)) - ! operator - case (42,43,45,47,60,62) - what = w_operator - op = find_operator(expr(i:i+1)) - IF(op==power) THEN - next_r = next_l+2 - else - next_r = next_l+1 - END if - case default -end select - -return -end subroutine what_is_next - -function find_close_bracket(a) -implicit none -INTEGER :: find_close_bracket -CHARACTER(*), INTENT(IN) :: a -INTEGER :: level, i, ln -level = 0 -ln=LEN(a) -do i = 1, ln - select case (a(i:i)) - case ('(') - level=level+1 - case (')') - level=level-1 - case default - end select - IF(level==0) exit -end do -IF(i>ln) then - WRITE(*,*) 'Error, no closing bracket in expression ',a - stop -END if - -find_close_bracket = i - -return -end function find_close_bracket - -function find_intrinsic(a,ln,ierror) -implicit none -INTEGER :: find_intrinsic -CHARACTER(*), INTENT(IN) :: a -CHARACTER(LEN=LEN(a)) :: ac -INTEGER, INTENT(IN OUT) :: ln -INTEGER, INTENT(OUT) :: ierror -INTEGER :: i - -ierror=0 -do i = 1, ln - select case (IACHAR(a(i:i))) - case (65:90,97:122) ! alphabet - case default - exit - end select -end do -IF(i>ln+1) then - WRITE(*,*) 'Error in expression ',a - stop -END if - -ln = i-1 -ac(1:ln) = a(1:ln) - -do i = 1, ln - IF(IACHAR(a(i:i))<97) ac(i:i)=ACHAR(IACHAR(a(i:i))+32) -END do - -select case (ac(1:ln)) - case ('sqrt') - find_intrinsic = square_root - case ('sin') - find_intrinsic = sine - case ('cos') - find_intrinsic = cosine - case ('tan') - find_intrinsic = tangent - case ('exp') - find_intrinsic = exponential - case ('log','ln') - find_intrinsic = logarithm - case ('log10') - find_intrinsic = logten - case ('asin') - find_intrinsic = arcsin - case ('acos') - find_intrinsic = arccos - case ('atan') - find_intrinsic = arctan - case ('sinh') - find_intrinsic = sinhyp - case ('cosh') - find_intrinsic = coshyp - case ('tanh') - find_intrinsic = tanhyp - case default - ierror=1 -end select -return -end function find_intrinsic - -function find_operator(a) -implicit none -INTEGER :: find_operator -CHARACTER(*), INTENT(IN) :: a - - select case (IACHAR(a(1:1))) - case (42) ! '*' - IF(IACHAR(a(2:2))==42) then - find_operator = power - else - find_operator = multiply - END if - case (43) ! '+' - find_operator = plus - case (45) ! '-' - find_operator = minus - case (47) ! '/' - find_operator = divide - case (60) ! '<' - find_operator = lessthan - case (62) ! '>' - find_operator = greaterthan - case default - WRITE(*,*) 'Error, operator ',a(1:1),' does not exist' - stop - end select - - return -end function find_operator - -function find_number_end(a) -implicit none -INTEGER :: find_number_end -CHARACTER(*), INTENT(IN) :: a -INTEGER :: i -LOGICAL :: l_point,l_minus,l_e - -l_point=.FALSE. -l_minus=.FALSE. -l_e=.FALSE. - -do i = 1, len(a) - select case (IACHAR(a(i:i))) - case (48:57) - l_minus = .true. - case (46) ! . - IF(l_point) exit - l_point = .true. - l_minus = .true. - case (45) ! - - IF(l_minus) exit - l_minus = .true. - case (69,101) ! e - IF(l_e) exit - l_e = .true. - l_minus = .false. - case default - exit - end select -end do - -find_number_end = i-1 - -return -end function find_number_end - -function char2real(a) -implicit none -REAL(amrex_real) :: char2real -CHARACTER(*), INTENT(IN) :: a - -READ(a(:),*) char2real - -return -end function char2real - -FUNCTION eval(a,b,op) -implicit none -REAL(amrex_real) :: eval -REAL(amrex_real), INTENT(IN) :: a, b -INTEGER, INTENT(IN) :: op - -select case (op) - case (plus) - eval=a+b - case (minus) - eval=a-b - case (multiply) - eval=a*b - case (divide) - eval=a/b - case (power) - eval=a**b - case (greaterthan) - eval=0. - if (a > b) eval=1. - case (lessthan) - eval=0. - if (a < b) eval=1. - case (square_root) - eval=SQRT(a) - case (exponential) - eval=EXP(a) - case (logarithm) - eval=LOG(a) - case (sine) - eval=SIN(a) - case (cosine) - eval=COS(a) - case (tangent) - eval=TAN(a) - case (logten) - eval=log10(a) - case (arcsin) - eval=asin(a) - case (arccos) - eval=acos(a) - case (arctan) - eval=atan(a) - case (sinhyp) - eval=sinh(a) - case (coshyp) - eval=cosh(a) - case (tanhyp) - eval=tanh(a) - case default - WRITE(*,*) 'Error in eval, wrong operator ',op - stop -end select - -return -END function eval - - -FUNCTION eval_array(ai,bi,op) -implicit none -REAL(amrex_real), dimension(:), pointer :: eval_array -REAL(amrex_real), INTENT(IN), dimension(:), pointer :: ai, bi -REAL(amrex_real), dimension(:), pointer :: a, b -INTEGER, INTENT(IN) :: op -integer::i1,i2,n - -i1 = size(ai) -i2 = size(bi) -n = maxval((/i1,i2/)) - -select case (op) - case (plus,minus,multiply,divide,power, greaterthan, lessthan) - - if (i1<n) then - allocate(a(n)) - a(:) = ai(1) - else - a => ai - end if - if (i2<n) then - allocate(b(n)) - b(:) = bi(1) - else - b => bi - end if - - case default - a => ai - -end select - -allocate(eval_array(n)) -eval_array=0. - -select case (op) - case (plus) - eval_array=a+b - case (minus) - eval_array=a-b - case (multiply) - eval_array=a*b - case (divide) - eval_array=a/b - case (power) - eval_array=a**b - case (square_root) - eval_array=SQRT(a) - case (exponential) - eval_array=EXP(a) - case (logarithm) - eval_array=LOG(a) - case (sine) - eval_array=SIN(a) - case (cosine) - eval_array=COS(a) - case (tangent) - eval_array=TAN(a) - case (logten) - eval_array=log10(a) - case (arcsin) - eval_array=asin(a) - case (arccos) - eval_array=acos(a) - case (arctan) - eval_array=atan(a) - case (sinhyp) - eval_array=sinh(a) - case (coshyp) - eval_array=cosh(a) - case (tanhyp) - eval_array=tanh(a) - case default - WRITE(*,*) 'Error in eval_array, wrong operator ',op - stop -end select - -return -END function eval_array - -function C_up2low(cline) -CHARACTER(*) :: cline -CHARACTER(LEN=LEN(cline)) :: C_up2low -INTEGER :: ln, i, j - ln = LEN(cline) - do i = 1, ln - j = IACHAR(cline(i:i)) - IF(j<91.and.j>64) then - C_up2low(i:i)=ACHAR(j+32) - else - C_up2low(i:i) = cline(i:i) - END if - END do -end function C_up2low - -RECURSIVE FUNCTION C_REPLACE(ST,R1,R2) RESULT(C_RES) -IMPLICIT NONE -CHARACTER(LEN=*), INTENT(IN) :: ST -CHARACTER(LEN=LEN(st)) :: c_res -CHARACTER(LEN=*), INTENT(IN) :: R1 -CHARACTER(LEN=*), INTENT(IN) :: R2 - -INTEGER :: i,ll - - c_res = ST - i=INDEX(TRIM(st),r1) - IF(i==0) return - ll = LEN(r2) - IF(ll>0) c_res(i:i+ll-1) = r2 - c_res(i+ll:) = c_replace(c_res(i+LEN(r1):),r1,r2) - - -RETURN -END FUNCTION C_REPLACE - -FUNCTION C_nboccur(ST,R1) -IMPLICIT NONE -CHARACTER(LEN=*), INTENT(IN) :: ST -integer :: c_nboccur -CHARACTER(LEN=*), INTENT(IN) :: R1 - -INTEGER :: i,is - - C_nboccur=0 - is=0 -10 i = INDEX(st(is+1:),r1) - IF(i/=0) then - C_nboccur = C_nboccur+1 - is=is+i+LEN(r1) - GO TO 10 - END if - -RETURN -END FUNCTION C_nboccur - -function stringlist(strin) RESULT(res) -CHARACTER(*), INTENT(IN) :: strin -INTEGER :: i1, nwords, i, i2 - -CHARACTER(LEN(strin)+1) :: str -CHARACTER(80), DIMENSION(:), pointer :: res - -str=ADJUSTL(strin)//' ' -i1=1 -nwords = 0 -do WHILE(TRIM(str(i1:))/='') - i2 = 1 - i2 = INDEX(str(i1:),' ') - do WHILE(i2==1) - i1 = i1 + 1 - i2 = INDEX(str(i1:),' ') - end do - i1=i1+i2 - nwords = nwords + 1 -end do -ALLOCATE(res(nwords)) -res(:) = '' -i1=1 -do i = 1, nwords - i2 = INDEX(str(i1:),' ') - do WHILE(i2==1) - i1 = i1 + 1 - i2 = INDEX(str(i1:),' ') - end do - res(i)(1:i2-1) = str(i1:i1+i2-1) - i1=i1+i2 -end do - -return -end function stringlist - -SUBROUTINE csv2list(strin, strinlen, lofstr) -CHARACTER(*), INTENT(IN) :: strin -INTEGER, INTENT(IN) :: strinlen -INTEGER :: isep, isep_rel, nwords, i, j -INTEGER, DIMENSION(strinlen) :: word_start, word_end -CHARACTER(strinlen) :: str -CHARACTER(len=10), DIMENSION(:), allocatable, INTENT(inout) :: lofstr -nwords = 1 -str = strin -isep_rel = INDEX(str, ',') -isep = isep_rel -word_start(nwords) = 1 -DO WHILE(isep_rel > 0) - word_end(nwords) = isep-1 - nwords = nwords + 1 - word_start(nwords) = isep+1 - str = strin(isep+1:strinlen) - isep_rel = INDEX(str, ',') - isep = isep_rel + isep -ENDDO -word_end(nwords) = strinlen - -ALLOCATE( lofstr(nwords) ) - -DO i=1, nwords - lofstr(i) = "" - DO j = 1, word_end(i)-word_start(i)+1 - lofstr(i)(j:j) = strin(word_start(i)+j-1:word_start(i)+j-1) - end DO -ENDDO -END SUBROUTINE csv2list -end module mod_interpret - -! --------------------- -! MODULE parser_wrapper -! Wrappers to use the parser defined in MODULE mod_interpret. -MODULE parser_wrapper -USE iso_c_binding -USE amrex_fort_module, only : amrex_real -USE amrex_error_module, only : amrex_error -USE mod_interpret, only : nb_res, table_of_res, calc_res, eval_res, csv2list - -IMPLICIT NONE - -CONTAINS - -! FUNCTION parser_initialize_function RESULT my_index_res -! Initialize a res_type and assign it a unique identifier. -! INPUTS: -!> instr_func : CHAR* for a mathematical expression -!> e.g. instr_func = "3*cos(x)+y". -!> instr_var : CHAR* for a comma-separated list of variables in instr_func -!> e.g. instr_var = "x,y". -! OUTPUT: -!> my_index_res : INT parser index. Necessary if this module is used for several -!> parsers (e.g. one for the electron density, one for the ion -!> density, one for the laser profile etc.). -FUNCTION parser_initialize_function(instr_func, length_func, instr_var, length_var) RESULT(my_index_res) & - bind(c,name='parser_initialize_function') - INTEGER(c_int), INTENT(IN), VALUE :: length_func, length_var - CHARACTER(kind=c_char), INTENT(IN) :: instr_func(length_func) - CHARACTER(kind=c_char), INTENT(IN) :: instr_var(length_var) - INTEGER :: i - CHARACTER(len=10), DIMENSION(:), allocatable :: lofstr - CHARACTER(len=length_func) :: str_func - CHARACTER(len=length_var) :: str_var - REAL(amrex_real), DIMENSION(1) :: list_var - INTEGER :: my_index_res - - nb_res = nb_res + 1 - my_index_res = nb_res - IF (nb_res>10) THEN - call amrex_error('Parser error: cannot have more than 10 parsers.') - ENDIF - - str_func = "" - DO i=1, length_func - str_func(i:i) = instr_func(i) - ENDDO - - str_var = "" - DO i=1, length_var - str_var(i:i) = instr_var(i) - ENDDO - - ! Convert variable list from csv string to list of strings. - CALL csv2list(str_var, len_trim(str_var), lofstr) - - ! Initialize the res object - CALL eval_res(table_of_res(my_index_res), str_func, list_var=lofstr) - -END FUNCTION parser_initialize_function - -! FUNCTION parser_evaluate_function RESULT out -! Evaluate parsed function -! INPUTS: -!> list_var : REAL* array of values for variables -!> e.g. list_var = (/3.14_8,2._8/). -!> nvar : INT number of variables -!> e.g. nvar = 2. -!> my_index_res : INT index of the res_type object in table table_of_res. -! OUTPUT: -!> out : REAL Result. -FUNCTION parser_evaluate_function(list_var, nvar, my_index_res) result(out) & - bind(c,name='parser_evaluate_function') - INTEGER, VALUE, INTENT(IN) :: nvar, my_index_res - REAL(amrex_real), INTENT(IN) :: list_var(1:nvar) - REAL(amrex_real) :: out - ! Evaluate parsed function in table_of_res(my_index_res), of type res_type - out = calc_res(table_of_res(my_index_res),list_var) - -END FUNCTION parser_evaluate_function - -END MODULE parser_wrapper diff --git a/Source/Laser/LaserParticleContainer.H b/Source/Laser/LaserParticleContainer.H index 77a52dc26..1620cd5d9 100644 --- a/Source/Laser/LaserParticleContainer.H +++ b/Source/Laser/LaserParticleContainer.H @@ -5,6 +5,7 @@ #include <WarpXParticleContainer.H> #include <WarpXConst.H> +#include <WarpXParser.H> enum class laser_t { Null, Gaussian, Harris, parse_field_function }; @@ -77,7 +78,7 @@ private: amrex::Real theta_stc = 0.; // parse_field_function profile - int parser_instance_number = 0; + WarpXParser parser; std::string field_function; // laser particle domain diff --git a/Source/Laser/LaserParticleContainer.cpp b/Source/Laser/LaserParticleContainer.cpp index 08d0ae861..20446bcc1 100644 --- a/Source/Laser/LaserParticleContainer.cpp +++ b/Source/Laser/LaserParticleContainer.cpp @@ -64,46 +64,41 @@ LaserParticleContainer::LaserParticleContainer (AmrCore* amr_core, int ispecies) pp.query("phi2", phi2); } - if ( profile == laser_t::Harris ) { - // Parse the properties of the Harris profile - pp.get("profile_waist", profile_waist); - pp.get("profile_duration", profile_duration); - pp.get("profile_focal_distance", profile_focal_distance); - } - - if ( profile == laser_t::parse_field_function ) { - // Parse the properties of the parse_field_function profile - pp.get("field_function(X,Y,t)", field_function); - // User-defined constants: replace names by value - my_constants.ReadParameters(); - field_function = my_constants.replaceStringValue(field_function); - // Pass math expression and list of variables to Fortran as char* - const std::string s_var = "X,Y,t"; - parser_instance_number = parser_initialize_function(field_function.c_str(), - field_function.length(), - s_var.c_str(), - s_var.length()); - } + if ( profile == laser_t::Harris ) { + // Parse the properties of the Harris profile + pp.get("profile_waist", profile_waist); + pp.get("profile_duration", profile_duration); + pp.get("profile_focal_distance", profile_focal_distance); + } + + if ( profile == laser_t::parse_field_function ) { + // Parse the properties of the parse_field_function profile + pp.get("field_function(X,Y,t)", field_function); + // User-defined constants: replace names by value + my_constants.ReadParameters(); + field_function = my_constants.replaceStringValue(field_function); + parser.define(field_function); + } // Plane normal Real s = 1.0/std::sqrt(nvec[0]*nvec[0] + nvec[1]*nvec[1] + nvec[2]*nvec[2]); nvec = { nvec[0]*s, nvec[1]*s, nvec[2]*s }; - if (WarpX::gamma_boost > 1.) { - // Check that the laser direction is equal to the boost direction - AMREX_ALWAYS_ASSERT_WITH_MESSAGE( - nvec[0]*WarpX::boost_direction[0] - + nvec[1]*WarpX::boost_direction[1] - + nvec[2]*WarpX::boost_direction[2] - 1. < 1.e-12, - "The Lorentz boost should be in the same direction as the laser propagation"); - // Get the position of the plane, along the boost direction, in the lab frame - // and convert the position of the antenna to the boosted frame - Z0_lab = nvec[0]*position[0] + nvec[1]*position[1] + nvec[2]*position[2]; - Real Z0_boost = Z0_lab/WarpX::gamma_boost; - position[0] += (Z0_boost-Z0_lab)*nvec[0]; - position[1] += (Z0_boost-Z0_lab)*nvec[1]; - position[2] += (Z0_boost-Z0_lab)*nvec[2]; - } + if (WarpX::gamma_boost > 1.) { + // Check that the laser direction is equal to the boost direction + AMREX_ALWAYS_ASSERT_WITH_MESSAGE( + nvec[0]*WarpX::boost_direction[0] + + nvec[1]*WarpX::boost_direction[1] + + nvec[2]*WarpX::boost_direction[2] - 1. < 1.e-12, + "The Lorentz boost should be in the same direction as the laser propagation"); + // Get the position of the plane, along the boost direction, in the lab frame + // and convert the position of the antenna to the boosted frame + Z0_lab = nvec[0]*position[0] + nvec[1]*position[1] + nvec[2]*position[2]; + Real Z0_boost = Z0_lab/WarpX::gamma_boost; + position[0] += (Z0_boost-Z0_lab)*nvec[0]; + position[1] += (Z0_boost-Z0_lab)*nvec[1]; + position[2] += (Z0_boost-Z0_lab)*nvec[2]; + } // The first polarization vector s = 1.0/std::sqrt(p_X[0]*p_X[0] + p_X[1]*p_X[1] + p_X[2]*p_X[2]); @@ -398,8 +393,15 @@ LaserParticleContainer::Evolve (int lev, } if (profile == laser_t::parse_field_function) { - parse_function_laser( &np, plane_Xp.dataPtr(), plane_Yp.dataPtr(), &t, - amplitude_E.dataPtr(), parser_instance_number ); + Real parser_X, parser_Y, parser_t; + parser.registerVariable("X", parser_X); + parser.registerVariable("Y", parser_Y); + parser.registerVariable("t", t); + for (int i = 0; i < np; ++i) { + parser_X = plane_Xp[i]; + parser_Y = plane_Yp[i]; + amplitude_E[i] = parser.eval(); + } } // Calculate the corresponding momentum and position for the particles update_laser_particle( diff --git a/Source/Laser/WarpX_laser.F90 b/Source/Laser/WarpX_laser.F90 index b08413019..e7a2736e1 100644 --- a/Source/Laser/WarpX_laser.F90 +++ b/Source/Laser/WarpX_laser.F90 @@ -11,7 +11,6 @@ module warpx_laser_module use iso_c_binding use amrex_fort_module, only : amrex_real use constants, only : clight, pi - use parser_wrapper, only : parser_evaluate_function implicit none @@ -186,24 +185,6 @@ contains end subroutine warpx_harris_laser #endif - ! Parse function from the input script for the laser temporal profile - subroutine parse_function_laser( np, Xp, Yp, t, amplitude, parser_instance_number ) bind(C, name="parse_function_laser") - integer(c_long), intent(in) :: np - real(amrex_real), intent(in) :: Xp(np),Yp(np) - real(amrex_real), intent(in) :: t - real(amrex_real), intent(inout) :: amplitude(np) - INTEGER, value, INTENT(IN) :: parser_instance_number - integer(c_long) :: i - INTEGER, PARAMETER :: nvar_parser = 3 - REAL(amrex_real) :: list_var(1:nvar_parser) - ! Loop through the macroparticle to calculate the proper amplitude - do i = 1, np - list_var = [Xp(i), Yp(i), t] - amplitude(i) = parser_evaluate_function(list_var, nvar_parser, parser_instance_number) - enddo - end subroutine parse_function_laser - - subroutine calculate_laser_plane_coordinates(np, xp, yp, zp, & plane_Xp, plane_Yp, u_Xx, u_Xy, u_Xz, u_Yx, u_Yy, u_Yz, & positionx, positiony, positionz ) & diff --git a/Source/Make.WarpX b/Source/Make.WarpX index bd5345f91..26eabcad3 100644 --- a/Source/Make.WarpX +++ b/Source/Make.WarpX @@ -40,6 +40,7 @@ include $(WARPX_HOME)/Source/FortranInterface/Make.package include $(WARPX_HOME)/Source/Initialization/Make.package include $(WARPX_HOME)/Source/Laser/Make.package include $(WARPX_HOME)/Source/Parallelization/Make.package +include $(WARPX_HOME)/Source/Parser/Make.package include $(WARPX_HOME)/Source/Particles/Make.package include $(WARPX_HOME)/Source/Python/Make.package include $(WARPX_HOME)/Source/Utils/Make.package diff --git a/Source/Parser/GNUmakefile b/Source/Parser/GNUmakefile new file mode 100644 index 000000000..dfc70ff23 --- /dev/null +++ b/Source/Parser/GNUmakefile @@ -0,0 +1,4 @@ + +default: + bison -d wp_parser.y + flex -o wp_parser.lex.c --header-file=wp_parser.lex.h wp_parser.l diff --git a/Source/Parser/Make.package b/Source/Parser/Make.package new file mode 100644 index 000000000..26ef4fb43 --- /dev/null +++ b/Source/Parser/Make.package @@ -0,0 +1,9 @@ + +cEXE_sources += wp_parser_y.c wp_parser.tab.c wp_parser.lex.c wp_parser_c.c +cEXE_headers += wp_parser_y.h wp_parser.tab.h wp_parser.lex.h wp_parser_c.h +CEXE_sources += WarpXParser.cpp +CEXE_headers += WarpXParser.H + +INCLUDE_LOCATIONS += $(WARPX_HOME)/Source/Parser +VPATH_LOCATIONS += $(WARPX_HOME)/Source/Parser + diff --git a/Source/Parser/WarpXParser.H b/Source/Parser/WarpXParser.H new file mode 100644 index 000000000..676a56615 --- /dev/null +++ b/Source/Parser/WarpXParser.H @@ -0,0 +1,76 @@ +#ifndef WARPX_PARSER_H_ +#define WARPX_PARSER_H_ + +#include <vector> +#include <string> +#include <cstdarg> + +#include "wp_parser_c.h" +#include "wp_parser_y.h" + +#ifdef _OPENMP +#include <omp.h> +#endif + + +class WarpXParser +{ +private: + std::string m_expression; + std::vector<struct wp_parser*> m_parser; + mutable std::vector<std::vector<double> > m_variables; + +public: + WarpXParser (std::string const& func_body); + WarpXParser () = default; + ~WarpXParser (); + void define (std::string const& func_body); + + void setConstant (std::string const& name, double c); + + // + // Option 1: Register every variable to an address provided. + // Assign values to external variables. + // Call eval(). + void registerVariable (std::string const& name, double& var); + // + inline double eval () const noexcept { +#ifdef _OPENMP + const int tid = omp_get_thread_num(); +#else + const int tid = 0; +#endif + return wp_ast_eval(m_parser[tid]->ast); + } + + // + // Option 2: Register all variables at once. Parser will create + // variables internally. + // Call eval(...) with variable values. + void registerVariables (std::vector<std::string> const& names); + // + inline double eval (double xyz...) const noexcept { +#ifdef _OPENMP + const int tid = omp_get_thread_num(); +#else + const int tid = 0; +#endif + va_list args; + va_start(args, xyz); + for (auto& v : m_variables[tid]) { + v = va_arg(args, double); + } + va_end(args); + + return wp_ast_eval(m_parser[tid]->ast); + } + + void print () const; + + std::string const& expr () const; + +private: + void clear (); +}; + +#endif diff --git a/Source/Parser/WarpXParser.cpp b/Source/Parser/WarpXParser.cpp new file mode 100644 index 000000000..3a868838a --- /dev/null +++ b/Source/Parser/WarpXParser.cpp @@ -0,0 +1,126 @@ + +#include "WarpXParser.H" + +WarpXParser::WarpXParser (std::string const& func_body) +{ + define(func_body); +} + +void +WarpXParser::define (std::string const& func_body) +{ + clear(); + + m_expression = func_body; + +#ifdef _OPENMP + int nthreads = omp_get_max_threads(); +#else + int nthreads = 1; +#endif + m_parser.resize(nthreads); + + std::string f = m_expression + "\n"; + m_parser[0] = wp_c_parser_new(f.c_str()); + +#ifdef _OPENMP +#pragma omp parallel + { + int tid = omp_get_thread_num(); + if (tid > 0) { + m_parser[tid] = wp_parser_dup(m_parser[0]); + } + } +#endif +} + +WarpXParser::~WarpXParser () +{ + clear(); +} + +void +WarpXParser::clear () +{ + if (!m_parser.empty()) + { +#ifdef _OPENMP +#pragma omp parallel + { + int tid = omp_get_thread_num(); + wp_parser_delete(m_parser[tid]); + } +#else + wp_parser_delete(m_parser[0]); +#endif + } + m_expression.clear(); + m_parser.clear(); + m_variables.clear(); +} + +void +WarpXParser::registerVariable (std::string const& name, double& var) +{ + // We assume this is called inside OMP parallel region +#ifdef _OPENMP + const int tid = omp_get_thread_num(); +#else + const int tid = 0; +#endif + wp_parser_regvar(m_parser[tid], name.c_str(), &var); +} + +// This must be called outside OpenMP parallel region. +void +WarpXParser::registerVariables (std::vector<std::string> const& names) +{ +#ifdef _OPENMP + const int nthreads = omp_get_max_threads(); +#else + const int nthreads = 1; +#endif + m_variables.resize(nthreads); + const int nnames = names.size(); +#ifdef _OPENMP +#pragma omp parallel for +#endif + for (int i = 0; i < nthreads; ++i) { + m_variables[i].resize(nnames); + for (int j = 0; j < nnames; ++i) { + wp_parser_regvar(m_parser[i], names[j].c_str(), &(m_variables[i][j])); + } + } +} + +void +WarpXParser::setConstant (std::string const& name, double c) +{ + // We don't know if this is inside OMP parallel region or not +#ifdef _OPENMP + bool in_parallel = omp_in_parallel(); +#pragma omp parallel if (!in_parallel) + { + wp_parser_setconst(m_parser[omp_get_thread_num()], name.c_str(), c); + } +#else + wp_parser_setconst(m_parser[0], name.c_str(), c); +#endif +} + +void +WarpXParser::print () const +{ +#ifdef _OPENMP +#pragma omp critical(warpx_parser_pint) + wp_ast_print(m_parser[omp_get_thread_num()]->ast); +#else + wp_ast_print(m_parser[0]->ast); +#endif +} + +std::string const& +WarpXParser::expr () const +{ + return m_expression; +} diff --git a/Source/Parser/wp_parser.l b/Source/Parser/wp_parser.l new file mode 100644 index 000000000..e8c99200d --- /dev/null +++ b/Source/Parser/wp_parser.l @@ -0,0 +1,68 @@ +%option noyywrap nodefault +%{ +#include "wp_parser_y.h" +#include "wp_parser.tab.h" +%} + + /* Tokens NUMBER, SYMBOL, F1, POW, F2, etc. are defined in wp_parser.y. */ + /* Types WP_SQRT, WP_SQRT, etc. are defined in wp_parser_y.h. */ + + /* Used leater to define NUMBER */ +EXP ([Ee][-+]?[0-9]+) + +%% + +"+" | +"-" | +"*" | +"/" | +"=" | +"|" | +"," | +"<" | +">" | +"(" | +")" { return yytext[0]; } /* simply pass through */ + + /* yylval is union type defined in wp_parser.tab.h that is generated + * by bison with wp_parser.y */ + +"sqrt" { yylval.f1 = WP_SQRT; return F1; } +"exp" { yylval.f1 = WP_EXP; return F1; } +"log" { yylval.f1 = WP_LOG; return F1; } +"log10" { yylval.f1 = WP_LOG10; return F1; } +"sin" { yylval.f1 = WP_SIN; return F1; } +"cos" { yylval.f1 = WP_COS; return F1; } +"tan" { yylval.f1 = WP_TAN; return F1; } +"asin" { yylval.f1 = WP_ASIN; return F1; } +"acos" { yylval.f1 = WP_ACOS; return F1; } +"atan" { yylval.f1 = WP_ATAN; return F1; } +"sinh" { yylval.f1 = WP_SINH; return F1; } +"cosh" { yylval.f1 = WP_COSH; return F1; } +"tanh" { yylval.f1 = WP_TANH; return F1; } +"abs" { yylval.f1 = WP_ABS; return F1; } +"fabs" { yylval.f1 = WP_ABS; return F1; } +"**" { yylval.f2 = WP_POW; return POW;} +"^" { yylval.f2 = WP_POW; return POW;} +"pow" { yylval.f2 = WP_POW; return F2; } +"heaviside" { yylval.f2 = WP_HEAVISIDE; return F2; } +"min" { yylval.f2 = WP_MIN; return F2; } +"max" { yylval.f2 = WP_MAX; return F2; } + + /* We use SYMBOL to hold variables and constants */ +[a-zA-Z][a-zA-Z0-9]* { yylval.s = wp_makesymbol(yytext); return SYMBOL; } + + /* Number */ +[0-9]+"."[0-9]*{EXP}? | +"."?[0-9]+{EXP}? { yylval.d = atof(yytext); return NUMBER; } + + /* Special characters */ +"//".* +[ \t] /* ignore white space */ +\\\n /* ignore line continuation */ +"\n" { return EOL; } + + /* everything else */ +. { yyerror("Unknow character %c\n", *yytext); } + +%% diff --git a/Source/Parser/wp_parser.lex.c b/Source/Parser/wp_parser.lex.c new file mode 100644 index 000000000..aca863a75 --- /dev/null +++ b/Source/Parser/wp_parser.lex.c @@ -0,0 +1,2012 @@ +#line 2 "wp_parser.lex.c" + +#line 4 "wp_parser.lex.c" + +#define YY_INT_ALIGNED short int + +/* A lexical scanner generated by flex */ + +#define FLEX_SCANNER +#define YY_FLEX_MAJOR_VERSION 2 +#define YY_FLEX_MINOR_VERSION 6 +#define YY_FLEX_SUBMINOR_VERSION 0 +#if YY_FLEX_SUBMINOR_VERSION > 0 +#define FLEX_BETA +#endif + +/* First, we deal with platform-specific or compiler-specific issues. */ + +/* begin standard C headers. */ +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> + +/* end standard C headers. */ + +/* flex integer type definitions */ + +#ifndef FLEXINT_H +#define FLEXINT_H + +/* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */ + +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + +/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, + * if you want the limit (max/min) macros for int types. + */ +#ifndef __STDC_LIMIT_MACROS +#define __STDC_LIMIT_MACROS 1 +#endif + +#include <inttypes.h> +typedef int8_t flex_int8_t; +typedef uint8_t flex_uint8_t; +typedef int16_t flex_int16_t; +typedef uint16_t flex_uint16_t; +typedef int32_t flex_int32_t; +typedef uint32_t flex_uint32_t; +#else +typedef signed char flex_int8_t; +typedef short int flex_int16_t; +typedef int flex_int32_t; +typedef unsigned char flex_uint8_t; +typedef unsigned short int flex_uint16_t; +typedef unsigned int flex_uint32_t; + +/* Limits of integral types. */ +#ifndef INT8_MIN +#define INT8_MIN (-128) +#endif +#ifndef INT16_MIN +#define INT16_MIN (-32767-1) +#endif +#ifndef INT32_MIN +#define INT32_MIN (-2147483647-1) +#endif +#ifndef INT8_MAX +#define INT8_MAX (127) +#endif +#ifndef INT16_MAX +#define INT16_MAX (32767) +#endif +#ifndef INT32_MAX +#define INT32_MAX (2147483647) +#endif +#ifndef UINT8_MAX +#define UINT8_MAX (255U) +#endif +#ifndef UINT16_MAX +#define UINT16_MAX (65535U) +#endif +#ifndef UINT32_MAX +#define UINT32_MAX (4294967295U) +#endif + +#endif /* ! C99 */ + +#endif /* ! FLEXINT_H */ + +#ifdef __cplusplus + +/* The "const" storage-class-modifier is valid. */ +#define YY_USE_CONST + +#else /* ! __cplusplus */ + +/* C99 requires __STDC__ to be defined as 1. */ +#if defined (__STDC__) + +#define YY_USE_CONST + +#endif /* defined (__STDC__) */ +#endif /* ! __cplusplus */ + +#ifdef YY_USE_CONST +#define yyconst const +#else +#define yyconst +#endif + +/* Returned upon end-of-file. */ +#define YY_NULL 0 + +/* Promotes a possibly negative, possibly signed char to an unsigned + * integer for use as an array index. If the signed char is negative, + * we want to instead treat it as an 8-bit unsigned char, hence the + * double cast. + */ +#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) + +/* Enter a start condition. This macro really ought to take a parameter, + * but we do it the disgusting crufty way forced on us by the ()-less + * definition of BEGIN. + */ +#define BEGIN (yy_start) = 1 + 2 * + +/* Translate the current start state into a value that can be later handed + * to BEGIN to return to the state. The YYSTATE alias is for lex + * compatibility. + */ +#define YY_START (((yy_start) - 1) / 2) +#define YYSTATE YY_START + +/* Action number for EOF rule of a given start state. */ +#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) + +/* Special action meaning "start processing a new file". */ +#define YY_NEW_FILE yyrestart(yyin ) + +#define YY_END_OF_BUFFER_CHAR 0 + +/* Size of default input buffer. */ +#ifndef YY_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k. + * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case. + * Ditto for the __ia64__ case accordingly. + */ +#define YY_BUF_SIZE 32768 +#else +#define YY_BUF_SIZE 16384 +#endif /* __ia64__ */ +#endif + +/* The state buf must be large enough to hold one state per character in the main buffer. + */ +#define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type)) + +#ifndef YY_TYPEDEF_YY_BUFFER_STATE +#define YY_TYPEDEF_YY_BUFFER_STATE +typedef struct yy_buffer_state *YY_BUFFER_STATE; +#endif + +#ifndef YY_TYPEDEF_YY_SIZE_T +#define YY_TYPEDEF_YY_SIZE_T +typedef size_t yy_size_t; +#endif + +extern yy_size_t yyleng; + +extern FILE *yyin, *yyout; + +#define EOB_ACT_CONTINUE_SCAN 0 +#define EOB_ACT_END_OF_FILE 1 +#define EOB_ACT_LAST_MATCH 2 + + #define YY_LESS_LINENO(n) + #define YY_LINENO_REWIND_TO(ptr) + +/* Return all but the first "n" matched characters back to the input stream. */ +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + *yy_cp = (yy_hold_char); \ + YY_RESTORE_YY_MORE_OFFSET \ + (yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ + YY_DO_BEFORE_ACTION; /* set up yytext again */ \ + } \ + while ( 0 ) + +#define unput(c) yyunput( c, (yytext_ptr) ) + +#ifndef YY_STRUCT_YY_BUFFER_STATE +#define YY_STRUCT_YY_BUFFER_STATE +struct yy_buffer_state + { + FILE *yy_input_file; + + char *yy_ch_buf; /* input buffer */ + char *yy_buf_pos; /* current position in input buffer */ + + /* Size of input buffer in bytes, not including room for EOB + * characters. + */ + yy_size_t yy_buf_size; + + /* Number of characters read into yy_ch_buf, not including EOB + * characters. + */ + int yy_n_chars; + + /* Whether we "own" the buffer - i.e., we know we created it, + * and can realloc() it to grow it, and should free() it to + * delete it. + */ + int yy_is_our_buffer; + + /* Whether this is an "interactive" input source; if so, and + * if we're using stdio for input, then we want to use getc() + * instead of fread(), to make sure we stop fetching input after + * each newline. + */ + int yy_is_interactive; + + /* Whether we're considered to be at the beginning of a line. + * If so, '^' rules will be active on the next match, otherwise + * not. + */ + int yy_at_bol; + + int yy_bs_lineno; /**< The line count. */ + int yy_bs_column; /**< The column count. */ + + /* Whether to try to fill the input buffer when we reach the + * end of it. + */ + int yy_fill_buffer; + + int yy_buffer_status; + +#define YY_BUFFER_NEW 0 +#define YY_BUFFER_NORMAL 1 + /* When an EOF's been seen but there's still some text to process + * then we mark the buffer as YY_EOF_PENDING, to indicate that we + * shouldn't try reading from the input source any more. We might + * still have a bunch of tokens to match, though, because of + * possible backing-up. + * + * When we actually see the EOF, we change the status to "new" + * (via yyrestart()), so that the user can continue scanning by + * just pointing yyin at a new input file. + */ +#define YY_BUFFER_EOF_PENDING 2 + + }; +#endif /* !YY_STRUCT_YY_BUFFER_STATE */ + +/* Stack of input buffers. */ +static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */ +static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */ +static YY_BUFFER_STATE * yy_buffer_stack = 0; /**< Stack as an array. */ + +/* We provide macros for accessing buffer states in case in the + * future we want to put the buffer states in a more general + * "scanner state". + * + * Returns the top of the stack, or NULL. + */ +#define YY_CURRENT_BUFFER ( (yy_buffer_stack) \ + ? (yy_buffer_stack)[(yy_buffer_stack_top)] \ + : NULL) + +/* Same as previous macro, but useful when we know that the buffer stack is not + * NULL or when we need an lvalue. For internal use only. + */ +#define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)] + +/* yy_hold_char holds the character lost when yytext is formed. */ +static char yy_hold_char; +static int yy_n_chars; /* number of characters read into yy_ch_buf */ +yy_size_t yyleng; + +/* Points to current character in buffer. */ +static char *yy_c_buf_p = (char *) 0; +static int yy_init = 0; /* whether we need to initialize */ +static int yy_start = 0; /* start state number */ + +/* Flag which is used to allow yywrap()'s to do buffer switches + * instead of setting up a fresh yyin. A bit of a hack ... + */ +static int yy_did_buffer_switch_on_eof; + +void yyrestart (FILE *input_file ); +void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ); +YY_BUFFER_STATE yy_create_buffer (FILE *file,int size ); +void yy_delete_buffer (YY_BUFFER_STATE b ); +void yy_flush_buffer (YY_BUFFER_STATE b ); +void yypush_buffer_state (YY_BUFFER_STATE new_buffer ); +void yypop_buffer_state (void ); + +static void yyensure_buffer_stack (void ); +static void yy_load_buffer_state (void ); +static void yy_init_buffer (YY_BUFFER_STATE b,FILE *file ); + +#define YY_FLUSH_BUFFER yy_flush_buffer(YY_CURRENT_BUFFER ) + +YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ); +YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ); +YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,yy_size_t len ); + +void *yyalloc (yy_size_t ); +void *yyrealloc (void *,yy_size_t ); +void yyfree (void * ); + +#define yy_new_buffer yy_create_buffer + +#define yy_set_interactive(is_interactive) \ + { \ + if ( ! YY_CURRENT_BUFFER ){ \ + yyensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + yy_create_buffer(yyin,YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ + } + +#define yy_set_bol(at_bol) \ + { \ + if ( ! YY_CURRENT_BUFFER ){\ + yyensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + yy_create_buffer(yyin,YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ + } + +#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) + +/* Begin user sect3 */ + +#define yywrap() (/*CONSTCOND*/1) +#define YY_SKIP_YYWRAP + +typedef unsigned char YY_CHAR; + +FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0; + +typedef int yy_state_type; + +extern int yylineno; + +int yylineno = 1; + +extern char *yytext; +#ifdef yytext_ptr +#undef yytext_ptr +#endif +#define yytext_ptr yytext + +static yy_state_type yy_get_previous_state (void ); +static yy_state_type yy_try_NUL_trans (yy_state_type current_state ); +static int yy_get_next_buffer (void ); +#if defined(__GNUC__) && __GNUC__ >= 3 +__attribute__((__noreturn__)) +#endif +static void yy_fatal_error (yyconst char msg[] ); + +/* Done after the current pattern has been matched and before the + * corresponding action - sets up yytext. + */ +#define YY_DO_BEFORE_ACTION \ + (yytext_ptr) = yy_bp; \ + yyleng = (size_t) (yy_cp - yy_bp); \ + (yy_hold_char) = *yy_cp; \ + *yy_cp = '\0'; \ + (yy_c_buf_p) = yy_cp; + +#define YY_NUM_RULES 41 +#define YY_END_OF_BUFFER 42 +/* This struct is not used in this scanner, + but its presence is necessary. */ +struct yy_trans_info + { + flex_int32_t yy_verify; + flex_int32_t yy_nxt; + }; +static yyconst flex_int16_t yy_accept[95] = + { 0, + 0, 0, 42, 40, 37, 39, 10, 11, 3, 1, + 7, 2, 40, 4, 35, 8, 5, 9, 33, 40, + 28, 33, 33, 33, 33, 33, 33, 33, 33, 33, + 33, 6, 27, 35, 36, 34, 35, 0, 33, 38, + 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, + 33, 33, 33, 33, 33, 36, 34, 0, 0, 35, + 25, 33, 33, 33, 17, 13, 33, 33, 14, 32, + 31, 29, 16, 33, 18, 0, 34, 20, 19, 21, + 23, 26, 33, 33, 22, 12, 24, 33, 15, 33, + 33, 33, 30, 0 + + } ; + +static yyconst YY_CHAR yy_ec[256] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 2, 1, 1, 1, 1, 1, 1, 1, 4, + 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 14, 14, 14, 14, 14, 14, 14, 1, 1, 15, + 16, 17, 1, 1, 18, 18, 18, 18, 19, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 1, 20, 1, 21, 1, 1, 22, 23, 24, 25, + + 26, 27, 28, 29, 30, 18, 18, 31, 32, 33, + 34, 35, 36, 37, 38, 39, 18, 40, 41, 42, + 18, 18, 1, 43, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 + } ; + +static yyconst YY_CHAR yy_meta[44] = + { 0, + 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, + 1, 3, 3, 3, 1, 1, 1, 3, 3, 1, + 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 1 + } ; + +static yyconst flex_uint16_t yy_base[97] = + { 0, + 0, 0, 148, 149, 149, 149, 149, 149, 141, 149, + 149, 149, 32, 135, 37, 149, 149, 149, 0, 142, + 149, 29, 110, 101, 120, 115, 106, 32, 105, 25, + 116, 149, 149, 45, 0, 60, 68, 76, 0, 149, + 99, 102, 105, 112, 95, 97, 108, 108, 101, 86, + 94, 85, 92, 87, 90, 0, 79, 88, 63, 94, + 0, 84, 88, 87, 90, 0, 80, 76, 91, 0, + 0, 0, 74, 60, 67, 97, 100, 0, 0, 0, + 0, 0, 54, 58, 0, 0, 0, 31, 0, 36, + 40, 34, 0, 149, 45, 114 + + } ; + +static yyconst flex_int16_t yy_def[97] = + { 0, + 94, 1, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 95, 94, + 94, 95, 95, 95, 95, 95, 95, 95, 95, 95, + 95, 94, 94, 94, 96, 94, 94, 94, 95, 94, + 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, + 95, 95, 95, 95, 95, 96, 94, 94, 94, 94, + 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, + 95, 95, 95, 95, 95, 94, 94, 95, 95, 95, + 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, + 95, 95, 95, 0, 94, 94 + + } ; + +static yyconst flex_uint16_t yy_nxt[193] = + { 0, + 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 15, 15, 16, 17, 18, 19, 19, 20, + 21, 22, 19, 23, 19, 24, 25, 19, 26, 19, + 27, 28, 19, 19, 29, 19, 19, 30, 31, 19, + 19, 19, 32, 34, 34, 34, 36, 39, 37, 37, + 37, 41, 42, 50, 53, 38, 34, 34, 34, 93, + 54, 51, 38, 38, 92, 91, 43, 44, 90, 89, + 38, 57, 57, 57, 60, 60, 60, 36, 58, 37, + 37, 37, 59, 88, 59, 58, 38, 60, 60, 60, + 57, 57, 57, 38, 76, 87, 76, 58, 86, 77, + + 77, 77, 85, 84, 58, 60, 60, 60, 77, 77, + 77, 77, 77, 77, 56, 83, 56, 82, 81, 80, + 79, 78, 75, 74, 73, 72, 71, 70, 69, 68, + 67, 66, 65, 64, 63, 62, 61, 55, 52, 49, + 48, 47, 46, 45, 40, 35, 33, 94, 3, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94 + + } ; + +static yyconst flex_int16_t yy_chk[193] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 13, 13, 13, 15, 95, 15, 15, + 15, 22, 22, 28, 30, 15, 34, 34, 34, 92, + 30, 28, 15, 34, 91, 90, 22, 22, 88, 84, + 34, 36, 36, 36, 59, 59, 59, 37, 36, 37, + 37, 37, 38, 83, 38, 36, 37, 38, 38, 38, + 57, 57, 57, 37, 58, 75, 58, 57, 74, 58, + + 58, 58, 73, 69, 57, 60, 60, 60, 76, 76, + 76, 77, 77, 77, 96, 68, 96, 67, 65, 64, + 63, 62, 55, 54, 53, 52, 51, 50, 49, 48, + 47, 46, 45, 44, 43, 42, 41, 31, 29, 27, + 26, 25, 24, 23, 20, 14, 9, 3, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, + 94, 94 + + } ; + +static yy_state_type yy_last_accepting_state; +static char *yy_last_accepting_cpos; + +extern int yy_flex_debug; +int yy_flex_debug = 0; + +/* The intent behind this definition is that it'll catch + * any uses of REJECT which flex missed. + */ +#define REJECT reject_used_but_not_detected +#define yymore() yymore_used_but_not_detected +#define YY_MORE_ADJ 0 +#define YY_RESTORE_YY_MORE_OFFSET +char *yytext; +#line 1 "wp_parser.l" +#line 3 "wp_parser.l" +#include "wp_parser_y.h" +#include "wp_parser.tab.h" +/* Tokens NUMBER, SYMBOL, F1, POW, F2, etc. are defined in wp_parser.y. */ +/* Types WP_SQRT, WP_SQRT, etc. are defined in wp_parser_y.h. */ +/* Used leater to define NUMBER */ +#line 551 "wp_parser.lex.c" + +#define INITIAL 0 + +#ifndef YY_NO_UNISTD_H +/* Special case for "unistd.h", since it is non-ANSI. We include it way + * down here because we want the user's section 1 to have been scanned first. + * The user has a chance to override it with an option. + */ +#include <unistd.h> +#endif + +#ifndef YY_EXTRA_TYPE +#define YY_EXTRA_TYPE void * +#endif + +static int yy_init_globals (void ); + +/* Accessor methods to globals. + These are made visible to non-reentrant scanners for convenience. */ + +int yylex_destroy (void ); + +int yyget_debug (void ); + +void yyset_debug (int debug_flag ); + +YY_EXTRA_TYPE yyget_extra (void ); + +void yyset_extra (YY_EXTRA_TYPE user_defined ); + +FILE *yyget_in (void ); + +void yyset_in (FILE * _in_str ); + +FILE *yyget_out (void ); + +void yyset_out (FILE * _out_str ); + +yy_size_t yyget_leng (void ); + +char *yyget_text (void ); + +int yyget_lineno (void ); + +void yyset_lineno (int _line_number ); + +/* Macros after this point can all be overridden by user definitions in + * section 1. + */ + +#ifndef YY_SKIP_YYWRAP +#ifdef __cplusplus +extern "C" int yywrap (void ); +#else +extern int yywrap (void ); +#endif +#endif + +#ifndef YY_NO_UNPUT + + static void yyunput (int c,char *buf_ptr ); + +#endif + +#ifndef yytext_ptr +static void yy_flex_strncpy (char *,yyconst char *,int ); +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * ); +#endif + +#ifndef YY_NO_INPUT + +#ifdef __cplusplus +static int yyinput (void ); +#else +static int input (void ); +#endif + +#endif + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k */ +#define YY_READ_BUF_SIZE 16384 +#else +#define YY_READ_BUF_SIZE 8192 +#endif /* __ia64__ */ +#endif + +/* Copy whatever the last rule matched to the standard output. */ +#ifndef ECHO +/* This used to be an fputs(), but since the string might contain NUL's, + * we now use fwrite(). + */ +#define ECHO do { if (fwrite( yytext, yyleng, 1, yyout )) {} } while (0) +#endif + +/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, + * is returned in "result". + */ +#ifndef YY_INPUT +#define YY_INPUT(buf,result,max_size) \ + if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \ + { \ + int c = '*'; \ + size_t n; \ + for ( n = 0; n < max_size && \ + (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ + buf[n] = (char) c; \ + if ( c == '\n' ) \ + buf[n++] = (char) c; \ + if ( c == EOF && ferror( yyin ) ) \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + result = n; \ + } \ + else \ + { \ + errno=0; \ + while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \ + { \ + if( errno != EINTR) \ + { \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + break; \ + } \ + errno=0; \ + clearerr(yyin); \ + } \ + }\ +\ + +#endif + +/* No semi-colon after return; correct usage is to write "yyterminate();" - + * we don't want an extra ';' after the "return" because that will cause + * some compilers to complain about unreachable statements. + */ +#ifndef yyterminate +#define yyterminate() return YY_NULL +#endif + +/* Number of entries by which start-condition stack grows. */ +#ifndef YY_START_STACK_INCR +#define YY_START_STACK_INCR 25 +#endif + +/* Report a fatal error. */ +#ifndef YY_FATAL_ERROR +#define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) +#endif + +/* end tables serialization structures and prototypes */ + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +#define YY_DECL_IS_OURS 1 + +extern int yylex (void); + +#define YY_DECL int yylex (void) +#endif /* !YY_DECL */ + +/* Code executed at the beginning of each rule, after yytext and yyleng + * have been set up. + */ +#ifndef YY_USER_ACTION +#define YY_USER_ACTION +#endif + +/* Code executed at the end of each rule. */ +#ifndef YY_BREAK +#define YY_BREAK /*LINTED*/break; +#endif + +#define YY_RULE_SETUP \ + YY_USER_ACTION + +/** The main scanner function which does all the work. + */ +YY_DECL +{ + yy_state_type yy_current_state; + char *yy_cp, *yy_bp; + int yy_act; + + if ( !(yy_init) ) + { + (yy_init) = 1; + +#ifdef YY_USER_INIT + YY_USER_INIT; +#endif + + if ( ! (yy_start) ) + (yy_start) = 1; /* first start state */ + + if ( ! yyin ) + yyin = stdin; + + if ( ! yyout ) + yyout = stdout; + + if ( ! YY_CURRENT_BUFFER ) { + yyensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + yy_create_buffer(yyin,YY_BUF_SIZE ); + } + + yy_load_buffer_state( ); + } + + { +#line 13 "wp_parser.l" + + +#line 772 "wp_parser.lex.c" + + while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */ + { + yy_cp = (yy_c_buf_p); + + /* Support of yytext. */ + *yy_cp = (yy_hold_char); + + /* yy_bp points to the position in yy_ch_buf of the start of + * the current run. + */ + yy_bp = yy_cp; + + yy_current_state = (yy_start); +yy_match: + do + { + YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)] ; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 95 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + ++yy_cp; + } + while ( yy_base[yy_current_state] != 149 ); + +yy_find_action: + yy_act = yy_accept[yy_current_state]; + if ( yy_act == 0 ) + { /* have to back up */ + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + yy_act = yy_accept[yy_current_state]; + } + + YY_DO_BEFORE_ACTION; + +do_action: /* This label is used only to access EOF actions. */ + + switch ( yy_act ) + { /* beginning of action switch */ + case 0: /* must back up */ + /* undo the effects of YY_DO_BEFORE_ACTION */ + *yy_cp = (yy_hold_char); + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + goto yy_find_action; + +case 1: +#line 16 "wp_parser.l" +case 2: +#line 17 "wp_parser.l" +case 3: +#line 18 "wp_parser.l" +case 4: +#line 19 "wp_parser.l" +case 5: +#line 20 "wp_parser.l" +case 6: +#line 21 "wp_parser.l" +case 7: +#line 22 "wp_parser.l" +case 8: +#line 23 "wp_parser.l" +case 9: +#line 24 "wp_parser.l" +case 10: +#line 25 "wp_parser.l" +case 11: +YY_RULE_SETUP +#line 25 "wp_parser.l" +{ return yytext[0]; } /* simply pass through */ + YY_BREAK +/* yylval is union type defined in wp_parser.tab.h that is generated + * by bison with wp_parser.y */ +case 12: +YY_RULE_SETUP +#line 30 "wp_parser.l" +{ yylval.f1 = WP_SQRT; return F1; } + YY_BREAK +case 13: +YY_RULE_SETUP +#line 31 "wp_parser.l" +{ yylval.f1 = WP_EXP; return F1; } + YY_BREAK +case 14: +YY_RULE_SETUP +#line 32 "wp_parser.l" +{ yylval.f1 = WP_LOG; return F1; } + YY_BREAK +case 15: +YY_RULE_SETUP +#line 33 "wp_parser.l" +{ yylval.f1 = WP_LOG10; return F1; } + YY_BREAK +case 16: +YY_RULE_SETUP +#line 34 "wp_parser.l" +{ yylval.f1 = WP_SIN; return F1; } + YY_BREAK +case 17: +YY_RULE_SETUP +#line 35 "wp_parser.l" +{ yylval.f1 = WP_COS; return F1; } + YY_BREAK +case 18: +YY_RULE_SETUP +#line 36 "wp_parser.l" +{ yylval.f1 = WP_TAN; return F1; } + YY_BREAK +case 19: +YY_RULE_SETUP +#line 37 "wp_parser.l" +{ yylval.f1 = WP_ASIN; return F1; } + YY_BREAK +case 20: +YY_RULE_SETUP +#line 38 "wp_parser.l" +{ yylval.f1 = WP_ACOS; return F1; } + YY_BREAK +case 21: +YY_RULE_SETUP +#line 39 "wp_parser.l" +{ yylval.f1 = WP_ATAN; return F1; } + YY_BREAK +case 22: +YY_RULE_SETUP +#line 40 "wp_parser.l" +{ yylval.f1 = WP_SINH; return F1; } + YY_BREAK +case 23: +YY_RULE_SETUP +#line 41 "wp_parser.l" +{ yylval.f1 = WP_COSH; return F1; } + YY_BREAK +case 24: +YY_RULE_SETUP +#line 42 "wp_parser.l" +{ yylval.f1 = WP_TANH; return F1; } + YY_BREAK +case 25: +YY_RULE_SETUP +#line 43 "wp_parser.l" +{ yylval.f1 = WP_ABS; return F1; } + YY_BREAK +case 26: +YY_RULE_SETUP +#line 44 "wp_parser.l" +{ yylval.f1 = WP_ABS; return F1; } + YY_BREAK +case 27: +YY_RULE_SETUP +#line 45 "wp_parser.l" +{ yylval.f2 = WP_POW; return POW;} + YY_BREAK +case 28: +YY_RULE_SETUP +#line 46 "wp_parser.l" +{ yylval.f2 = WP_POW; return POW;} + YY_BREAK +case 29: +YY_RULE_SETUP +#line 47 "wp_parser.l" +{ yylval.f2 = WP_POW; return F2; } + YY_BREAK +case 30: +YY_RULE_SETUP +#line 48 "wp_parser.l" +{ yylval.f2 = WP_HEAVISIDE; return F2; } + YY_BREAK +case 31: +YY_RULE_SETUP +#line 49 "wp_parser.l" +{ yylval.f2 = WP_MIN; return F2; } + YY_BREAK +case 32: +YY_RULE_SETUP +#line 50 "wp_parser.l" +{ yylval.f2 = WP_MAX; return F2; } + YY_BREAK +/* We use SYMBOL to hold variables and constants */ +case 33: +YY_RULE_SETUP +#line 53 "wp_parser.l" +{ yylval.s = wp_makesymbol(yytext); return SYMBOL; } + YY_BREAK +/* Number */ +case 34: +#line 57 "wp_parser.l" +case 35: +YY_RULE_SETUP +#line 57 "wp_parser.l" +{ yylval.d = atof(yytext); return NUMBER; } + YY_BREAK +/* Special characters */ +case 36: +YY_RULE_SETUP +#line 60 "wp_parser.l" + + YY_BREAK +case 37: +YY_RULE_SETUP +#line 61 "wp_parser.l" +/* ignore white space */ + YY_BREAK +case 38: +/* rule 38 can match eol */ +YY_RULE_SETUP +#line 62 "wp_parser.l" +/* ignore line continuation */ + YY_BREAK +case 39: +/* rule 39 can match eol */ +YY_RULE_SETUP +#line 63 "wp_parser.l" +{ return EOL; } + YY_BREAK +/* everything else */ +case 40: +YY_RULE_SETUP +#line 66 "wp_parser.l" +{ yyerror("Unknow character %c\n", *yytext); } + YY_BREAK +case 41: +YY_RULE_SETUP +#line 68 "wp_parser.l" +YY_FATAL_ERROR( "flex scanner jammed" ); + YY_BREAK +#line 1009 "wp_parser.lex.c" +case YY_STATE_EOF(INITIAL): + yyterminate(); + + case YY_END_OF_BUFFER: + { + /* Amount of text matched not including the EOB char. */ + int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1; + + /* Undo the effects of YY_DO_BEFORE_ACTION. */ + *yy_cp = (yy_hold_char); + YY_RESTORE_YY_MORE_OFFSET + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW ) + { + /* We're scanning a new file or input source. It's + * possible that this happened because the user + * just pointed yyin at a new source and called + * yylex(). If so, then we have to assure + * consistency between YY_CURRENT_BUFFER and our + * globals. Here is the right place to do so, because + * this is the first action (other than possibly a + * back-up) that will match for the new input source. + */ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; + } + + /* Note that here we test for yy_c_buf_p "<=" to the position + * of the first EOB in the buffer, since yy_c_buf_p will + * already have been incremented past the NUL character + * (since all states make transitions on EOB to the + * end-of-buffer state). Contrast this with the test + * in input(). + */ + if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + { /* This was really a NUL. */ + yy_state_type yy_next_state; + + (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + /* Okay, we're now positioned to make the NUL + * transition. We couldn't have + * yy_get_previous_state() go ahead and do it + * for us because it doesn't know how to deal + * with the possibility of jamming (and we don't + * want to build jamming into it because then it + * will run more slowly). + */ + + yy_next_state = yy_try_NUL_trans( yy_current_state ); + + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + + if ( yy_next_state ) + { + /* Consume the NUL. */ + yy_cp = ++(yy_c_buf_p); + yy_current_state = yy_next_state; + goto yy_match; + } + + else + { + yy_cp = (yy_c_buf_p); + goto yy_find_action; + } + } + + else switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_END_OF_FILE: + { + (yy_did_buffer_switch_on_eof) = 0; + + if ( yywrap( ) ) + { + /* Note: because we've taken care in + * yy_get_next_buffer() to have set up + * yytext, we can now set up + * yy_c_buf_p so that if some total + * hoser (like flex itself) wants to + * call the scanner after we return the + * YY_NULL, it'll still work - another + * YY_NULL will get returned. + */ + (yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ; + + yy_act = YY_STATE_EOF(YY_START); + goto do_action; + } + + else + { + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; + } + break; + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = + (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_match; + + case EOB_ACT_LAST_MATCH: + (yy_c_buf_p) = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_find_action; + } + break; + } + + default: + YY_FATAL_ERROR( + "fatal flex scanner internal error--no action found" ); + } /* end of action switch */ + } /* end of scanning one token */ + } /* end of user's declarations */ +} /* end of yylex */ + +/* yy_get_next_buffer - try to read in a new buffer + * + * Returns a code representing an action: + * EOB_ACT_LAST_MATCH - + * EOB_ACT_CONTINUE_SCAN - continue scanning from current position + * EOB_ACT_END_OF_FILE - end of file + */ +static int yy_get_next_buffer (void) +{ + char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; + char *source = (yytext_ptr); + yy_size_t number_to_move, i; + int ret_val; + + if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] ) + YY_FATAL_ERROR( + "fatal flex scanner internal error--end of buffer missed" ); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 ) + { /* Don't try to fill the buffer, so this is an EOF. */ + if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 ) + { + /* We matched a single character, the EOB, so + * treat this as a final EOF. + */ + return EOB_ACT_END_OF_FILE; + } + + else + { + /* We matched some text prior to the EOB, first + * process it. + */ + return EOB_ACT_LAST_MATCH; + } + } + + /* Try to read more data. */ + + /* First move last chars to start of buffer. */ + number_to_move = (yy_size_t) ((yy_c_buf_p) - (yytext_ptr)) - 1; + + for ( i = 0; i < number_to_move; ++i ) + *(dest++) = *(source++); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING ) + /* don't do the read, it's not guaranteed to return an EOF, + * just force an EOF + */ + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0; + + else + { + yy_size_t num_to_read = + YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; + + while ( num_to_read <= 0 ) + { /* Not enough room in the buffer - grow it. */ + + /* just a shorter name for the current buffer */ + YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE; + + int yy_c_buf_p_offset = + (int) ((yy_c_buf_p) - b->yy_ch_buf); + + if ( b->yy_is_our_buffer ) + { + yy_size_t new_size = b->yy_buf_size * 2; + + if ( new_size <= 0 ) + b->yy_buf_size += b->yy_buf_size / 8; + else + b->yy_buf_size *= 2; + + b->yy_ch_buf = (char *) + /* Include room in for 2 EOB chars. */ + yyrealloc((void *) b->yy_ch_buf,b->yy_buf_size + 2 ); + } + else + /* Can't grow it, we don't own it. */ + b->yy_ch_buf = 0; + + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( + "fatal error - scanner input buffer overflow" ); + + (yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset]; + + num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - + number_to_move - 1; + + } + + if ( num_to_read > YY_READ_BUF_SIZE ) + num_to_read = YY_READ_BUF_SIZE; + + /* Read in more data. */ + YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), + (yy_n_chars), num_to_read ); + + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + if ( (yy_n_chars) == 0 ) + { + if ( number_to_move == YY_MORE_ADJ ) + { + ret_val = EOB_ACT_END_OF_FILE; + yyrestart(yyin ); + } + + else + { + ret_val = EOB_ACT_LAST_MATCH; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = + YY_BUFFER_EOF_PENDING; + } + } + + else + ret_val = EOB_ACT_CONTINUE_SCAN; + + if ((int) ((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) { + /* Extend the array by 50%, plus the number we really need. */ + int new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1); + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size ); + if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" ); + } + + (yy_n_chars) += number_to_move; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR; + + (yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; + + return ret_val; +} + +/* yy_get_previous_state - get the state just before the EOB char was reached */ + + static yy_state_type yy_get_previous_state (void) +{ + yy_state_type yy_current_state; + char *yy_cp; + + yy_current_state = (yy_start); + + for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp ) + { + YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 95 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + } + + return yy_current_state; +} + +/* yy_try_NUL_trans - try to make a transition on the NUL character + * + * synopsis + * next_state = yy_try_NUL_trans( current_state ); + */ + static yy_state_type yy_try_NUL_trans (yy_state_type yy_current_state ) +{ + int yy_is_jam; + char *yy_cp = (yy_c_buf_p); + + YY_CHAR yy_c = 1; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 95 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + yy_is_jam = (yy_current_state == 94); + + return yy_is_jam ? 0 : yy_current_state; +} + +#ifndef YY_NO_UNPUT + + static void yyunput (int c, char * yy_bp ) +{ + char *yy_cp; + + yy_cp = (yy_c_buf_p); + + /* undo effects of setting up yytext */ + *yy_cp = (yy_hold_char); + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + { /* need to shift things up to make room */ + /* +2 for EOB chars. */ + yy_size_t number_to_move = (yy_n_chars) + 2; + char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[ + YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2]; + char *source = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]; + + while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) + *--dest = *--source; + + yy_cp += (int) (dest - source); + yy_bp += (int) (dest - source); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_buf_size; + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + YY_FATAL_ERROR( "flex scanner push-back overflow" ); + } + + *--yy_cp = (char) c; + + (yytext_ptr) = yy_bp; + (yy_hold_char) = *yy_cp; + (yy_c_buf_p) = yy_cp; +} + +#endif + +#ifndef YY_NO_INPUT +#ifdef __cplusplus + static int yyinput (void) +#else + static int input (void) +#endif + +{ + int c; + + *(yy_c_buf_p) = (yy_hold_char); + + if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR ) + { + /* yy_c_buf_p now points to the character we want to return. + * If this occurs *before* the EOB characters, then it's a + * valid NUL; if not, then we've hit the end of the buffer. + */ + if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + /* This was really a NUL. */ + *(yy_c_buf_p) = '\0'; + + else + { /* need more input */ + yy_size_t offset = (yy_c_buf_p) - (yytext_ptr); + ++(yy_c_buf_p); + + switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_LAST_MATCH: + /* This happens because yy_g_n_b() + * sees that we've accumulated a + * token and flags that we need to + * try matching the token before + * proceeding. But for input(), + * there's no matching to consider. + * So convert the EOB_ACT_LAST_MATCH + * to EOB_ACT_END_OF_FILE. + */ + + /* Reset buffer status. */ + yyrestart(yyin ); + + /*FALLTHROUGH*/ + + case EOB_ACT_END_OF_FILE: + { + if ( yywrap( ) ) + return EOF; + + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; +#ifdef __cplusplus + return yyinput(); +#else + return input(); +#endif + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = (yytext_ptr) + offset; + break; + } + } + } + + c = *(unsigned char *) (yy_c_buf_p); /* cast for 8-bit char's */ + *(yy_c_buf_p) = '\0'; /* preserve yytext */ + (yy_hold_char) = *++(yy_c_buf_p); + + return c; +} +#endif /* ifndef YY_NO_INPUT */ + +/** Immediately switch to a different input stream. + * @param input_file A readable stream. + * + * @note This function does not reset the start condition to @c INITIAL . + */ + void yyrestart (FILE * input_file ) +{ + + if ( ! YY_CURRENT_BUFFER ){ + yyensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + yy_create_buffer(yyin,YY_BUF_SIZE ); + } + + yy_init_buffer(YY_CURRENT_BUFFER,input_file ); + yy_load_buffer_state( ); +} + +/** Switch to a different input buffer. + * @param new_buffer The new input buffer. + * + */ + void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ) +{ + + /* TODO. We should be able to replace this entire function body + * with + * yypop_buffer_state(); + * yypush_buffer_state(new_buffer); + */ + yyensure_buffer_stack (); + if ( YY_CURRENT_BUFFER == new_buffer ) + return; + + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + YY_CURRENT_BUFFER_LVALUE = new_buffer; + yy_load_buffer_state( ); + + /* We don't actually know whether we did this switch during + * EOF (yywrap()) processing, but the only time this flag + * is looked at is after yywrap() is called, so it's safe + * to go ahead and always set it. + */ + (yy_did_buffer_switch_on_eof) = 1; +} + +static void yy_load_buffer_state (void) +{ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + (yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; + yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file; + (yy_hold_char) = *(yy_c_buf_p); +} + +/** Allocate and initialize an input buffer state. + * @param file A readable stream. + * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. + * + * @return the allocated buffer state. + */ + YY_BUFFER_STATE yy_create_buffer (FILE * file, int size ) +{ + YY_BUFFER_STATE b; + + b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_buf_size = (yy_size_t)size; + + /* yy_ch_buf has to be 2 characters longer than the size given because + * we need to put in 2 end-of-buffer characters. + */ + b->yy_ch_buf = (char *) yyalloc(b->yy_buf_size + 2 ); + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_is_our_buffer = 1; + + yy_init_buffer(b,file ); + + return b; +} + +/** Destroy the buffer. + * @param b a buffer created with yy_create_buffer() + * + */ + void yy_delete_buffer (YY_BUFFER_STATE b ) +{ + + if ( ! b ) + return; + + if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */ + YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; + + if ( b->yy_is_our_buffer ) + yyfree((void *) b->yy_ch_buf ); + + yyfree((void *) b ); +} + +/* Initializes or reinitializes a buffer. + * This function is sometimes called more than once on the same buffer, + * such as during a yyrestart() or at EOF. + */ + static void yy_init_buffer (YY_BUFFER_STATE b, FILE * file ) + +{ + int oerrno = errno; + + yy_flush_buffer(b ); + + b->yy_input_file = file; + b->yy_fill_buffer = 1; + + /* If b is the current buffer, then yy_init_buffer was _probably_ + * called from yyrestart() or through yy_get_next_buffer. + * In that case, we don't want to reset the lineno or column. + */ + if (b != YY_CURRENT_BUFFER){ + b->yy_bs_lineno = 1; + b->yy_bs_column = 0; + } + + b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; + + errno = oerrno; +} + +/** Discard all buffered characters. On the next scan, YY_INPUT will be called. + * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. + * + */ + void yy_flush_buffer (YY_BUFFER_STATE b ) +{ + if ( ! b ) + return; + + b->yy_n_chars = 0; + + /* We always need two end-of-buffer characters. The first causes + * a transition to the end-of-buffer state. The second causes + * a jam in that state. + */ + b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; + b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; + + b->yy_buf_pos = &b->yy_ch_buf[0]; + + b->yy_at_bol = 1; + b->yy_buffer_status = YY_BUFFER_NEW; + + if ( b == YY_CURRENT_BUFFER ) + yy_load_buffer_state( ); +} + +/** Pushes the new state onto the stack. The new state becomes + * the current state. This function will allocate the stack + * if necessary. + * @param new_buffer The new state. + * + */ +void yypush_buffer_state (YY_BUFFER_STATE new_buffer ) +{ + if (new_buffer == NULL) + return; + + yyensure_buffer_stack(); + + /* This block is copied from yy_switch_to_buffer. */ + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + /* Only push if top exists. Otherwise, replace top. */ + if (YY_CURRENT_BUFFER) + (yy_buffer_stack_top)++; + YY_CURRENT_BUFFER_LVALUE = new_buffer; + + /* copied from yy_switch_to_buffer. */ + yy_load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; +} + +/** Removes and deletes the top of the stack, if present. + * The next element becomes the new top. + * + */ +void yypop_buffer_state (void) +{ + if (!YY_CURRENT_BUFFER) + return; + + yy_delete_buffer(YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + if ((yy_buffer_stack_top) > 0) + --(yy_buffer_stack_top); + + if (YY_CURRENT_BUFFER) { + yy_load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; + } +} + +/* Allocates the stack if it does not exist. + * Guarantees space for at least one push. + */ +static void yyensure_buffer_stack (void) +{ + yy_size_t num_to_alloc; + + if (!(yy_buffer_stack)) { + + /* First allocation is just for 2 elements, since we don't know if this + * scanner will even need a stack. We use 2 instead of 1 to avoid an + * immediate realloc on the next call. + */ + num_to_alloc = 1; /* After all that talk, this was set to 1 anyways... */ + (yy_buffer_stack) = (struct yy_buffer_state**)yyalloc + (num_to_alloc * sizeof(struct yy_buffer_state*) + ); + if ( ! (yy_buffer_stack) ) + YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); + + memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*)); + + (yy_buffer_stack_max) = num_to_alloc; + (yy_buffer_stack_top) = 0; + return; + } + + if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){ + + /* Increase the buffer to prepare for a possible push. */ + yy_size_t grow_size = 8 /* arbitrary grow size */; + + num_to_alloc = (yy_buffer_stack_max) + grow_size; + (yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc + ((yy_buffer_stack), + num_to_alloc * sizeof(struct yy_buffer_state*) + ); + if ( ! (yy_buffer_stack) ) + YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); + + /* zero only the new slots.*/ + memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*)); + (yy_buffer_stack_max) = num_to_alloc; + } +} + +/** Setup the input buffer state to scan directly from a user-specified character buffer. + * @param base the character buffer + * @param size the size in bytes of the character buffer + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE yy_scan_buffer (char * base, yy_size_t size ) +{ + YY_BUFFER_STATE b; + + if ( size < 2 || + base[size-2] != YY_END_OF_BUFFER_CHAR || + base[size-1] != YY_END_OF_BUFFER_CHAR ) + /* They forgot to leave room for the EOB's. */ + return 0; + + b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" ); + + b->yy_buf_size = size - 2; /* "- 2" to take care of EOB's */ + b->yy_buf_pos = b->yy_ch_buf = base; + b->yy_is_our_buffer = 0; + b->yy_input_file = 0; + b->yy_n_chars = b->yy_buf_size; + b->yy_is_interactive = 0; + b->yy_at_bol = 1; + b->yy_fill_buffer = 0; + b->yy_buffer_status = YY_BUFFER_NEW; + + yy_switch_to_buffer(b ); + + return b; +} + +/** Setup the input buffer state to scan a string. The next call to yylex() will + * scan from a @e copy of @a str. + * @param yystr a NUL-terminated string to scan + * + * @return the newly allocated buffer state object. + * @note If you want to scan bytes that may contain NUL values, then use + * yy_scan_bytes() instead. + */ +YY_BUFFER_STATE yy_scan_string (yyconst char * yystr ) +{ + + return yy_scan_bytes(yystr,strlen(yystr) ); +} + +/** Setup the input buffer state to scan the given bytes. The next call to yylex() will + * scan from a @e copy of @a bytes. + * @param yybytes the byte buffer to scan + * @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes. + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE yy_scan_bytes (yyconst char * yybytes, yy_size_t _yybytes_len ) +{ + YY_BUFFER_STATE b; + char *buf; + yy_size_t n; + yy_size_t i; + + /* Get memory for full buffer, including space for trailing EOB's. */ + n = _yybytes_len + 2; + buf = (char *) yyalloc(n ); + if ( ! buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" ); + + for ( i = 0; i < _yybytes_len; ++i ) + buf[i] = yybytes[i]; + + buf[_yybytes_len] = buf[_yybytes_len+1] = YY_END_OF_BUFFER_CHAR; + + b = yy_scan_buffer(buf,n ); + if ( ! b ) + YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" ); + + /* It's okay to grow etc. this buffer, and we should throw it + * away when we're done. + */ + b->yy_is_our_buffer = 1; + + return b; +} + +#ifndef YY_EXIT_FAILURE +#define YY_EXIT_FAILURE 2 +#endif + +static void yy_fatal_error (yyconst char* msg ) +{ + (void) fprintf( stderr, "%s\n", msg ); + exit( YY_EXIT_FAILURE ); +} + +/* Redefine yyless() so it works in section 3 code. */ + +#undef yyless +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + yytext[yyleng] = (yy_hold_char); \ + (yy_c_buf_p) = yytext + yyless_macro_arg; \ + (yy_hold_char) = *(yy_c_buf_p); \ + *(yy_c_buf_p) = '\0'; \ + yyleng = yyless_macro_arg; \ + } \ + while ( 0 ) + +/* Accessor methods (get/set functions) to struct members. */ + +/** Get the current line number. + * + */ +int yyget_lineno (void) +{ + + return yylineno; +} + +/** Get the input stream. + * + */ +FILE *yyget_in (void) +{ + return yyin; +} + +/** Get the output stream. + * + */ +FILE *yyget_out (void) +{ + return yyout; +} + +/** Get the length of the current token. + * + */ +yy_size_t yyget_leng (void) +{ + return yyleng; +} + +/** Get the current token. + * + */ + +char *yyget_text (void) +{ + return yytext; +} + +/** Set the current line number. + * @param _line_number line number + * + */ +void yyset_lineno (int _line_number ) +{ + + yylineno = _line_number; +} + +/** Set the input stream. This does not discard the current + * input buffer. + * @param _in_str A readable stream. + * + * @see yy_switch_to_buffer + */ +void yyset_in (FILE * _in_str ) +{ + yyin = _in_str ; +} + +void yyset_out (FILE * _out_str ) +{ + yyout = _out_str ; +} + +int yyget_debug (void) +{ + return yy_flex_debug; +} + +void yyset_debug (int _bdebug ) +{ + yy_flex_debug = _bdebug ; +} + +static int yy_init_globals (void) +{ + /* Initialization is the same as for the non-reentrant scanner. + * This function is called from yylex_destroy(), so don't allocate here. + */ + + (yy_buffer_stack) = 0; + (yy_buffer_stack_top) = 0; + (yy_buffer_stack_max) = 0; + (yy_c_buf_p) = (char *) 0; + (yy_init) = 0; + (yy_start) = 0; + +/* Defined in main.c */ +#ifdef YY_STDINIT + yyin = stdin; + yyout = stdout; +#else + yyin = (FILE *) 0; + yyout = (FILE *) 0; +#endif + + /* For future reference: Set errno on error, since we are called by + * yylex_init() + */ + return 0; +} + +/* yylex_destroy is for both reentrant and non-reentrant scanners. */ +int yylex_destroy (void) +{ + + /* Pop the buffer stack, destroying each element. */ + while(YY_CURRENT_BUFFER){ + yy_delete_buffer(YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + yypop_buffer_state(); + } + + /* Destroy the stack itself. */ + yyfree((yy_buffer_stack) ); + (yy_buffer_stack) = NULL; + + /* Reset the globals. This is important in a non-reentrant scanner so the next time + * yylex() is called, initialization will occur. */ + yy_init_globals( ); + + return 0; +} + +/* + * Internal utility routines. + */ + +#ifndef yytext_ptr +static void yy_flex_strncpy (char* s1, yyconst char * s2, int n ) +{ + + int i; + for ( i = 0; i < n; ++i ) + s1[i] = s2[i]; +} +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * s ) +{ + int n; + for ( n = 0; s[n]; ++n ) + ; + + return n; +} +#endif + +void *yyalloc (yy_size_t size ) +{ + return (void *) malloc( size ); +} + +void *yyrealloc (void * ptr, yy_size_t size ) +{ + + /* The cast to (char *) in the following accommodates both + * implementations that use char* generic pointers, and those + * that use void* generic pointers. It works with the latter + * because both ANSI C and C++ allow castless assignment from + * any pointer type to void*, and deal with argument conversions + * as though doing an assignment. + */ + return (void *) realloc( (char *) ptr, size ); +} + +void yyfree (void * ptr ) +{ + free( (char *) ptr ); /* see yyrealloc() for (char *) cast */ +} + +#define YYTABLES_NAME "yytables" + +#line 68 "wp_parser.l" + + + diff --git a/Source/Parser/wp_parser.lex.h b/Source/Parser/wp_parser.lex.h new file mode 100644 index 000000000..13e286b05 --- /dev/null +++ b/Source/Parser/wp_parser.lex.h @@ -0,0 +1,338 @@ +#ifndef yyHEADER_H +#define yyHEADER_H 1 +#define yyIN_HEADER 1 + +#line 6 "wp_parser.lex.h" + +#line 8 "wp_parser.lex.h" + +#define YY_INT_ALIGNED short int + +/* A lexical scanner generated by flex */ + +#define FLEX_SCANNER +#define YY_FLEX_MAJOR_VERSION 2 +#define YY_FLEX_MINOR_VERSION 6 +#define YY_FLEX_SUBMINOR_VERSION 0 +#if YY_FLEX_SUBMINOR_VERSION > 0 +#define FLEX_BETA +#endif + +/* First, we deal with platform-specific or compiler-specific issues. */ + +/* begin standard C headers. */ +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> + +/* end standard C headers. */ + +/* flex integer type definitions */ + +#ifndef FLEXINT_H +#define FLEXINT_H + +/* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */ + +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + +/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, + * if you want the limit (max/min) macros for int types. + */ +#ifndef __STDC_LIMIT_MACROS +#define __STDC_LIMIT_MACROS 1 +#endif + +#include <inttypes.h> +typedef int8_t flex_int8_t; +typedef uint8_t flex_uint8_t; +typedef int16_t flex_int16_t; +typedef uint16_t flex_uint16_t; +typedef int32_t flex_int32_t; +typedef uint32_t flex_uint32_t; +#else +typedef signed char flex_int8_t; +typedef short int flex_int16_t; +typedef int flex_int32_t; +typedef unsigned char flex_uint8_t; +typedef unsigned short int flex_uint16_t; +typedef unsigned int flex_uint32_t; + +/* Limits of integral types. */ +#ifndef INT8_MIN +#define INT8_MIN (-128) +#endif +#ifndef INT16_MIN +#define INT16_MIN (-32767-1) +#endif +#ifndef INT32_MIN +#define INT32_MIN (-2147483647-1) +#endif +#ifndef INT8_MAX +#define INT8_MAX (127) +#endif +#ifndef INT16_MAX +#define INT16_MAX (32767) +#endif +#ifndef INT32_MAX +#define INT32_MAX (2147483647) +#endif +#ifndef UINT8_MAX +#define UINT8_MAX (255U) +#endif +#ifndef UINT16_MAX +#define UINT16_MAX (65535U) +#endif +#ifndef UINT32_MAX +#define UINT32_MAX (4294967295U) +#endif + +#endif /* ! C99 */ + +#endif /* ! FLEXINT_H */ + +#ifdef __cplusplus + +/* The "const" storage-class-modifier is valid. */ +#define YY_USE_CONST + +#else /* ! __cplusplus */ + +/* C99 requires __STDC__ to be defined as 1. */ +#if defined (__STDC__) + +#define YY_USE_CONST + +#endif /* defined (__STDC__) */ +#endif /* ! __cplusplus */ + +#ifdef YY_USE_CONST +#define yyconst const +#else +#define yyconst +#endif + +/* Size of default input buffer. */ +#ifndef YY_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k. + * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case. + * Ditto for the __ia64__ case accordingly. + */ +#define YY_BUF_SIZE 32768 +#else +#define YY_BUF_SIZE 16384 +#endif /* __ia64__ */ +#endif + +#ifndef YY_TYPEDEF_YY_BUFFER_STATE +#define YY_TYPEDEF_YY_BUFFER_STATE +typedef struct yy_buffer_state *YY_BUFFER_STATE; +#endif + +#ifndef YY_TYPEDEF_YY_SIZE_T +#define YY_TYPEDEF_YY_SIZE_T +typedef size_t yy_size_t; +#endif + +extern yy_size_t yyleng; + +extern FILE *yyin, *yyout; + +#ifndef YY_STRUCT_YY_BUFFER_STATE +#define YY_STRUCT_YY_BUFFER_STATE +struct yy_buffer_state + { + FILE *yy_input_file; + + char *yy_ch_buf; /* input buffer */ + char *yy_buf_pos; /* current position in input buffer */ + + /* Size of input buffer in bytes, not including room for EOB + * characters. + */ + yy_size_t yy_buf_size; + + /* Number of characters read into yy_ch_buf, not including EOB + * characters. + */ + int yy_n_chars; + + /* Whether we "own" the buffer - i.e., we know we created it, + * and can realloc() it to grow it, and should free() it to + * delete it. + */ + int yy_is_our_buffer; + + /* Whether this is an "interactive" input source; if so, and + * if we're using stdio for input, then we want to use getc() + * instead of fread(), to make sure we stop fetching input after + * each newline. + */ + int yy_is_interactive; + + /* Whether we're considered to be at the beginning of a line. + * If so, '^' rules will be active on the next match, otherwise + * not. + */ + int yy_at_bol; + + int yy_bs_lineno; /**< The line count. */ + int yy_bs_column; /**< The column count. */ + + /* Whether to try to fill the input buffer when we reach the + * end of it. + */ + int yy_fill_buffer; + + int yy_buffer_status; + + }; +#endif /* !YY_STRUCT_YY_BUFFER_STATE */ + +void yyrestart (FILE *input_file ); +void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ); +YY_BUFFER_STATE yy_create_buffer (FILE *file,int size ); +void yy_delete_buffer (YY_BUFFER_STATE b ); +void yy_flush_buffer (YY_BUFFER_STATE b ); +void yypush_buffer_state (YY_BUFFER_STATE new_buffer ); +void yypop_buffer_state (void ); + +YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ); +YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ); +YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,yy_size_t len ); + +void *yyalloc (yy_size_t ); +void *yyrealloc (void *,yy_size_t ); +void yyfree (void * ); + +/* Begin user sect3 */ + +#define yywrap() (/*CONSTCOND*/1) +#define YY_SKIP_YYWRAP + +extern int yylineno; + +extern char *yytext; +#ifdef yytext_ptr +#undef yytext_ptr +#endif +#define yytext_ptr yytext + +#ifdef YY_HEADER_EXPORT_START_CONDITIONS +#define INITIAL 0 + +#endif + +#ifndef YY_NO_UNISTD_H +/* Special case for "unistd.h", since it is non-ANSI. We include it way + * down here because we want the user's section 1 to have been scanned first. + * The user has a chance to override it with an option. + */ +#include <unistd.h> +#endif + +#ifndef YY_EXTRA_TYPE +#define YY_EXTRA_TYPE void * +#endif + +/* Accessor methods to globals. + These are made visible to non-reentrant scanners for convenience. */ + +int yylex_destroy (void ); + +int yyget_debug (void ); + +void yyset_debug (int debug_flag ); + +YY_EXTRA_TYPE yyget_extra (void ); + +void yyset_extra (YY_EXTRA_TYPE user_defined ); + +FILE *yyget_in (void ); + +void yyset_in (FILE * _in_str ); + +FILE *yyget_out (void ); + +void yyset_out (FILE * _out_str ); + +yy_size_t yyget_leng (void ); + +char *yyget_text (void ); + +int yyget_lineno (void ); + +void yyset_lineno (int _line_number ); + +/* Macros after this point can all be overridden by user definitions in + * section 1. + */ + +#ifndef YY_SKIP_YYWRAP +#ifdef __cplusplus +extern "C" int yywrap (void ); +#else +extern int yywrap (void ); +#endif +#endif + +#ifndef yytext_ptr +static void yy_flex_strncpy (char *,yyconst char *,int ); +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * ); +#endif + +#ifndef YY_NO_INPUT + +#endif + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +#ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k */ +#define YY_READ_BUF_SIZE 16384 +#else +#define YY_READ_BUF_SIZE 8192 +#endif /* __ia64__ */ +#endif + +/* Number of entries by which start-condition stack grows. */ +#ifndef YY_START_STACK_INCR +#define YY_START_STACK_INCR 25 +#endif + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +#define YY_DECL_IS_OURS 1 + +extern int yylex (void); + +#define YY_DECL int yylex (void) +#endif /* !YY_DECL */ + +/* yy_get_previous_state - get the state just before the EOB char was reached */ + +#undef YY_NEW_FILE +#undef YY_FLUSH_BUFFER +#undef yy_set_bol +#undef yy_new_buffer +#undef yy_set_interactive +#undef YY_DO_BEFORE_ACTION + +#ifdef YY_DECL_IS_OURS +#undef YY_DECL_IS_OURS +#undef YY_DECL +#endif + +#line 68 "wp_parser.l" + + +#line 337 "wp_parser.lex.h" +#undef yyIN_HEADER +#endif /* yyHEADER_H */ diff --git a/Source/Parser/wp_parser.tab.c b/Source/Parser/wp_parser.tab.c new file mode 100644 index 000000000..fbc6920c2 --- /dev/null +++ b/Source/Parser/wp_parser.tab.c @@ -0,0 +1,1561 @@ +/* A Bison parser, made by GNU Bison 3.0.4. */ + +/* Bison implementation for Yacc-like parsers in C + + Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + +/* C LALR(1) parser skeleton written by Richard Stallman, by + simplifying the original so-called "semantic" parser. */ + +/* All symbols defined below should begin with yy or YY, to avoid + infringing on user name space. This should be done even for local + variables, as they might otherwise be expanded by user macros. + There are some unavoidable exceptions within include files to + define necessary library symbols; they are noted "INFRINGES ON + USER NAME SPACE" below. */ + +/* Identify Bison output. */ +#define YYBISON 1 + +/* Bison version. */ +#define YYBISON_VERSION "3.0.4" + +/* Skeleton name. */ +#define YYSKELETON_NAME "yacc.c" + +/* Pure parsers. */ +#define YYPURE 0 + +/* Push parsers. */ +#define YYPUSH 0 + +/* Pull parsers. */ +#define YYPULL 1 + + + + +/* Copy the first part of user declarations. */ +#line 2 "wp_parser.y" /* yacc.c:339 */ + + #include <stdio.h> + #include <stdlib.h> + #include <math.h> + #include "wp_parser_y.h" + int yylex (void); + +#line 74 "wp_parser.tab.c" /* yacc.c:339 */ + +# ifndef YY_NULLPTR +# if defined __cplusplus && 201103L <= __cplusplus +# define YY_NULLPTR nullptr +# else +# define YY_NULLPTR 0 +# endif +# endif + +/* Enabling verbose error messages. */ +#ifdef YYERROR_VERBOSE +# undef YYERROR_VERBOSE +# define YYERROR_VERBOSE 1 +#else +# define YYERROR_VERBOSE 0 +#endif + +/* In a future release of Bison, this section will be replaced + by #include "wp_parser.tab.h". */ +#ifndef YY_YY_WP_PARSER_TAB_H_INCLUDED +# define YY_YY_WP_PARSER_TAB_H_INCLUDED +/* Debug traces. */ +#ifndef YYDEBUG +# define YYDEBUG 0 +#endif +#if YYDEBUG +extern int yydebug; +#endif + +/* Token type. */ +#ifndef YYTOKENTYPE +# define YYTOKENTYPE + enum yytokentype + { + NODE = 258, + NUMBER = 259, + SYMBOL = 260, + F1 = 261, + F2 = 262, + EOL = 263, + POW = 264, + NEG = 265 + }; +#endif + +/* Value type. */ +#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED + +union YYSTYPE +{ +#line 19 "wp_parser.y" /* yacc.c:355 */ + + struct wp_node* n; + double d; + struct wp_symbol* s; + enum wp_f1_t f1; + enum wp_f2_t f2; + +#line 133 "wp_parser.tab.c" /* yacc.c:355 */ +}; + +typedef union YYSTYPE YYSTYPE; +# define YYSTYPE_IS_TRIVIAL 1 +# define YYSTYPE_IS_DECLARED 1 +#endif + + +extern YYSTYPE yylval; + +int yyparse (void); + +#endif /* !YY_YY_WP_PARSER_TAB_H_INCLUDED */ + +/* Copy the second part of user declarations. */ + +#line 150 "wp_parser.tab.c" /* yacc.c:358 */ + +#ifdef short +# undef short +#endif + +#ifdef YYTYPE_UINT8 +typedef YYTYPE_UINT8 yytype_uint8; +#else +typedef unsigned char yytype_uint8; +#endif + +#ifdef YYTYPE_INT8 +typedef YYTYPE_INT8 yytype_int8; +#else +typedef signed char yytype_int8; +#endif + +#ifdef YYTYPE_UINT16 +typedef YYTYPE_UINT16 yytype_uint16; +#else +typedef unsigned short int yytype_uint16; +#endif + +#ifdef YYTYPE_INT16 +typedef YYTYPE_INT16 yytype_int16; +#else +typedef short int yytype_int16; +#endif + +#ifndef YYSIZE_T +# ifdef __SIZE_TYPE__ +# define YYSIZE_T __SIZE_TYPE__ +# elif defined size_t +# define YYSIZE_T size_t +# elif ! defined YYSIZE_T +# include <stddef.h> /* INFRINGES ON USER NAME SPACE */ +# define YYSIZE_T size_t +# else +# define YYSIZE_T unsigned int +# endif +#endif + +#define YYSIZE_MAXIMUM ((YYSIZE_T) -1) + +#ifndef YY_ +# if defined YYENABLE_NLS && YYENABLE_NLS +# if ENABLE_NLS +# include <libintl.h> /* INFRINGES ON USER NAME SPACE */ +# define YY_(Msgid) dgettext ("bison-runtime", Msgid) +# endif +# endif +# ifndef YY_ +# define YY_(Msgid) Msgid +# endif +#endif + +#ifndef YY_ATTRIBUTE +# if (defined __GNUC__ \ + && (2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__))) \ + || defined __SUNPRO_C && 0x5110 <= __SUNPRO_C +# define YY_ATTRIBUTE(Spec) __attribute__(Spec) +# else +# define YY_ATTRIBUTE(Spec) /* empty */ +# endif +#endif + +#ifndef YY_ATTRIBUTE_PURE +# define YY_ATTRIBUTE_PURE YY_ATTRIBUTE ((__pure__)) +#endif + +#ifndef YY_ATTRIBUTE_UNUSED +# define YY_ATTRIBUTE_UNUSED YY_ATTRIBUTE ((__unused__)) +#endif + +#if !defined _Noreturn \ + && (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112) +# if defined _MSC_VER && 1200 <= _MSC_VER +# define _Noreturn __declspec (noreturn) +# else +# define _Noreturn YY_ATTRIBUTE ((__noreturn__)) +# endif +#endif + +/* Suppress unused-variable warnings by "using" E. */ +#if ! defined lint || defined __GNUC__ +# define YYUSE(E) ((void) (E)) +#else +# define YYUSE(E) /* empty */ +#endif + +#if defined __GNUC__ && 407 <= __GNUC__ * 100 + __GNUC_MINOR__ +/* Suppress an incorrect diagnostic about yylval being uninitialized. */ +# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \ + _Pragma ("GCC diagnostic push") \ + _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")\ + _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"") +# define YY_IGNORE_MAYBE_UNINITIALIZED_END \ + _Pragma ("GCC diagnostic pop") +#else +# define YY_INITIAL_VALUE(Value) Value +#endif +#ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN +# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN +# define YY_IGNORE_MAYBE_UNINITIALIZED_END +#endif +#ifndef YY_INITIAL_VALUE +# define YY_INITIAL_VALUE(Value) /* Nothing. */ +#endif + + +#if ! defined yyoverflow || YYERROR_VERBOSE + +/* The parser invokes alloca or malloc; define the necessary symbols. */ + +# ifdef YYSTACK_USE_ALLOCA +# if YYSTACK_USE_ALLOCA +# ifdef __GNUC__ +# define YYSTACK_ALLOC __builtin_alloca +# elif defined __BUILTIN_VA_ARG_INCR +# include <alloca.h> /* INFRINGES ON USER NAME SPACE */ +# elif defined _AIX +# define YYSTACK_ALLOC __alloca +# elif defined _MSC_VER +# include <malloc.h> /* INFRINGES ON USER NAME SPACE */ +# define alloca _alloca +# else +# define YYSTACK_ALLOC alloca +# if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS +# include <stdlib.h> /* INFRINGES ON USER NAME SPACE */ + /* Use EXIT_SUCCESS as a witness for stdlib.h. */ +# ifndef EXIT_SUCCESS +# define EXIT_SUCCESS 0 +# endif +# endif +# endif +# endif +# endif + +# ifdef YYSTACK_ALLOC + /* Pacify GCC's 'empty if-body' warning. */ +# define YYSTACK_FREE(Ptr) do { /* empty */; } while (0) +# ifndef YYSTACK_ALLOC_MAXIMUM + /* The OS might guarantee only one guard page at the bottom of the stack, + and a page size can be as small as 4096 bytes. So we cannot safely + invoke alloca (N) if N exceeds 4096. Use a slightly smaller number + to allow for a few compiler-allocated temporary stack slots. */ +# define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */ +# endif +# else +# define YYSTACK_ALLOC YYMALLOC +# define YYSTACK_FREE YYFREE +# ifndef YYSTACK_ALLOC_MAXIMUM +# define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM +# endif +# if (defined __cplusplus && ! defined EXIT_SUCCESS \ + && ! ((defined YYMALLOC || defined malloc) \ + && (defined YYFREE || defined free))) +# include <stdlib.h> /* INFRINGES ON USER NAME SPACE */ +# ifndef EXIT_SUCCESS +# define EXIT_SUCCESS 0 +# endif +# endif +# ifndef YYMALLOC +# define YYMALLOC malloc +# if ! defined malloc && ! defined EXIT_SUCCESS +void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */ +# endif +# endif +# ifndef YYFREE +# define YYFREE free +# if ! defined free && ! defined EXIT_SUCCESS +void free (void *); /* INFRINGES ON USER NAME SPACE */ +# endif +# endif +# endif +#endif /* ! defined yyoverflow || YYERROR_VERBOSE */ + + +#if (! defined yyoverflow \ + && (! defined __cplusplus \ + || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL))) + +/* A type that is properly aligned for any stack member. */ +union yyalloc +{ + yytype_int16 yyss_alloc; + YYSTYPE yyvs_alloc; +}; + +/* The size of the maximum gap between one aligned stack and the next. */ +# define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1) + +/* The size of an array large to enough to hold all stacks, each with + N elements. */ +# define YYSTACK_BYTES(N) \ + ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \ + + YYSTACK_GAP_MAXIMUM) + +# define YYCOPY_NEEDED 1 + +/* Relocate STACK from its old location to the new one. The + local variables YYSIZE and YYSTACKSIZE give the old and new number of + elements in the stack, and YYPTR gives the new location of the + stack. Advance YYPTR to a properly aligned location for the next + stack. */ +# define YYSTACK_RELOCATE(Stack_alloc, Stack) \ + do \ + { \ + YYSIZE_T yynewbytes; \ + YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \ + Stack = &yyptr->Stack_alloc; \ + yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \ + yyptr += yynewbytes / sizeof (*yyptr); \ + } \ + while (0) + +#endif + +#if defined YYCOPY_NEEDED && YYCOPY_NEEDED +/* Copy COUNT objects from SRC to DST. The source and destination do + not overlap. */ +# ifndef YYCOPY +# if defined __GNUC__ && 1 < __GNUC__ +# define YYCOPY(Dst, Src, Count) \ + __builtin_memcpy (Dst, Src, (Count) * sizeof (*(Src))) +# else +# define YYCOPY(Dst, Src, Count) \ + do \ + { \ + YYSIZE_T yyi; \ + for (yyi = 0; yyi < (Count); yyi++) \ + (Dst)[yyi] = (Src)[yyi]; \ + } \ + while (0) +# endif +# endif +#endif /* !YYCOPY_NEEDED */ + +/* YYFINAL -- State number of the termination state. */ +#define YYFINAL 2 +/* YYLAST -- Last index in YYTABLE. */ +#define YYLAST 97 + +/* YYNTOKENS -- Number of terminals. */ +#define YYNTOKENS 22 +/* YYNNTS -- Number of nonterminals. */ +#define YYNNTS 3 +/* YYNRULES -- Number of rules. */ +#define YYNRULES 16 +/* YYNSTATES -- Number of states. */ +#define YYNSTATES 36 + +/* YYTRANSLATE[YYX] -- Symbol number corresponding to YYX as returned + by yylex, with out-of-bounds checking. */ +#define YYUNDEFTOK 2 +#define YYMAXUTOK 265 + +#define YYTRANSLATE(YYX) \ + ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK) + +/* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM + as returned by yylex, without out-of-bounds checking. */ +static const yytype_uint8 yytranslate[] = +{ + 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 19, 20, 14, 12, 21, 13, 2, 15, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 16, 11, 17, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 10, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 1, 2, 3, 4, + 5, 6, 7, 8, 9, 18 +}; + +#if YYDEBUG + /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ +static const yytype_uint8 yyrline[] = +{ + 0, 58, 58, 59, 68, 69, 70, 71, 72, 73, + 74, 75, 76, 77, 78, 79, 80 +}; +#endif + +#if YYDEBUG || YYERROR_VERBOSE || 0 +/* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. + First, the terminals, then, starting at YYNTOKENS, nonterminals. */ +static const char *const yytname[] = +{ + "$end", "error", "$undefined", "NODE", "NUMBER", "SYMBOL", "F1", "F2", + "EOL", "\"**\"", "'^'", "'='", "'+'", "'-'", "'*'", "'/'", "'<'", "'>'", + "NEG", "'('", "')'", "','", "$accept", "input", "exp", YY_NULLPTR +}; +#endif + +# ifdef YYPRINT +/* YYTOKNUM[NUM] -- (External) token number corresponding to the + (internal) symbol number NUM (which must be that of a token). */ +static const yytype_uint16 yytoknum[] = +{ + 0, 256, 257, 258, 259, 260, 261, 262, 263, 264, + 94, 61, 43, 45, 42, 47, 60, 62, 265, 40, + 41, 44 +}; +# endif + +#define YYPACT_NINF -18 + +#define yypact_value_is_default(Yystate) \ + (!!((Yystate) == (-18))) + +#define YYTABLE_NINF -1 + +#define yytable_value_is_error(Yytable_value) \ + 0 + + /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing + STATE-NUM. */ +static const yytype_int8 yypact[] = +{ + -18, 15, -18, -18, -18, -17, -14, 25, 25, 71, + 25, 25, -3, 43, -18, 25, 25, 25, 25, 25, + 25, 25, 52, 33, -18, -3, 80, 80, 7, 7, + -3, -3, -18, 25, 61, -18 +}; + + /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM. + Performed when YYTABLE does not specify something else to do. Zero + means the default is an error. */ +static const yytype_uint8 yydefact[] = +{ + 2, 0, 1, 4, 5, 0, 0, 0, 0, 0, + 0, 0, 13, 0, 3, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 10, 14, 6, 7, 8, 9, + 11, 12, 15, 0, 0, 16 +}; + + /* YYPGOTO[NTERM-NUM]. */ +static const yytype_int8 yypgoto[] = +{ + -18, -18, -7 +}; + + /* YYDEFGOTO[NTERM-NUM]. */ +static const yytype_int8 yydefgoto[] = +{ + -1, 1, 9 +}; + + /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If + positive, shift that token. If negative, reduce the rule whose + number is the opposite. If YYTABLE_NINF, syntax error. */ +static const yytype_uint8 yytable[] = +{ + 12, 13, 10, 22, 23, 11, 15, 0, 25, 26, + 27, 28, 29, 30, 31, 2, 15, 0, 0, 3, + 4, 5, 6, 20, 21, 0, 34, 0, 7, 3, + 4, 5, 6, 0, 8, 0, 0, 0, 7, 0, + 0, 0, 15, 0, 8, 16, 17, 18, 19, 20, + 21, 0, 15, 0, 33, 16, 17, 18, 19, 20, + 21, 15, 0, 24, 16, 17, 18, 19, 20, 21, + 15, 0, 32, 16, 17, 18, 19, 20, 21, 14, + 15, 35, 0, 16, 17, 18, 19, 20, 21, 15, + 0, 0, 0, 0, 18, 19, 20, 21 +}; + +static const yytype_int8 yycheck[] = +{ + 7, 8, 19, 10, 11, 19, 9, -1, 15, 16, + 17, 18, 19, 20, 21, 0, 9, -1, -1, 4, + 5, 6, 7, 16, 17, -1, 33, -1, 13, 4, + 5, 6, 7, -1, 19, -1, -1, -1, 13, -1, + -1, -1, 9, -1, 19, 12, 13, 14, 15, 16, + 17, -1, 9, -1, 21, 12, 13, 14, 15, 16, + 17, 9, -1, 20, 12, 13, 14, 15, 16, 17, + 9, -1, 20, 12, 13, 14, 15, 16, 17, 8, + 9, 20, -1, 12, 13, 14, 15, 16, 17, 9, + -1, -1, -1, -1, 14, 15, 16, 17 +}; + + /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing + symbol of state STATE-NUM. */ +static const yytype_uint8 yystos[] = +{ + 0, 23, 0, 4, 5, 6, 7, 13, 19, 24, + 19, 19, 24, 24, 8, 9, 12, 13, 14, 15, + 16, 17, 24, 24, 20, 24, 24, 24, 24, 24, + 24, 24, 20, 21, 24, 20 +}; + + /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ +static const yytype_uint8 yyr1[] = +{ + 0, 22, 23, 23, 24, 24, 24, 24, 24, 24, + 24, 24, 24, 24, 24, 24, 24 +}; + + /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */ +static const yytype_uint8 yyr2[] = +{ + 0, 2, 0, 3, 1, 1, 3, 3, 3, 3, + 3, 3, 3, 2, 3, 4, 6 +}; + + +#define yyerrok (yyerrstatus = 0) +#define yyclearin (yychar = YYEMPTY) +#define YYEMPTY (-2) +#define YYEOF 0 + +#define YYACCEPT goto yyacceptlab +#define YYABORT goto yyabortlab +#define YYERROR goto yyerrorlab + + +#define YYRECOVERING() (!!yyerrstatus) + +#define YYBACKUP(Token, Value) \ +do \ + if (yychar == YYEMPTY) \ + { \ + yychar = (Token); \ + yylval = (Value); \ + YYPOPSTACK (yylen); \ + yystate = *yyssp; \ + goto yybackup; \ + } \ + else \ + { \ + yyerror (YY_("syntax error: cannot back up")); \ + YYERROR; \ + } \ +while (0) + +/* Error token number */ +#define YYTERROR 1 +#define YYERRCODE 256 + + + +/* Enable debugging if requested. */ +#if YYDEBUG + +# ifndef YYFPRINTF +# include <stdio.h> /* INFRINGES ON USER NAME SPACE */ +# define YYFPRINTF fprintf +# endif + +# define YYDPRINTF(Args) \ +do { \ + if (yydebug) \ + YYFPRINTF Args; \ +} while (0) + +/* This macro is provided for backward compatibility. */ +#ifndef YY_LOCATION_PRINT +# define YY_LOCATION_PRINT(File, Loc) ((void) 0) +#endif + + +# define YY_SYMBOL_PRINT(Title, Type, Value, Location) \ +do { \ + if (yydebug) \ + { \ + YYFPRINTF (stderr, "%s ", Title); \ + yy_symbol_print (stderr, \ + Type, Value); \ + YYFPRINTF (stderr, "\n"); \ + } \ +} while (0) + + +/*----------------------------------------. +| Print this symbol's value on YYOUTPUT. | +`----------------------------------------*/ + +static void +yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep) +{ + FILE *yyo = yyoutput; + YYUSE (yyo); + if (!yyvaluep) + return; +# ifdef YYPRINT + if (yytype < YYNTOKENS) + YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep); +# endif + YYUSE (yytype); +} + + +/*--------------------------------. +| Print this symbol on YYOUTPUT. | +`--------------------------------*/ + +static void +yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep) +{ + YYFPRINTF (yyoutput, "%s %s (", + yytype < YYNTOKENS ? "token" : "nterm", yytname[yytype]); + + yy_symbol_value_print (yyoutput, yytype, yyvaluep); + YYFPRINTF (yyoutput, ")"); +} + +/*------------------------------------------------------------------. +| yy_stack_print -- Print the state stack from its BOTTOM up to its | +| TOP (included). | +`------------------------------------------------------------------*/ + +static void +yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop) +{ + YYFPRINTF (stderr, "Stack now"); + for (; yybottom <= yytop; yybottom++) + { + int yybot = *yybottom; + YYFPRINTF (stderr, " %d", yybot); + } + YYFPRINTF (stderr, "\n"); +} + +# define YY_STACK_PRINT(Bottom, Top) \ +do { \ + if (yydebug) \ + yy_stack_print ((Bottom), (Top)); \ +} while (0) + + +/*------------------------------------------------. +| Report that the YYRULE is going to be reduced. | +`------------------------------------------------*/ + +static void +yy_reduce_print (yytype_int16 *yyssp, YYSTYPE *yyvsp, int yyrule) +{ + unsigned long int yylno = yyrline[yyrule]; + int yynrhs = yyr2[yyrule]; + int yyi; + YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n", + yyrule - 1, yylno); + /* The symbols being reduced. */ + for (yyi = 0; yyi < yynrhs; yyi++) + { + YYFPRINTF (stderr, " $%d = ", yyi + 1); + yy_symbol_print (stderr, + yystos[yyssp[yyi + 1 - yynrhs]], + &(yyvsp[(yyi + 1) - (yynrhs)]) + ); + YYFPRINTF (stderr, "\n"); + } +} + +# define YY_REDUCE_PRINT(Rule) \ +do { \ + if (yydebug) \ + yy_reduce_print (yyssp, yyvsp, Rule); \ +} while (0) + +/* Nonzero means print parse trace. It is left uninitialized so that + multiple parsers can coexist. */ +int yydebug; +#else /* !YYDEBUG */ +# define YYDPRINTF(Args) +# define YY_SYMBOL_PRINT(Title, Type, Value, Location) +# define YY_STACK_PRINT(Bottom, Top) +# define YY_REDUCE_PRINT(Rule) +#endif /* !YYDEBUG */ + + +/* YYINITDEPTH -- initial size of the parser's stacks. */ +#ifndef YYINITDEPTH +# define YYINITDEPTH 200 +#endif + +/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only + if the built-in stack extension method is used). + + Do not make this value too large; the results are undefined if + YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH) + evaluated with infinite-precision integer arithmetic. */ + +#ifndef YYMAXDEPTH +# define YYMAXDEPTH 10000 +#endif + + +#if YYERROR_VERBOSE + +# ifndef yystrlen +# if defined __GLIBC__ && defined _STRING_H +# define yystrlen strlen +# else +/* Return the length of YYSTR. */ +static YYSIZE_T +yystrlen (const char *yystr) +{ + YYSIZE_T yylen; + for (yylen = 0; yystr[yylen]; yylen++) + continue; + return yylen; +} +# endif +# endif + +# ifndef yystpcpy +# if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE +# define yystpcpy stpcpy +# else +/* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in + YYDEST. */ +static char * +yystpcpy (char *yydest, const char *yysrc) +{ + char *yyd = yydest; + const char *yys = yysrc; + + while ((*yyd++ = *yys++) != '\0') + continue; + + return yyd - 1; +} +# endif +# endif + +# ifndef yytnamerr +/* Copy to YYRES the contents of YYSTR after stripping away unnecessary + quotes and backslashes, so that it's suitable for yyerror. The + heuristic is that double-quoting is unnecessary unless the string + contains an apostrophe, a comma, or backslash (other than + backslash-backslash). YYSTR is taken from yytname. If YYRES is + null, do not copy; instead, return the length of what the result + would have been. */ +static YYSIZE_T +yytnamerr (char *yyres, const char *yystr) +{ + if (*yystr == '"') + { + YYSIZE_T yyn = 0; + char const *yyp = yystr; + + for (;;) + switch (*++yyp) + { + case '\'': + case ',': + goto do_not_strip_quotes; + + case '\\': + if (*++yyp != '\\') + goto do_not_strip_quotes; + /* Fall through. */ + default: + if (yyres) + yyres[yyn] = *yyp; + yyn++; + break; + + case '"': + if (yyres) + yyres[yyn] = '\0'; + return yyn; + } + do_not_strip_quotes: ; + } + + if (! yyres) + return yystrlen (yystr); + + return yystpcpy (yyres, yystr) - yyres; +} +# endif + +/* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message + about the unexpected token YYTOKEN for the state stack whose top is + YYSSP. + + Return 0 if *YYMSG was successfully written. Return 1 if *YYMSG is + not large enough to hold the message. In that case, also set + *YYMSG_ALLOC to the required number of bytes. Return 2 if the + required number of bytes is too large to store. */ +static int +yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg, + yytype_int16 *yyssp, int yytoken) +{ + YYSIZE_T yysize0 = yytnamerr (YY_NULLPTR, yytname[yytoken]); + YYSIZE_T yysize = yysize0; + enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 }; + /* Internationalized format string. */ + const char *yyformat = YY_NULLPTR; + /* Arguments of yyformat. */ + char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM]; + /* Number of reported tokens (one for the "unexpected", one per + "expected"). */ + int yycount = 0; + + /* There are many possibilities here to consider: + - If this state is a consistent state with a default action, then + the only way this function was invoked is if the default action + is an error action. In that case, don't check for expected + tokens because there are none. + - The only way there can be no lookahead present (in yychar) is if + this state is a consistent state with a default action. Thus, + detecting the absence of a lookahead is sufficient to determine + that there is no unexpected or expected token to report. In that + case, just report a simple "syntax error". + - Don't assume there isn't a lookahead just because this state is a + consistent state with a default action. There might have been a + previous inconsistent state, consistent state with a non-default + action, or user semantic action that manipulated yychar. + - Of course, the expected token list depends on states to have + correct lookahead information, and it depends on the parser not + to perform extra reductions after fetching a lookahead from the + scanner and before detecting a syntax error. Thus, state merging + (from LALR or IELR) and default reductions corrupt the expected + token list. However, the list is correct for canonical LR with + one exception: it will still contain any token that will not be + accepted due to an error action in a later state. + */ + if (yytoken != YYEMPTY) + { + int yyn = yypact[*yyssp]; + yyarg[yycount++] = yytname[yytoken]; + if (!yypact_value_is_default (yyn)) + { + /* Start YYX at -YYN if negative to avoid negative indexes in + YYCHECK. In other words, skip the first -YYN actions for + this state because they are default actions. */ + int yyxbegin = yyn < 0 ? -yyn : 0; + /* Stay within bounds of both yycheck and yytname. */ + int yychecklim = YYLAST - yyn + 1; + int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS; + int yyx; + + for (yyx = yyxbegin; yyx < yyxend; ++yyx) + if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR + && !yytable_value_is_error (yytable[yyx + yyn])) + { + if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM) + { + yycount = 1; + yysize = yysize0; + break; + } + yyarg[yycount++] = yytname[yyx]; + { + YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULLPTR, yytname[yyx]); + if (! (yysize <= yysize1 + && yysize1 <= YYSTACK_ALLOC_MAXIMUM)) + return 2; + yysize = yysize1; + } + } + } + } + + switch (yycount) + { +# define YYCASE_(N, S) \ + case N: \ + yyformat = S; \ + break + YYCASE_(0, YY_("syntax error")); + YYCASE_(1, YY_("syntax error, unexpected %s")); + YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s")); + YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s")); + YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s")); + YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s")); +# undef YYCASE_ + } + + { + YYSIZE_T yysize1 = yysize + yystrlen (yyformat); + if (! (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM)) + return 2; + yysize = yysize1; + } + + if (*yymsg_alloc < yysize) + { + *yymsg_alloc = 2 * yysize; + if (! (yysize <= *yymsg_alloc + && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM)) + *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM; + return 1; + } + + /* Avoid sprintf, as that infringes on the user's name space. + Don't have undefined behavior even if the translation + produced a string with the wrong number of "%s"s. */ + { + char *yyp = *yymsg; + int yyi = 0; + while ((*yyp = *yyformat) != '\0') + if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount) + { + yyp += yytnamerr (yyp, yyarg[yyi++]); + yyformat += 2; + } + else + { + yyp++; + yyformat++; + } + } + return 0; +} +#endif /* YYERROR_VERBOSE */ + +/*-----------------------------------------------. +| Release the memory associated to this symbol. | +`-----------------------------------------------*/ + +static void +yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep) +{ + YYUSE (yyvaluep); + if (!yymsg) + yymsg = "Deleting"; + YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp); + + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + YYUSE (yytype); + YY_IGNORE_MAYBE_UNINITIALIZED_END +} + + + + +/* The lookahead symbol. */ +int yychar; + +/* The semantic value of the lookahead symbol. */ +YYSTYPE yylval; +/* Number of syntax errors so far. */ +int yynerrs; + + +/*----------. +| yyparse. | +`----------*/ + +int +yyparse (void) +{ + int yystate; + /* Number of tokens to shift before error messages enabled. */ + int yyerrstatus; + + /* The stacks and their tools: + 'yyss': related to states. + 'yyvs': related to semantic values. + + Refer to the stacks through separate pointers, to allow yyoverflow + to reallocate them elsewhere. */ + + /* The state stack. */ + yytype_int16 yyssa[YYINITDEPTH]; + yytype_int16 *yyss; + yytype_int16 *yyssp; + + /* The semantic value stack. */ + YYSTYPE yyvsa[YYINITDEPTH]; + YYSTYPE *yyvs; + YYSTYPE *yyvsp; + + YYSIZE_T yystacksize; + + int yyn; + int yyresult; + /* Lookahead token as an internal (translated) token number. */ + int yytoken = 0; + /* The variables used to return semantic value and location from the + action routines. */ + YYSTYPE yyval; + +#if YYERROR_VERBOSE + /* Buffer for error messages, and its allocated size. */ + char yymsgbuf[128]; + char *yymsg = yymsgbuf; + YYSIZE_T yymsg_alloc = sizeof yymsgbuf; +#endif + +#define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N)) + + /* The number of symbols on the RHS of the reduced rule. + Keep to zero when no symbol should be popped. */ + int yylen = 0; + + yyssp = yyss = yyssa; + yyvsp = yyvs = yyvsa; + yystacksize = YYINITDEPTH; + + YYDPRINTF ((stderr, "Starting parse\n")); + + yystate = 0; + yyerrstatus = 0; + yynerrs = 0; + yychar = YYEMPTY; /* Cause a token to be read. */ + goto yysetstate; + +/*------------------------------------------------------------. +| yynewstate -- Push a new state, which is found in yystate. | +`------------------------------------------------------------*/ + yynewstate: + /* In all cases, when you get here, the value and location stacks + have just been pushed. So pushing a state here evens the stacks. */ + yyssp++; + + yysetstate: + *yyssp = yystate; + + if (yyss + yystacksize - 1 <= yyssp) + { + /* Get the current used size of the three stacks, in elements. */ + YYSIZE_T yysize = yyssp - yyss + 1; + +#ifdef yyoverflow + { + /* Give user a chance to reallocate the stack. Use copies of + these so that the &'s don't force the real ones into + memory. */ + YYSTYPE *yyvs1 = yyvs; + yytype_int16 *yyss1 = yyss; + + /* Each stack pointer address is followed by the size of the + data in use in that stack, in bytes. This used to be a + conditional around just the two extra args, but that might + be undefined if yyoverflow is a macro. */ + yyoverflow (YY_("memory exhausted"), + &yyss1, yysize * sizeof (*yyssp), + &yyvs1, yysize * sizeof (*yyvsp), + &yystacksize); + + yyss = yyss1; + yyvs = yyvs1; + } +#else /* no yyoverflow */ +# ifndef YYSTACK_RELOCATE + goto yyexhaustedlab; +# else + /* Extend the stack our own way. */ + if (YYMAXDEPTH <= yystacksize) + goto yyexhaustedlab; + yystacksize *= 2; + if (YYMAXDEPTH < yystacksize) + yystacksize = YYMAXDEPTH; + + { + yytype_int16 *yyss1 = yyss; + union yyalloc *yyptr = + (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize)); + if (! yyptr) + goto yyexhaustedlab; + YYSTACK_RELOCATE (yyss_alloc, yyss); + YYSTACK_RELOCATE (yyvs_alloc, yyvs); +# undef YYSTACK_RELOCATE + if (yyss1 != yyssa) + YYSTACK_FREE (yyss1); + } +# endif +#endif /* no yyoverflow */ + + yyssp = yyss + yysize - 1; + yyvsp = yyvs + yysize - 1; + + YYDPRINTF ((stderr, "Stack size increased to %lu\n", + (unsigned long int) yystacksize)); + + if (yyss + yystacksize - 1 <= yyssp) + YYABORT; + } + + YYDPRINTF ((stderr, "Entering state %d\n", yystate)); + + if (yystate == YYFINAL) + YYACCEPT; + + goto yybackup; + +/*-----------. +| yybackup. | +`-----------*/ +yybackup: + + /* Do appropriate processing given the current state. Read a + lookahead token if we need one and don't already have one. */ + + /* First try to decide what to do without reference to lookahead token. */ + yyn = yypact[yystate]; + if (yypact_value_is_default (yyn)) + goto yydefault; + + /* Not known => get a lookahead token if don't already have one. */ + + /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */ + if (yychar == YYEMPTY) + { + YYDPRINTF ((stderr, "Reading a token: ")); + yychar = yylex (); + } + + if (yychar <= YYEOF) + { + yychar = yytoken = YYEOF; + YYDPRINTF ((stderr, "Now at end of input.\n")); + } + else + { + yytoken = YYTRANSLATE (yychar); + YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc); + } + + /* If the proper action on seeing token YYTOKEN is to reduce or to + detect an error, take that action. */ + yyn += yytoken; + if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken) + goto yydefault; + yyn = yytable[yyn]; + if (yyn <= 0) + { + if (yytable_value_is_error (yyn)) + goto yyerrlab; + yyn = -yyn; + goto yyreduce; + } + + /* Count tokens shifted since error; after three, turn off error + status. */ + if (yyerrstatus) + yyerrstatus--; + + /* Shift the lookahead token. */ + YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc); + + /* Discard the shifted token. */ + yychar = YYEMPTY; + + yystate = yyn; + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + *++yyvsp = yylval; + YY_IGNORE_MAYBE_UNINITIALIZED_END + + goto yynewstate; + + +/*-----------------------------------------------------------. +| yydefault -- do the default action for the current state. | +`-----------------------------------------------------------*/ +yydefault: + yyn = yydefact[yystate]; + if (yyn == 0) + goto yyerrlab; + goto yyreduce; + + +/*-----------------------------. +| yyreduce -- Do a reduction. | +`-----------------------------*/ +yyreduce: + /* yyn is the number of a rule to reduce with. */ + yylen = yyr2[yyn]; + + /* If YYLEN is nonzero, implement the default value of the action: + '$$ = $1'. + + Otherwise, the following line sets YYVAL to garbage. + This behavior is undocumented and Bison + users should not rely upon it. Assigning to YYVAL + unconditionally makes the parser a bit smaller, and it avoids a + GCC warning that YYVAL may be used uninitialized. */ + yyval = yyvsp[1-yylen]; + + + YY_REDUCE_PRINT (yyn); + switch (yyn) + { + case 3: +#line 59 "wp_parser.y" /* yacc.c:1646 */ + { + wp_defexpr((yyvsp[-1].n)); + } +#line 1251 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 4: +#line 68 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newnumber((yyvsp[0].d)); } +#line 1257 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 5: +#line 69 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newsymbol((yyvsp[0].s)); } +#line 1263 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 6: +#line 70 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newnode(WP_ADD, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1269 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 7: +#line 71 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newnode(WP_SUB, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1275 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 8: +#line 72 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newnode(WP_MUL, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1281 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 9: +#line 73 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newnode(WP_DIV, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1287 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 10: +#line 74 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = (yyvsp[-1].n); } +#line 1293 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 11: +#line 75 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newf2(WP_LT, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1299 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 12: +#line 76 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newf2(WP_GT, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1305 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 13: +#line 77 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newnode(WP_NEG, (yyvsp[0].n), NULL); } +#line 1311 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 14: +#line 78 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newf2(WP_POW, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1317 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 15: +#line 79 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newf1((yyvsp[-3].f1), (yyvsp[-1].n)); } +#line 1323 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 16: +#line 80 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newf2((yyvsp[-5].f2), (yyvsp[-3].n), (yyvsp[-1].n)); } +#line 1329 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + +#line 1333 "wp_parser.tab.c" /* yacc.c:1646 */ + default: break; + } + /* User semantic actions sometimes alter yychar, and that requires + that yytoken be updated with the new translation. We take the + approach of translating immediately before every use of yytoken. + One alternative is translating here after every semantic action, + but that translation would be missed if the semantic action invokes + YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or + if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an + incorrect destructor might then be invoked immediately. In the + case of YYERROR or YYBACKUP, subsequent parser actions might lead + to an incorrect destructor call or verbose syntax error message + before the lookahead is translated. */ + YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc); + + YYPOPSTACK (yylen); + yylen = 0; + YY_STACK_PRINT (yyss, yyssp); + + *++yyvsp = yyval; + + /* Now 'shift' the result of the reduction. Determine what state + that goes to, based on the state we popped back to and the rule + number reduced by. */ + + yyn = yyr1[yyn]; + + yystate = yypgoto[yyn - YYNTOKENS] + *yyssp; + if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp) + yystate = yytable[yystate]; + else + yystate = yydefgoto[yyn - YYNTOKENS]; + + goto yynewstate; + + +/*--------------------------------------. +| yyerrlab -- here on detecting error. | +`--------------------------------------*/ +yyerrlab: + /* Make sure we have latest lookahead translation. See comments at + user semantic actions for why this is necessary. */ + yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar); + + /* If not already recovering from an error, report this error. */ + if (!yyerrstatus) + { + ++yynerrs; +#if ! YYERROR_VERBOSE + yyerror (YY_("syntax error")); +#else +# define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \ + yyssp, yytoken) + { + char const *yymsgp = YY_("syntax error"); + int yysyntax_error_status; + yysyntax_error_status = YYSYNTAX_ERROR; + if (yysyntax_error_status == 0) + yymsgp = yymsg; + else if (yysyntax_error_status == 1) + { + if (yymsg != yymsgbuf) + YYSTACK_FREE (yymsg); + yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc); + if (!yymsg) + { + yymsg = yymsgbuf; + yymsg_alloc = sizeof yymsgbuf; + yysyntax_error_status = 2; + } + else + { + yysyntax_error_status = YYSYNTAX_ERROR; + yymsgp = yymsg; + } + } + yyerror (yymsgp); + if (yysyntax_error_status == 2) + goto yyexhaustedlab; + } +# undef YYSYNTAX_ERROR +#endif + } + + + + if (yyerrstatus == 3) + { + /* If just tried and failed to reuse lookahead token after an + error, discard it. */ + + if (yychar <= YYEOF) + { + /* Return failure if at end of input. */ + if (yychar == YYEOF) + YYABORT; + } + else + { + yydestruct ("Error: discarding", + yytoken, &yylval); + yychar = YYEMPTY; + } + } + + /* Else will try to reuse lookahead token after shifting the error + token. */ + goto yyerrlab1; + + +/*---------------------------------------------------. +| yyerrorlab -- error raised explicitly by YYERROR. | +`---------------------------------------------------*/ +yyerrorlab: + + /* Pacify compilers like GCC when the user code never invokes + YYERROR and the label yyerrorlab therefore never appears in user + code. */ + if (/*CONSTCOND*/ 0) + goto yyerrorlab; + + /* Do not reclaim the symbols of the rule whose action triggered + this YYERROR. */ + YYPOPSTACK (yylen); + yylen = 0; + YY_STACK_PRINT (yyss, yyssp); + yystate = *yyssp; + goto yyerrlab1; + + +/*-------------------------------------------------------------. +| yyerrlab1 -- common code for both syntax error and YYERROR. | +`-------------------------------------------------------------*/ +yyerrlab1: + yyerrstatus = 3; /* Each real token shifted decrements this. */ + + for (;;) + { + yyn = yypact[yystate]; + if (!yypact_value_is_default (yyn)) + { + yyn += YYTERROR; + if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR) + { + yyn = yytable[yyn]; + if (0 < yyn) + break; + } + } + + /* Pop the current state because it cannot handle the error token. */ + if (yyssp == yyss) + YYABORT; + + + yydestruct ("Error: popping", + yystos[yystate], yyvsp); + YYPOPSTACK (1); + yystate = *yyssp; + YY_STACK_PRINT (yyss, yyssp); + } + + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + *++yyvsp = yylval; + YY_IGNORE_MAYBE_UNINITIALIZED_END + + + /* Shift the error token. */ + YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp); + + yystate = yyn; + goto yynewstate; + + +/*-------------------------------------. +| yyacceptlab -- YYACCEPT comes here. | +`-------------------------------------*/ +yyacceptlab: + yyresult = 0; + goto yyreturn; + +/*-----------------------------------. +| yyabortlab -- YYABORT comes here. | +`-----------------------------------*/ +yyabortlab: + yyresult = 1; + goto yyreturn; + +#if !defined yyoverflow || YYERROR_VERBOSE +/*-------------------------------------------------. +| yyexhaustedlab -- memory exhaustion comes here. | +`-------------------------------------------------*/ +yyexhaustedlab: + yyerror (YY_("memory exhausted")); + yyresult = 2; + /* Fall through. */ +#endif + +yyreturn: + if (yychar != YYEMPTY) + { + /* Make sure we have latest lookahead translation. See comments at + user semantic actions for why this is necessary. */ + yytoken = YYTRANSLATE (yychar); + yydestruct ("Cleanup: discarding lookahead", + yytoken, &yylval); + } + /* Do not reclaim the symbols of the rule whose action triggered + this YYABORT or YYACCEPT. */ + YYPOPSTACK (yylen); + YY_STACK_PRINT (yyss, yyssp); + while (yyssp != yyss) + { + yydestruct ("Cleanup: popping", + yystos[*yyssp], yyvsp); + YYPOPSTACK (1); + } +#ifndef yyoverflow + if (yyss != yyssa) + YYSTACK_FREE (yyss); +#endif +#if YYERROR_VERBOSE + if (yymsg != yymsgbuf) + YYSTACK_FREE (yymsg); +#endif + return yyresult; +} +#line 83 "wp_parser.y" /* yacc.c:1906 */ + diff --git a/Source/Parser/wp_parser.tab.h b/Source/Parser/wp_parser.tab.h new file mode 100644 index 000000000..add7999ce --- /dev/null +++ b/Source/Parser/wp_parser.tab.h @@ -0,0 +1,85 @@ +/* A Bison parser, made by GNU Bison 3.0.4. */ + +/* Bison interface for Yacc-like parsers in C + + Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + +#ifndef YY_YY_WP_PARSER_TAB_H_INCLUDED +# define YY_YY_WP_PARSER_TAB_H_INCLUDED +/* Debug traces. */ +#ifndef YYDEBUG +# define YYDEBUG 0 +#endif +#if YYDEBUG +extern int yydebug; +#endif + +/* Token type. */ +#ifndef YYTOKENTYPE +# define YYTOKENTYPE + enum yytokentype + { + NODE = 258, + NUMBER = 259, + SYMBOL = 260, + F1 = 261, + F2 = 262, + EOL = 263, + POW = 264, + NEG = 265 + }; +#endif + +/* Value type. */ +#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED + +union YYSTYPE +{ +#line 19 "wp_parser.y" /* yacc.c:1909 */ + + struct wp_node* n; + double d; + struct wp_symbol* s; + enum wp_f1_t f1; + enum wp_f2_t f2; + +#line 73 "wp_parser.tab.h" /* yacc.c:1909 */ +}; + +typedef union YYSTYPE YYSTYPE; +# define YYSTYPE_IS_TRIVIAL 1 +# define YYSTYPE_IS_DECLARED 1 +#endif + + +extern YYSTYPE yylval; + +int yyparse (void); + +#endif /* !YY_YY_WP_PARSER_TAB_H_INCLUDED */ diff --git a/Source/Parser/wp_parser.y b/Source/Parser/wp_parser.y new file mode 100644 index 000000000..e3a007949 --- /dev/null +++ b/Source/Parser/wp_parser.y @@ -0,0 +1,83 @@ + +%{ + #include <stdio.h> + #include <stdlib.h> + #include <math.h> + #include "wp_parser_y.h" + int yylex (void); +%} + +/* We do not need to make this reentrant safe, because we use flex and + bison for generating AST only and this part doesn't need to be + thread safe. +*/ +/*%define api.pure full */ + +/* This is the type returned by functions wp_new* declared in + wp_parser_y.h. See also bison rules at the end of this file. +*/ +%union { + struct wp_node* n; + double d; + struct wp_symbol* s; + enum wp_f1_t f1; + enum wp_f2_t f2; +} + +/* Define tokens. They are used by flex too. */ +%token <n> NODE +%token <d> NUMBER +%token <s> SYMBOL +%token <f1> F1 +%token <f2> F2 +%token EOL +%token POW "**" '^' + +%nonassoc F1 F2 +%right '=' +%left '+' '-' +%left '*' '/' +%left '<' '>' +%nonassoc NEG +%right POW + +/* This specifies the type of `exp` (i.e., struct wp_node*). Rules + specified later pass `exp` to wp_new* functions declared in + wp_parser_y.h. +*/ +%type <n> exp + +%start input + +%% + +/* Given `\n` terminated input, a tree is generated and passed to + * function wp_defexpr defined in wp_parser_y.c. + */ +input: + %empty +| input exp EOL { + wp_defexpr($2); + } +; + +/* Enum types WP_ADD, WP_SUB, etc. are defined in wp_parser_y.h. + * Functions wp_new* are also declared in that file. + */ +exp: + NUMBER { $$ = wp_newnumber($1); } +| SYMBOL { $$ = wp_newsymbol($1); } +| exp '+' exp { $$ = wp_newnode(WP_ADD, $1, $3); } +| exp '-' exp { $$ = wp_newnode(WP_SUB, $1, $3); } +| exp '*' exp { $$ = wp_newnode(WP_MUL, $1, $3); } +| exp '/' exp { $$ = wp_newnode(WP_DIV, $1, $3); } +| '(' exp ')' { $$ = $2; } +| exp '<' exp { $$ = wp_newf2(WP_LT, $1, $3); } +| exp '>' exp { $$ = wp_newf2(WP_GT, $1, $3); } +| '-'exp %prec NEG { $$ = wp_newnode(WP_NEG, $2, NULL); } +| exp POW exp { $$ = wp_newf2(WP_POW, $1, $3); } +| F1 '(' exp ')' { $$ = wp_newf1($1, $3); } +| F2 '(' exp ',' exp ')' { $$ = wp_newf2($1, $3, $5); } +; + +%% diff --git a/Source/Parser/wp_parser_c.c b/Source/Parser/wp_parser_c.c new file mode 100644 index 000000000..ad7d5ec51 --- /dev/null +++ b/Source/Parser/wp_parser_c.c @@ -0,0 +1,13 @@ +#include "wp_parser_c.h" +#include "wp_parser.lex.h" +#include "wp_parser.tab.h" + +struct wp_parser* +wp_c_parser_new (char const* body) +{ + YY_BUFFER_STATE buffer = yy_scan_string(body); + yyparse(); + struct wp_parser* parser = wp_parser_new(); + yy_delete_buffer(buffer); + return parser; +} diff --git a/Source/Parser/wp_parser_c.h b/Source/Parser/wp_parser_c.h new file mode 100644 index 000000000..1c55433a0 --- /dev/null +++ b/Source/Parser/wp_parser_c.h @@ -0,0 +1,16 @@ +#ifndef WP_PARSER_C_H_ +#define WP_PARSER_C_H_ + +#include "wp_parser_y.h" + +#ifdef __cplusplus +extern "C" { +#endif + + struct wp_parser* wp_c_parser_new (char const* function_body); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Source/Parser/wp_parser_y.c b/Source/Parser/wp_parser_y.c new file mode 100644 index 000000000..c4815a2a9 --- /dev/null +++ b/Source/Parser/wp_parser_y.c @@ -0,0 +1,1082 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include <math.h> +#include "wp_parser_y.h" +#include "wp_parser.tab.h" + +static struct wp_node* wp_root = NULL; + +/* This is called by a bison rule to store the original AST in a + * static variable. Accessing this directly is not thread safe. So + * this will be duplicated later for each thread. + */ +void +wp_defexpr (struct wp_node* body) +{ + wp_root = body; +} + +struct wp_node* +wp_newnumber (double d) +{ + struct wp_number* r = malloc(sizeof(struct wp_number)); + r->type = WP_NUMBER; + r->value = d; + return (struct wp_node*) r; +} + +struct wp_symbol* +wp_makesymbol (char* name) +{ + struct wp_symbol* symbol = malloc(sizeof(struct wp_symbol)); + symbol->type = WP_SYMBOL; + symbol->name = strdup(name); + symbol->pointer = NULL; + return symbol; +} + +struct wp_node* +wp_newsymbol (struct wp_symbol* symbol) +{ + return (struct wp_node*) symbol; +} + +struct wp_node* +wp_newnode (enum wp_node_t type, struct wp_node* l, struct wp_node* r) +{ + struct wp_node* tmp = malloc(sizeof(struct wp_node)); + tmp->type = type; + tmp->l = l; + tmp->r = r; + return tmp; +} + +struct wp_node* +wp_newf1 (enum wp_f1_t ftype, struct wp_node* l) +{ + struct wp_f1* tmp = malloc(sizeof(struct wp_f1)); + tmp->type = WP_F1; + tmp->l = l; + tmp->ftype = ftype; + return (struct wp_node*) tmp; +} + +struct wp_node* +wp_newf2 (enum wp_f2_t ftype, struct wp_node* l, struct wp_node* r) +{ + struct wp_f2* tmp = malloc(sizeof(struct wp_f2)); + tmp->type = WP_F2; + tmp->l = l; + tmp->r = r; + tmp->ftype = ftype; + return (struct wp_node*) tmp; +} + +void +yyerror (char const *s, ...) +{ + va_list vl; + va_start(vl, s); + vfprintf(stderr, s, vl); + fprintf(stderr, "\n"); + va_end(vl); +} + +/*******************************************************************/ + +struct wp_parser* +wp_parser_new (void) +{ + struct wp_parser* my_parser = malloc(sizeof(struct wp_parser)); + + my_parser->sz_mempool = wp_ast_size(wp_root); + my_parser->p_root = malloc(my_parser->sz_mempool); + my_parser->p_free = my_parser->p_root; + + my_parser->ast = wp_parser_ast_dup(my_parser, wp_root,1); /* 1: free the source wp_root */ + + if (my_parser->p_root + my_parser->sz_mempool != my_parser->p_free) { + yyerror("wp_parser_new: error in memory size"); + exit(1); + } + + wp_ast_optimize(my_parser->ast); + + return my_parser; +} + +void +wp_parser_delete (struct wp_parser* parser) +{ + free(parser->p_root); + free(parser); +} + +static size_t +wp_aligned_size (size_t N) +{ + const unsigned int align_size = 16; + size_t x = N + (align_size-1); + x -= x & (align_size-1); + return x; +} + +static +void* +wp_parser_allocate (struct wp_parser* my_parser, size_t N) +{ + void* r = my_parser->p_free; + my_parser->p_free = (char*)r + wp_aligned_size(N); + return r; +} + +struct wp_parser* +wp_parser_dup (struct wp_parser* source) +{ + struct wp_parser* dest = malloc(sizeof(struct wp_parser)); + dest->sz_mempool = source->sz_mempool; + dest->p_root = malloc(dest->sz_mempool); + dest->p_free = dest->p_root; + + dest->ast = wp_parser_ast_dup(dest, source->ast, 0); /* 0: don't free the source */ + + return dest; +} + +static +double +wp_call_f1 (struct wp_f1* f1) +{ + double a = wp_ast_eval(f1->l); + switch (f1->ftype) { + case WP_SQRT: return sqrt(a); + case WP_EXP: return exp(a); + case WP_LOG: return log(a); + case WP_LOG10: return log10(a); + case WP_SIN: return sin(a); + case WP_COS: return cos(a); + case WP_TAN: return tan(a); + case WP_ASIN: return asin(a); + case WP_ACOS: return acos(a); + case WP_ATAN: return atan(a); + case WP_SINH: return sinh(a); + case WP_COSH: return cosh(a); + case WP_TANH: return tanh(a); + case WP_ABS: return fabs(a); + case WP_POW_M3: return 1.0/(a*a*a); + case WP_POW_M2: return 1.0/(a*a); + case WP_POW_M1: return 1.0/a; + case WP_POW_P1: return a; + case WP_POW_P2: return a*a; + case WP_POW_P3: return a*a*a; + default: + yyerror("wp_call_f1: Unknow function %d", f1->ftype); + return 0.0; + } +} + +static +double +wp_call_f2 (struct wp_f2* f2) +{ + double a = wp_ast_eval(f2->l); + double b = wp_ast_eval(f2->r); + switch (f2->ftype) { + case WP_POW: + return pow(a,b); + case WP_GT: + return (a > b) ? 1.0 : 0.0; + case WP_LT: + return (a < b) ? 1.0 : 0.0; + case WP_HEAVISIDE: + return (a < 0.0) ? 0.0 : ((a > 0.0) ? 1.0 : b); + case WP_MIN: + return (a < b) ? a : b; + case WP_MAX: + return (a > b) ? a : b; + default: + yyerror("wp_call_f2: Unknow function %d", f2->ftype); + return 0.0; + } +} + +double +wp_ast_eval (struct wp_node* node) +{ + double result; + + switch (node->type) + { + case WP_NUMBER: + result = ((struct wp_number*)node)->value; + break; + case WP_SYMBOL: + result = *(((struct wp_symbol*)node)->pointer); + break; + case WP_ADD: + result = wp_ast_eval(node->l) + wp_ast_eval(node->r); + break; + case WP_SUB: + result = wp_ast_eval(node->l) - wp_ast_eval(node->r); + break; + case WP_MUL: + result = wp_ast_eval(node->l) * wp_ast_eval(node->r); + break; + case WP_DIV: + result = wp_ast_eval(node->l) / wp_ast_eval(node->r); + break; + case WP_NEG: + result = -wp_ast_eval(node->l); + break; + case WP_F1: + result = wp_call_f1((struct wp_f1*)node); + break; + case WP_F2: + result = wp_call_f2((struct wp_f2*)node); + break; + case WP_ADD_VP: + result = node->lvp.v + *(node->rp); + break; + case WP_ADD_PP: + result = *(node->lvp.p) + *(node->rp); + break; + case WP_SUB_VP: + result = node->lvp.v - *(node->rp); + break; + case WP_SUB_PP: + result = *(node->lvp.p) - *(node->rp); + break; + case WP_MUL_VP: + result = node->lvp.v * *(node->rp); + break; + case WP_MUL_PP: + result = *(node->lvp.p) * *(node->rp); + break; + case WP_DIV_VP: + result = node->lvp.v / *(node->rp); + break; + case WP_DIV_PP: + result = *(node->lvp.p) / *(node->rp); + break; + case WP_NEG_P: + result = -*(node->lvp.p); + break; + default: + yyerror("wp_ast_eval: unknown node type %d\n", node->type); + } + + return result; +} + +size_t +wp_ast_size (struct wp_node* node) +{ + size_t result; + + switch (node->type) + { + case WP_NUMBER: + result = wp_aligned_size(sizeof(struct wp_number)); + break; + case WP_SYMBOL: + result = wp_aligned_size(sizeof(struct wp_symbol)) + + wp_aligned_size(strlen(((struct wp_symbol*)node)->name)+1); + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + result = wp_aligned_size(sizeof(struct wp_node)) + + wp_ast_size(node->l) + wp_ast_size(node->r); + break; + case WP_NEG: + result = wp_aligned_size(sizeof(struct wp_node)) + + wp_ast_size(node->l); + break; + case WP_F1: + result = wp_aligned_size(sizeof(struct wp_f1)) + + wp_ast_size(node->l); + break; + case WP_F2: + result = wp_aligned_size(sizeof(struct wp_f2)) + + wp_ast_size(node->l) + wp_ast_size(node->r); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + result = wp_aligned_size(sizeof(struct wp_node)) + + wp_ast_size(node->r); + break; + case WP_NEG_P: + result = wp_aligned_size(sizeof(struct wp_node)) + + wp_ast_size(node->l); + break; + default: + yyerror("wp_ast_size: unknown node type %d\n", node->type); + exit(1); + } + + return result; +} + +struct wp_node* +wp_parser_ast_dup (struct wp_parser* my_parser, struct wp_node* node, int move) +{ + void* result; + + switch (node->type) + { + case WP_NUMBER: + result = wp_parser_allocate(my_parser, sizeof(struct wp_number)); + memcpy(result, node , sizeof(struct wp_number)); + break; + case WP_SYMBOL: + result = wp_parser_allocate(my_parser, sizeof(struct wp_symbol)); + memcpy(result, node , sizeof(struct wp_symbol)); + ((struct wp_symbol*)result)->name = wp_parser_allocate + (my_parser, strlen(((struct wp_symbol*)node)->name)+1); + strcpy(((struct wp_symbol*)result)->name, + ((struct wp_symbol*)node )->name); + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + result = wp_parser_allocate(my_parser, sizeof(struct wp_node)); + memcpy(result, node , sizeof(struct wp_node)); + ((struct wp_node*)result)->l = wp_parser_ast_dup(my_parser, node->l, move); + ((struct wp_node*)result)->r = wp_parser_ast_dup(my_parser, node->r, move); + break; + case WP_NEG: + result = wp_parser_allocate(my_parser, sizeof(struct wp_node)); + memcpy(result, node , sizeof(struct wp_node)); + ((struct wp_node*)result)->l = wp_parser_ast_dup(my_parser, node->l, move); + ((struct wp_node*)result)->r = NULL; + break; + case WP_F1: + result = wp_parser_allocate(my_parser, sizeof(struct wp_f1)); + memcpy(result, node , sizeof(struct wp_f1)); + ((struct wp_f1*)result)->l = wp_parser_ast_dup(my_parser, ((struct wp_f1*)node)->l, move); + break; + case WP_F2: + result = wp_parser_allocate(my_parser, sizeof(struct wp_f2)); + memcpy(result, node , sizeof(struct wp_f2)); + ((struct wp_f2*)result)->l = wp_parser_ast_dup(my_parser, ((struct wp_f2*)node)->l, move); + ((struct wp_f2*)result)->r = wp_parser_ast_dup(my_parser, ((struct wp_f2*)node)->r, move); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + result = wp_parser_allocate(my_parser, sizeof(struct wp_node)); + memcpy(result, node , sizeof(struct wp_node)); + ((struct wp_node*)result)->r = wp_parser_ast_dup(my_parser, node->r, move); + break; + case WP_NEG_P: + result = wp_parser_allocate(my_parser, sizeof(struct wp_node)); + memcpy(result, node , sizeof(struct wp_node)); + ((struct wp_node*)result)->l = wp_parser_ast_dup(my_parser, node->l, move); + break; + default: + yyerror("wp_ast_dup: unknown node type %d\n", node->type); + exit(1); + } + if (move) { + /* Note that we only do this on the original AST. We do not + * need to call free for AST stored in wp_parser because the + * memory is not allocated with malloc directly. + */ + if (node->type == WP_SYMBOL) { + free(((struct wp_symbol*)node)->name); + } + free((void*)node); + } + return (struct wp_node*)result; +} + +#define WP_MOVEUP_R(node, v) \ + struct wp_node* n = node->r->r; \ + double* p = node->r->rp; \ + node->r = n; \ + node->lvp.v = v; \ + node->rp = p; +#define WP_MOVEUP_L(node, v) \ + struct wp_node* n = node->l->r; \ + double* p = node->l->rp; \ + node->r = n; \ + node->lvp.v = v; \ + node->rp = p; +#define WP_EVAL_R(node) node->r->lvp.v +#define WP_EVAL_L(node) node->l->lvp.v + +#define WP_NEG_MOVEUP(node) \ + node->r = node->l->r; \ + node->lvp.v = -node->l->lvp.v; \ + node->rp = node->l->rp; + +void +wp_ast_optimize (struct wp_node* node) +{ + /* No need to free memory because we only call this on ASTs in + * wp_parser that are allocated from the memory pool. + */ + switch (node->type) + { + case WP_NUMBER: + case WP_SYMBOL: + break; + case WP_ADD: + case WP_ADD_PP: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SYMBOL) + { + node->lvp.v = wp_ast_eval(node->l); + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_NUMBER) + { + node->lvp.v = wp_ast_eval(node->r); + node->rp = ((struct wp_symbol*)(node->l))->pointer; + node->r = node->l; + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_ADD_PP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_ADD_VP) + { + double v = wp_ast_eval(node->l) + WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SUB_VP) + { + double v = wp_ast_eval(node->l) + WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_SUB_VP; + } + else if (node->l->type == WP_ADD_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) + wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SUB_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) + wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_SUB_VP; + } + break; + case WP_SUB: + case WP_SUB_PP: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SYMBOL) + { + node->lvp.v = wp_ast_eval(node->l); + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_SUB_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_NUMBER) + { + node->lvp.v = -wp_ast_eval(node->r); + node->rp = ((struct wp_symbol*)(node->l))->pointer; + node->r = node->l; + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_SUB_PP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_ADD_VP) + { + double v = wp_ast_eval(node->l) - WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_SUB_VP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SUB_VP) + { + double v = wp_ast_eval(node->l) - WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_ADD_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) - wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SUB_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) - wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_SUB_VP; + } + break; + case WP_MUL: + case WP_MUL_PP: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SYMBOL) + { + node->lvp.v = wp_ast_eval(node->l); + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_NUMBER) + { + node->lvp.v = wp_ast_eval(node->r); + node->rp = ((struct wp_symbol*)(node->l))->pointer; + node->r = node->l; + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_MUL_PP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_MUL_VP) + { + double v = wp_ast_eval(node->l) * WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_DIV_VP) + { + double v = wp_ast_eval(node->l) * WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_DIV_VP; + } + else if (node->l->type == WP_MUL_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) * wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_DIV_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) * wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_DIV_VP; + } + break; + case WP_DIV: + case WP_DIV_PP: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SYMBOL) + { + node->lvp.v = wp_ast_eval(node->l); + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_DIV_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_NUMBER) + { + node->lvp.v = 1./wp_ast_eval(node->r); + node->rp = ((struct wp_symbol*)(node->l))->pointer; + node->r = node->l; + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_DIV_PP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_MUL_VP) + { + double v = wp_ast_eval(node->l) / WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_DIV_VP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_DIV_VP) + { + double v = wp_ast_eval(node->l) / WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_MUL_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) / wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_DIV_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) / wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_DIV_VP; + } + break; + case WP_NEG: + wp_ast_optimize(node->l); + if (node->l->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->type = WP_NEG_P; + } + else if (node->l->type == WP_ADD_VP) + { + WP_NEG_MOVEUP(node); + node->type = WP_SUB_VP; + } + else if (node->l->type == WP_SUB_VP) + { + WP_NEG_MOVEUP(node); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_MUL_VP) + { + WP_NEG_MOVEUP(node); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_DIV_VP) + { + WP_NEG_MOVEUP(node); + node->type = WP_DIV_VP; + } + break; + case WP_F1: + wp_ast_optimize(node->l); + if (node->l->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_F2: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->r->type == WP_NUMBER && ((struct wp_f2*)node)->ftype == WP_POW) + { + struct wp_node* n = node->l; + double v = wp_ast_eval(node->r); + if (-3.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_M3; + } else if (-2.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_M2; + } else if (-1.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_M1; + } else if (0.0 == v) { + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = 1.0; + } else if (1.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_P1; + } else if (2.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_P2; + } else if (3.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_P3; + } + } + break; + case WP_ADD_VP: + wp_ast_optimize(node->r); + if (node->r->type == WP_NUMBER) + { + double v = node->lvp.v + wp_ast_eval(node->r); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_SUB_VP: + wp_ast_optimize(node->r); + if (node->r->type == WP_NUMBER) + { + double v = node->lvp.v - wp_ast_eval(node->r); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_MUL_VP: + wp_ast_optimize(node->r); + if (node->r->type == WP_NUMBER) + { + double v = node->lvp.v * wp_ast_eval(node->r); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_DIV_VP: + wp_ast_optimize(node->r); + if (node->r->type == WP_NUMBER) + { + double v = node->lvp.v / wp_ast_eval(node->r); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_NEG_P: + wp_ast_optimize(node->l); + if (node->l->type == WP_NUMBER) + { + double v = -wp_ast_eval(node->l); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + default: + yyerror("wp_ast_optimize: unknown node type %d\n", node->type); + exit(1); + } +} + +static +void +wp_ast_print_f1 (struct wp_f1* f1) +{ + wp_ast_print(f1->l); + switch (f1->ftype) { + case WP_SQRT: printf("SQRT\n"); break; + case WP_EXP: printf("EXP\n"); break; + case WP_LOG: printf("LOG\n"); break; + case WP_LOG10: printf("LOG10\n"); break; + case WP_SIN: printf("SIN\n"); break; + case WP_COS: printf("COS\n"); break; + case WP_TAN: printf("TAN\n"); break; + case WP_ASIN: printf("ASIN\n"); break; + case WP_ACOS: printf("ACOS\n"); break; + case WP_ATAN: printf("ATAN\n"); break; + case WP_SINH: printf("SINH\n"); break; + case WP_COSH: printf("COSH\n"); break; + case WP_TANH: printf("TANH\n"); break; + case WP_ABS: printf("ABS\n"); break; + case WP_POW_M3: printf("POW(,-3)\n"); break; + case WP_POW_M2: printf("POW(,-2)\n"); break; + case WP_POW_M1: printf("POW(,-1)\n"); break; + case WP_POW_P1: printf("POW(,1)\n"); break; + case WP_POW_P2: printf("POW(,2)\n"); break; + case WP_POW_P3: printf("POW(,3)\n"); break; + default: + yyerror("wp_ast+print_f1: Unknow function %d", f1->ftype); + } +} + +static +void +wp_ast_print_f2 (struct wp_f2* f2) +{ + wp_ast_print(f2->l); + wp_ast_print(f2->r); + switch (f2->ftype) { + case WP_POW: + printf("POW\n"); + break; + case WP_GT: + printf("GT\n"); + break; + case WP_LT: + printf("LT\n"); + break; + case WP_HEAVISIDE: + printf("HEAVISIDE\n"); + break; + case WP_MIN: + printf("MIN\n"); + break; + case WP_MAX: + printf("MAX\n"); + break; + default: + yyerror("wp_ast_print_f2: Unknow function %d", f2->ftype); + } +} + +void +wp_ast_print (struct wp_node* node) +{ + switch (node->type) + { + case WP_NUMBER: + printf("NUMBER: %.17g\n", wp_ast_eval(node)); + break; + case WP_SYMBOL: + printf("VARIABLE: %s\n", ((struct wp_symbol*)node)->name); + break; + case WP_ADD: + wp_ast_print(node->l); + wp_ast_print(node->r); + printf("ADD\n"); + break; + case WP_SUB: + wp_ast_print(node->l); + wp_ast_print(node->r); + printf("SUB\n"); + break; + case WP_MUL: + wp_ast_print(node->l); + wp_ast_print(node->r); + printf("MUL\n"); + break; + case WP_DIV: + wp_ast_print(node->l); + wp_ast_print(node->r); + printf("DIV\n"); + break; + case WP_NEG: + wp_ast_print(node->l); + printf("NEG\n"); + break; + case WP_F1: + wp_ast_print_f1((struct wp_f1*)node); + break; + case WP_F2: + wp_ast_print_f2((struct wp_f2*)node); + break; + case WP_ADD_VP: + printf("ADD: %.17g %s\n", node->lvp.v, ((struct wp_symbol*)(node->r))->name); + break; + case WP_SUB_VP: + printf("SUM: %.17g %s\n", node->lvp.v, ((struct wp_symbol*)(node->r))->name); + break; + case WP_MUL_VP: + printf("MUL: %.17g %s\n", node->lvp.v, ((struct wp_symbol*)(node->r))->name); + break; + case WP_DIV_VP: + printf("DIV: %.17g %s\n", node->lvp.v, ((struct wp_symbol*)(node->r))->name); + break; + case WP_NEG_P: + printf("NEG: %s\n", ((struct wp_symbol*)(node->l))->name); + break; + case WP_ADD_PP: + printf("ADD: %s %s\n", ((struct wp_symbol*)(node->l))->name, + ((struct wp_symbol*)(node->r))->name); + break; + case WP_SUB_PP: + printf("SUB: %s %s\n", ((struct wp_symbol*)(node->l))->name, + ((struct wp_symbol*)(node->r))->name); + break; + case WP_MUL_PP: + printf("MUL: %s %s\n", ((struct wp_symbol*)(node->l))->name, + ((struct wp_symbol*)(node->r))->name); + break; + case WP_DIV_PP: + printf("DIV: %s %s\n", ((struct wp_symbol*)(node->l))->name, + ((struct wp_symbol*)(node->r))->name); + break; + default: + yyerror("wp_ast_print: unknown node type %d\n", node->type); + exit(1); + } +} + +void +wp_ast_regvar (struct wp_node* node, char const* name, double* p) +{ + switch (node->type) + { + case WP_NUMBER: + break; + case WP_SYMBOL: + if (strcmp(name, ((struct wp_symbol*)node)->name) == 0) { + ((struct wp_symbol*)node)->pointer = p; + } + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + wp_ast_regvar(node->l, name, p); + wp_ast_regvar(node->r, name, p); + break; + case WP_NEG: + wp_ast_regvar(node->l, name, p); + break; + case WP_F1: + wp_ast_regvar(node->l, name, p); + break; + case WP_F2: + wp_ast_regvar(node->l, name, p); + wp_ast_regvar(node->r, name, p); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + wp_ast_regvar(node->r, name, p); + node->rp = ((struct wp_symbol*)(node->r))->pointer; + break; + case WP_NEG_P: + wp_ast_regvar(node->l, name, p); + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + break; + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + wp_ast_regvar(node->l, name, p); + wp_ast_regvar(node->r, name, p); + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + break; + default: + yyerror("wp_ast_regvar: unknown node type %d\n", node->type); + exit(1); + } +} + +void wp_ast_setconst (struct wp_node* node, char const* name, double c) +{ + switch (node->type) + { + case WP_NUMBER: + break; + case WP_SYMBOL: + if (strcmp(name, ((struct wp_symbol*)node)->name) == 0) { + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = c; + } + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + wp_ast_setconst(node->l, name, c); + wp_ast_setconst(node->r, name, c); + break; + case WP_NEG: + wp_ast_setconst(node->l, name, c); + break; + case WP_F1: + wp_ast_setconst(node->l, name, c); + break; + case WP_F2: + wp_ast_setconst(node->l, name, c); + wp_ast_setconst(node->r, name, c); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + wp_ast_setconst(node->r, name, c); + break; + case WP_NEG_P: + wp_ast_setconst(node->l, name, c); + break; + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + wp_ast_setconst(node->l, name, c); + wp_ast_setconst(node->r, name, c); + break; + default: + yyerror("wp_ast_setconst: unknown node type %d\n", node->type); + exit(1); + } +} + +void +wp_parser_regvar (struct wp_parser* parser, char const* name, double* p) +{ + wp_ast_regvar(parser->ast, name, p); +} + +void +wp_parser_setconst (struct wp_parser* parser, char const* name, double c) +{ + wp_ast_setconst(parser->ast, name, c); + wp_ast_optimize(parser->ast); +} + diff --git a/Source/Parser/wp_parser_y.h b/Source/Parser/wp_parser_y.h new file mode 100644 index 000000000..9c1a0d448 --- /dev/null +++ b/Source/Parser/wp_parser_y.h @@ -0,0 +1,157 @@ +#ifndef WP_PARSER_Y_H_ +#define WP_PARSER_Y_H_ + +#ifdef __cplusplus +#include <cstdlib> +extern "C" { +#else +#include <stdlib.h> +#endif + +enum wp_f1_t { // Bulit-in functions with one argument + WP_SQRT = 1, + WP_EXP, + WP_LOG, + WP_LOG10, + WP_SIN, + WP_COS, + WP_TAN, + WP_ASIN, + WP_ACOS, + WP_ATAN, + WP_SINH, + WP_COSH, + WP_TANH, + WP_ABS, + WP_POW_M3, + WP_POW_M2, + WP_POW_M1, + WP_POW_P1, + WP_POW_P2, + WP_POW_P3 +}; + +enum wp_f2_t { // Built-in functions with two arguments + WP_POW = 1, + WP_GT, + WP_LT, + WP_HEAVISIDE, + WP_MIN, + WP_MAX +}; + +enum wp_node_t { + WP_NUMBER = 1, + WP_SYMBOL, + WP_ADD, + WP_SUB, + WP_MUL, + WP_DIV, + WP_NEG, + WP_F1, + WP_F2, + WP_ADD_VP, /* types below are generated by optimization */ + WP_ADD_PP, + WP_SUB_VP, + WP_SUB_PP, + WP_MUL_VP, + WP_MUL_PP, + WP_DIV_VP, + WP_DIV_PP, + WP_NEG_P +}; + +/* In C, the address of the first member of a struct is the same as + * the address of the struct itself. Because of this, all struct wp_* + * pointers can be passed around as struct wp_node pointer and enum + * wp_node_t type can be safely checked to determine their real type. + */ + +union wp_vp { + double v; + double* p; +}; + +struct wp_node { + enum wp_node_t type; + struct wp_node* l; + struct wp_node* r; + union wp_vp lvp; // After optimization, this may store left value/pointer. + double* rp; // this may store right pointer. +}; + +struct wp_number { + enum wp_node_t type; + double value; +}; + +struct wp_symbol { + enum wp_node_t type; + char* name; + double* pointer; +}; + +struct wp_f1 { /* Builtin functions with one argument */ + enum wp_node_t type; + struct wp_node* l; + enum wp_f1_t ftype; +}; + +struct wp_f2 { /* Builtin functions with two arguments */ + enum wp_node_t type; + struct wp_node* l; + struct wp_node* r; + enum wp_f2_t ftype; +}; + +/*******************************************************************/ + +/* These functions are used in bison rules to generate the original + * AST. */ +void wp_defexpr (struct wp_node* body); +struct wp_node* wp_newnumber (double d); +struct wp_symbol* wp_makesymbol (char* name); +struct wp_node* wp_newsymbol (struct wp_symbol* sym); +struct wp_node* wp_newnode (enum wp_node_t type, struct wp_node* l, + struct wp_node* r); +struct wp_node* wp_newf1 (enum wp_f1_t ftype, struct wp_node* l); +struct wp_node* wp_newf2 (enum wp_f2_t ftype, struct wp_node* l, + struct wp_node* r); + +void yyerror (char const *s, ...); + +/*******************************************************************/ + +/* This is our struct for storing AST in a more packed way. The whole + * tree is stored in a contiguous chunk of memory starting from void* + * p_root with a size of sz_mempool. + */ +struct wp_parser { + void* p_root; + void* p_free; + struct wp_node* ast; + size_t sz_mempool; +}; + +struct wp_parser* wp_parser_new (void); +void wp_parser_delete (struct wp_parser* parser); + +struct wp_parser* wp_parser_dup (struct wp_parser* source); +struct wp_node* wp_parser_ast_dup (struct wp_parser* parser, struct wp_node* src, int move); + +void wp_parser_regvar (struct wp_parser* parser, char const* name, double* p); +void wp_parser_setconst (struct wp_parser* parser, char const* name, double c); + +/* We need to walk the tree in these functions */ +double wp_ast_eval (struct wp_node* node); +void wp_ast_optimize (struct wp_node* node); +size_t wp_ast_size (struct wp_node* node); +void wp_ast_print (struct wp_node* node); +void wp_ast_regvar (struct wp_node* node, char const* name, double* p); +void wp_ast_setconst (struct wp_node* node, char const* name, double c); + +#ifdef __cplusplus +} +#endif + +#endif |