/usr/lib/libreoffice/share/extensions/DmathsAddon/Dmaths/Parser.xba is in libreoffice-dmaths 3.4+dfsg1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Parser" script:language="StarBasic">'************************************************
'Copyright (C) Andy Lewis. (lewiss@ntlworld.com)
'téléchargé sur www.ooomacros.org
'updated by Jean-Luc Guillot 2010:
' add some functions (min, max, sin, int,fact)
' add management of synonimous of functions
' understand constantes pi and e constants
' management of functions with optionals parameters
' add Statistical and mathematical Calc functions
' possibility to calculate f(x,y,z)
'
'
'This library is free software; you can redistribute it and/or
'modify it under the terms of the GNU Lesser General Public Licence (LGPL)
'as published by the Free Software Foundation; either
'version 2.1 of the License, or (at your option) any later version.
'This library 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 Licence (GPL)
'along with this library; if not, write to the Free Software
'Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'************************************************
'Parser to evaluate functions typed in as a string
'This needs tidying up really so no documentation yet But it works
Option Explicit
'This constant is used as an undefined value eg resulting from division by zero
Global Const notdef = 1e38
Public DegreesFlag as Boolean
Private l2$(50),l20$(50), ln2(50)
Private lp2, lp20 As Integer
Private a$, a0$
Private oFunctionAccess As Object
private CalcFunctions$ As string
'___________________________________________________________________________________________
Sub convertfn (a$, l2$(), lp2, erflag)
Dim l1$(50), priority(50)
Dim la, c, p, s As Integer
Dim ch$, st$, fl$, FunctionsList$,sep$
sep$="no"
a$ = LCase(a$)
FunctionsList$ = "¦fact¦abs¦int¦uminus¦sin¦cos¦tan¦sqr¦exp¦ln¦log¦lg¦"
FunctionsList$ = FunctionsList$+"sign¦logten¦logtwo¦frac¦min¦max¦"
FunctionsList$ = FunctionsList$+"sec¦cosec¦cot¦sinh¦cosh¦tanh¦sech¦cosech¦coth¦"
FunctionsList$ = FunctionsList$+"arcsin¦arccos¦arctan¦arcsec¦arccosec¦arccot¦"
FunctionsList$ = FunctionsList$+"arsinh¦arcosh¦artanh¦arsech¦arcosech¦arcoth¦"
'Statistical Functions
CalcFunctions$ = "¦betadist¦betainv¦chidist¦chisqdist¦chiinv¦chisqinv¦"
CalcFunctions$ = CalcFunctions$+"confidence¦expondist¦finv¦fisher¦fisherinv¦"
CalcFunctions$ = CalcFunctions$+"fdist¦gamma¦gammainv¦gammaln¦gammadist¦"
CalcFunctions$ = CalcFunctions$+"gauss¦loginv¦lognormdist¦norminv¦normdist¦"
CalcFunctions$ = CalcFunctions$+"normsinv¦normsdist¦phi¦tdist¦tinv¦weibull¦"
'Financial Functions
CalcFunctions$ = CalcFunctions$+"db¦ddb¦sln¦syd¦vdb¦"
CalcFunctions$ = CalcFunctions$+"cumipmt¦cumipmt_add¦cumprinc¦cumprinc_add¦fv¦ipmt¦"
CalcFunctions$ = CalcFunctions$+"ispmt¦nper¦npv¦"
CalcFunctions$ = CalcFunctions$+"rate¦rri¦duration¦"
CalcFunctions$ = CalcFunctions$+"effective¦effect_add¦nominal¦nominal_add¦"
s = 0: la = Len(a$): c = 1: ch$ = Mid$(a$, c, 1)
Do While c <= la
st$ = ch$: fl$ = "C"
Select Case ch$
Case "a" To "z"
fl$ = "S": st$ = ""
Do While c <= la And (ch$ >= "a" And ch$ <= "z")
st$ = st$ + ch$
c = c + 1: ch$ = Mid$(a$, c, 1)
Loop
Case "0" To "9", "."
fl$ = "N": st$ = ""
Do While c <= la And ((ch$ >= "0" And ch$ <= "9") Or ch$ = ".")
st$ = st$ + ch$
c = c + 1: ch$ = Mid$(a$, c, 1)
Loop
End Select
If fl$ = "C" Then c = c + 1: If c <= la Then ch$ = Mid$(a$, c, 1)
If st$ = "-" And (s = 0 Or (priority(s) >= 1 And priority(s) <= 5) Or l1$(s)=";" or l1$(s)=",") Then st$ = "uminus": fl$ = "S"
If st$ = " " Or (st$ = "+" And (s = 0 Or (priority(s) >= 1 And priority(s) <= 5))) Then fl$ = "X"
erflag = 0
If fl$ = "S" Then
If InStr(FunctionsList$, "¦" + st$ + "¦") Then
p = 1
ElseIf InStr(CalcFunctions$, "¦" + st$ + "¦") Then
if IsNull(oFunctionAccess) then oFunctionAccess=createUnoService( "com.sun.star.sheet.FunctionAccess" )
p = 1
Else p = -1
End If
End If
If fl$ = "N" Then p = 0
If fl$ = "C" Then
Select Case st$
Case "^": p = 2
Case "*", "/": p = 3
Case "+", "-": p = 4
Case "(": p = 5
Case ")": p = 6
Case ",",";": p = 6 : sep$="yes"
Case Else: erflag = -1: Exit Sub
End Select
End If
If fl$ <> "X" Then
If s > 0 And (priority(s) <= 0 Or priority(s) = 6) And (p <= 1 Or p = 5) Then
rem doit gerer le ; ici
if sep$="no" then
s = s + 1: l1$(s) = "*": priority(s) = 3
else sep$="no"
end if
End If
s = s + 1: l1$(s) = st$: priority(s) = p
End If
Loop
prioritise l1$(), s, priority(), l2$(), lp2, erflag
End Sub
'___________________________________________________________________________________________
Function evaluate (l2$(), lp2, X, erflag, optional Xname, optional Y, optional Yname, optional Z, optional Zname) as double
On Error GoTo errorhandler
Dim v(50)
Dim sp, pl2,nbArg,i As Integer
Dim CurValue As String
Dim PossibleX, PossibleY, PossibleZ As String
Dim Args() as double
if Ismissing(Xname) then possibleX="¦x¦" else PossibleX="¦"+Xname+"¦"
if Ismissing(Yname) then PossibleY="¦y¦" else PossibleY="¦"+Yname+"¦"
if Ismissing(Zname) then PossibleZ="¦z¦" else PossibleZ="¦"+Zname+"¦"
if Ismissing(Xname) and Ismissing(Y) and Ismissing(Z) then possibleX="¦x¦y¦z¦t¦u¦v¦"
sp = 0: pl2 = 0: erflag = 0
Do While pl2 < lp2
pl2 = pl2 + 1
CurValue=l2$(pl2)
If Left$(CurValue, 1) = "#" Then
sp = sp + 1: v(sp) = Val(Mid$(CurValue, 2))
ElseIf Instr(PossibleX, CurValue) Then
sp = sp + 1: v(sp) = X
ElseIf Instr(PossibleY, CurValue) Then
sp = sp + 1: v(sp) = Y
ElseIf Instr(PossibleZ, CurValue) Then
sp = sp + 1: v(sp) = Z
ElseIf CurValue="rnd" Then
sp = sp + 1: v(sp) = rnd()
ElseIf CurValue="e" Then
sp = sp + 1: v(sp) = 2.71828182845904
ElseIf sp = 0 Then
erflag = -3: Exit Function
ElseIf CurValue >= "a" And CurValue <= "z" Then
If InStr(CalcFunctions$,"¦"+l2$(pl2)+"¦") then
nbArg=ln2(pl2)
redim Args(nbArg-1)
sp = sp - nbArg+1
for i =0 to nbArg-1
Args(i)=v(sp+i)
next
v(sp)=oFunctionAccess.CallFunction( l2$(pl2),Args )
else
Select Case l2$(pl2)
Case "sin": v(sp) = Sin(angle(v(sp)))
Case "cos": v(sp) = Cos(angle(v(sp)))
Case "tan": if Cos(angle(v(sp)))=0 Then erflag=-4 Else v(sp) = Tan(angle(v(sp)))
Case "uminus": v(sp) = -v(sp)
Case "sign": if v(sp)<0 then v(sp)=-1 else if v(sp)=0 then v(sp)=0 else v(sp)=1
Case "abs": v(sp) = abs(v(sp))
Case "int": v(sp) = int(v(sp))
Case "frac": v(sp) = v(sp)-int(v(sp))
Case "min" :
nbArg=ln2(pl2)
sp = sp - nbArg+1
for i =1 to nbArg-1
if v(sp)>v(sp+i) then v(sp)=v(sp+i)
next
Case "max" :
nbArg=ln2(pl2)
sp = sp - nbArg+1
for i =1 to nbArg-1
if v(sp)<v(sp+i) then v(sp)=v(sp+i)
next
Case "sqr": If v(sp) < 0 Then erflag = -4 Else v(sp) = Sqr(v(sp))
Case "ln": If v(sp) <= 0 Then erflag = -4 Else v(sp) = Log(v(sp))
Case "log", "lg": If v(sp) <= 0 Then erflag = -4 Else v(sp) = Log(v(sp))
Case "logten": If v(sp) <= 0 Then erflag = -4 Else v(sp) = Log(v(sp))/Log(10)
Case "logtwo": If v(sp) <= 0 Then erflag = -4 Else v(sp) = Log(v(sp))/Log(2)
Case "fact": If ( v(sp) < 0) or (v(sp)>31) or (v(sp)<>int(v(sp))) Then erflag = -4 Else v(sp) = Fact(v(sp))
Case "exp": v(sp) = Exp(v(sp))
Case "sec": If Cos(angle(v(sp))) = 0 Then erflag = -4 Else v(sp) = 1 / Cos(angle(v(sp)))
Case "cosec": If Sin(angle(v(sp))) = 0 Then erflag = -4 Else v(sp) = 1 / Sin(angle(v(sp)))
Case "cot": If Tan(angle(v(sp))) = 0 Then erflag = -4 Else v(sp) = 1 / Tan(angle(v(sp)))
Case "arcsin":
If v(sp) < -1 Or v(sp) > 1 Then
erflag = -4
ElseIf v(sp) = 1 Then
v(sp) = 2 * Atn(1)
ElseIf v(sp) = -1 Then
v(sp) = -2 * Atn(1)
Else
v(sp) = Atn(v(sp) / Sqr(-v(sp) * v(sp) + 1))
End If
v(sp)=toAngle(v(sp))
Case "arccos":
If v(sp) < -1 Or v(sp) > 1 Then
erflag = -4
ElseIf v(sp) = 1 Then
v(sp) = 0
ElseIf v(sp) = -1 Then
v(sp) = 4 * Atn(1)
Else
v(sp) = Atn(-v(sp) / Sqr(-v(sp) * v(sp) + 1)) + 1.5708
End If
v(sp)=toAngle(v(sp))
Case "arctan": v(sp) = toAngle(Atn(v(sp)))
Case "arcsec":
If v(sp) >= -1 And v(sp) <= 1 Then
erflag = -4
Else
v(sp) = toAngle(Atn(v(sp) / Sqr(v(sp) * v(sp) - 1)) + Sgn(Sgn(v(sp)) - 1) * 1.5708)
End If
Case "arccosec":
If v(sp) >= -1 And v(sp) <= 1 Then
erflag = -4
Else
v(sp) = toAngle(Atn(v(sp) / Sqr(v(sp) * v(sp) - 1)) + (Sgn(v(sp)) - 1) * 1.5708)
End If
Case "arccot": v(sp) = toAngle(Atn(v(sp)) + 1.5708)
Case "sinh": v(sp) = (Exp(v(sp)) - Exp(-v(sp))) / 2
Case "cosh": v(sp) = (Exp(v(sp)) + Exp(-v(sp))) / 2
Case "tanh": v(sp) = (Exp(v(sp)) - Exp(-v(sp))) / (Exp(v(sp)) + Exp(-v(sp)))
Case "sech": v(sp) = 2 / (Exp(v(sp)) + Exp(-v(sp)))
Case "cosech": If v(sp) = 0 Then erflag = -4 Else v(sp) = 2 / (Exp(v(sp)) - Exp(-v(sp)))
Case "coth": If v(sp) = 0 Then erflag = -4 Else v(sp) = (Exp(v(sp)) + Exp(-v(sp))) / (Exp(v(sp)) - Exp(-v(sp)))
Case "arsinh": v(sp) = Log(v(sp) + Sqr(v(sp) * v(sp) + 1))
Case "arcosh": If v(sp) <= 1 Then erflag = -4 Else v(sp) = Log(v(sp) + Sqr(v(sp) * v(sp) - 1))
Case "artanh": If v(sp) >= 1 Or v(sp) <= -1 Then erflag = -4 Else v(sp) = Log((1 + v(sp)) / (1 - v(sp))) / 2
Case "arsech": If v(sp) <= 0 Or v(sp) > 1 Then erflag = -4 Else v(sp) = Log((Sqr(-v(sp) * v(sp) + 1) + 1) / v(sp))
Case "arcosech": If Abs(v(sp)) < .0000000001 Then erflag = -4 Else v(sp) = Log((Sgn(v(sp)) * Sqr(v(sp) * v(sp) + 1) + 1) / v(sp))
Case "arcoth": If v(sp) <= 1 And v(sp) >= -1 Then erflag = -4 Else v(sp) = Log((v(sp) + 1) / (v(sp) - 1)) / 2
End Select
End If
ElseIf sp <= 1 Then
erflag = -5: Exit Function
Else
sp = sp - 1
Select Case l2$(pl2)
Case "+": v(sp) = v(sp) + v(sp + 1)
Case "-": v(sp) = v(sp) - v(sp + 1)
Case "*": v(sp) = v(sp) * v(sp + 1)
Case "/": If v(sp + 1) = 0 Then erflag = -4 Else v(sp) = v(sp) / v(sp + 1)
Case "^": v(sp) = v(sp) ^ v(sp + 1)
End Select
End If
Loop
If sp <> 1 Then evaluate = 999: erflag = -6: Exit Function
evaluate = v(1)
Exit Function
errorhandler:
evaluate = notdef
erflag = -9
End Function
'___________________________________________________________________________________________
Function f (X) As Double
Dim erflag As Integer
f = evaluate(l2$(), lp2, X, erflag)
If erflag < 0 Then f = notdef
End Function
Function f0 (X) As Double
Dim erflag0 As Integer
f0 = evaluate(l20$(), lp20, X, erflag0)
If erflag0 < 0 Then f0 = notdef
End Function
Function f1 (X,Numero,NumInter) As Double
Dim erflag as Integer
If Len(sNewFormulSpline(Numero,0,1))<>0 and NumInter<Ubound(sNewFormulSpline(),2) Then
If (X>Csng(sNewFormulSpline(Numero,NumInter+1,1)) and Len(sNewFormulSpline(Numero,NumInter+1,0))>0) Then
NumInter = NumInter+1
parse(sNewFormulSpline(Numero,NumInter,0),erflag)
EndIf
Endif
' Print X,f(X)
f1 = f(X)
End Function
'___________________________________________________________________________________________
Sub parse (a$, erflag)
convertfn translateF(a$), l2$(), lp2, erflag
End Sub
Sub parse0 (a0$, erflag0)
convertfn translateF(a0$), l20$(), lp20, erflag0
End Sub
'___________________________________________________________________________________________
Sub prioritise (l1$(), s, priority(), l2$(), lp2, erflag)
Dim stack$(50), ps(50), lnsp(50)
Dim lp1, sp As Integer
lp1 = 0: lp2 = 0: sp = 0
Do While lp1 < s
lp1 = lp1 + 1
Select Case priority(lp1)
Case Is < 0
lp2 = lp2 + 1: l2$(lp2) = l1$(lp1)
Case 0
lp2 = lp2 + 1: l2$(lp2) = "#" + l1$(lp1)
Case 6
Do While stack$(sp) <> "(" And sp > 0
lp2 = lp2 + 1:
l2$(lp2) = stack$(sp)
If sp<50 Then ln2(lp2)=lnsp(sp+1)
sp = sp - 1
Loop
if l1$(lp1)=")" then
sp = sp - 1: If sp < 0 Then erflag = -2: Exit Sub
else
If sp < 1 Then erflag = -2: Exit Sub
lnsp(sp)=lnsp(sp)+1
end if
Case Else
Do
If sp = 0 Or priority(lp1) < ps(sp) Or (priority(lp1) = 1 And ps(sp) = 1) Or l1$(lp1) = "(" Then
sp = sp + 1:
stack$(sp) = l1$(lp1)
ps(sp) = priority(lp1)
if stack$(sp) = "(" then lnsp(sp)=1
Exit Do
Else
lp2 = lp2 + 1:
l2$(lp2) = stack$(sp)
if sp<50 then ln2(lp2)=lnsp(sp+1)
sp = sp - 1
End If
Loop
End Select
Loop
Do While sp > 0
If stack$(sp) = "(" Then
erflag = -2: Exit Sub
Else
lp2 = lp2 + 1
l2$(lp2) = stack$(sp)
if sp<50 then ln2(lp2)=lnsp(sp+1)
sp = sp - 1
End If
Loop
End Sub
'___________________________________________________________________________________________
function angle(x as double) as double
if degreesflag then angle=x/45*atn(1) else angle=x
end function
'___________________________________________________________________________________________
function toAngle(x as double) as double
if degreesflag then toAngle=x/atn(1)*45 else toAngle=x
end function
function translateF(byval FuncStr as string) as string
Dim separator as string
rem Convert local decimal separator to "."
separator =Mid(CStr(1.1),2,1)
FuncStr = replace(FuncStr,separator,".")
rem convert equation language to maths
FuncStr = replace(FuncStr,"cdot","*")
FuncStr = replace(FuncStr,"times","*")
FuncStr = replace(FuncStr,"over","/")
FuncStr = replace(FuncStr,"div","/")
rem parenthesis
FuncStr = replace(FuncStr,"left","")
FuncStr = replace(FuncStr,"right","")
FuncStr = replace(FuncStr,"{","(")
FuncStr = replace(FuncStr,"}",")")
FuncStr = replace(FuncStr,"[","(")
FuncStr = replace(FuncStr,"]",")")
rem spaces
FuncStr = replace(FuncStr,"~"," ")
FuncStr = replace(FuncStr,"`"," ")
rem math constants
FuncStr = replace(FuncStr,"%pi","pi")
FuncStr = replace(FuncStr,"pi"," 3.14159265358979 ")
FuncStr = replace(FuncStr,"e^"," 2.71828182845904^")
rem functions
FuncStr = replace(FuncStr,"sqrt","sqr")
FuncStr = replace(FuncStr,"tg","tan")
FuncStr = replace(FuncStr,"cotan","cot")
FuncStr = replace(FuncStr,"csc","cosec")
FuncStr = replace(FuncStr,"arcsech","arsech")
FuncStr = replace(FuncStr,"arccosech","arcosech")
FuncStr = replace(FuncStr,"acot","arccot")
FuncStr = replace(FuncStr,"arcot","arccot")
FuncStr = replace(FuncStr,"arccoth","arcoth")
FuncStr = replace(FuncStr,"atan","arctan")
FuncStr = replace(FuncStr,"artan","arctan")
FuncStr = replace(FuncStr,"arctanh","artanh")
FuncStr = replace(FuncStr,"acos","arccos")
FuncStr = replace(FuncStr,"arcos","arccos")
FuncStr = replace(FuncStr,"arccosh","arcosh")
FuncStr = replace(FuncStr,"asin","arcsin")
FuncStr = replace(FuncStr,"arsin","arcsin")
FuncStr = replace(FuncStr,"arcsinh","arsinh")
FuncStr = replace(FuncStr,"log_10","logten")
FuncStr = replace(FuncStr,"log10","logten")
FuncStr = replace(FuncStr,"log2","logtwo")
FuncStr = replace(FuncStr,"log_2","logtwo")
FuncStr = replace(FuncStr,"factorial","fact")
FuncStr = replace(FuncStr,"rand","rnd")
FuncStr = replace(FuncStr,"random","rnd")
FuncStr = replace(FuncStr,"rnd()","rnd ")
translateF = FuncStr
end function
function fact(n as single) as double
select case n
case 0,1: fact=1
case 2 : fact=2
case 3 : fact=6
case 4 : fact=24
case 5 : fact=120
case 6 : fact=720
case 7 : fact=5040
case 8 : fact=40320
case 9 : fact=362880
case 10 : fact=3628800
case 11 : fact=39916800
case 12 : fact=479001600
case 13 : fact=6227020800
case 14 : fact=87178291200
case 15 : fact=1307674368000
case 16 : fact=20922789888000
case 17 : fact=355687428096000
case 18 : fact=6.402373705728E+015
case 19 : fact=1.216451004088E+017
case 20 : fact=2.432902008177E+018
case 21 : fact=5.109094217171E+019
case 22 : fact=1.124000727778E+021
case 23 : fact=2.585201673889E+022
case 24 : fact=6.204484017332E+023
case 25 : fact=1.551121004333E+025
case 26 : fact=4.032914611266E+026
case 27 : fact=1.088886945042E+028
case 28 : fact=3.048883446117E+029
case 29 : fact=8.841761993740E+030
case 30 : fact=2.652528598122E+032
case 31 : fact=8.222838654178E+033
case else
fact=notdef
end select
end function
</script:module>
|