forked from alpertron/calculators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.c
1415 lines (1379 loc) · 38.4 KB
/
expression.c
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
/*
This file is part of Alpertron Calculators.
Copyright 2015 Dario Alejandro Alpern
Alpertron Calculators is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Alpertron Calculators is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Alpertron Calculators. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "bignbr.h"
#include "expression.h"
#include "factor.h"
#define PAREN_STACK_SIZE 100
#define OPER_POWER 1
#define OPER_MULTIPLY 2
#define OPER_DIVIDE 3
#define OPER_REMAINDER 4
#define OPER_UNARY_MINUS 5
#define OPER_PLUS 6
#define OPER_MINUS 7
#define OPER_SHR 8
#define OPER_SHL 9
#define OPER_NOT_GREATER 10
#define OPER_NOT_LESS 11
#define OPER_NOT_EQUAL 12
#define OPER_EQUAL 13
#define OPER_GREATER 14
#define OPER_LESS 15
#define OPER_NOT 16
#define OPER_AND 17
#define OPER_OR 18
#define OPER_XOR 19
#define MAXIMUM_OPERATOR 19
static BigInteger stackValues[PAREN_STACK_SIZE];
static char stackOperators[PAREN_STACK_SIZE];
static limb fibon2[MAX_LEN];
extern limb MontgomeryR1[MAX_LEN];
static int stackIndex, exprIndex;
static int exprLength;
int lang;
char output[3000000];
limb Mult1[MAX_LEN];
limb Mult3[MAX_LEN];
limb Mult4[MAX_LEN];
int q[MAX_LEN];
BigInteger valueX;
int counterC;
#define fibon1 MontgomeryR1
static enum eExprErr ComputeSubExpr(void);
static void SkipSpaces(char *expr);
static int ComputeBack(void);
static int ComputeNext(void);
#ifdef FACTORIZATION_FUNCTIONS
static int ComputeTotient(void);
static int ComputeNumDivs(void);
static int ComputeSumDivs(void);
static int ComputeConcatFact(void);
static char textFactor[30000];
#endif
static int ComputeSumDigits(void);
static int ComputeRevDigits(void);
static int ComputeNumDigits(void);
static enum eExprErr ComputeModInv(void);
static enum eExprErr ComputeFibLucas(int origValue);
static enum eExprErr ComputePartition(void);
static enum eExprErr ComputeExpr(char *expr, BigInteger *ExpressionResult);
static enum eExprErr ShiftLeft(BigInteger* first, BigInteger *second, BigInteger *result);
static int func(char *expr, BigInteger *ExpressionResult,
char *funcName, int funcArgs, int leftNumberFlag);
static int type;
static int valueXused;
static char priority[] =
{
1, // Power
2, 2, 2, // Multiply, divide and remainder.
3, // Unary minus.
4, 4, // Plus and minus.
5, 5, // Shift right and left.
6, 6, 6, 6, 6, 6, // Six comparison operators (equal, greater, less, etc.)
7, // NOT.
8, 8, 8, // AND, OR, XOR.
};
enum eExprErr ComputeExpression(char *expr, int typ, BigInteger *ExpressionResult)
{
int retcode;
valueXused = FALSE;
stackIndex = 0;
exprIndex = 0;
type = typ;
retcode = ComputeExpr(expr, ExpressionResult);
if (retcode != 0) { return retcode; }
if (ExpressionResult[0].nbrLimbs > 33219 / BITS_PER_GROUP + 1) // 10000/log_10(2) = 33219
{
return EXPR_NUMBER_TOO_HIGH;
}
if (valueX.nbrLimbs > 0 && valueXused == FALSE)
{
return EXPR_VAR_OR_COUNTER_REQUIRED;
}
return 0;
}
static enum eExprErr ComputeExpr(char *expr, BigInteger *ExpressionResult)
{
int i, j, shLeft;
int retcode;
limb carry;
boolean leftNumberFlag = FALSE;
int exprIndexAux, offset;
enum eExprErr SubExprResult;
int len;
limb largeLen;
limb *ptrLimb;
BigInteger factorial;
BigInteger *pBigInt;
int c;
int startStackIndex = stackIndex;
exprLength = (int)strlen(expr);
while (exprIndex < exprLength)
{
char charValue;
charValue = *(expr+exprIndex);
if (charValue == ' ' || charValue == 9)
{ // Ignore spaces and horizontal tabs.
exprIndex++;
continue;
}
if (charValue == '^')
{ // Caret is exponentiation operation.
charValue = OPER_POWER;
exprIndex++;
}
else if (charValue == '*' && *(expr + exprIndex + 1) == '*')
{ // Double asterisk is exponentiation operation too.
charValue = OPER_POWER;
exprIndex += 2;
}
else if (charValue == '*')
{
charValue = OPER_MULTIPLY;
exprIndex++;
}
else if (charValue == '/')
{
charValue = OPER_DIVIDE;
exprIndex++;
}
else if (charValue == '%')
{
charValue = OPER_REMAINDER;
exprIndex++;
}
else if (charValue == '+')
{
charValue = OPER_PLUS;
exprIndex++;
}
else if (charValue == '-')
{
charValue = OPER_MINUS;
exprIndex++;
}
else if (charValue == '<' && *(expr + exprIndex + 1) == '=')
{
charValue = OPER_NOT_GREATER;
exprIndex += 2;
}
else if (charValue == '>' && *(expr + exprIndex + 1) == '=')
{
charValue = OPER_NOT_LESS;
exprIndex += 2;
}
else if (charValue == '!' && *(expr + exprIndex + 1) == '=')
{
charValue = OPER_NOT_EQUAL;
exprIndex += 2;
}
else if (charValue == '=' && *(expr + exprIndex + 1) == '=')
{
charValue = OPER_EQUAL;
exprIndex += 2;
}
else if (charValue == '>')
{
charValue = OPER_GREATER;
exprIndex++;
}
else if (charValue == '<')
{
charValue = OPER_LESS;
exprIndex++;
}
else if ((charValue & 0xDF) == 'N' && (*(expr + exprIndex + 1) & 0xDF) == 'O' &&
(*(expr + exprIndex + 2) & 0xDF) == 'T')
{
charValue = OPER_NOT;
exprIndex += 3;
}
else if ((charValue & 0xDF) == 'A' && (*(expr + exprIndex + 1) & 0xDF) == 'N' &&
(*(expr + exprIndex + 2) & 0xDF) == 'D')
{
charValue = OPER_AND;
exprIndex += 3;
}
else if ((charValue & 0xDF) == 'O' && (*(expr + exprIndex + 1) & 0xDF) == 'R')
{
charValue = OPER_OR;
exprIndex += 2;
}
else if ((charValue & 0xDF) == 'X' && (*(expr + exprIndex + 1) & 0xDF) == 'O' &&
(*(expr + exprIndex + 2) & 0xDF) == 'R')
{
charValue = OPER_XOR;
exprIndex += 3;
}
else if ((charValue & 0xDF) == 'S' && (*(expr + exprIndex + 1) & 0xDF) == 'H' &&
(*(expr + exprIndex + 2) & 0xDF) == 'L')
{
charValue = OPER_SHL;
exprIndex += 3;
}
else if ((charValue & 0xDF) == 'S' && (*(expr + exprIndex + 1) & 0xDF) == 'H' &&
(*(expr + exprIndex + 2) & 0xDF) == 'R')
{
charValue = OPER_SHR;
exprIndex += 3;
}
else if (charValue == '!')
{ // Calculating factorial.
if (leftNumberFlag == FALSE)
{
return EXPR_SYNTAX_ERROR;
}
if (stackValues[stackIndex].nbrLimbs > 1)
{
return EXPR_INTERM_TOO_HIGH;
}
if (stackValues[stackIndex].limbs[0].x < 0 || stackValues[stackIndex].limbs[0].x > 5984)
{
return EXPR_INTERM_TOO_HIGH;
}
len = (int)stackValues[stackIndex].limbs[0].x;
factorial.limbs[0].x = 1;
factorial.nbrLimbs = 1;
factorial.sign = SIGN_POSITIVE;
for (i = 2; i <= len; i++)
{ // Multiply by all integers up to the argument of factorial.
multint(&factorial, &factorial, i);
}
stackValues[stackIndex] = factorial;
exprIndex++;
continue;
}
else if (charValue == '#')
{ // Calculating primorial.
if (leftNumberFlag == FALSE)
{
return EXPR_SYNTAX_ERROR;
}
if (stackValues[stackIndex].nbrLimbs > 2)
{
return EXPR_INTERM_TOO_HIGH;
}
if (stackValues[stackIndex].nbrLimbs == 2)
{
largeLen.x = stackValues[stackIndex].limbs[0].x +
(stackValues[stackIndex].limbs[1].x << BITS_PER_GROUP);
}
else
{
largeLen.x = stackValues[stackIndex].limbs[0].x;
}
if (largeLen.x < 0 || largeLen.x > 46049)
{
return EXPR_INTERM_TOO_HIGH;
}
len = (int)largeLen.x;
// Check if number is prime
for (i = 2; i*i <= len; i++)
{
if (len / i*i == len)
{ // Number is not prime, so go out.
return EXPR_INVALID_PARAM;
}
}
factorial.limbs[0].x = 1;
factorial.nbrLimbs = 1;
factorial.sign = SIGN_POSITIVE;
for (i = 2; i <= len; i++)
{ // Multiply by prime numbers only.
for (j = 2; j*j <= i; j++)
{
if (i / j*j == i)
{ // Number is not prime.
break;
}
}
if (j*j > i)
{ // Number is prime, perform multiplication.
multint(&factorial, &factorial, i);
}
}
stackValues[stackIndex] = factorial;
exprIndex++;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"GCD", 2, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
BigIntGcd(&stackValues[stackIndex], &stackValues[stackIndex + 1], &stackValues[stackIndex]);
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"MODPOW", 3, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = BigIntGeneralModularPower(&stackValues[stackIndex], &stackValues[stackIndex + 1],
&stackValues[stackIndex + 2], &stackValues[stackIndex]);
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"MODINV", 2, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputeModInv();
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
#ifdef FACTORIZATION_FUNCTIONS
else if ((retcode = func(expr, ExpressionResult,
"TOTIENT", 1, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputeTotient();
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"NUMDIVS", 1, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputeNumDivs();
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"SUMDIVS", 1, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputeSumDivs();
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"CONCATFACT", 2, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputeConcatFact();
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
#endif
else if ((retcode = func(expr, ExpressionResult,
"SUMDIGITS", 2, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputeSumDigits();
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"NUMDIGITS", 2, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputeNumDigits();
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"REVDIGITS", 2, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputeRevDigits();
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"ISPRIME", 1, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
if (BpswPrimalityTest(&stackValues[stackIndex]) == 0)
{ // Argument is a probable prime.
intToBigInteger(&stackValues[stackIndex], -1);
}
else
{ // Argument is not a probable prime.
intToBigInteger(&stackValues[stackIndex], 0);
}
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"F", 1, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputeFibLucas(0);
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"L", 1, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputeFibLucas(2);
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"P", 1, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputePartition();
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"N", 1, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputeNext();
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
else if ((retcode = func(expr, ExpressionResult,
"B", 1, leftNumberFlag)) <= 0)
{
if (retcode != 0) { return retcode; }
retcode = ComputeBack();
if (retcode != 0) { return retcode; }
leftNumberFlag = 1;
continue;
}
else if ((charValue & 0xDF) == 'X')
{
if (leftNumberFlag || valueX.nbrLimbs == 0)
{
return EXPR_SYNTAX_ERROR;
}
CopyBigInt(&stackValues[stackIndex], &valueX);
valueXused = TRUE;
exprIndex++;
leftNumberFlag = TRUE;
continue;
}
else if ((charValue & 0xDF) == 'C')
{
if (leftNumberFlag || valueX.nbrLimbs == 0)
{
return EXPR_SYNTAX_ERROR;
}
intToBigInteger(&stackValues[stackIndex], counterC);
valueXused = TRUE;
exprIndex++;
leftNumberFlag = TRUE;
continue;
}
else if (charValue == '(')
{
if (leftNumberFlag == TRUE)
{
return EXPR_SYNTAX_ERROR;
}
if (stackIndex >= PAREN_STACK_SIZE)
{
return EXPR_TOO_MANY_PAREN;
}
stackOperators[stackIndex++] = charValue;
exprIndex++;
continue;
}
else if (charValue == ')' || charValue == ',')
{
if (leftNumberFlag == 0)
{
return EXPR_SYNTAX_ERROR;
}
while (stackIndex > startStackIndex &&
stackOperators[stackIndex - 1] != '(')
{
if ((SubExprResult = ComputeSubExpr()) != 0)
{
return SubExprResult;
}
}
if (stackIndex == startStackIndex)
{
break;
}
if (charValue == ',')
{
return EXPR_PAREN_MISMATCH;
}
stackIndex--; /* Discard ')' */
stackValues[stackIndex] = stackValues[stackIndex + 1];
leftNumberFlag = 1;
exprIndex++;
continue;
}
else if (charValue >= '0' && charValue <= '9')
{
exprIndexAux = exprIndex;
if (charValue == '0' && exprIndexAux < exprLength - 2 &&
*(expr+exprIndexAux + 1) == 'x')
{ // hexadecimal
exprIndexAux++;
while (exprIndexAux < exprLength - 1)
{
charValue = *(expr+exprIndexAux + 1);
if ((charValue >= '0' && charValue <= '9') ||
(charValue >= 'A' && charValue <= 'F') ||
(charValue >= 'a' && charValue <= 'f'))
{
exprIndexAux++;
}
else
{
break;
}
}
// Generate big integer from hexadecimal number from right to left.
carry.x = 0;
i = 0; // limb number.
shLeft = 0;
offset = exprIndexAux;
ptrLimb = &stackValues[stackIndex].limbs[0];
for (; exprIndexAux >= exprIndex + 2; exprIndexAux--)
{
c = *(expr + exprIndexAux);
if (c >= '0' && c <= '9')
{
c -= '0';
}
else if (c >= 'A' && c <= 'F')
{
c -= 'A' - 10;
}
else
{
c -= 'a' - 10;
}
carry.x += c << shLeft;
shLeft += 4; // 4 bits per hex digit.
if (shLeft >= BITS_PER_GROUP)
{
shLeft -= BITS_PER_GROUP;
(ptrLimb++)->x = carry.x & MAX_VALUE_LIMB;
carry.x = c >> (4-shLeft);
}
}
if (carry.x != 0 || ptrLimb == &stackValues[stackIndex].limbs[0])
{
(ptrLimb++)->x = carry.x;
}
exprIndex = offset+1;
stackValues[stackIndex].nbrLimbs = (int)(ptrLimb - &stackValues[stackIndex].limbs[0]);
stackValues[stackIndex].sign = SIGN_POSITIVE;
}
else
{ // Decimal number.
while (exprIndexAux < exprLength - 1)
{
charValue = *(expr+exprIndexAux + 1);
if (charValue >= '0' && charValue <= '9')
{
exprIndexAux++;
}
else
{
break;
}
}
// Generate big integer from decimal number
pBigInt = &stackValues[stackIndex];
Dec2Bin(expr + exprIndex, pBigInt->limbs,
exprIndexAux + 1 - exprIndex, &pBigInt -> nbrLimbs);
pBigInt -> sign = SIGN_POSITIVE;
exprIndex = exprIndexAux + 1;
}
leftNumberFlag = TRUE;
continue;
}
if (charValue <= MAXIMUM_OPERATOR)
{
if ((charValue == OPER_PLUS || charValue == OPER_MINUS) && leftNumberFlag == 0)
{ // Unary plus/minus operator
if (charValue == OPER_PLUS)
{
continue;
}
else
{
if (stackIndex > startStackIndex && stackOperators[stackIndex - 1] == OPER_UNARY_MINUS)
{
stackIndex--;
continue;
}
if (stackIndex >= PAREN_STACK_SIZE)
{
return EXPR_TOO_MANY_PAREN;
}
stackOperators[stackIndex++] = OPER_UNARY_MINUS; /* Unary minus */
continue;
}
}
if ((leftNumberFlag == 0) != (charValue == OPER_NOT))
{ // Missing left operator if operator is not NOT or
// extra left operator if operator is NOT.
return EXPR_SYNTAX_ERROR;
}
if (charValue != OPER_POWER)
{ // Power operator has right associativity.
while (stackIndex > startStackIndex &&
stackOperators[stackIndex - 1] != '(' &&
priority[(int)stackOperators[stackIndex - 1] - 1] <= priority[(int)charValue] - 1)
{
if ((SubExprResult = ComputeSubExpr()) != 0)
{
return SubExprResult;
}
}
}
stackOperators[stackIndex++] = charValue;
leftNumberFlag = 0;
continue;
}
return EXPR_SYNTAX_ERROR;
} /* end while */
if (leftNumberFlag == FALSE)
{
return EXPR_SYNTAX_ERROR;
}
while (stackIndex > startStackIndex && stackOperators[stackIndex - 1] != '(')
{
if ((SubExprResult = ComputeSubExpr()) != 0)
{
return SubExprResult;
}
}
if (stackIndex != startStackIndex)
{
return EXPR_PAREN_MISMATCH;
}
CopyBigInt(ExpressionResult, &stackValues[0]);
return EXPR_OK;
}
static void SkipSpaces(char *expr)
{
while (*(expr + exprIndex))
{
if (*(expr + exprIndex) > ' ')
{
break;
}
exprIndex++;
}
return;
}
static void ConvertToTwosComplement(BigInteger *value)
{
int idx;
int nbrLimbs;
limb *ptrLimb;
if (value->sign == SIGN_POSITIVE)
{ // If number is positive, no conversion is needed.
return;
}
nbrLimbs = value->nbrLimbs;
ptrLimb = &value->limbs[0];
for (idx = 0; idx < nbrLimbs; idx++)
{
if (ptrLimb->x != 0)
{
break;
}
ptrLimb++;
}
if (idx < nbrLimbs)
{
ptrLimb->x = 0x80000000 - ptrLimb->x;
ptrLimb++;
}
for (; idx < nbrLimbs; idx++)
{
ptrLimb->x = 0x7FFFFFFF - ptrLimb->x;
ptrLimb++;
}
}
static enum eExprErr ComputeSubExpr(void)
{
char stackOper = stackOperators[--stackIndex];
BigInteger *firstArg = &stackValues[stackIndex];
BigInteger *secondArg = &stackValues[stackIndex + 1];
BigInteger *result = &stackValues[stackIndex];
BigInteger *tmpptr;
int idx;
switch (stackOper)
{
case OPER_PLUS:
BigIntAdd(firstArg, secondArg, result);
return EXPR_OK;
case OPER_MINUS:
BigIntSubt(firstArg, secondArg, result);
return EXPR_OK;
case OPER_UNARY_MINUS:
BigIntNegate(secondArg, result);
return EXPR_OK;
case OPER_DIVIDE:
return BigIntDivide(firstArg, secondArg, result);
case OPER_MULTIPLY:
return BigIntMultiply(firstArg, secondArg, result);
case OPER_REMAINDER:
return BigIntRemainder(firstArg, secondArg, result);
case OPER_POWER:
return BigIntPower(firstArg, secondArg, result);
case OPER_EQUAL:
BigIntSubt(firstArg, secondArg, result);
intToBigInteger(result, (result->nbrLimbs == 1 && result->limbs[0].x == 0? -1: 0));
return EXPR_OK;
case OPER_NOT_EQUAL:
BigIntSubt(firstArg, secondArg, result);
intToBigInteger(result, (result->nbrLimbs == 1 && result->limbs[0].x == 0 ? 0 : -1));
return EXPR_OK;
case OPER_GREATER:
BigIntSubt(secondArg, firstArg, result);
intToBigInteger(result, result->sign == SIGN_NEGATIVE ? -1 : 0);
return EXPR_OK;
case OPER_NOT_GREATER:
BigIntSubt(secondArg, firstArg, result);
intToBigInteger(result, result->sign == SIGN_NEGATIVE ? 0 : -1);
return EXPR_OK;
case OPER_LESS:
BigIntSubt(firstArg, secondArg, result);
intToBigInteger(result, result->sign == SIGN_NEGATIVE ? -1 : 0);
return EXPR_OK;
case OPER_NOT_LESS:
BigIntSubt(firstArg, secondArg, result);
intToBigInteger(result, result->sign == SIGN_NEGATIVE ? 0 : -1);
return EXPR_OK;
case OPER_SHL:
ShiftLeft(firstArg, secondArg, result);
return EXPR_OK;
case OPER_SHR:
if (secondArg->sign == SIGN_POSITIVE)
{
secondArg->sign = SIGN_NEGATIVE;
}
else
{
secondArg->sign = SIGN_POSITIVE;
}
ShiftLeft(firstArg, secondArg, result);
return EXPR_OK;
case OPER_NOT: // Perform binary NOT as result <- -1 - argument.
intToBigInteger(firstArg, -1);
BigIntSubt(firstArg, secondArg, result);
return EXPR_OK;
case OPER_AND: // Perform binary AND.
if (firstArg->nbrLimbs < secondArg->nbrLimbs)
{ // After the exchange, firstArg has not fewer limbs than secondArg.
tmpptr = firstArg;
firstArg = secondArg;
secondArg = tmpptr;
}
ConvertToTwosComplement(firstArg);
ConvertToTwosComplement(secondArg);
for (idx = 0; idx < secondArg->nbrLimbs; idx++)
{
result->limbs[idx].x = firstArg->limbs[idx].x & secondArg->limbs[idx].x;
}
if (secondArg->sign == SIGN_POSITIVE)
{
result->nbrLimbs = secondArg->nbrLimbs;
}
else
{
result->nbrLimbs = firstArg->nbrLimbs;
for (; idx < firstArg->nbrLimbs; idx++)
{
result->limbs[idx].x = firstArg->limbs[idx].x;
}
}
if (firstArg->sign == SIGN_POSITIVE || secondArg->sign == SIGN_POSITIVE)
{
result->sign = SIGN_POSITIVE;
}
else
{
result->sign = SIGN_NEGATIVE;
}
ConvertToTwosComplement(result);
return EXPR_OK;
case OPER_OR: // Perform binary OR.
if (firstArg->nbrLimbs < secondArg->nbrLimbs)
{ // After the exchange, firstArg has not fewer limbs than secondArg.
tmpptr = firstArg;
firstArg = secondArg;
secondArg = tmpptr;
}
ConvertToTwosComplement(firstArg);
ConvertToTwosComplement(secondArg);
for (idx = 0; idx < secondArg->nbrLimbs; idx++)
{
result->limbs[idx].x = firstArg->limbs[idx].x | secondArg->limbs[idx].x;
}
if (secondArg->sign == SIGN_NEGATIVE)
{
result->nbrLimbs = secondArg->nbrLimbs;
}
else
{
result->nbrLimbs = firstArg->nbrLimbs;
for (; idx < firstArg->nbrLimbs; idx++)
{
result->limbs[idx].x = firstArg->limbs[idx].x;
}
}
if (firstArg->sign == SIGN_NEGATIVE || secondArg->sign == SIGN_NEGATIVE)
{
result->sign = SIGN_NEGATIVE;
}
else
{
result->sign = SIGN_POSITIVE;
}
ConvertToTwosComplement(result);
return EXPR_OK;
case OPER_XOR: // Perform binary XOR.
if (firstArg->nbrLimbs < secondArg->nbrLimbs)
{ // After the exchange, firstArg has not fewer limbs than secondArg.
tmpptr = firstArg;
firstArg = secondArg;
secondArg = tmpptr;
}
ConvertToTwosComplement(firstArg);
ConvertToTwosComplement(secondArg);
for (idx = 0; idx < secondArg->nbrLimbs; idx++)
{
result->limbs[idx].x = firstArg->limbs[idx].x ^ secondArg->limbs[idx].x;
}
if (secondArg->sign == SIGN_POSITIVE)
{
for (; idx < firstArg->nbrLimbs; idx++)
{
result->limbs[idx].x = firstArg->limbs[idx].x;
}
}
else
{
for (; idx < firstArg->nbrLimbs; idx++)
{
result->limbs[idx].x = firstArg->limbs[idx].x ^ MAX_INT_NBR;
}
}
if ((firstArg->sign == SIGN_NEGATIVE) != (secondArg->sign == SIGN_NEGATIVE))
{
result->sign = SIGN_NEGATIVE;
}
else
{
result->sign = SIGN_POSITIVE;
}
result->nbrLimbs = firstArg->nbrLimbs;
ConvertToTwosComplement(result);
return EXPR_OK;
}
return EXPR_OK;
}
static int func(char *expr, BigInteger *ExpressionResult,
char *funcName, int funcArgs, int leftNumberFlag)
{
int index;
int funcNameLen = (int)strlen(funcName);
char *ptrExpr, *ptrFuncName;
if (exprIndex + funcNameLen > exprLength)
{
return 1;
}
ptrExpr = expr + exprIndex;
ptrFuncName = funcName;
while (*ptrFuncName)
{
if ((*ptrExpr & 0xDF) != *ptrFuncName)
{
return 1;
}
ptrExpr++;
ptrFuncName++;
}
exprIndex += funcNameLen;
if (leftNumberFlag == 1)
{
return EXPR_SYNTAX_ERROR;
}
SkipSpaces(expr);
if (exprIndex == exprLength || *(expr + exprIndex++) != '(')
{
return EXPR_SYNTAX_ERROR;
}
for (index = 0; index < funcArgs; index++)
{
int retcode;
char compareChar;
SkipSpaces(expr);
if (stackIndex >= PAREN_STACK_SIZE)
{
return EXPR_TOO_MANY_PAREN;
}
retcode = ComputeExpr(expr, ExpressionResult);
if (retcode != 0) { return retcode; }
SkipSpaces(expr);
compareChar = (index == funcArgs - 1 ? ')' : ',');
if (exprIndex == exprLength || *(expr + exprIndex++) != compareChar)
{
return EXPR_SYNTAX_ERROR;
}
stackIndex++;
}
stackIndex -= funcArgs;
return 0;
}
// Compute previous probable prime
static int ComputeBack(void)
{
BigInteger *pArgument = &stackValues[stackIndex];;
BigInteger *pResult = &stackValues[stackIndex];
limb *pResultLimbs = pResult->limbs;
limb *pArgumentLimbs = pArgument->limbs;
int nbrLimbs = pArgument->nbrLimbs;
pResult->sign = SIGN_POSITIVE;
if (pArgument->sign == SIGN_NEGATIVE || (nbrLimbs == 1 && pArgumentLimbs->x < 3))
{
return EXPR_INVALID_PARAM;
}
if (nbrLimbs == 1 && pArgumentLimbs->x == 3)
{
pResult->nbrLimbs = 1;
pResultLimbs->x = 2;
return EXPR_OK;
}
memcpy(pResultLimbs, pArgumentLimbs, nbrLimbs * sizeof(limb));
pResultLimbs->x |= 1; // If number is even, use next odd number.
pResult->nbrLimbs = nbrLimbs;
do
{ // Loop that searches for previous or next probable prime.
int ctr;
pResultLimbs->x -= 2;
if (pResultLimbs->x < 0)
{