-
-
Notifications
You must be signed in to change notification settings - Fork 614
/
interpr1.cxx
9368 lines (8880 loc) · 307 KB
/
interpr1.cxx
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "interpre.hxx"
#include "scitems.hxx"
#include <editeng/langitem.hxx>
#include <editeng/justifyitem.hxx>
#include <osl/thread.h>
#include <svx/algitem.hxx>
#include <unotools/textsearch.hxx>
#include <svl/zforlist.hxx>
#include <svl/zformat.hxx>
#include <tools/urlobj.hxx>
#include <unotools/charclass.hxx>
#include <sfx2/docfile.hxx>
#include <sfx2/printer.hxx>
#include <unotools/collatorwrapper.hxx>
#include <unotools/transliterationwrapper.hxx>
#include <rtl/ustring.hxx>
#include <unicode/uchar.h>
#include "patattr.hxx"
#include "global.hxx"
#include "document.hxx"
#include "dociter.hxx"
#include "formulacell.hxx"
#include "scmatrix.hxx"
#include "docoptio.hxx"
#include "globstr.hrc"
#include "attrib.hxx"
#include "jumpmatrix.hxx"
#include "cellkeytranslator.hxx"
#include "lookupcache.hxx"
#include "rangenam.hxx"
#include "rangeutl.hxx"
#include "compiler.hxx"
#include "externalrefmgr.hxx"
#include <basic/sbstar.hxx>
#include "doubleref.hxx"
#include "queryparam.hxx"
#include "queryentry.hxx"
#include "tokenarray.hxx"
#include "compare.hxx"
#include <comphelper/processfactory.hxx>
#include <comphelper/random.hxx>
#include <comphelper/string.hxx>
#include <svl/sharedstringpool.hxx>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <memory>
#include <limits>
static const sal_uInt64 n2power48 = SAL_CONST_UINT64( 281474976710656); // 2^48
IMPL_FIXEDMEMPOOL_NEWDEL( ScTokenStack )
IMPL_FIXEDMEMPOOL_NEWDEL( ScInterpreter )
ScCalcConfig *ScInterpreter::mpGlobalConfig = nullptr;
using namespace formula;
using ::std::unique_ptr;
void ScInterpreter::ScIfJump()
{
const short* pJump = pCur->GetJump();
short nJumpCount = pJump[ 0 ];
MatrixJumpConditionToMatrix();
switch ( GetStackType() )
{
case svMatrix:
{
ScMatrixRef pMat = PopMatrix();
if ( !pMat )
PushIllegalParameter();
else
{
FormulaConstTokenRef xNew;
ScTokenMatrixMap::const_iterator aMapIter;
// DoubleError handled by JumpMatrix
pMat->SetErrorInterpreter( nullptr);
SCSIZE nCols, nRows;
pMat->GetDimensions( nCols, nRows );
if ( nCols == 0 || nRows == 0 )
{
PushIllegalArgument();
return;
}
else if (pTokenMatrixMap && ((aMapIter = pTokenMatrixMap->find( pCur)) != pTokenMatrixMap->end()))
xNew = (*aMapIter).second;
else
{
std::shared_ptr<ScJumpMatrix> pJumpMat( std::make_shared<ScJumpMatrix>( nCols, nRows ) );
for ( SCSIZE nC=0; nC < nCols; ++nC )
{
for ( SCSIZE nR=0; nR < nRows; ++nR )
{
double fVal;
bool bTrue;
bool bIsValue = pMat->IsValue(nC, nR);
if (bIsValue)
{
fVal = pMat->GetDouble(nC, nR);
bIsValue = ::rtl::math::isFinite(fVal);
bTrue = bIsValue && (fVal != 0.0);
if (bTrue)
fVal = 1.0;
}
else
{
// Treat empty and empty path as 0, but string
// as error.
bIsValue = (!pMat->IsString(nC, nR) || pMat->IsEmpty(nC, nR));
bTrue = false;
fVal = (bIsValue ? 0.0 : CreateDoubleError( FormulaError::NoValue));
}
if ( bTrue )
{ // TRUE
if( nJumpCount >= 2 )
{ // THEN path
pJumpMat->SetJump( nC, nR, fVal,
pJump[ 1 ],
pJump[ nJumpCount ]);
}
else
{ // no parameter given for THEN
pJumpMat->SetJump( nC, nR, fVal,
pJump[ nJumpCount ],
pJump[ nJumpCount ]);
}
}
else
{ // FALSE
if( nJumpCount == 3 && bIsValue )
{ // ELSE path
pJumpMat->SetJump( nC, nR, fVal,
pJump[ 2 ],
pJump[ nJumpCount ]);
}
else
{ // no parameter given for ELSE,
// or DoubleError
pJumpMat->SetJump( nC, nR, fVal,
pJump[ nJumpCount ],
pJump[ nJumpCount ]);
}
}
}
}
xNew = new ScJumpMatrixToken( pJumpMat );
GetTokenMatrixMap().emplace(pCur, xNew);
}
if (!xNew.get())
{
PushIllegalArgument();
return;
}
PushTokenRef( xNew);
// set endpoint of path for main code line
aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
}
}
break;
default:
{
if ( GetBool() )
{ // TRUE
if( nJumpCount >= 2 )
{ // THEN path
aCode.Jump( pJump[ 1 ], pJump[ nJumpCount ] );
}
else
{ // no parameter given for THEN
nFuncFmtType = css::util::NumberFormat::LOGICAL;
PushInt(1);
aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
}
}
else
{ // FALSE
if( nJumpCount == 3 )
{ // ELSE path
aCode.Jump( pJump[ 2 ], pJump[ nJumpCount ] );
}
else
{ // no parameter given for ELSE
nFuncFmtType = css::util::NumberFormat::LOGICAL;
PushInt(0);
aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
}
}
}
}
}
/** Store a matrix value in another matrix in the context of that other matrix
is the result matrix of a jump matrix. All arguments must be valid and are
not checked. */
static void lcl_storeJumpMatResult(
const ScMatrix* pMat, ScJumpMatrix* pJumpMat, SCSIZE nC, SCSIZE nR )
{
if ( pMat->IsValue( nC, nR ) )
{
double fVal = pMat->GetDouble( nC, nR );
pJumpMat->PutResultDouble( fVal, nC, nR );
}
else if ( pMat->IsEmpty( nC, nR ) )
{
pJumpMat->PutResultEmpty( nC, nR );
}
else
{
pJumpMat->PutResultString(pMat->GetString(nC, nR), nC, nR);
}
}
void ScInterpreter::ScIfError( bool bNAonly )
{
const short* pJump = pCur->GetJump();
short nJumpCount = pJump[ 0 ];
if (!sp || nJumpCount != 2)
{
// Reset nGlobalError here to not propagate the old error, if any.
nGlobalError = (sp ? FormulaError::ParameterExpected : FormulaError::UnknownStackVariable);
PushError( nGlobalError);
aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
return;
}
FormulaConstTokenRef xToken( pStack[ sp - 1 ] );
bool bError = false;
FormulaError nOldGlobalError = nGlobalError;
nGlobalError = FormulaError::NONE;
MatrixJumpConditionToMatrix();
switch (GetStackType())
{
default:
Pop();
// Act on implicitly propagated error, if any.
if (nOldGlobalError != FormulaError::NONE)
nGlobalError = nOldGlobalError;
if (nGlobalError != FormulaError::NONE)
bError = true;
break;
case svError:
PopError();
bError = true;
break;
case svDoubleRef:
case svSingleRef:
{
ScAddress aAdr;
if (!PopDoubleRefOrSingleRef( aAdr))
bError = true;
else
{
ScRefCellValue aCell(*pDok, aAdr);
nGlobalError = GetCellErrCode(aCell);
if (nGlobalError != FormulaError::NONE)
bError = true;
}
}
break;
case svExternalSingleRef:
case svExternalDoubleRef:
{
double fVal;
svl::SharedString aStr;
// Handles also existing jump matrix case and sets error on
// elements.
GetDoubleOrStringFromMatrix( fVal, aStr);
if (nGlobalError != FormulaError::NONE)
bError = true;
}
break;
case svMatrix:
{
const ScMatrixRef pMat = PopMatrix();
if (!pMat || (nGlobalError != FormulaError::NONE && (!bNAonly || nGlobalError == FormulaError::NotAvailable)))
{
bError = true;
break; // switch
}
// If the matrix has no queried error at all we can simply use
// it as result and don't need to bother with jump matrix.
SCSIZE nErrorCol = ::std::numeric_limits<SCSIZE>::max(),
nErrorRow = ::std::numeric_limits<SCSIZE>::max();
SCSIZE nCols, nRows;
pMat->GetDimensions( nCols, nRows );
if (nCols == 0 || nRows == 0)
{
bError = true;
break; // switch
}
for (SCSIZE nC=0; nC < nCols && !bError; ++nC)
{
for (SCSIZE nR=0; nR < nRows && !bError; ++nR)
{
FormulaError nErr = pMat->GetError( nC, nR );
if (nErr != FormulaError::NONE && (!bNAonly || nErr == FormulaError::NotAvailable))
{
bError = true;
nErrorCol = nC;
nErrorRow = nR;
}
}
}
if (!bError)
break; // switch, we're done and have the result
FormulaConstTokenRef xNew;
ScTokenMatrixMap::const_iterator aMapIter;
if (pTokenMatrixMap && ((aMapIter = pTokenMatrixMap->find( pCur)) != pTokenMatrixMap->end()))
{
xNew = (*aMapIter).second;
}
else
{
const ScMatrix* pMatPtr = pMat.get();
std::shared_ptr<ScJumpMatrix> pJumpMat( std::make_shared<ScJumpMatrix>( nCols, nRows ) );
// Init all jumps to no error to save single calls. Error
// is the exceptional condition.
const double fFlagResult = CreateDoubleError( FormulaError::JumpMatHasResult);
pJumpMat->SetAllJumps( fFlagResult, pJump[ nJumpCount ], pJump[ nJumpCount ] );
// Up to first error position simply store results, no need
// to evaluate error conditions again.
SCSIZE nC = 0, nR = 0;
for ( ; nC < nCols && (nC != nErrorCol || nR != nErrorRow); /*nop*/ )
{
for (nR = 0 ; nR < nRows && (nC != nErrorCol || nR != nErrorRow); ++nR)
{
lcl_storeJumpMatResult(pMatPtr, pJumpMat.get(), nC, nR);
}
if (nC != nErrorCol && nR != nErrorRow)
++nC;
}
// Now the mixed cases.
for ( ; nC < nCols; ++nC)
{
for ( ; nR < nRows; ++nR)
{
FormulaError nErr = pMat->GetError( nC, nR );
if (nErr != FormulaError::NONE && (!bNAonly || nErr == FormulaError::NotAvailable))
{ // TRUE, THEN path
pJumpMat->SetJump( nC, nR, 1.0, pJump[ 1 ], pJump[ nJumpCount ] );
}
else
{ // FALSE, EMPTY path, store result instead
lcl_storeJumpMatResult(pMatPtr, pJumpMat.get(), nC, nR);
}
}
nR = 0;
}
xNew = new ScJumpMatrixToken( pJumpMat );
GetTokenMatrixMap().emplace( pCur, xNew );
}
nGlobalError = nOldGlobalError;
PushTokenRef( xNew );
// set endpoint of path for main code line
aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
return;
}
break;
}
if (bError && (!bNAonly || nGlobalError == FormulaError::NotAvailable))
{
// error, calculate 2nd argument
nGlobalError = FormulaError::NONE;
aCode.Jump( pJump[ 1 ], pJump[ nJumpCount ] );
}
else
{
// no error, push 1st argument and continue
nGlobalError = nOldGlobalError;
PushTokenRef( xToken);
aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
}
}
void ScInterpreter::ScChooseJump()
{
// We have to set a jump, if there was none chosen because of an error set
// it to endpoint.
bool bHaveJump = false;
const short* pJump = pCur->GetJump();
short nJumpCount = pJump[ 0 ];
MatrixJumpConditionToMatrix();
switch ( GetStackType() )
{
case svMatrix:
{
ScMatrixRef pMat = PopMatrix();
if ( !pMat )
PushIllegalParameter();
else
{
FormulaConstTokenRef xNew;
ScTokenMatrixMap::const_iterator aMapIter;
// DoubleError handled by JumpMatrix
pMat->SetErrorInterpreter( nullptr);
SCSIZE nCols, nRows;
pMat->GetDimensions( nCols, nRows );
if ( nCols == 0 || nRows == 0 )
PushIllegalParameter();
else if (pTokenMatrixMap && ((aMapIter = pTokenMatrixMap->find(
pCur)) != pTokenMatrixMap->end()))
xNew = (*aMapIter).second;
else
{
std::shared_ptr<ScJumpMatrix> pJumpMat( std::make_shared<ScJumpMatrix>( nCols, nRows ) );
for ( SCSIZE nC=0; nC < nCols; ++nC )
{
for ( SCSIZE nR=0; nR < nRows; ++nR )
{
double fVal;
bool bIsValue = pMat->IsValue(nC, nR);
if ( bIsValue )
{
fVal = pMat->GetDouble(nC, nR);
bIsValue = ::rtl::math::isFinite( fVal );
if ( bIsValue )
{
fVal = ::rtl::math::approxFloor( fVal);
if ( (fVal < 1) || (fVal >= nJumpCount))
{
bIsValue = false;
fVal = CreateDoubleError(
FormulaError::IllegalArgument);
}
}
}
else
{
fVal = CreateDoubleError( FormulaError::NoValue);
}
if ( bIsValue )
{
pJumpMat->SetJump( nC, nR, fVal,
pJump[ (short)fVal ],
pJump[ nJumpCount ]);
}
else
{
pJumpMat->SetJump( nC, nR, fVal,
pJump[ nJumpCount ],
pJump[ nJumpCount ]);
}
}
}
xNew = new ScJumpMatrixToken( pJumpMat );
GetTokenMatrixMap().emplace(pCur, xNew);
}
if (xNew.get())
{
PushTokenRef( xNew);
// set endpoint of path for main code line
aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
bHaveJump = true;
}
}
}
break;
default:
{
sal_Int16 nJumpIndex = GetInt16();
if (nGlobalError == FormulaError::NONE && (nJumpIndex >= 1) && (nJumpIndex < nJumpCount))
{
aCode.Jump( pJump[ (short) nJumpIndex ], pJump[ nJumpCount ] );
bHaveJump = true;
}
else
PushIllegalArgument();
}
}
if (!bHaveJump)
aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] );
}
static void lcl_AdjustJumpMatrix( ScJumpMatrix* pJumpM, SCSIZE nParmCols, SCSIZE nParmRows )
{
SCSIZE nJumpCols, nJumpRows;
SCSIZE nResCols, nResRows;
SCSIZE nAdjustCols, nAdjustRows;
pJumpM->GetDimensions( nJumpCols, nJumpRows );
pJumpM->GetResMatDimensions( nResCols, nResRows );
if (( nJumpCols == 1 && nParmCols > nResCols ) ||
( nJumpRows == 1 && nParmRows > nResRows ))
{
if ( nJumpCols == 1 && nJumpRows == 1 )
{
nAdjustCols = nParmCols > nResCols ? nParmCols : nResCols;
nAdjustRows = nParmRows > nResRows ? nParmRows : nResRows;
}
else if ( nJumpCols == 1 )
{
nAdjustCols = nParmCols;
nAdjustRows = nResRows;
}
else
{
nAdjustCols = nResCols;
nAdjustRows = nParmRows;
}
pJumpM->SetNewResMat( nAdjustCols, nAdjustRows );
}
}
bool ScInterpreter::JumpMatrix( short nStackLevel )
{
pJumpMatrix = pStack[sp-nStackLevel]->GetJumpMatrix();
bool bHasResMat = pJumpMatrix->HasResultMatrix();
SCSIZE nC, nR;
if ( nStackLevel == 2 )
{
if ( aCode.HasStacked() )
aCode.Pop(); // pop what Jump() pushed
else
{
OSL_FAIL( "ScInterpreter::JumpMatrix: pop goes the weasel" );
}
if ( !bHasResMat )
{
Pop();
SetError( FormulaError::UnknownStackVariable );
}
else
{
pJumpMatrix->GetPos( nC, nR );
switch ( GetStackType() )
{
case svDouble:
{
double fVal = GetDouble();
if ( nGlobalError != FormulaError::NONE )
{
fVal = CreateDoubleError( nGlobalError );
nGlobalError = FormulaError::NONE;
}
pJumpMatrix->PutResultDouble( fVal, nC, nR );
}
break;
case svString:
{
svl::SharedString aStr = GetString();
if ( nGlobalError != FormulaError::NONE )
{
pJumpMatrix->PutResultDouble( CreateDoubleError( nGlobalError),
nC, nR);
nGlobalError = FormulaError::NONE;
}
else
pJumpMatrix->PutResultString(aStr, nC, nR);
}
break;
case svSingleRef:
{
FormulaConstTokenRef xRef = pStack[sp-1];
ScAddress aAdr;
PopSingleRef( aAdr );
if ( nGlobalError != FormulaError::NONE )
{
pJumpMatrix->PutResultDouble( CreateDoubleError( nGlobalError),
nC, nR);
nGlobalError = FormulaError::NONE;
}
else
{
ScRefCellValue aCell(*pDok, aAdr);
if (aCell.hasEmptyValue())
pJumpMatrix->PutResultEmpty( nC, nR );
else if (aCell.hasNumeric())
{
double fVal = GetCellValue(aAdr, aCell);
if ( nGlobalError != FormulaError::NONE )
{
fVal = CreateDoubleError(
nGlobalError);
nGlobalError = FormulaError::NONE;
}
pJumpMatrix->PutResultDouble( fVal, nC, nR );
}
else
{
svl::SharedString aStr;
GetCellString(aStr, aCell);
if ( nGlobalError != FormulaError::NONE )
{
pJumpMatrix->PutResultDouble( CreateDoubleError(
nGlobalError), nC, nR);
nGlobalError = FormulaError::NONE;
}
else
pJumpMatrix->PutResultString(aStr, nC, nR);
}
}
formula::ParamClass eReturnType = ScParameterClassification::GetParameterType( pCur, SAL_MAX_UINT16);
if (eReturnType == ParamClass::Reference)
{
/* TODO: What about error handling and do we actually
* need the result matrix above at all in this case? */
ScComplexRefData aRef;
aRef.Ref1 = aRef.Ref2 = *(xRef->GetSingleRef());
pJumpMatrix->GetRefList().push_back( aRef);
}
}
break;
case svDoubleRef:
{ // upper left plus offset within matrix
FormulaConstTokenRef xRef = pStack[sp-1];
double fVal;
ScRange aRange;
PopDoubleRef( aRange );
if ( nGlobalError != FormulaError::NONE )
{
fVal = CreateDoubleError( nGlobalError );
nGlobalError = FormulaError::NONE;
pJumpMatrix->PutResultDouble( fVal, nC, nR );
}
else
{
// Do not modify the original range because we use it
// to adjust the size of the result matrix if necessary.
ScAddress aAdr( aRange.aStart);
sal_uLong nCol = (sal_uLong)aAdr.Col() + nC;
sal_uLong nRow = (sal_uLong)aAdr.Row() + nR;
if ((nCol > static_cast<sal_uLong>(aRange.aEnd.Col()) &&
aRange.aEnd.Col() != aRange.aStart.Col())
|| (nRow > static_cast<sal_uLong>(aRange.aEnd.Row()) &&
aRange.aEnd.Row() != aRange.aStart.Row()))
{
fVal = CreateDoubleError( FormulaError::NotAvailable );
pJumpMatrix->PutResultDouble( fVal, nC, nR );
}
else
{
// Replicate column and/or row of a vector if it is
// one. Note that this could be a range reference
// that in fact consists of only one cell, e.g. A1:A1
if (aRange.aEnd.Col() == aRange.aStart.Col())
nCol = aRange.aStart.Col();
if (aRange.aEnd.Row() == aRange.aStart.Row())
nRow = aRange.aStart.Row();
aAdr.SetCol( static_cast<SCCOL>(nCol) );
aAdr.SetRow( static_cast<SCROW>(nRow) );
ScRefCellValue aCell(*pDok, aAdr);
if (aCell.hasEmptyValue())
pJumpMatrix->PutResultEmpty( nC, nR );
else if (aCell.hasNumeric())
{
double fCellVal = GetCellValue(aAdr, aCell);
if ( nGlobalError != FormulaError::NONE )
{
fCellVal = CreateDoubleError(
nGlobalError);
nGlobalError = FormulaError::NONE;
}
pJumpMatrix->PutResultDouble( fCellVal, nC, nR );
}
else
{
svl::SharedString aStr;
GetCellString(aStr, aCell);
if ( nGlobalError != FormulaError::NONE )
{
pJumpMatrix->PutResultDouble( CreateDoubleError(
nGlobalError), nC, nR);
nGlobalError = FormulaError::NONE;
}
else
pJumpMatrix->PutResultString(aStr, nC, nR);
}
}
SCSIZE nParmCols = aRange.aEnd.Col() - aRange.aStart.Col() + 1;
SCSIZE nParmRows = aRange.aEnd.Row() - aRange.aStart.Row() + 1;
lcl_AdjustJumpMatrix( pJumpMatrix, nParmCols, nParmRows );
}
formula::ParamClass eReturnType = ScParameterClassification::GetParameterType( pCur, SAL_MAX_UINT16);
if (eReturnType == ParamClass::Reference)
{
/* TODO: What about error handling and do we actually
* need the result matrix above at all in this case? */
pJumpMatrix->GetRefList().push_back( *(xRef->GetDoubleRef()));
}
}
break;
case svMatrix:
{ // match matrix offsets
double fVal;
ScMatrixRef pMat = PopMatrix();
if ( nGlobalError != FormulaError::NONE )
{
fVal = CreateDoubleError( nGlobalError );
nGlobalError = FormulaError::NONE;
pJumpMatrix->PutResultDouble( fVal, nC, nR );
}
else if ( !pMat )
{
fVal = CreateDoubleError( FormulaError::UnknownVariable );
pJumpMatrix->PutResultDouble( fVal, nC, nR );
}
else
{
SCSIZE nCols, nRows;
pMat->GetDimensions( nCols, nRows );
if ((nCols <= nC && nCols != 1) ||
(nRows <= nR && nRows != 1))
{
fVal = CreateDoubleError( FormulaError::NotAvailable );
pJumpMatrix->PutResultDouble( fVal, nC, nR );
}
else
{
lcl_storeJumpMatResult(pMat.get(), pJumpMatrix, nC, nR);
}
lcl_AdjustJumpMatrix( pJumpMatrix, nCols, nRows );
}
}
break;
case svError:
{
PopError();
double fVal = CreateDoubleError( nGlobalError);
nGlobalError = FormulaError::NONE;
pJumpMatrix->PutResultDouble( fVal, nC, nR );
}
break;
default:
{
Pop();
double fVal = CreateDoubleError( FormulaError::IllegalArgument);
pJumpMatrix->PutResultDouble( fVal, nC, nR );
}
}
}
}
bool bCont = pJumpMatrix->Next( nC, nR );
if ( bCont )
{
double fBool;
short nStart, nNext, nStop;
pJumpMatrix->GetJump( nC, nR, fBool, nStart, nNext, nStop );
while ( bCont && nStart == nNext )
{ // push all results that have no jump path
if ( bHasResMat && (GetDoubleErrorValue( fBool) != FormulaError::JumpMatHasResult) )
{
// a false without path results in an empty path value
if ( fBool == 0.0 )
pJumpMatrix->PutResultEmptyPath( nC, nR );
else
pJumpMatrix->PutResultDouble( fBool, nC, nR );
}
bCont = pJumpMatrix->Next( nC, nR );
if ( bCont )
pJumpMatrix->GetJump( nC, nR, fBool, nStart, nNext, nStop );
}
if ( bCont && nStart != nNext )
{
const ScTokenVec* pParams = pJumpMatrix->GetJumpParameters();
if ( pParams )
{
for ( ScTokenVec::const_iterator i = pParams->begin(); i != pParams->end(); ++i )
{
// This is not the current state of the interpreter, so
// push without error, and elements' errors are coded into
// double.
PushWithoutError( *(*i));
}
}
aCode.Jump( nStart, nNext, nStop );
}
}
if ( !bCont )
{ // We're done with it, throw away jump matrix, keep result.
// For an intermediate result of Reference use the array of references,
// else (also for a final result of Reference) use the matrix.
formula::ParamClass eReturnType = ScParameterClassification::GetParameterType( pCur, SAL_MAX_UINT16);
if (eReturnType == ParamClass::Reference && aCode.PeekNextOperator())
{
FormulaTokenRef xRef = new ScRefListToken(true);
*(xRef->GetRefList()) = pJumpMatrix->GetRefList();
pJumpMatrix = nullptr;
Pop();
PushTokenRef( xRef);
if (pTokenMatrixMap)
{
pTokenMatrixMap->erase( pCur);
// There's no result matrix to remember in this case.
}
}
else
{
ScMatrix* pResMat = pJumpMatrix->GetResultMatrix();
pJumpMatrix = nullptr;
Pop();
PushMatrix( pResMat );
// Remove jump matrix from map and remember result matrix in case it
// could be reused in another path of the same condition.
if (pTokenMatrixMap)
{
pTokenMatrixMap->erase( pCur);
pTokenMatrixMap->emplace(pCur, pStack[sp-1]);
}
}
return true;
}
return false;
}
double ScInterpreter::Compare( ScQueryOp eOp )
{
sc::Compare aComp;
aComp.meOp = eOp;
aComp.mbIgnoreCase = pDok->GetDocOptions().IsIgnoreCase();
for( short i = 1; i >= 0; i-- )
{
sc::Compare::Cell& rCell = aComp.maCells[i];
switch ( GetRawStackType() )
{
case svEmptyCell:
Pop();
rCell.mbEmpty = true;
break;
case svMissing:
case svDouble:
rCell.mfValue = GetDouble();
rCell.mbValue = true;
break;
case svString:
rCell.maStr = GetString();
rCell.mbValue = false;
break;
case svDoubleRef :
case svSingleRef :
{
ScAddress aAdr;
if ( !PopDoubleRefOrSingleRef( aAdr ) )
break;
ScRefCellValue aCell(*pDok, aAdr);
if (aCell.hasEmptyValue())
rCell.mbEmpty = true;
else if (aCell.hasString())
{
svl::SharedString aStr;
GetCellString(aStr, aCell);
rCell.maStr = aStr;
rCell.mbValue = false;
}
else
{
rCell.mfValue = GetCellValue(aAdr, aCell);
rCell.mbValue = true;
}
}
break;
case svExternalSingleRef:
{
ScMatrixRef pMat = GetMatrix();
if (!pMat)
{
SetError( FormulaError::IllegalParameter);
break;
}
SCSIZE nC, nR;
pMat->GetDimensions(nC, nR);
if (!nC || !nR)
{
SetError( FormulaError::IllegalParameter);
break;
}
if (pMat->IsEmpty(0, 0))
rCell.mbEmpty = true;
else if (pMat->IsString(0, 0))
{
rCell.maStr = pMat->GetString(0, 0);
rCell.mbValue = false;
}
else
{
rCell.mfValue = pMat->GetDouble(0, 0);
rCell.mbValue = true;
}
}
break;
case svExternalDoubleRef:
// TODO: Find out how to handle this...
// Xcl generates a position dependent intersection using
// col/row, as it seems to do for all range references, not
// only in compare context. We'd need a general implementation
// for that behavior similar to svDoubleRef in scalar and array
// mode. Which also means we'd have to change all places where
// it currently is handled along with svMatrix.
default:
PopError();
SetError( FormulaError::IllegalParameter);
break;
}
}
if( nGlobalError != FormulaError::NONE )
return 0;
nCurFmtType = nFuncFmtType = css::util::NumberFormat::LOGICAL;
return sc::CompareFunc(aComp);
}
sc::RangeMatrix ScInterpreter::CompareMat( ScQueryOp eOp, sc::CompareOptions* pOptions )
{
sc::Compare aComp;
aComp.meOp = eOp;
aComp.mbIgnoreCase = pDok->GetDocOptions().IsIgnoreCase();
sc::RangeMatrix aMat[2];
ScAddress aAdr;
for( short i = 1; i >= 0; i-- )
{
sc::Compare::Cell& rCell = aComp.maCells[i];
switch (GetRawStackType())
{
case svEmptyCell:
Pop();
rCell.mbEmpty = true;
break;
case svMissing:
case svDouble:
rCell.mfValue = GetDouble();
rCell.mbValue = true;
break;
case svString:
rCell.maStr = GetString();
rCell.mbValue = false;
break;
case svSingleRef:
{
PopSingleRef( aAdr );
ScRefCellValue aCell(*pDok, aAdr);
if (aCell.hasEmptyValue())
rCell.mbEmpty = true;
else if (aCell.hasString())
{
svl::SharedString aStr;
GetCellString(aStr, aCell);
rCell.maStr = aStr;
rCell.mbValue = false;
}
else
{
rCell.mfValue = GetCellValue(aAdr, aCell);
rCell.mbValue = true;
}
}
break;
case svExternalSingleRef:
case svExternalDoubleRef:
case svDoubleRef:
case svMatrix:
aMat[i] = GetRangeMatrix();
if (!aMat[i].mpMat)
SetError( FormulaError::IllegalParameter);
else
aMat[i].mpMat->SetErrorInterpreter(nullptr);
// errors are transported as DoubleError inside matrix
break;
default:
PopError();
SetError( FormulaError::IllegalParameter);
break;
}
}
sc::RangeMatrix aRes;
if (nGlobalError != FormulaError::NONE)
{
nCurFmtType = nFuncFmtType = css::util::NumberFormat::LOGICAL;