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