Debian Codegraph @

src/fparser.f90

raw
1 !
2 ! Copyright (c) 2000-2008, Roland Schmehl. All rights reserved.
3 !
4 ! This software is distributable under the BSD license. See the terms of the
5 ! BSD license in the documentation provided with this software.
6 !
7 module fparser
8 !------- -------- --------- --------- --------- --------- --------- --------- -------
9 ! Fortran 90 function parser v1.1
10 !------- -------- --------- --------- --------- --------- --------- --------- -------
11 !
12 ! This function parser module is intended for applications where a set of mathematical
13 ! fortran-style expressions is specified at runtime and is then evaluated for a large
14 ! number of variable values. This is done by compiling the set of function strings
15 ! into byte code, which is interpreted efficiently for the various variable values.
16 !
17 ! The source code is available from http://fparser.sourceforge.net
18 !
19 ! Please send comments, corrections or questions to the author:
20 ! Roland Schmehl <[email protected]>
21 !
22 !------- -------- --------- --------- --------- --------- --------- --------- -------
23 ! The function parser concept is based on a C++ class library written by Juha
24 ! Nieminen <[email protected]> available from http://warp.povusers.org/FunctionParser/
25 !------- -------- --------- --------- --------- --------- --------- --------- -------
26 !
27 ! Modifications by D, Price for integration in SPLASH:
28 ! 7th Aug 2009: added checkf interface routine to check syntax without compiling code
29 ! added endf routine to stop memory leaks, also called from initf if needed
30 ! bug fix with error message for sqrt(-ve)
31 !
32 ! 9th Aug 2009: added Mathematical constant recognition (pi)
33 !
34 ! 27th Jan 2010: check for -ve numbers to fractional powers and zero to negative power added
35 !
36 ! 19th Oct 2016: added Fortran 2008 intrinsic functions
37 ! added optional iErrType argument for error message printing
38 !
39 ! 5th Mar 2019: added atan2 function (and parsing of functions with two arguments)
40
41 implicit none
42 !--modification here by D.Price: define type parameters here rather than in a separate module
43 integer, parameter, public :: rn = KIND(0.0d0) ! Precision of real numbers
44 !--modification (22/6/2018), adjusted integer kind to allow more than 128 variables
45 integer, parameter, private :: is = SELECTED_INT_KIND(4) ! Data type of bytecode
46 !--end modification
47
48 !------- -------- --------- --------- --------- --------- --------- --------- -------
49 PUBLIC :: initf, & ! Initialize function parser for n functions
50 parsef, & ! Parse single function string
51 evalf, & ! Evaluate single function
52 checkf, & ! Check syntax in a function string
53 endf, & ! Clean up memory once finished
54 EvalErrMsg ! Error message (Use only when EvalErrType>0)
55 integer, PUBLIC :: EvalErrType ! =0: no error occured, >0: evaluation error
56 !--modification by D. Price: add parseErr parameter (used in checkf)
57 integer, PRIVATE :: ParseErrType ! =0: no error occured, >0: parse error
58 !--modification by D. Price: add verboseness internal variable (used in checkf)
59 LOGICAL, PRIVATE :: PrintErrors = .true. ! =0: no error occured, >0: parse error
60 !--modification by J. Wurster: added cgs and code versions of mu0
61 real, PUBLIC :: mu0=1.0_rn ! =1 for code units, 4pi for cgs
62 !------- -------- --------- --------- --------- --------- --------- --------- -------
63 PRIVATE
64 SAVE
65 integer(is), PARAMETER :: cImmed = 1, &
66 cNeg = 2, &
67 cAdd = 3, &
68 cSub = 4, &
69 cMul = 5, &
70 cDiv = 6, &
71 cPow = 7, &
72 cAbs = 8, &
73 cExp = 9, &
74 cLog10 = 10, &
75 cLog = 11, &
76 cSqrt = 12, &
77 cSinh = 13, &
78 cCosh = 14, &
79 cTanh = 15, &
80 cSin = 16, &
81 cCos = 17, &
82 cTan = 18, &
83 cAsin = 19, &
84 cAcos = 20, &
85 cAtan2 = 21, &
86 cAtan = 22, &
87 cBesj0 = 23, &
88 cBesj1 = 24, &
89 cBesy0 = 25, &
90 cBesy1 = 26, &
91 cerfcs = 27, &
92 cerfc = 28, &
93 cerf = 29, &
94 cgamma = 30, &
95 VarBegin = 31
96 CHARACTER (LEN=1), dimension(cAdd:cPow), PARAMETER :: Ops = (/ '+', &
97 '-', &
98 '*', &
99 '/', &
100 '^' /)
101 CHARACTER (LEN=5), dimension(cAbs:cgamma), PARAMETER :: Funcs = (/'abs ', &
102 'exp ', &
103 'log10', &
104 'log ', &
105 'sqrt ', &
106 'sinh ', &
107 'cosh ', &
108 'tanh ', &
109 'sin ', &
110 'cos ', &
111 'tan ', &
112 'asin ', &
113 'acos ', &
114 'atan2', &
115 'atan ', &
116 'besj0', &
117 'besj1', &
118 'besy0', &
119 'besy1', &
120 'erfcs', &
121 'erfc ', &
122 'erf ', &
123 'gamf ' /)
124 TYPE tComp
125 integer(is), dimension(:), POINTER :: ByteCode
126 integer :: ByteCodeSize
127 real(rn), dimension(:), POINTER :: Immed
128 integer :: ImmedSize
129 real(rn), dimension(:), POINTER :: Stack
130 integer :: StackSize, &
131 StackPtr
132 END TYPE tComp
133 TYPE (tComp), dimension(:), POINTER :: Comp ! Bytecode
134 integer, dimension(:), ALLOCATABLE :: ipos ! Associates function strings
135 !
136 contains
137 !
138 subroutine initf (n)
139 !----- -------- --------- --------- --------- --------- --------- --------- -------
140 ! Initialize function parser for n functions
141 !----- -------- --------- --------- --------- --------- --------- --------- -------
142 IMPLICIT NONE
143 integer, INTENT(in) :: n ! Number of functions
144 integer :: i
145 !----- -------- --------- --------- --------- --------- --------- --------- -------
146 if (ASSOCIATED(Comp)) then
147 print "(a)",' fparser warning: initf called repeatedly without prior call to endf'
148 CALL endf
149 ENDIF
150 ALLOCATE (Comp(n))
151 DO i=1,n
152 NULLIFY (Comp(i)%ByteCode,Comp(i)%Immed,Comp(i)%Stack)
153 end do
154 end subroutine initf
155 !
156 subroutine endf()
157 !----- -------- --------- --------- --------- --------- --------- --------- -------
158 ! Clean up memory at the end of the function parsing/evaluation calls (D. Price)
159 !----- -------- --------- --------- --------- --------- --------- --------- -------
160 IMPLICIT NONE
161 integer :: i
162 !----- -------- --------- --------- --------- --------- --------- --------- -------
163 if (ASSOCIATED(Comp)) then
164 DO i=1,size(Comp)
165 if (ASSOCIATED(Comp(i)%ByteCode)) DEALLOCATE ( Comp(i)%ByteCode, &
166 Comp(i)%Immed, &
167 Comp(i)%Stack )
168 enddo
169 DEALLOCATE(Comp)
170 ENDIF
171 end subroutine endf
172 !
173 subroutine parsef (i, FuncStr, Var, err, Verbose)
174 !----- -------- --------- --------- --------- --------- --------- --------- -------
175 ! Parse ith function string FuncStr and compile it into bytecode
176 !----- -------- --------- --------- --------- --------- --------- --------- -------
177 IMPLICIT NONE
178 integer, INTENT(in) :: i ! Function identifier
179 CHARACTER (LEN=*), INTENT(in) :: FuncStr ! Function string
180 CHARACTER (LEN=*), dimension(:), INTENT(in) :: Var ! Array with variable names
181 CHARACTER (LEN=LEN(FuncStr)) :: Func ! Function string, local use
182 integer, INTENT(OUT), OPTIONAL :: err
183 LOGICAL, INTENT(IN), OPTIONAL :: Verbose ! Turn error messages on/off
184 !----- -------- --------- --------- --------- --------- --------- --------- -------
185 if (i < 1 .OR. i > SIZE(Comp)) then
186 write(*,*) '*** Parser error: Function number ',i,' out of range'
187 if (present(err)) err = 1
188 return
189 END IF
190 EvalErrType = 0 ! D. Price : to prevent accidental misuse
191 ParseErrType = 0
192 PrintErrors = .true.
193 if (present(Verbose)) PrintErrors = Verbose
194
195 ALLOCATE (ipos(LEN(Func))) ! Char. positions in orig. string
196 Func = FuncStr ! Local copy of function string
197 CALL Replace ('**','^ ',Func) ! Exponent into 1-Char. format
198 CALL RemoveSpaces (Func) ! Condense function string
199 !CALL GetConstants (Func)
200 CALL CheckSyntax (Func,FuncStr,Var)
201 DEALLOCATE (ipos)
202 if (present(err)) err = ParseErrType
203 PrintErrors = .true. ! reset this to true
204
205 !--D. Price: return after ParseErr here instead of stop inside CheckSyntax
206 if (ParseErrType /= 0) return
207 CALL Compile (i,Func,Var) ! Compile into bytecode
208 end subroutine parsef
209 !
210 integer FUNCTION checkf(FuncStr, Var, Verbose)
211 !----- -------- --------- --------- --------- --------- --------- --------- -------
212 ! Check syntax in a function string (added by D. Price) but do not compile it
213 ! Returns an error code NOT related to ErrMsg
214 ! Optional variable "verbose" determines whether or not error messages are printed
215 !----- -------- --------- --------- --------- --------- --------- --------- -------
216 IMPLICIT NONE
217 CHARACTER (LEN=*), INTENT(IN) :: FuncStr ! Function string
218 CHARACTER (LEN=*), dimension(:), INTENT(IN) :: Var ! Array with variable names
219 LOGICAL, INTENT(IN), OPTIONAL :: Verbose ! Turn error messages on/off
220 CHARACTER (LEN=LEN(FuncStr)) :: Func ! Function string, local use
221 !----- -------- --------- --------- --------- --------- --------- --------- -------
222 EvalErrType = 0 ! D. Price : to prevent accidental misuse
223 ParseErrType = 0
224 PrintErrors = .true.
225 if (present(Verbose)) PrintErrors = Verbose
226
227 ALLOCATE (ipos(LEN(Func))) ! Char. positions in orig. string
228 Func = FuncStr ! Local copy of function string
229 CALL Replace ('**','^ ',Func) ! Exponent into 1-Char. format
230 CALL RemoveSpaces (Func) ! Condense function string
231 !CALL GetConstants (Func)
232 CALL CheckSyntax (Func,FuncStr,Var)
233 DEALLOCATE (ipos)
234
235 PrintErrors = .true. ! reset this to true
236 checkf = ParseErrType
237 END FUNCTION checkf
238 !
239 FUNCTION evalf (i, Val) RESULT (res)
240 !----- -------- --------- --------- --------- --------- --------- --------- -------
241 ! Evaluate bytecode of ith function for the values passed in array Val(:)
242 !----- -------- --------- --------- --------- --------- --------- --------- -------
243 IMPLICIT NONE
244 integer, INTENT(in) :: i ! Function identifier
245 real(rn), dimension(:), INTENT(in) :: Val ! Variable values
246 real(rn) :: res ! Result
247 integer :: IP, & ! Instruction pointer
248 DP, & ! Data pointer
249 SP ! Stack pointer
250 real(rn), PARAMETER :: zero = 0._rn
251 !----- -------- --------- --------- --------- --------- --------- --------- -------
252 DP = 1
253 SP = 0
254 DO IP=1,Comp(i)%ByteCodeSize
255 SELECT CASE (Comp(i)%ByteCode(IP))
256
257 CASE (cImmed); SP=SP+1; Comp(i)%Stack(SP)=Comp(i)%Immed(DP); DP=DP+1
258 CASE (cNeg); Comp(i)%Stack(SP)=-Comp(i)%Stack(SP)
259 CASE (cAdd); Comp(i)%Stack(SP-1)=Comp(i)%Stack(SP-1)+Comp(i)%Stack(SP); SP=SP-1
260 CASE (cSub); Comp(i)%Stack(SP-1)=Comp(i)%Stack(SP-1)-Comp(i)%Stack(SP); SP=SP-1
261 CASE (cMul); Comp(i)%Stack(SP-1)=Comp(i)%Stack(SP-1)*Comp(i)%Stack(SP); SP=SP-1
262 CASE (cDiv); if (Comp(i)%Stack(SP)==0._rn) then; EvalErrType=1; res=zero; return; ENDIF
263 Comp(i)%Stack(SP-1)=Comp(i)%Stack(SP-1)/Comp(i)%Stack(SP); SP=SP-1
264 ! D. Price: check for zero to negative powers and negative numbers to fractional powers
265 CASE (cPow); if (Comp(i)%Stack(SP-1)==0._rn .and.Comp(i)%Stack(SP)<0._rn) &
266 THEN; EvalErrType=1; res=zero; return; ENDIF
267 if (Comp(i)%Stack(SP-1)<=0._rn .and.(Comp(i)%Stack(SP) /= nint(Comp(i)%Stack(SP)))) &
268 THEN; EvalErrType=5; res=zero; return; ENDIF
269 Comp(i)%Stack(SP-1)=Comp(i)%Stack(SP-1)**Comp(i)%Stack(SP); SP=SP-1
270 CASE (cAbs); Comp(i)%Stack(SP)=ABS(Comp(i)%Stack(SP))
271 CASE (cExp); Comp(i)%Stack(SP)=exp(Comp(i)%Stack(SP))
272 CASE (cLog10); if (Comp(i)%Stack(SP)<=0._rn) then; EvalErrType=3; res=zero; return; ENDIF
273 Comp(i)%Stack(SP)=LOG10(Comp(i)%Stack(SP))
274 CASE (cLog); if (Comp(i)%Stack(SP)<=0._rn) then; EvalErrType=3; res=zero; return; ENDIF
275 Comp(i)%Stack(SP)=log(Comp(i)%Stack(SP))
276 CASE (cSqrt); if (Comp(i)%Stack(SP)<0._rn) then; EvalErrType=2; res=zero; return; ENDIF
277 Comp(i)%Stack(SP)=sqrt(Comp(i)%Stack(SP))
278 CASE (cSinh); Comp(i)%Stack(SP)=SINH(Comp(i)%Stack(SP))
279 CASE (cCosh); Comp(i)%Stack(SP)=COSH(Comp(i)%Stack(SP))
280 CASE (cTanh); Comp(i)%Stack(SP)=TANH(Comp(i)%Stack(SP))
281 CASE (cSin); Comp(i)%Stack(SP)=sin(Comp(i)%Stack(SP))
282 CASE (cCos); Comp(i)%Stack(SP)=cos(Comp(i)%Stack(SP))
283 CASE (cTan); Comp(i)%Stack(SP)=TAN(Comp(i)%Stack(SP))
284 CASE (cAsin); if ((Comp(i)%Stack(SP)<-1._rn).OR.(Comp(i)%Stack(SP)>1._rn)) then
285 EvalErrType=4; res=zero; return; ENDIF
286 Comp(i)%Stack(SP)=asin(Comp(i)%Stack(SP))
287 CASE (cAcos); if ((Comp(i)%Stack(SP)<-1._rn).OR.(Comp(i)%Stack(SP)>1._rn)) then
288 EvalErrType=4; res=zero; return; ENDIF
289 Comp(i)%Stack(SP)=acos(Comp(i)%Stack(SP))
290 CASE (cAtan2); Comp(i)%Stack(SP-1)=atan2(Comp(i)%Stack(SP-1),Comp(i)%Stack(SP)); SP=SP-1
291 CASE (cAtan); Comp(i)%Stack(SP)=atan(Comp(i)%Stack(SP))
292 CASE (cBesj0); Comp(i)%Stack(SP)=bessel_j0(Comp(i)%Stack(SP))
293 CASE (cBesj1); Comp(i)%Stack(SP)=bessel_j1(Comp(i)%Stack(SP))
294 CASE (cBesy0); if (Comp(i)%Stack(SP)<=0._rn) then; EvalErrType=6; res=zero; return; ENDIF
295 Comp(i)%Stack(SP)=bessel_y0(Comp(i)%Stack(SP))
296 CASE (cBesy1); if (Comp(i)%Stack(SP)<=0._rn) then; EvalErrType=7; res=zero; return; ENDIF
297 Comp(i)%Stack(SP)=bessel_y1(Comp(i)%Stack(SP))
298 CASE (cerf); Comp(i)%Stack(SP)=erf(Comp(i)%Stack(SP))
299 CASE (cerfc); Comp(i)%Stack(SP)=erfc(Comp(i)%Stack(SP))
300 CASE (cerfcs); Comp(i)%Stack(SP)=erfc_scaled(Comp(i)%Stack(SP))
301 CASE (cgamma); if (Comp(i)%Stack(SP)==-abs(nint(Comp(i)%Stack(SP)))) then; EvalErrType=8; res=zero; return; ENDIF
302 Comp(i)%Stack(SP)=gamma(Comp(i)%Stack(SP))
303 CASE DEFAULT; SP=SP+1; Comp(i)%Stack(SP)=Val(Comp(i)%ByteCode(IP)-VarBegin+1)
304 END SELECT
305 end do
306 EvalErrType = 0
307 res = Comp(i)%Stack(1)
308 END FUNCTION evalf
309 !
310 subroutine CheckSyntax (Func,FuncStr,Var)
311 !----- -------- --------- --------- --------- --------- --------- --------- -------
312 ! Check syntax of function string, returns 0 if syntax is ok
313 !----- -------- --------- --------- --------- --------- --------- --------- -------
314 IMPLICIT NONE
315 CHARACTER (LEN=*), INTENT(in) :: Func ! Function string without spaces
316 CHARACTER (LEN=*), INTENT(in) :: FuncStr ! Original function string
317 CHARACTER (LEN=*), dimension(:), INTENT(in) :: Var ! Array with variable names
318 integer(is) :: n
319 CHARACTER (LEN=1) :: c
320 real(rn) :: r
321 LOGICAL :: err
322 integer :: ParCnt, & ! Parenthesis counter
323 j,ib,in,lFunc,inold,ibold
324 !----- -------- --------- --------- --------- --------- --------- --------- -------
325 j = 1
326 ParCnt = 0
327 lFunc = LEN_TRIM(Func)
328
329 step: DO
330 if (j > lFunc) then
331 CALL ParseErrMsg (j, FuncStr)
332 EXIT
333 ENDIF
334 c = Func(j:j)
335 !-- -------- --------- --------- --------- --------- --------- --------- -------
336 ! Check for valid operand (must appear)
337 !-- -------- --------- --------- --------- --------- --------- --------- -------
338 if (c == '-' .OR. c == '+') then ! Check for leading - or +
339 j = j+1
340 if (j > lFunc) then
341 CALL ParseErrMsg (j, FuncStr, 'Missing operand')
342 EXIT
343 ENDIF
344 c = Func(j:j)
345 if (ANY(c == Ops) .or. c == ',') then
346 CALL ParseErrMsg (j, FuncStr, 'Multiple operators')
347 EXIT
348 ENDIF
349 END IF
350 n = MathFunctionIndex (Func(j:))
351 if (n > 0) then ! Check for math function
352 j = j+LEN_TRIM(Funcs(n))
353 if (j > lFunc) then
354 CALL ParseErrMsg (j, FuncStr, 'Missing function argument')
355 EXIT
356 ENDIF
357 c = Func(j:j)
358 if (c /= '(') then
359 CALL ParseErrMsg (j, FuncStr, 'Missing opening parenthesis')
360 EXIT
361 ENDIF
362 if (n == cAtan2) then ! Check #args for function with two arguments
363 if (CountArgs(Func(j:)) /= 2) then
364 CALL ParseErrMsg (j, FuncStr, 'Function expects two arguments')
365 EXIT
366 ENDIF
367 ELSE
368 if (CountArgs(Func(j:)) > 1) then
369 CALL ParseErrMsg (j, FuncStr, 'Too many function arguments')
370 EXIT
371 ENDIF
372 ENDIF
373 END IF
374 if (c == '(') then ! Check for opening parenthesis
375 ParCnt = ParCnt+1
376 j = j+1
377 CYCLE step
378 END IF
379 if (SCAN(c,'0123456789.') > 0) then ! Check for number
380 r = RealNum (Func(j:),ib,in,err)
381 if (err) then
382 CALL ParseErrMsg (j, FuncStr, 'Invalid number format: '//Func(j+ib-1:j+in-2))
383 EXIT
384 ENDIF
385 j = j+in-1
386 if (j > lFunc) EXIT
387 c = Func(j:j)
388 ELSE ! Check for variable
389 n = VariableIndex (Func(j:),Var,ib,in)
390 if (n == 0) then ! DP: If not a variable, check for constants
391 ibold = ib
392 inold = in
393 r = MathConst (Func(j:),ib,in,err)
394 if (err) then ! Return error if constants not found
395 CALL ParseErrMsg (j, FuncStr, 'Invalid element: '//Func(j+ib-1:j+in-2))
396 ib = ibold
397 in = inold
398 EXIT
399 ENDIF
400 ENDIF
401 j = j+in-1
402 if (j > lFunc) EXIT
403 c = Func(j:j)
404 END IF
405 DO WHILE (c == ')') ! Check for closing parenthesis
406 ParCnt = ParCnt-1
407 if (ParCnt < 0) CALL ParseErrMsg (j, FuncStr, 'Mismatched parenthesis')
408 if (Func(j-1:j-1) == '(') CALL ParseErrMsg (j-1, FuncStr, 'Empty parentheses')
409 j = j+1
410 if (j > lFunc) EXIT
411 c = Func(j:j)
412 end do
413 !-- -------- --------- --------- --------- --------- --------- --------- -------
414 ! Now, we have a legal operand: A legal operator or end of string must follow
415 !-- -------- --------- --------- --------- --------- --------- --------- -------
416 if (j > lFunc) EXIT
417 if (ANY(c == Ops) .or. c == ',') then ! Check for multiple operators
418 if (j+1 > lFunc) CALL ParseErrMsg (j, FuncStr)
419 if (ANY(Func(j+1:j+1) == Ops)) CALL ParseErrMsg (j+1, FuncStr, 'Multiple operators')
420 ELSE ! Check for next operand
421 CALL ParseErrMsg (j, FuncStr, 'Missing operator')
422 END IF
423 !-- -------- --------- --------- --------- --------- --------- --------- -------
424 ! Now, we have an operand and an operator: the next loop will check for another
425 ! operand (must appear)
426 !-- -------- --------- --------- --------- --------- --------- --------- -------
427 j = j+1
428 end do step
429 if (ParCnt > 0) CALL ParseErrMsg (j, FuncStr, 'Missing )')
430 end subroutine CheckSyntax
431 !
432 FUNCTION EvalErrMsg ( ierrType ) RESULT (msg)
433 !----- -------- --------- --------- --------- --------- --------- --------- -------
434 ! Return error message
435 !----- -------- --------- --------- --------- --------- --------- --------- -------
436 IMPLICIT NONE
437 CHARACTER (LEN=*), dimension(8), PARAMETER :: m = (/ 'Division by zero ', &
438 'Argument of SQRT negative ', &
439 'Argument of LOG <= 0 ', &
440 'Argument of ASIN or ACOS illegal ', &
441 '-ve number to fractional power ', &
442 'Argument of Bessel_y0 <= 0 ', &
443 'Argument of Bessel_y1 <= 0 ', &
444 'Argument of Gamma function illegal '/)
445 CHARACTER (LEN=LEN(m)) :: msg
446 integer, INTENT(in), OPTIONAL :: ierrType
447 !----- -------- --------- --------- --------- --------- --------- --------- -------
448 if (present(ierrType)) then
449 if (iErrType < 1 .OR. iErrType > SIZE(m)) then
450 msg = ''
451 ELSE
452 msg = m(iErrType)
453 ENDIF
454 ELSE
455 if (EvalErrType < 1 .OR. EvalErrType > SIZE(m)) then
456 msg = ''
457 ELSE
458 msg = m(EvalErrType)
459 ENDIF
460 ENDIF
461 END FUNCTION EvalErrMsg
462 !
463 subroutine ParseErrMsg (j, FuncStr, Msg)
464 !----- -------- --------- --------- --------- --------- --------- --------- -------
465 ! Print error message (modification by D.Price: do not terminate program,
466 ! also added option to not print error message)
467 !----- -------- --------- --------- --------- --------- --------- --------- -------
468 IMPLICIT NONE
469 integer, INTENT(in) :: j
470 CHARACTER (LEN=*), INTENT(in) :: FuncStr ! Original function string
471 CHARACTER (LEN=*), OPTIONAL, INTENT(in) :: Msg
472 integer :: k
473 !----- -------- --------- --------- --------- --------- --------- --------- -------
474
475 if (PrintErrors) then
476 if (PRESENT(Msg)) then
477 write(*,*) '*** Error in syntax of function string: '//Msg
478 ELSE
479 write(*,*) '*** Error in syntax of function string:'
480 ENDIF
481 write(*,*)
482 write(*,'(A)') ' '//FuncStr
483 if (ALLOCATED(ipos)) then ! Avoid out-of-bounds-errors
484 if (SIZE(ipos) >= j) then
485 DO k=1,ipos(j)
486 write(*,'(A)',ADVANCE='NO') ' ' ! Advance to the jth position
487 end do
488 write(*,'(A)') '?'
489 ENDIF
490 ENDIF
491 ENDIF
492 ParseErrType = 1
493
494 end subroutine ParseErrMsg
495 !
496 FUNCTION OperatorIndex (c) RESULT (n)
497 !----- -------- --------- --------- --------- --------- --------- --------- -------
498 ! Return operator index
499 !----- -------- --------- --------- --------- --------- --------- --------- -------
500 IMPLICIT NONE
501 CHARACTER (LEN=1), INTENT(in) :: c
502 integer(is) :: n,j
503 !----- -------- --------- --------- --------- --------- --------- --------- -------
504 n = 0
505 DO j=cAdd,cPow
506 if (c == Ops(j)) then
507 n = j
508 EXIT
509 END IF
510 end do
511 END FUNCTION OperatorIndex
512 !
513 FUNCTION MathFunctionIndex (str) RESULT (n)
514 !----- -------- --------- --------- --------- --------- --------- --------- -------
515 ! Return index of math function beginnig at 1st position of string str
516 !----- -------- --------- --------- --------- --------- --------- --------- -------
517 IMPLICIT NONE
518 CHARACTER (LEN=*), INTENT(in) :: str
519 integer(is) :: n,j
520 integer :: k
521 CHARACTER (LEN=LEN(Funcs)) :: fun
522 !----- -------- --------- --------- --------- --------- --------- --------- -------
523 n = 0
524 DO j=cAbs,cgamma ! Check all math functions
525 k = MIN(LEN_TRIM(Funcs(j)), LEN(str))
526 CALL LowCase (str(1:k), fun)
527 if (fun == Funcs(j)) then ! Compare lower case letters
528 n = j ! Found a matching function
529 EXIT
530 END IF
531 end do
532 END FUNCTION MathFunctionIndex
533 !
534 FUNCTION VariableIndex (str, Var, ibegin, inext) RESULT (n)
535 !----- -------- --------- --------- --------- --------- --------- --------- -------
536 ! Return index of variable at begin of string str (returns 0 if no variable found)
537 !----- -------- --------- --------- --------- --------- --------- --------- -------
538 IMPLICIT NONE
539 CHARACTER (LEN=*), INTENT(in) :: str ! String
540 CHARACTER (LEN=*), dimension(:), INTENT(in) :: Var ! Array with variable names
541 integer(is) :: n,j ! Index of variable
542 integer, OPTIONAL, INTENT(out) :: ibegin, & ! Start position of variable name
543 inext ! Position of character after name
544 integer :: ib,in,lstr
545 !----- -------- --------- --------- --------- --------- --------- --------- -------
546 n = 0
547 lstr = LEN_TRIM(str)
548 if (lstr > 0) then
549 DO ib=1,lstr ! Search for first character in str
550 if (str(ib:ib) /= ' ') EXIT ! When lstr>0 at least 1 char in str
551 end do
552 DO in=ib,lstr ! Search for name terminators
553 if (SCAN(str(in:in),'+-*/^,) ') > 0) EXIT
554 end do
555 DO j=1,SIZE(Var,kind=is)
556 if (str(ib:in-1) == Var(j)) then
557 n = j ! Variable name found
558 EXIT
559 END IF
560 end do
561 !--else below added by D. Price - should never be required though
562 ELSE ! blank string
563 ib = 1 ! to avoid compiler warnings
564 in = 2 ! and any possible seg fault
565 END IF
566 if (PRESENT(ibegin)) ibegin = ib
567 if (PRESENT(inext)) inext = in
568 END FUNCTION VariableIndex
569 !
570 subroutine RemoveSpaces (str)
571 !----- -------- --------- --------- --------- --------- --------- --------- -------
572 ! Remove Spaces from string, remember positions of characters in old string
573 !----- -------- --------- --------- --------- --------- --------- --------- -------
574 IMPLICIT NONE
575 CHARACTER (LEN=*), INTENT(inout) :: str
576 integer :: k,lstr
577 !----- -------- --------- --------- --------- --------- --------- --------- -------
578 lstr = LEN_TRIM(str)
579 ipos = 0
580 do k=1,lstr
581 ipos(k) = k
582 enddo
583 k = 1
584 DO WHILE (str(k:lstr) /= ' ')
585 if (str(k:k) == ' ') then
586 str(k:lstr) = str(k+1:lstr)//' ' ! Move 1 character to left
587 ipos(k:lstr) = (/ ipos(k+1:lstr), 0 /) ! Move 1 element to left
588 k = k-1
589 END IF
590 k = k+1
591 end do
592 end subroutine RemoveSpaces
593 !
594 subroutine Replace (ca,cb,str)
595 !----- -------- --------- --------- --------- --------- --------- --------- -------
596 ! Replace ALL appearances of character set ca in string str by character set cb
597 !----- -------- --------- --------- --------- --------- --------- --------- -------
598 IMPLICIT NONE
599 CHARACTER (LEN=*), INTENT(in) :: ca
600 CHARACTER (LEN=LEN(ca)), INTENT(in) :: cb ! LEN(ca) must be LEN(cb)
601 CHARACTER (LEN=*), INTENT(inout) :: str
602 integer :: j,lca
603 !----- -------- --------- --------- --------- --------- --------- --------- -------
604 lca = LEN(ca)
605 DO j=1,LEN_TRIM(str)-lca+1
606 if (str(j:j+lca-1) == ca) str(j:j+lca-1) = cb
607 end do
608 end subroutine Replace
609 !
610 FUNCTION CountArgs(str) RESULT(n)
611 !----- -------- --------- --------- --------- --------- --------- --------- -------
612 ! Count number of arguments in a function string
613 !----- -------- --------- --------- --------- --------- --------- --------- -------
614 CHARACTER (LEN=*), INTENT(in) :: str
615 integer :: n,j
616 n = 1
617 DO j=1,len_trim(str)
618 if (str(j:j) == ',') n = n + 1
619 enddo
620 END FUNCTION CountArgs
621 !
622 subroutine Compile (i, F, Var)
623 !----- -------- --------- --------- --------- --------- --------- --------- -------
624 ! Compile i-th function string F into bytecode
625 !----- -------- --------- --------- --------- --------- --------- --------- -------
626 IMPLICIT NONE
627 integer, INTENT(in) :: i ! Function identifier
628 CHARACTER (LEN=*), INTENT(in) :: F ! Function string
629 CHARACTER (LEN=*), dimension(:), INTENT(in) :: Var ! Array with variable names
630 integer :: istat
631 !----- -------- --------- --------- --------- --------- --------- --------- -------
632 if (ASSOCIATED(Comp(i)%ByteCode)) DEALLOCATE ( Comp(i)%ByteCode, &
633 Comp(i)%Immed, &
634 Comp(i)%Stack )
635 Comp(i)%ByteCodeSize = 0
636 Comp(i)%ImmedSize = 0
637 Comp(i)%StackSize = 0
638 Comp(i)%StackPtr = 0
639 CALL CompileSubstr (i,F,1,LEN_TRIM(F),Var) ! Compile string to determine size
640 ALLOCATE ( Comp(i)%ByteCode(Comp(i)%ByteCodeSize), &
641 Comp(i)%Immed(Comp(i)%ImmedSize), &
642 Comp(i)%Stack(Comp(i)%StackSize), &
643 STAT = istat )
644 if (istat /= 0) then
645 write(*,*) '*** Parser error: Memory allocation for byte code failed'
646 stop
647 ELSE
648 Comp(i)%ByteCodeSize = 0
649 Comp(i)%ImmedSize = 0
650 Comp(i)%StackSize = 0
651 Comp(i)%StackPtr = 0
652 CALL CompileSubstr (i,F,1,LEN_TRIM(F),Var) ! Compile string into bytecode
653 END IF
654 !
655 end subroutine Compile
656 !
657 subroutine AddCompiledByte (i, b)
658 !----- -------- --------- --------- --------- --------- --------- --------- -------
659 ! Add compiled byte to bytecode
660 !----- -------- --------- --------- --------- --------- --------- --------- -------
661 IMPLICIT NONE
662 integer, INTENT(in) :: i ! Function identifier
663 integer(is), INTENT(in) :: b ! Value of byte to be added
664 !----- -------- --------- --------- --------- --------- --------- --------- -------
665 Comp(i)%ByteCodeSize = Comp(i)%ByteCodeSize + 1
666 if (ASSOCIATED(Comp(i)%ByteCode)) Comp(i)%ByteCode(Comp(i)%ByteCodeSize) = b
667 end subroutine AddCompiledByte
668 !
669 FUNCTION MathItemIndex (i, F, Var) RESULT (n)
670 !----- -------- --------- --------- --------- --------- --------- --------- -------
671 ! Return math item index, if item is real number, enter it into Comp-structure
672 !----- -------- --------- --------- --------- --------- --------- --------- -------
673 IMPLICIT NONE
674 integer, INTENT(in) :: i ! Function identifier
675 CHARACTER (LEN=*), INTENT(in) :: F ! Function substring
676 CHARACTER (LEN=*), dimension(:), INTENT(in) :: Var ! Array with variable names
677 integer(is) :: n ! Byte value of math item
678 !----- -------- --------- --------- --------- --------- --------- --------- -------
679 n = 0
680 if (SCAN(F(1:1),'0123456789.') > 0) then ! Check for begin of a number
681 Comp(i)%ImmedSize = Comp(i)%ImmedSize + 1
682 if (ASSOCIATED(Comp(i)%Immed)) Comp(i)%Immed(Comp(i)%ImmedSize) = RealNum (F)
683 n = cImmed
684 ELSE ! Check for a variable
685 n = VariableIndex (F, Var)
686 if (n > 0) then
687 n = VarBegin+n-1_is
688 ELSE ! Check for Mathematical constants
689 n = MathConstIndex(i, F)
690 ENDIF
691 END IF
692 END FUNCTION MathItemIndex
693 !
694 FUNCTION MathConstIndex (i, F, ibegin, inext) RESULT (n)
695 !----- -------- --------- --------- --------- --------- --------- --------- -------
696 ! Routine added by D. Price
697 ! Substitute values for Mathematical Constants (e.g. pi)
698 !----- -------- --------- --------- --------- --------- --------- --------- -------
699 IMPLICIT NONE
700 integer, INTENT(in) :: i ! Function identifier
701 CHARACTER (LEN=*), INTENT(in) :: F ! Function substring
702 integer, OPTIONAL, INTENT(out) :: ibegin, & ! Start position of real number
703 inext ! 1st character after real number
704 integer(is) :: n ! Byte value of math item
705 real(rn) :: res
706 LOGICAL :: err
707 integer :: ib,in
708 !----- -------- --------- --------- --------- --------- --------- --------- -------
709 n = 0
710 res = MathConst(F,ib,in,err)
711 if (.not.err) then
712 Comp(i)%ImmedSize = Comp(i)%ImmedSize + 1
713 if (ASSOCIATED(Comp(i)%Immed)) Comp(i)%Immed(Comp(i)%ImmedSize) = res
714 n = cImmed
715 ELSE
716 ib = 1
717 in = 1
718 END IF
719 if (PRESENT(ibegin)) ibegin = ib
720 if (PRESENT(inext)) inext = in
721
722 END FUNCTION MathConstIndex
723 !
724 FUNCTION CompletelyEnclosed (F, b, e) RESULT (res)
725 !----- -------- --------- --------- --------- --------- --------- --------- -------
726 ! Check if function substring F(b:e) is completely enclosed by a pair of parenthesis
727 !----- -------- --------- --------- --------- --------- --------- --------- -------
728 IMPLICIT NONE
729 CHARACTER (LEN=*), INTENT(in) :: F ! Function substring
730 integer, INTENT(in) :: b,e ! First and last pos. of substring
731 LOGICAL :: res
732 integer :: j,k
733 !----- -------- --------- --------- --------- --------- --------- --------- -------
734 res=.false.
735 if (F(b:b) == '(' .AND. F(e:e) == ')') then
736 k = 0
737 DO j=b+1,e-1
738 IF (F(j:j) == '(') then
739 k = k+1
740 ELSEif (F(j:j) == ')') then
741 k = k-1
742 END IF
743 if (k < 0) EXIT
744 end do
745 if (k == 0) res=.true. ! All opened parenthesis closed
746 END IF
747 END FUNCTION CompletelyEnclosed
748
749 FUNCTION TwoArgs (F, b, e, m) RESULT (res)
750 !----- -------- --------- --------- --------- --------- --------- --------- -------
751 ! Check if function substring F(b:e) has two arguments i.e. f(a,b)
752 !----- -------- --------- --------- --------- --------- --------- --------- -------
753 IMPLICIT NONE
754 CHARACTER (LEN=*), INTENT(in) :: F ! Function substring
755 integer, INTENT(in) :: b,e ! First and last pos. of substring
756 integer, INTENT(out) :: m
757 LOGICAL :: res
758
759 res = .false.
760 m = INDEX(F(b:e),',')
761 if (m > 0) then
762 res = .true.
763 m = b + m - 1
764 ENDIF
765
766 END FUNCTION TwoArgs
767 !
768 RECURSIVE subroutine CompileSubstr (i, F, b, e, Var)
769 !----- -------- --------- --------- --------- --------- --------- --------- -------
770 ! Compile i-th function string F into bytecode
771 !----- -------- --------- --------- --------- --------- --------- --------- -------
772 IMPLICIT NONE
773 integer, INTENT(in) :: i ! Function identifier
774 CHARACTER (LEN=*), INTENT(in) :: F ! Function substring
775 integer, INTENT(in) :: b,e ! Begin and end position substring
776 CHARACTER (LEN=*), dimension(:), INTENT(in) :: Var ! Array with variable names
777 integer(is) :: n
778 integer :: b2,j,k,io,m
779 CHARACTER (LEN=*), PARAMETER :: calpha = 'abcdefghijklmnopqrstuvwxyz'// &
780 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
781 !----- -------- --------- --------- --------- --------- --------- --------- -------
782 ! Check for special cases of substring
783 !----- -------- --------- --------- --------- --------- --------- --------- -------
784 ! write(*,*) 'PARSING F(b:e) = '//F(b:e)
785 IF (F(b:b) == '+') then ! Case 1: F(b:e) = '+...'
786 ! write(*,*)'1. F(b:e) = "+..."'
787 CALL CompileSubstr (i, F, b+1, e, Var)
788 return
789 ELSEif (CompletelyEnclosed (F, b, e)) then ! Case 2: F(b:e) = '(...)'
790 ! write(*,*)'2. F(b:e) = "(...)"'
791 CALL CompileSubstr (i, F, b+1, e-1, Var)
792 return
793 ELSEif (SCAN(F(b:b),calpha) > 0) then
794 n = MathFunctionIndex (F(b:e))
795 if (n > 0) then
796 b2 = b+INDEX(F(b:e),'(')-1
797 if (CompletelyEnclosed(F, b2, e)) then ! Case 3: F(b:e) = 'fcn(...)'
798 ! write(*,*)'3. F(b:e) = "fcn(...)"'
799 if (n == cAtan2 .and. TwoArgs(F,b2+1,e-1,m)) then
800 ! print*,' SPLITTING ',b,m,e,' F(b:e)=',F(b2+1:e-1)
801 CALL CompileSubstr (i, F, b2+1, m-1, Var)
802 CALL CompileSubstr (i, F, m+1, e-1, Var)
803 ELSE
804 CALL CompileSubstr(i, F, b2+1, e-1, Var)
805 ENDIF
806 CALL AddCompiledByte (i, n)
807 return
808 END IF
809 END IF
810 ELSEif (F(b:b) == '-') then
811 if (CompletelyEnclosed (F, b+1, e)) then ! Case 4: F(b:e) = '-(...)'
812 ! write(*,*)'4. F(b:e) = "-(...)"'
813 CALL CompileSubstr (i, F, b+2, e-1, Var)
814 CALL AddCompiledByte (i, cNeg)
815 return
816 ELSEif (SCAN(F(b+1:b+1),calpha) > 0) then
817 n = MathFunctionIndex (F(b+1:e))
818 if (n > 0) then
819 b2 = b+INDEX(F(b+1:e),'(')
820 if (CompletelyEnclosed(F, b2, e)) then ! Case 5: F(b:e) = '-fcn(...)'
821 ! write(*,*)'5. F(b:e) = "-fcn(...)"'
822 if (n == cAtan2 .and. TwoArgs(F,b2+1,e-1,m)) then
823 CALL CompileSubstr (i, F, b2+1, m-1, Var)
824 CALL CompileSubstr (i, F, m+1, e-1, Var)
825 ELSE
826 CALL CompileSubstr(i, F, b2+1, e-1, Var)
827 ENDIF
828 CALL AddCompiledByte (i, n)
829 CALL AddCompiledByte (i, cNeg)
830 return
831 END IF
832 END IF
833 ENDIF
834 ENDIF
835 ! write(*,*) 'PROCEED TO OPERATORS',TwoArgs(F,b,e,m),F(b:e)
836 !----- -------- --------- --------- --------- --------- --------- --------- -------
837 ! Check for operator in substring: check only base level (k=0), exclude expr. in ()
838 !----- -------- --------- --------- --------- --------- --------- --------- -------
839 DO io=cAdd,cPow ! Increasing priority +-*/^
840 k = 0
841 DO j=e,b,-1
842 IF (F(j:j) == ')') then
843 k = k+1
844 ELSEif (F(j:j) == '(') then
845 k = k-1
846 END IF
847 if (k == 0 .AND. F(j:j) == Ops(io) .AND. IsBinaryOp (j, F)) then
848 if (ANY(F(j:j) == Ops(cMul:cPow)) .AND. F(b:b) == '-') then ! Case 6: F(b:e) = '-...Op...' with Op > -
849 ! write(*,*)'6. F(b:e) = "-...Op..." with Op > -'
850 CALL CompileSubstr (i, F, b+1, e, Var)
851 CALL AddCompiledByte (i, cNeg)
852 return
853 ELSE ! Case 7: F(b:e) = '...BinOp...'
854 ! write(*,*)'7. Binary operator',F(j:j)
855 CALL CompileSubstr (i, F, b, j-1, Var)
856 CALL CompileSubstr (i, F, j+1, e, Var)
857 CALL AddCompiledByte (i, OperatorIndex(Ops(io)))
858 Comp(i)%StackPtr = Comp(i)%StackPtr - 1
859 return
860 END IF
861 END IF
862 end do
863 end do
864 !----- -------- --------- --------- --------- --------- --------- --------- -------
865 ! Check for remaining items, i.e. variables or explicit numbers
866 !----- -------- --------- --------- --------- --------- --------- --------- -------
867 b2 = b
868 if (F(b:b) == '-') b2 = b2+1
869 n = MathItemIndex(i, F(b2:e), Var)
870 ! write(*,*)'8. AddCompiledByte ',n
871 CALL AddCompiledByte (i, n)
872 Comp(i)%StackPtr = Comp(i)%StackPtr + 1
873 if (Comp(i)%StackPtr > Comp(i)%StackSize) Comp(i)%StackSize = Comp(i)%StackSize + 1
874 if (b2 > b) CALL AddCompiledByte (i, cNeg)
875 end subroutine CompileSubstr
876 !
877 FUNCTION IsBinaryOp (j, F) RESULT (res)
878 !----- -------- --------- --------- --------- --------- --------- --------- -------
879 ! Check if operator F(j:j) in string F is binary operator
880 ! Special cases already covered elsewhere: (that is corrected in v1.1)
881 ! - operator character F(j:j) is first character of string (j=1)
882 !----- -------- --------- --------- --------- --------- --------- --------- -------
883 IMPLICIT NONE
884 integer, INTENT(in) :: j ! Position of Operator
885 CHARACTER (LEN=*), INTENT(in) :: F ! String
886 LOGICAL :: res ! Result
887 integer :: k
888 LOGICAL :: Dflag,Pflag
889 !----- -------- --------- --------- --------- --------- --------- --------- -------
890 res=.true.
891 if (F(j:j) == '+' .OR. F(j:j) == '-') then ! Plus or minus sign:
892 if (j == 1) then ! - leading unary operator ?
893 res = .false.
894 ELSEif (SCAN(F(j-1:j-1),'+-*/^(') > 0) then ! - other unary operator ?
895 res = .false.
896 ELSEif (SCAN(F(j+1:j+1),'0123456789') > 0 .AND. & ! - in exponent of real number ?
897 SCAN(F(j-1:j-1),'eEdD') > 0) then
898 Dflag=.false.; Pflag=.false.
899 k = j-1
900 DO WHILE (k > 1) ! step to the left in mantissa
901 k = k-1
902 IF (SCAN(F(k:k),'0123456789') > 0) then
903 Dflag=.true.
904 ELSEif (F(k:k) == '.') then
905 if (Pflag) then
906 EXIT ! * EXIT: 2nd appearance of '.'
907 ELSE
908 Pflag=.true. ! * mark 1st appearance of '.'
909 ENDIF
910 ELSE
911 EXIT ! * all other characters
912 END IF
913 end do
914 if (Dflag .AND. (k == 1 .OR. SCAN(F(k:k),'+-*/^(') > 0)) res = .false.
915 END IF
916 END IF
917 END FUNCTION IsBinaryOp
918 !
919 FUNCTION RealNum (str, ibegin, inext, error) RESULT (res)
920 !----- -------- --------- --------- --------- --------- --------- --------- -------
921 ! Get real number from string - Format: [blanks][+|-][nnn][.nnn][e|E|d|D[+|-]nnn]
922 !----- -------- --------- --------- --------- --------- --------- --------- -------
923 IMPLICIT NONE
924 CHARACTER (LEN=*), INTENT(in) :: str ! String
925 real(rn) :: res ! Real number
926 integer, OPTIONAL, INTENT(out) :: ibegin, & ! Start position of real number
927 inext ! 1st character after real number
928 LOGICAL, OPTIONAL, INTENT(out) :: error ! Error flag
929 integer :: ib,in,istat
930 LOGICAL :: Bflag, & ! .T. at begin of number in str
931 InMan, & ! .T. in mantissa of number
932 Pflag, & ! .T. after 1st '.' encountered
933 Eflag, & ! .T. at exponent identifier 'eEdD'
934 InExp, & ! .T. in exponent of number
935 DInMan, & ! .T. if at least 1 digit in mant.
936 DInExp, & ! .T. if at least 1 digit in exp.
937 err ! Local error flag
938 !----- -------- --------- --------- --------- --------- --------- --------- -------
939 Bflag=.true.; InMan=.false.; Pflag=.false.; Eflag=.false.; InExp=.false.
940 DInMan=.false.; DInExp=.false.
941 ib = 1
942 in = 1
943 DO WHILE (in <= LEN_TRIM(str))
944 SELECT CASE (str(in:in))
945 CASE (' ') ! Only leading blanks permitted
946 ib = ib+1
947 if (InMan .OR. Eflag .OR. InExp) EXIT
948 CASE ('+','-') ! Permitted only
949 IF (Bflag) then
950 InMan=.true.; Bflag=.false. ! - at beginning of mantissa
951 ELSEif (Eflag) then
952 InExp=.true.; Eflag=.false. ! - at beginning of exponent
953 ELSE
954 EXIT ! - otherwise stop
955 ENDIF
956 CASE ('0':'9') ! Mark
957 IF (Bflag) then
958 InMan=.true.; Bflag=.false. ! - beginning of mantissa
959 ELSEif (Eflag) then
960 InExp=.true.; Eflag=.false. ! - beginning of exponent
961 ENDIF
962 if (InMan) DInMan=.true. ! Mantissa contains digit
963 if (InExp) DInExp=.true. ! Exponent contains digit
964 CASE ('.')
965 IF (Bflag) then
966 Pflag=.true. ! - mark 1st appearance of '.'
967 InMan=.true.; Bflag=.false. ! mark beginning of mantissa
968 ELSEif (InMan .AND..NOT.Pflag) then
969 Pflag=.true. ! - mark 1st appearance of '.'
970 ELSE
971 EXIT ! - otherwise stop
972 END IF
973 CASE ('e','E','d','D') ! Permitted only
974 if (InMan) then
975 Eflag=.true.; InMan=.false. ! - following mantissa
976 ELSE
977 EXIT ! - otherwise stop
978 ENDIF
979 CASE DEFAULT
980 EXIT ! stop at all other characters
981 END SELECT
982 in = in+1
983 end do
984 err = (ib > in-1) .OR. (.NOT.DInMan) .OR. ((Eflag.OR.InExp).AND..NOT.DInExp)
985 if (err) then
986 res = 0.0_rn
987 ELSE
988 read(str(ib:in-1),*,IOSTAT=istat) res
989 err = istat /= 0
990 END IF
991 if (PRESENT(ibegin)) ibegin = ib
992 if (PRESENT(inext)) inext = in
993 if (PRESENT(error)) error = err
994 END FUNCTION RealNum
995
996 !
997 FUNCTION MathConst (str, ibegin, inext, error) RESULT (res)
998 !----- -------- --------- --------- --------- --------- --------- --------- -------
999 ! Return values of Mathematical constants in string
1000 !----- -------- --------- --------- --------- --------- --------- --------- -------
1001 IMPLICIT NONE
1002 CHARACTER (LEN=*), INTENT(in) :: str ! String
1003 real(rn) :: res ! Real number
1004 integer, OPTIONAL, INTENT(out) :: ibegin, & ! Start position of real number
1005 inext ! 1st character after real number
1006 LOGICAL, OPTIONAL, INTENT(out) :: error ! Error flag
1007 integer :: ib,in
1008 LOGICAL :: err ! Local error flag
1009 !----- -------- --------- --------- --------- --------- --------- --------- -------
1010 ib = 1
1011 in = 1
1012 err = .false.
1013 if (len(str) < 2) then
1014 res = 0.0_rn
1015 err = .true.
1016 ELSE
1017 if (str(1:2)=='pi') then
1018 res = 3.14159265358979323846_rn
1019 in = 3
1020 ELSEif (str(1:2)=='mu') then
1021 res = mu0
1022 in = 3
1023 ELSE
1024 res = 0.0_rn
1025 err = .true.
1026 ENDIF
1027 ENDIF
1028
1029 if (PRESENT(ibegin)) ibegin = ib
1030 if (PRESENT(inext)) inext = in
1031 if (PRESENT(error)) error = err
1032
1033 END FUNCTION MathConst
1034
1035 !
1036 subroutine LowCase (str1, str2)
1037 !----- -------- --------- --------- --------- --------- --------- --------- -------
1038 ! Transform upper case letters in str1 into lower case letters, result is str2
1039 !----- -------- --------- --------- --------- --------- --------- --------- -------
1040 IMPLICIT NONE
1041 CHARACTER (LEN=*), INTENT(in) :: str1
1042 CHARACTER (LEN=*), INTENT(out) :: str2
1043 integer :: j,k
1044 CHARACTER (LEN=*), PARAMETER :: lc = 'abcdefghijklmnopqrstuvwxyz'
1045 CHARACTER (LEN=*), PARAMETER :: uc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1046 !----- -------- --------- --------- --------- --------- --------- --------- -------
1047 str2 = str1
1048 DO j=1,LEN_TRIM(str1)
1049 k = INDEX(uc,str1(j:j))
1050 if (k > 0) str2(j:j) = lc(k:k)
1051 end do
1052 end subroutine LowCase
1053 !
1054 end module fparser