1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22 #ifndef SC_INTERPRE_HXX
23 #define SC_INTERPRE_HXX
24
25 #include <math.h>
26 #include <rtl/math.hxx>
27 #include "formula/errorcodes.hxx"
28 #include "cell.hxx"
29 #include "scdll.hxx"
30 #include "document.hxx"
31 #include "scmatrix.hxx"
32
33 #include <math.h>
34 #include <map>
35
36 // STLport definitions
37 // This works around some issues with Boost
38 //
39 #ifdef WNT
40 #define _STLP_HAS_NATIVE_FLOAT_ABS
41 #endif
42
43 // Math policy definitions for Boost
44 // This header must be included before including any Boost
45 // math function.
46 //
47 #ifndef BOOST_MATH_OVERFLOW_ERROR_POLICY
48 #define BOOST_MATH_OVERFLOW_ERROR_POLICY errno_on_error
49 #endif
50
51 class ScDocument;
52 class SbxVariable;
53 class ScBaseCell;
54 class ScFormulaCell;
55 class SvNumberFormatter;
56 class ScDBRangeBase;
57 struct MatrixDoubleOp;
58 struct ScQueryParam;
59 struct ScDBQueryParamBase;
60
61 struct ScCompare
62 {
63 double nVal[2];
64 String* pVal[2];
65 sal_Bool bVal[2];
66 sal_Bool bEmpty[2];
ScCompareScCompare67 ScCompare( String* p1, String* p2 )
68 {
69 pVal[ 0 ] = p1;
70 pVal[ 1 ] = p2;
71 bEmpty[0] = sal_False;
72 bEmpty[1] = sal_False;
73 }
74 };
75
76 struct ScCompareOptions
77 {
78 ScQueryEntry aQueryEntry;
79 bool bRegEx;
80 bool bMatchWholeCell;
81 bool bIgnoreCase;
82
83 ScCompareOptions( ScDocument* pDoc, const ScQueryEntry& rEntry, bool bReg );
84 private:
85 // Not implemented, prevent usage.
86 ScCompareOptions();
87 ScCompareOptions( const ScCompareOptions & );
88 ScCompareOptions& operator=( const ScCompareOptions & );
89 };
90
91 class ScToken;
92
93 #define MAXSTACK (16384 / sizeof(formula::FormulaToken*))
94
95 class ScTokenStack
96 {
97 public:
98 DECL_FIXEDMEMPOOL_NEWDEL( ScTokenStack )
99 formula::FormulaToken* pPointer[ MAXSTACK ];
100 };
101
102 enum ScIterFunc
103 {
104 ifSUM, // Sum
105 ifSUMSQ, // Sum squares
106 ifPRODUCT, // Product
107 ifAVERAGE, // Average
108 ifCOUNT, // Count
109 ifCOUNT2, // Count non-empty
110 ifMIN, // Minimum
111 ifMAX // Maximum
112 };
113
114 enum ScIterFuncIf
115 {
116 ifSUMIF, // Conditional sum
117 ifAVERAGEIF // Conditional average
118 };
119
120 enum ScIterFuncIfs
121 {
122 ifSUMIFS, // Multi-Conditional sum
123 ifAVERAGEIFS, // Multi-Conditional average
124 ifCOUNTIFS // Multi-Conditional count
125 };
126
127 struct FormulaTokenRef_less
128 {
operator ()FormulaTokenRef_less129 bool operator () ( const formula::FormulaConstTokenRef& r1, const formula::FormulaConstTokenRef& r2 ) const
130 { return &r1 < &r2; }
131 };
132 typedef ::std::map< const formula::FormulaConstTokenRef, formula::FormulaTokenRef, FormulaTokenRef_less> ScTokenMatrixMap;
133
134 class ScInterpreter
135 {
136 // distribution function objects need the GetxxxDist methods
137 friend class ScGammaDistFunction;
138 friend class ScBetaDistFunction;
139 friend class ScTDistFunction;
140 friend class ScFDistFunction;
141 friend class ScFDistLeftFunction;
142 friend class ScChiDistFunction;
143 friend class ScChiSqDistFunction;
144
145 public:
146 struct bitOperations {
147 enum bitArithmetic
148 {
149 BITAND,
150 BITOR,
151 BITXOR
152 };
153
154 enum bitShift
155 {
156 BITLSHIFT,
157 BITRSHIFT
158 };
159 };
160
161 DECL_FIXEDMEMPOOL_NEWDEL( ScInterpreter )
162
163 static void GlobalExit(); // aus ScGlobal::Clear() gerufen
164
165 /// Could string be a regular expression?
166 /// If pDoc!=NULL the document options are taken into account and if
167 /// RegularExpressions are disabled the function returns sal_False regardless
168 /// of the string content.
169 static sal_Bool MayBeRegExp( const String& rStr, const ScDocument* pDoc );
170
171 /// Fail safe division, returning an errDivisionByZero coded into a double
172 /// if denominator is 0.0
173 static inline double div( const double& fNumerator, const double& fDenominator );
174
175 ScMatrixRef GetNewMat(SCSIZE nC, SCSIZE nR);
176 private:
177 static ScTokenStack* pGlobalStack;
178 static sal_Bool bGlobalStackInUse;
179
180 formula::FormulaTokenIterator aCode;
181 ScAddress aPos;
182 ScTokenArray& rArr;
183 ScDocument* pDok;
184 formula::FormulaTokenRef xResult;
185 ScJumpMatrix* pJumpMatrix; // currently active array condition, if any
186 ScTokenMatrixMap* pTokenMatrixMap; // map ScToken* to formula::FormulaTokenRef if in array condition
187 ScFormulaCell* pMyFormulaCell; // the cell of this formula expression
188 SvNumberFormatter* pFormatter;
189
190 const formula::FormulaToken*
191 pCur; // current token
192 ScToken* pLastStackRefToken; // i120962: current valid reference token
193 bool bRefFunc; // i120962: is a reference function
194 String aTempStr; // for GetString()
195 ScTokenStack* pStackObj; // contains the stacks
196 formula::FormulaToken** pStack; // the current stack
197 sal_uInt16 nGlobalError; // global (local to this formula expression) error
198 sal_uInt16 sp; // stack pointer
199 sal_uInt16 maxsp; // the maximal used stack pointer
200 sal_uLong nFuncFmtIndex; // NumberFormatIndex of a function
201 sal_uLong nCurFmtIndex; // current NumberFormatIndex
202 sal_uLong nRetFmtIndex; // NumberFormatIndex of an expression, if any
203 short nFuncFmtType; // NumberFormatType of a function
204 short nCurFmtType; // current NumberFormatType
205 short nRetFmtType; // NumberFormatType of an expression
206 sal_uInt16 mnStringNoValueError; // the error set in ConvertStringToValue() if no value
207 sal_Bool glSubTotal; // flag for subtotal functions
208 sal_uInt8 cPar; // current count of parameters
209 sal_Bool bCalcAsShown; // precision as shown
210 sal_Bool bMatrixFormula; // formula cell is a matrix formula
211
212 //---------------------------------Funktionen in interpre.cxx---------
213 // nMust <= nAct <= nMax ? ok : PushError
214 inline sal_Bool MustHaveParamCount( short nAct, short nMust );
215 inline sal_Bool MustHaveParamCount( short nAct, short nMust, short nMax );
216 inline sal_Bool MustHaveParamCountMin( short nAct, short nMin );
217 void PushParameterExpected();
218 void PushIllegalParameter();
219 void PushIllegalArgument();
220 void PushNoValue();
221 void PushNA();
222 //-------------------------------------------------------------------------
223 // Funktionen fuer den Zugriff auf das Document
224 //-------------------------------------------------------------------------
225 void ReplaceCell( ScAddress& ); // for TableOp
226 void ReplaceCell( SCCOL& rCol, SCROW& rRow, SCTAB& rTab ); // for TableOp
227 sal_Bool IsTableOpInRange( const ScRange& );
228 sal_uLong GetCellNumberFormat( const ScAddress&, const ScBaseCell* );
229 double ConvertStringToValue( const String& );
230 double GetCellValue( const ScAddress&, const ScBaseCell* );
231 double GetCellValueOrZero( const ScAddress&, const ScBaseCell* );
232 double GetValueCellValue( const ScAddress&, const ScValueCell* );
GetCell(const ScAddress & rPos)233 ScBaseCell* GetCell( const ScAddress& rPos )
234 { return pDok->GetCell( rPos ); }
235 void GetCellString( String& rStr, const ScBaseCell* pCell );
GetCellErrCode(const ScBaseCell * pCell)236 inline sal_uInt16 GetCellErrCode( const ScBaseCell* pCell )
237 { return pCell ? pCell->GetErrorCode() : 0; }
GetCellType(const ScBaseCell * pCell)238 inline CellType GetCellType( const ScBaseCell* pCell )
239 { return pCell ? pCell->GetCellType() : CELLTYPE_NONE; }
240 /// Really empty or inherited emptiness.
HasCellEmptyData(const ScBaseCell * pCell)241 inline sal_Bool HasCellEmptyData( const ScBaseCell* pCell )
242 { return pCell ? pCell->HasEmptyData() : sal_True; }
243 /// This includes inherited emptiness, which usually is regarded as value!
HasCellValueData(const ScBaseCell * pCell)244 inline sal_Bool HasCellValueData( const ScBaseCell* pCell )
245 { return pCell ? pCell->HasValueData() : sal_False; }
246 /// Not empty and not value.
HasCellStringData(const ScBaseCell * pCell)247 inline sal_Bool HasCellStringData( const ScBaseCell* pCell )
248 { return pCell ? pCell->HasStringData() : sal_False; }
249
250 sal_Bool CreateDoubleArr(SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
251 SCCOL nCol2, SCROW nRow2, SCTAB nTab2, sal_uInt8* pCellArr);
252 sal_Bool CreateStringArr(SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
253 SCCOL nCol2, SCROW nRow2, SCTAB nTab2, sal_uInt8* pCellArr);
254 sal_Bool CreateCellArr(SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
255 SCCOL nCol2, SCROW nRow2, SCTAB nTab2, sal_uInt8* pCellArr);
256
257 //-----------------------------------------------------------------------------
258 // Stack operations
259 //-----------------------------------------------------------------------------
260
261 /** Does substitute with formula::FormulaErrorToken in case nGlobalError is set and the token
262 passed is not formula::FormulaErrorToken.
263 Increments RefCount of the original token if not substituted. */
264 void Push( formula::FormulaToken& r );
265
266 /** Does not substitute with formula::FormulaErrorToken in case nGlobalError is set.
267 Used to push RPN tokens or from within Push() or tokens that are already
268 explicit formula::FormulaErrorToken. Increments RefCount. */
269 void PushWithoutError( formula::FormulaToken& r );
270
271 /** Clones the token to be pushed or substitutes with formula::FormulaErrorToken if
272 nGlobalError is set and the token passed is not formula::FormulaErrorToken. */
273 void PushTempToken( const formula::FormulaToken& );
274
275 /** Does substitute with formula::FormulaErrorToken in case nGlobalError is set and the token
276 passed is not formula::FormulaErrorToken.
277 Increments RefCount of the original token if not substituted.
278 ATTENTION! The token had to be allocated with `new' and must not be used
279 after this call if no RefCount was set because possibly it gets immediately
280 deleted in case of an errStackOverflow or if substituted with formula::FormulaErrorToken! */
281 void PushTempToken( formula::FormulaToken* );
282
283 /** Does not substitute with formula::FormulaErrorToken in case nGlobalError is set.
284 Used to push tokens from within PushTempToken() or tokens that are already
285 explicit formula::FormulaErrorToken. Increments RefCount.
286 ATTENTION! The token had to be allocated with `new' and must not be used
287 after this call if no RefCount was set because possibly it gets immediately
288 decremented again and thus deleted in case of an errStackOverflow! */
289 void PushTempTokenWithoutError( formula::FormulaToken* );
290
291 /** If nGlobalError is set push formula::FormulaErrorToken.
292 If nGlobalError is not set do nothing.
293 Used in PushTempToken() and alike to simplify handling.
294 @return: <TRUE/> if nGlobalError. */
IfErrorPushError()295 inline bool IfErrorPushError()
296 {
297 if (nGlobalError)
298 {
299 PushTempTokenWithoutError( new formula::FormulaErrorToken( nGlobalError));
300 return true;
301 }
302 return false;
303 }
304
305 /** Obtain cell result / content from address and push as temp token.
306 bDisplayEmptyAsString is passed to ScEmptyCell in case of an empty cell
307 result. Also obtain number format and type if _both_, type and index
308 pointer, are not NULL. */
309 void PushCellResultToken( bool bDisplayEmptyAsString, const ScAddress & rAddress,
310 short * pRetTypeExpr, sal_uLong * pRetIndexExpr );
311
312 formula::FormulaTokenRef PopToken();
313 void Pop();
314 void PopError();
315 double PopDouble();
316 const String& PopString();
317 void ValidateRef( const ScSingleRefData & rRef );
318 void ValidateRef( const ScComplexRefData & rRef );
319 void ValidateRef( const ScRefList & rRefList );
320 void SingleRefToVars( const ScSingleRefData & rRef, SCCOL & rCol, SCROW & rRow, SCTAB & rTab );
321 void PopSingleRef( ScAddress& );
322 void PopSingleRef(SCCOL& rCol, SCROW &rRow, SCTAB& rTab);
323 void DoubleRefToRange( const ScComplexRefData&, ScRange&, sal_Bool bDontCheckForTableOp = sal_False );
324 /** If formula::StackVar formula::svDoubleRef pop ScDoubleRefToken and return values of
325 ScComplexRefData.
326 Else if StackVar svRefList return values of the ScComplexRefData where
327 rRefInList is pointing to. rRefInList is incremented. If rRefInList was the
328 last element in list pop ScRefListToken and set rRefInList to 0, else
329 rParam is incremented (!) to allow usage as in
330 while(nParamCount--) PopDoubleRef(aRange,nParamCount,nRefInList);
331 */
332 void PopDoubleRef( ScRange & rRange, short & rParam, size_t & rRefInList );
333 void PopDoubleRef( ScRange&, sal_Bool bDontCheckForTableOp = sal_False );
334 void DoubleRefToVars( const ScToken* p,
335 SCCOL& rCol1, SCROW &rRow1, SCTAB& rTab1,
336 SCCOL& rCol2, SCROW &rRow2, SCTAB& rTab2,
337 sal_Bool bDontCheckForTableOp = sal_False );
338 ScDBRangeBase* PopDoubleRef();
339 void PopDoubleRef(SCCOL& rCol1, SCROW &rRow1, SCTAB& rTab1,
340 SCCOL& rCol2, SCROW &rRow2, SCTAB& rTab2,
341 sal_Bool bDontCheckForTableOp = sal_False );
342 sal_Bool PopDoubleRefOrSingleRef( ScAddress& rAdr );
343 void PopDoubleRefPushMatrix();
344 // If MatrixFormula: convert formula::svDoubleRef to svMatrix, create JumpMatrix.
345 // Else convert area reference parameters marked as ForceArray to array.
346 // Returns sal_True if JumpMatrix created.
347 bool ConvertMatrixParameters();
348 inline void MatrixDoubleRefToMatrix(); // if MatrixFormula: PopDoubleRefPushMatrix
349 // If MatrixFormula or ForceArray: ConvertMatrixParameters()
350 inline bool MatrixParameterConversion();
351 ScMatrixRef PopMatrix();
352 //void PushByte(sal_uInt8 nVal);
353 void PushDouble(double nVal);
354 void PushInt( int nVal );
355 void PushStringBuffer( const sal_Unicode* pString );
356 void PushString( const String& rString );
357 void PushSingleRef(SCCOL nCol, SCROW nRow, SCTAB nTab);
358 void PushDoubleRef(SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
359 SCCOL nCol2, SCROW nRow2, SCTAB nTab2);
360 void PushMatrix(ScMatrix* pMat);
361 void PushError( sal_uInt16 nError );
362 /// Raw stack type without default replacements.
363 formula::StackVar GetRawStackType();
364 /// Stack type with replacement of defaults, e.g. svMissing and formula::svEmptyCell will result in formula::svDouble.
365 formula::StackVar GetStackType();
366 // peek StackType of Parameter, Parameter 1 == TOS, 2 == TOS-1, ...
367 formula::StackVar GetStackType( sal_uInt8 nParam );
GetByte()368 sal_uInt8 GetByte() { return cPar; }
369 // generiert aus DoubleRef positionsabhaengige SingleRef
370 sal_Bool DoubleRefToPosSingleRef( const ScRange& rRange, ScAddress& rAdr );
371 double GetDouble();
372 double GetDoubleWithDefault(double nDefault);
373 sal_Bool IsMissing();
GetBool()374 sal_Bool GetBool() { return GetDouble() != 0.0; }
375 const String& GetString();
376 // pop matrix and obtain one element, upper left or according to jump matrix
377 ScMatValType GetDoubleOrStringFromMatrix( double& rDouble, String& rString );
378 ScMatrixRef CreateMatrixFromDoubleRef( const formula::FormulaToken* pToken,
379 SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
380 SCCOL nCol2, SCROW nRow2, SCTAB nTab2 );
381 inline ScTokenMatrixMap& GetTokenMatrixMap();
382 ScTokenMatrixMap* CreateTokenMatrixMap();
383 ScMatrixRef GetMatrix();
384 void ScTableOp(); // Mehrfachoperationen
385 void ScErrCell(); // Sonderbehandlung
386 // Fehlerzelle
387 //-----------------------------allgemeine Hilfsfunktionen
388 void SetMaxIterationCount(sal_uInt16 n);
CurFmtToFuncFmt()389 inline void CurFmtToFuncFmt()
390 { nFuncFmtType = nCurFmtType; nFuncFmtIndex = nCurFmtIndex; }
391 // Check for String overflow of rResult+rAdd and set error and erase rResult
392 // if so. Return sal_True if ok, sal_False if overflow
393 inline sal_Bool CheckStringResultLen( String& rResult, const String& rAdd );
394 // Set error according to rVal, and set rVal to 0.0 if there was an error.
395 inline void TreatDoubleError( double& rVal );
396 // Lookup using ScLookupCache, @returns sal_True if found and result address
397 bool LookupQueryWithCache( ScAddress & o_rResultPos,
398 const ScQueryParam & rParam ) const;
399
400 //---------------------------------Funktionen in interpr1.cxx---------
401 void ScIfJump();
402 void ScChoseJump();
403
404 // Be sure to only call this if pStack[sp-nStackLevel] really contains a
405 // ScJumpMatrixToken, no further checks are applied!
406 // Returns true if last jump was executed and result matrix pushed.
407 bool JumpMatrix( short nStackLevel );
408
409 /** @param pOptions
410 NULL means case sensitivity document option is to be used!
411 */
412 double CompareFunc( const ScCompare& rComp, ScCompareOptions* pOptions = NULL );
413 double Compare();
414 /** @param pOptions
415 NULL means case sensitivity document option is to be used!
416 */
417 ScMatrixRef CompareMat( ScCompareOptions* pOptions = NULL );
418 ScMatrixRef QueryMat( ScMatrix* pMat, ScCompareOptions& rOptions );
419 void ScEqual();
420 void ScNotEqual();
421 void ScLess();
422 void ScGreater();
423 void ScLessEqual();
424 void ScGreaterEqual();
425 void ScAnd();
426 void ScOr();
427 void ScXor();
428 void ScNot();
429 void ScNeg();
430 void ScPercentSign();
431 void ScIntersect();
432 void ScRangeFunc();
433 void ScUnionFunc();
434 void ScPi();
435 void ScRandom();
436 void ScTrue();
437 void ScFalse();
438 void ScDeg();
439 void ScRad();
440 void ScSin();
441 void ScCos();
442 void ScTan();
443 void ScCot();
444 void ScArcSin();
445 void ScArcCos();
446 void ScArcTan();
447 void ScArcCot();
448 void ScSinHyp();
449 void ScCosHyp();
450 void ScTanHyp();
451 void ScCotHyp();
452 void ScArcSinHyp();
453 void ScArcCosHyp();
454 void ScArcTanHyp();
455 void ScArcCotHyp();
456 void ScCosecant();
457 void ScSecant();
458 void ScCosecantHyp();
459 void ScSecantHyp();
460 void ScExp();
461 void ScLn();
462 void ScLog10();
463 void ScSqrt();
464 void ScIsEmpty();
465 short IsString();
466 void ScIsString();
467 void ScIsNonString();
468 void ScIsLogical();
469 void ScType();
470 void ScCell();
471 void ScIsRef();
472 void ScIsValue();
473 void ScIsFormula();
474 void ScFormula();
475 void ScRoman();
476 void ScArabic();
477 void ScIsNV();
478 void ScIsErr();
479 void ScIsError();
480 short IsEven();
481 void ScIsEven();
482 void ScIsOdd();
483 void ScN();
484 void ScCode();
485 void ScTrim();
486 void ScUpper();
487 void ScPropper();
488 void ScLower();
489 void ScLen();
490 void ScT();
491 void ScValue();
492 void ScClean();
493 void ScChar();
494 void ScJis();
495 void ScAsc();
496 void ScUnicode();
497 void ScUnichar();
498 void ScMin( sal_Bool bTextAsZero = sal_False );
499 void ScMax( sal_Bool bTextAsZero = sal_False );
500 double IterateParameters( ScIterFunc, sal_Bool bTextAsZero = sal_False );
501 void ScSumSQ();
502 void ScSum();
503 void ScProduct();
504 void ScAverage( sal_Bool bTextAsZero = sal_False );
505 void ScCount();
506 void ScCount2();
507 void GetStVarParams( double& rVal, double& rValCount, sal_Bool bTextAsZero = sal_False );
508 void ScVar( sal_Bool bTextAsZero = sal_False );
509 void ScVarP( sal_Bool bTextAsZero = sal_False );
510 void ScStDev( sal_Bool bTextAsZero = sal_False );
511 void ScStDevP( sal_Bool bTextAsZero = sal_False );
512 void ScColumns();
513 void ScRows();
514 void ScTables();
515 void ScColumn();
516 void ScRow();
517 void ScTable();
518 void ScMatch();
519 double IterateParametersIf( ScIterFuncIf );
520 void ScCountIf();
521 void ScSumIf();
522 void ScAverageIf();
523 double IterateParametersIfs( ScIterFuncIfs );
524 void ScSumIfs();
525 void ScAverageIfs();
526 void ScCountIfs();
527 void ScCountEmptyCells();
528 void ScLookup();
529 void ScHLookup();
530 void ScVLookup();
531 void ScSubTotal();
532 void ScBitAnd();
533 void ScBitOr();
534 void ScBitXor();
535 void ScBitArithmeticOps( bitOperations::bitArithmetic );
536 void ScBitLShift();
537 void ScBitRShift();
538 void ScBitShiftOps(bitOperations::bitShift);
539
540 // If upon call rMissingField==sal_True then the database field parameter may be
541 // missing (Xcl DCOUNT() syntax), or may be faked as missing by having the
542 // value 0.0 or being exactly the entire database range reference (old SO
543 // compatibility). If this was the case then rMissingField is set to sal_True upon
544 // return. If rMissingField==sal_False upon call all "missing cases" are considered
545 // to be an error.
546 ScDBQueryParamBase* GetDBParams( sal_Bool& rMissingField );
547
548 void DBIterator( ScIterFunc );
549 void ScDBSum();
550 void ScDBCount();
551 void ScDBCount2();
552 void ScDBAverage();
553 void ScDBGet();
554 void ScDBMax();
555 void ScDBMin();
556 void ScDBProduct();
557 void GetDBStVarParams( double& rVal, double& rValCount );
558 void ScDBStdDev();
559 void ScDBStdDevP();
560 void ScDBVar();
561 void ScDBVarP();
562 void ScIndirect();
563 void ScAddressFunc();
564 void ScOffset();
565 void ScIndex();
566 void ScMultiArea();
567 void ScAreas();
568 void ScCurrency();
569 void ScReplace();
570 void ScFixed();
571 void ScFind();
572 void ScExact();
573 void ScLeft();
574 void ScRight();
575 void ScSearch();
576 void ScMid();
577 void ScText();
578 void ScSubstitute();
579 void ScRept();
580 void ScConcat();
581 void ScExternal();
582 void ScMissing();
583 void ScMacro();
584 sal_Bool SetSbxVariable( SbxVariable* pVar, const ScAddress& );
585 sal_Bool SetSbxVariable( SbxVariable* pVar, SCCOL nCol, SCROW nRow, SCTAB nTab );
586 void ScErrorType( bool bODF = false );
587 void ScDBArea();
588 void ScColRowNameAuto();
589 void ScExternalRef();
590 void ScGetPivotData();
591 void ScHyperLink();
592 void ScBahtText();
593 void ScTTT();
594
595 //----------------Funktionen in interpr2.cxx---------------
596
597 /** Obtain the date serial number for a given date.
598 @param bStrict
599 If sal_False, nYear < 100 takes the two-digit year setting into account,
600 and rollover of invalid calendar dates takes place, e.g. 1999-02-31 =>
601 1999-03-03.
602 If sal_True, the date passed must be a valid Gregorian calendar date. No
603 two-digit expanding or rollover is done.
604 */
605 double GetDateSerial( sal_Int16 nYear, sal_Int16 nMonth, sal_Int16 nDay, bool bStrict );
606
607 void ScGetActDate();
608 void ScGetActTime();
609 void ScGetYear();
610 void ScGetMonth();
611 void ScGetDay();
612 void ScGetDayOfWeek();
613 void ScGetWeekOfYear();
614 void ScIsoWeekNum();
615 void ScNumberValue();
616 void ScFindB();
617 void ScSearchB();
618 void ScReplaceB();
619 void ScEasterSunday();
620 void ScGetHour();
621 void ScGetMin();
622 void ScGetSec();
623 void ScPlusMinus();
624 void ScAbs();
625 void ScInt();
626 void ScEven();
627 void ScOdd();
628 void ScCeil();
629 void ScFloor();
630 void RoundNumber( rtl_math_RoundingMode eMode );
631 void ScRound();
632 void ScRoundUp();
633 void ScRoundDown();
634 void ScGetDateValue();
635 void ScGetTimeValue();
636 void ScArcTan2();
637 void ScLog();
638 void ScGetDate();
639 void ScGetTime();
640 void ScGetDiffDate();
641 void ScGetDiffDate360();
642 void ScDateDif();
643 void ScCurrent();
644 void ScStyle();
645 void ScDde();
646 void ScBase();
647 void ScDecimal();
648 void ScConvert();
649 void ScEuroConvert();
650
651 //----------------------- Finanzfunktionen ------------------------------------
652 void ScNPV();
653 void ScIRR();
654 void ScMIRR();
655 void ScISPMT();
656
657 double ScGetBw(double fZins, double fZzr, double fRmz,
658 double fZw, double fF);
659 void ScBW();
660 void ScDIA();
661 double ScGetGDA(double fWert, double fRest, double fDauer,
662 double fPeriode, double fFaktor);
663 void ScGDA();
664 void ScGDA2();
665 double ScInterVDB(double fWert,double fRest,double fDauer,double fDauer1,
666 double fPeriode,double fFaktor);
667 void ScVDB();
668 void ScLaufz();
669 void ScLIA();
670 double ScGetRmz(double fZins, double fZzr, double fBw,
671 double fZw, double fF);
672 void ScRMZ();
673 void ScZGZ();
674 double ScGetZw(double fZins, double fZzr, double fRmz,
675 double fBw, double fF);
676 void ScZW();
677 void ScZZR();
678 bool RateIteration(double fNper, double fPayment, double fPv,
679 double fFv, double fPayType, double& fGuess);
680 void ScZins();
681 double ScGetZinsZ(double fZins, double fZr, double fZzr, double fBw,
682 double fZw, double fF, double& fRmz);
683 void ScZinsZ();
684 void ScKapz();
685 void ScKumZinsZ();
686 void ScKumKapZ();
687 void ScEffektiv();
688 void ScNominal();
689 void ScMod();
690 void ScBackSolver();
691 void ScIntercept();
692 //-------------------------Funktionen in interpr5.cxx--------------------------
693 double ScGetGCD(double fx, double fy);
694 void ScGCD();
695 void ScLCM();
696 void ScPower();
697 void ScAmpersand();
698 void ScAdd();
699 void ScSub();
700 void ScMul();
701 void ScDiv();
702 void ScPow();
703 //-------------------------- Matrixfunktionen ---------------------------------
704
705 void ScMatValue();
706 void MEMat(ScMatrix* mM, SCSIZE n);
707 void ScMatDet();
708 void ScMatInv();
709 void ScMatMult();
710 void ScMatTrans();
711 void ScEMat();
712 void ScMatRef();
713 ScMatrixRef MatConcat(ScMatrix* pMat1, ScMatrix* pMat2);
714 void ScSumProduct();
715 void ScSumX2MY2();
716 void ScSumX2DY2();
717 void ScSumXMY2();
718 void ScGrowth();
719 bool CalculateSkew(double& fSum,double& fCount,double& vSum,std::vector<double>& values);
720 void CalculateSlopeIntercept(sal_Bool bSlope);
721 void CalculateSmallLarge(sal_Bool bSmall);
722 void CalculatePearsonCovar(sal_Bool _bPearson,sal_Bool _bStexy);
723 bool CalculateTest( sal_Bool _bTemplin
724 ,const SCSIZE nC1, const SCSIZE nC2,const SCSIZE nR1,const SCSIZE nR2
725 ,const ScMatrixRef& pMat1,const ScMatrixRef& pMat2
726 ,double& fT,double& fF);
727 void CalculateLookup(sal_Bool HLookup);
728 bool FillEntry(ScQueryEntry& rEntry);
729 void CalculateAddSub(sal_Bool _bSub);
730 void CalculateTrendGrowth(bool _bGrowth);
731 void CalulateRGPRKP(bool _bRKP);
732 void CalculateSumX2MY2SumX2DY2(sal_Bool _bSumX2DY2);
733 void CalculateMatrixValue(const ScMatrix* pMat,SCSIZE nC,SCSIZE nR);
734 bool CheckMatrix(bool _bLOG,sal_uInt8& nCase,SCSIZE& nCX,SCSIZE& nCY,SCSIZE& nRX,SCSIZE& nRY,SCSIZE& M,SCSIZE& N,ScMatrixRef& pMatX,ScMatrixRef& pMatY);
735 void ScRGP();
736 void ScRKP();
737 void ScForecast();
738 //------------------------- Functions in interpr3.cxx -------------------------
739 void ScNoName();
740 void ScBadName();
741 // Statistik:
742 double phi(double x);
743 double integralPhi(double x);
744 double taylor(double* pPolynom, sal_uInt16 nMax, double x);
745 double gauss(double x);
746 double gaussinv(double x);
747 double GetBetaDist(double x, double alpha, double beta); //cumulative distribution function
748 double GetBetaDistPDF(double fX, double fA, double fB); //probability density function)
749 double GetChiDist(double fChi, double fDF); // for LEGACY.CHIDIST, returns right tail
750 double GetChiSqDistCDF(double fX, double fDF); // for CHISQDIST, returns left tail
751 double GetChiSqDistPDF(double fX, double fDF); // probability density function
752 double GetFDist(double x, double fF1, double fF2);
753 double GetHypGeomDist( double x, double n, double M, double N );
754 double GetTDist(double T, double fDF);
755 double Fakultaet(double x);
756 double BinomKoeff(double n, double k);
757 double GetGamma(double x);
758 double GetLogGamma(double x);
759 double GetBeta(double fAlpha, double fBeta);
760 double GetLogBeta(double fAlpha, double fBeta);
761 double GetBinomDistPMF(double x, double n, double p); //probability mass function
762 void ScLogGamma();
763 void ScGamma();
764 void ScPhi();
765 void ScGauss();
766 void ScStdNormDist();
767 void ScFisher();
768 void ScFisherInv();
769 void ScFact();
770 void ScNormDist();
771 void ScGammaDist();
772 void ScGammaInv();
773 void ScExpDist();
774 void ScBinomDist();
775 void ScPoissonDist();
776 void ScKombin();
777 void ScKombin2();
778 void ScVariationen();
779 void ScVariationen2();
780 void ScB();
781 void ScHypGeomDist();
782 void ScLogNormDist();
783 void ScLogNormInv();
784 void ScTDist();
785 void ScFDist();
786 void ScFDistLeft();
787 void ScChiDist(); // for LEGACY.CHIDIST, returns right tail
788 void ScChiSqDist(); // returns left tail or density
789 void ScChiSqInv(); //invers to CHISQDIST
790 void ScWeibull();
791 void ScBetaDist();
792 void ScFInv();
793 void ScFInvLeft();
794 void ScTInv();
795 void ScChiInv();
796 void ScBetaInv();
797 void ScCritBinom();
798 void ScNegBinomDist();
799 void ScKurt();
800 void ScHarMean();
801 void ScGeoMean();
802 void ScStandard();
803 void ScSkew();
804 void ScSkewP();
805 void ScIfna();
806 void ScIfError();
807 void ScIfnaOrError( bool bAnyError );
808 void ScMedian();
809 double GetMedian( ::std::vector<double> & rArray );
810 double GetPercentile( ::std::vector<double> & rArray, double fPercentile );
811 void GetNumberSequenceArray( sal_uInt8 nParamCount, ::std::vector<double>& rArray );
812 void GetSortArray(sal_uInt8 nParamCount, ::std::vector<double>& rSortArray, ::std::vector<long>* pIndexOrder = NULL);
813 void QuickSort(::std::vector<double>& rSortArray, ::std::vector<long>* pIndexOrder = NULL);
814 void ScModalValue();
815 void ScAveDev();
816 void ScDevSq();
817 void ScZTest();
818 void ScTTest();
819 void ScFTest();
820 void ScChiTest();
821 void ScRank();
822 void ScPercentile();
823 void ScPercentrank();
824 void ScLarge();
825 void ScSmall();
826 void ScFrequency();
827 void ScQuartile();
828 void ScNormInv();
829 void ScSNormInv();
830 void ScConfidence();
831 void ScTrimMean();
832 void ScProbability();
833 void ScCorrel();
834 void ScCovar();
835 void ScPearson();
836 void ScRSQ();
837 void ScSTEXY();
838 void ScSlope();
839 void ScTrend();
840 void ScInfo();
841 void ScLenB();
842 void ScRightB();
843 void ScLeftB();
844 void ScMidB();
845
846 //------------------------ Functions in interpr6.cxx -------------------------
847
848 static const double fMaxGammaArgument; // defined in interpr3.cxx
849
850 double GetGammaContFraction(double fA,double fX);
851 double GetGammaSeries(double fA,double fX);
852 double GetLowRegIGamma(double fA,double fX); // lower regularized incomplete gamma function, GAMMAQ
853 double GetUpRegIGamma(double fA,double fX); // upper regularized incomplete gamma function, GAMMAP
854 // probability density function; fLambda is "scale" parameter
855 double GetGammaDistPDF(double fX, double fAlpha, double fLambda);
856 // cumulative distribution function; fLambda is "scale" parameter
857 double GetGammaDist(double fX, double fAlpha, double fLambda);
858
859 //----------------------------------------------------------------------------
860 public:
861 ScInterpreter( ScFormulaCell* pCell, ScDocument* pDoc,
862 const ScAddress&, ScTokenArray& );
863 ~ScInterpreter();
864
865 formula::StackVar Interpret();
866
SetError(sal_uInt16 nError)867 void SetError(sal_uInt16 nError)
868 { if (nError && !nGlobalError) nGlobalError = nError; }
869
GetError() const870 sal_uInt16 GetError() const { return nGlobalError; }
GetResultType() const871 formula::StackVar GetResultType() const { return xResult->GetType(); }
GetStringResult() const872 const String& GetStringResult() const { return xResult->GetString(); }
GetNumResult() const873 double GetNumResult() const { return xResult->GetDouble(); }
874 formula::FormulaTokenRef
GetResultToken() const875 GetResultToken() const { return xResult; }
GetRetFormatType() const876 short GetRetFormatType() const { return nRetFmtType; }
GetRetFormatIndex() const877 sal_uLong GetRetFormatIndex() const { return nRetFmtIndex; }
GetLastStackRefToken()878 ScToken* GetLastStackRefToken() { return pLastStackRefToken; }
IsReferenceFunc()879 bool IsReferenceFunc() { return bRefFunc; }
880 };
881
882
MatrixDoubleRefToMatrix()883 inline void ScInterpreter::MatrixDoubleRefToMatrix()
884 {
885 if ( bMatrixFormula && GetStackType() == formula::svDoubleRef )
886 {
887 GetTokenMatrixMap(); // make sure it exists, create if not.
888 PopDoubleRefPushMatrix();
889 }
890 }
891
892
MatrixParameterConversion()893 inline bool ScInterpreter::MatrixParameterConversion()
894 {
895 if ( (bMatrixFormula || pCur->HasForceArray()) && !pJumpMatrix && sp > 0 )
896 return ConvertMatrixParameters();
897 return false;
898 }
899
900
GetTokenMatrixMap()901 inline ScTokenMatrixMap& ScInterpreter::GetTokenMatrixMap()
902 {
903 if (!pTokenMatrixMap)
904 pTokenMatrixMap = CreateTokenMatrixMap();
905 return *pTokenMatrixMap;
906 }
907
908
MustHaveParamCount(short nAct,short nMust)909 inline sal_Bool ScInterpreter::MustHaveParamCount( short nAct, short nMust )
910 {
911 if ( nAct == nMust )
912 return sal_True;
913 if ( nAct < nMust )
914 PushParameterExpected();
915 else
916 PushIllegalParameter();
917 return sal_False;
918 }
919
920
MustHaveParamCount(short nAct,short nMust,short nMax)921 inline sal_Bool ScInterpreter::MustHaveParamCount( short nAct, short nMust, short nMax )
922 {
923 if ( nMust <= nAct && nAct <= nMax )
924 return sal_True;
925 if ( nAct < nMust )
926 PushParameterExpected();
927 else
928 PushIllegalParameter();
929 return sal_False;
930 }
931
932
MustHaveParamCountMin(short nAct,short nMin)933 inline sal_Bool ScInterpreter::MustHaveParamCountMin( short nAct, short nMin )
934 {
935 if ( nAct >= nMin )
936 return sal_True;
937 PushParameterExpected();
938 return sal_False;
939 }
940
941
CheckStringResultLen(String & rResult,const String & rAdd)942 inline sal_Bool ScInterpreter::CheckStringResultLen( String& rResult, const String& rAdd )
943 {
944 if ( (sal_uLong) rResult.Len() + rAdd.Len() > STRING_MAXLEN )
945 {
946 SetError( errStringOverflow );
947 rResult.Erase();
948 return sal_False;
949 }
950 return sal_True;
951 }
952
953
TreatDoubleError(double & rVal)954 inline void ScInterpreter::TreatDoubleError( double& rVal )
955 {
956 if ( !::rtl::math::isFinite( rVal ) )
957 {
958 sal_uInt16 nErr = GetDoubleErrorValue( rVal );
959 if ( nErr )
960 SetError( nErr );
961 else
962 SetError( errNoValue );
963 rVal = 0.0;
964 }
965 }
966
967
968 // static
div(const double & fNumerator,const double & fDenominator)969 inline double ScInterpreter::div( const double& fNumerator, const double& fDenominator )
970 {
971 return (fDenominator != 0.0) ? (fNumerator / fDenominator) :
972 CreateDoubleError( errDivisionByZero);
973 }
974
975 #endif
976