forked from zsaleeba/picoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.c
1599 lines (1403 loc) · 72.5 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
/* picoc expression evaluator - a stack-based expression evaluation system
* which handles operator precedence */
#include "interpreter.h"
/* whether evaluation is left to right for a given precedence level */
#define IS_LEFT_TO_RIGHT(p) ((p) != 2 && (p) != 14)
#define BRACKET_PRECEDENCE 20
/* If the destination is not float, we can't assign a floating value to it, we need to convert it to integer instead */
#define ASSIGN_FP_OR_INT(value) \
if (IS_FP(BottomValue)) { ResultFP = ExpressionAssignFP(Parser, BottomValue, value); } \
else { ResultInt = ExpressionAssignInt(Parser, BottomValue, (long)(value), FALSE); ResultIsInt = TRUE; } \
#define DEEP_PRECEDENCE (BRACKET_PRECEDENCE*1000)
#ifdef DEBUG_EXPRESSIONS
#define debugf printf
#else
void debugf(char *Format, ...)
{
UNUSED(Format);
}
#endif
/* local prototypes */
enum OperatorOrder
{
OrderNone,
OrderPrefix,
OrderInfix,
OrderPostfix
};
/* a stack of expressions we use in evaluation */
struct ExpressionStack
{
struct ExpressionStack *Next; /* the next lower item on the stack */
struct Value *Val; /* the value for this stack node */
enum LexToken Op; /* the operator */
short unsigned int Precedence; /* the operator precedence of this node */
unsigned char Order; /* the evaluation order of this operator */
};
/* operator precedence definitions */
struct OpPrecedence
{
unsigned int PrefixPrecedence:4;
unsigned int PostfixPrecedence:4;
unsigned int InfixPrecedence:4;
char *Name;
};
/* NOTE: the order of this array must correspond exactly to the order of these tokens in enum LexToken */
static struct OpPrecedence OperatorPrecedence[] =
{
/* TokenNone, */ { 0, 0, 0, "none" },
/* TokenComma, */ { 0, 0, 0, "," },
/* TokenAssign, */ { 0, 0, 2, "=" }, /* TokenAddAssign, */ { 0, 0, 2, "+=" }, /* TokenSubtractAssign, */ { 0, 0, 2, "-=" },
/* TokenMultiplyAssign, */ { 0, 0, 2, "*=" }, /* TokenDivideAssign, */ { 0, 0, 2, "/=" }, /* TokenModulusAssign, */ { 0, 0, 2, "%=" },
/* TokenShiftLeftAssign, */ { 0, 0, 2, "<<=" }, /* TokenShiftRightAssign, */ { 0, 0, 2, ">>=" }, /* TokenArithmeticAndAssign, */ { 0, 0, 2, "&=" },
/* TokenArithmeticOrAssign, */ { 0, 0, 2, "|=" }, /* TokenArithmeticExorAssign, */ { 0, 0, 2, "^=" },
/* TokenQuestionMark, */ { 0, 0, 3, "?" }, /* TokenColon, */ { 0, 0, 3, ":" },
/* TokenLogicalOr, */ { 0, 0, 4, "||" },
/* TokenLogicalAnd, */ { 0, 0, 5, "&&" },
/* TokenArithmeticOr, */ { 0, 0, 6, "|" },
/* TokenArithmeticExor, */ { 0, 0, 7, "^" },
/* TokenAmpersand, */ { 14, 0, 8, "&" },
/* TokenEqual, */ { 0, 0, 9, "==" }, /* TokenNotEqual, */ { 0, 0, 9, "!=" },
/* TokenLessThan, */ { 0, 0, 10, "<" }, /* TokenGreaterThan, */ { 0, 0, 10, ">" }, /* TokenLessEqual, */ { 0, 0, 10, "<=" }, /* TokenGreaterEqual, */ { 0, 0, 10, ">=" },
/* TokenShiftLeft, */ { 0, 0, 11, "<<" }, /* TokenShiftRight, */ { 0, 0, 11, ">>" },
/* TokenPlus, */ { 14, 0, 12, "+" }, /* TokenMinus, */ { 14, 0, 12, "-" },
/* TokenAsterisk, */ { 14, 0, 13, "*" }, /* TokenSlash, */ { 0, 0, 13, "/" }, /* TokenModulus, */ { 0, 0, 13, "%" },
/* TokenIncrement, */ { 14, 15, 0, "++" }, /* TokenDecrement, */ { 14, 15, 0, "--" }, /* TokenUnaryNot, */ { 14, 0, 0, "!" }, /* TokenUnaryExor, */ { 14, 0, 0, "~" }, /* TokenSizeof, */ { 14, 0, 0, "sizeof" }, /* TokenCast, */ { 14, 0, 0, "cast" },
/* TokenLeftSquareBracket, */ { 0, 0, 15, "[" }, /* TokenRightSquareBracket, */ { 0, 15, 0, "]" }, /* TokenDot, */ { 0, 0, 15, "." }, /* TokenArrow, */ { 0, 0, 15, "->" },
/* TokenOpenBracket, */ { 15, 0, 0, "(" }, /* TokenCloseBracket, */ { 0, 15, 0, ")" }
};
void ExpressionParseFunctionCall(struct ParseState *Parser, struct ExpressionStack **StackTop, const char *FuncName, int RunIt);
#ifdef DEBUG_EXPRESSIONS
/* show the contents of the expression stack */
void ExpressionStackShow(Picoc *pc, struct ExpressionStack *StackTop)
{
printf("Expression stack [0x%lx,0x%lx]: ", (long)pc->HeapStackTop, (long)StackTop);
while (StackTop != NULL)
{
if (StackTop->Order == OrderNone)
{
/* it's a value */
if (StackTop->Val->IsLValue)
printf("lvalue=");
else
printf("value=");
switch (StackTop->Val->Typ->Base)
{
case TypeVoid: printf("void"); break;
case TypeInt: printf("%d:int", StackTop->Val->Val->Integer); break;
case TypeShort: printf("%d:short", StackTop->Val->Val->ShortInteger); break;
case TypeChar: printf("%d:char", StackTop->Val->Val->Character); break;
case TypeLong: printf("%ld:long", StackTop->Val->Val->LongInteger); break;
case TypeUnsignedShort: printf("%d:unsigned short", StackTop->Val->Val->UnsignedShortInteger); break;
case TypeUnsignedInt: printf("%d:unsigned int", StackTop->Val->Val->UnsignedInteger); break;
case TypeUnsignedLong: printf("%ld:unsigned long", StackTop->Val->Val->UnsignedLongInteger); break;
case TypeFP: printf("%f:fp", StackTop->Val->Val->FP); break;
case TypeFunction: printf("%s:function", StackTop->Val->Val->Identifier); break;
case TypeMacro: printf("%s:macro", StackTop->Val->Val->Identifier); break;
case TypePointer:
if (StackTop->Val->Val->Pointer == NULL)
printf("ptr(NULL)");
else if (StackTop->Val->Typ->FromType->Base == TypeChar)
printf("\"%s\":string", (char *)StackTop->Val->Val->Pointer);
else
printf("ptr(0x%lx)", (long)StackTop->Val->Val->Pointer);
break;
case TypeArray: printf("array"); break;
case TypeStruct: printf("%s:struct", StackTop->Val->Val->Identifier); break;
case TypeUnion: printf("%s:union", StackTop->Val->Val->Identifier); break;
case TypeEnum: printf("%s:enum", StackTop->Val->Val->Identifier); break;
case Type_Type: PrintType(StackTop->Val->Val->Typ, pc->CStdOut); printf(":type"); break;
default: printf("unknown"); break;
}
printf("[0x%lx,0x%lx]", (long)StackTop, (long)StackTop->Val);
}
else
{
/* it's an operator */
printf("op='%s' %s %d", OperatorPrecedence[(int)StackTop->Op].Name,
(StackTop->Order == OrderPrefix) ? "prefix" : ((StackTop->Order == OrderPostfix) ? "postfix" : "infix"),
StackTop->Precedence);
printf("[0x%lx]", (long)StackTop);
}
StackTop = StackTop->Next;
if (StackTop != NULL)
printf(", ");
}
printf("\n");
}
#endif
int IsTypeToken(struct ParseState * Parser, enum LexToken t, struct Value * LexValue)
{
if (t >= TokenIntType && t <= TokenUnsignedType)
return 1; /* base type */
/* typedef'ed type? */
if (t == TokenIdentifier) /* see TypeParseFront, case TokenIdentifier and ParseTypedef */
{
struct Value * VarValue;
if (VariableDefined(Parser->pc, (char *) LexValue->Val->Pointer))
{
VariableGet(Parser->pc, Parser, (char *) LexValue->Val->Pointer, &VarValue);
if (VarValue->Typ == &Parser->pc->TypeType)
return 1;
}
}
return 0;
}
int64_t ExpressionCoerceLong(struct Value *Val)
{
switch (Val->Typ->Base)
{
case TypeInt: return (long)Val->Val->Integer;
case TypeChar: return (long)Val->Val->Character;
case TypeShort: return (long)Val->Val->ShortInteger;
case TypeLong: return (int64_t)Val->Val->LongInteger;
case TypeUnsignedInt: return (unsigned long)Val->Val->UnsignedInteger;
case TypeUnsignedShort: return (unsigned long)Val->Val->UnsignedShortInteger;
case TypeUnsignedLong: return (uint64_t)Val->Val->UnsignedLongInteger;
case TypeUnsignedChar: return (unsigned long)Val->Val->UnsignedCharacter;
case TypePointer: return (uintptr_t)Val->Val->Pointer;
#ifndef NO_FP
case TypeFP: return (long)Val->Val->FP;
#endif
default: return 0;
}
}
uint64_t ExpressionCoerceUnsignedLong(struct Value *Val)
{
switch (Val->Typ->Base)
{
case TypeInt: return (unsigned long)Val->Val->Integer;
case TypeChar: return (unsigned long)Val->Val->Character;
case TypeShort: return (unsigned long)Val->Val->ShortInteger;
case TypeLong: return (uint64_t)Val->Val->LongInteger;
case TypeUnsignedInt: return (unsigned long)Val->Val->UnsignedInteger;
case TypeUnsignedShort: return (unsigned long)Val->Val->UnsignedShortInteger;
case TypeUnsignedLong: return (uint64_t)Val->Val->UnsignedLongInteger;
case TypeUnsignedChar: return (unsigned long)Val->Val->UnsignedCharacter;
case TypePointer: return (uintptr_t)Val->Val->Pointer;
#ifndef NO_FP
case TypeFP: return (unsigned long)Val->Val->FP;
#endif
default: return 0;
}
}
#ifndef NO_FP
double ExpressionCoerceFP(struct Value *Val)
{
#ifndef BROKEN_FLOAT_CASTS
int64_t IntVal;
uint64_t UnsignedVal;
switch (Val->Typ->Base)
{
case TypeInt: IntVal = Val->Val->Integer; return (double)IntVal;
case TypeChar: IntVal = Val->Val->Character; return (double)IntVal;
case TypeShort: IntVal = Val->Val->ShortInteger; return (double)IntVal;
case TypeLong: IntVal = Val->Val->LongInteger; return (double)IntVal;
case TypeUnsignedInt: UnsignedVal = Val->Val->UnsignedInteger; return (double)UnsignedVal;
case TypeUnsignedShort: UnsignedVal = Val->Val->UnsignedShortInteger; return (double)UnsignedVal;
case TypeUnsignedLong: UnsignedVal = Val->Val->UnsignedLongInteger; return (double)UnsignedVal;
case TypeUnsignedChar: UnsignedVal = Val->Val->UnsignedCharacter; return (double)UnsignedVal;
case TypeFP: return Val->Val->FP;
default: return 0.0;
}
#else
switch (Val->Typ->Base)
{
case TypeInt: return (double)Val->Val->Integer;
case TypeChar: return (double)Val->Val->Character;
case TypeShort: return (double)Val->Val->ShortInteger;
case TypeLong: return (double)Val->Val->LongInteger;
case TypeUnsignedInt: return (double)Val->Val->UnsignedInteger;
case TypeUnsignedShort: return (double)Val->Val->UnsignedShortInteger;
case TypeUnsignedLong: return (double)Val->Val->UnsignedLongInteger;
case TypeUnsignedChar: return (double)Val->Val->UnsignedCharacter;
case TypeFP: return (double)Val->Val->FP;
default: return 0.0;
}
#endif
}
#endif
/* assign an integer value */
int64_t ExpressionAssignInt(struct ParseState *Parser, struct Value *DestValue, int64_t FromInt, int After)
{
int64_t Result;
if (!DestValue->IsLValue)
ProgramFail(Parser, "can't assign to this");
if (After)
Result = ExpressionCoerceLong(DestValue);
else
Result = FromInt;
switch (DestValue->Typ->Base)
{
case TypeInt: DestValue->Val->Integer = (int)FromInt; break;
case TypeShort: DestValue->Val->ShortInteger = (short)FromInt; break;
case TypeChar: DestValue->Val->Character = (char)FromInt; break;
case TypeLong: DestValue->Val->LongInteger = (int64_t)FromInt; break;
case TypeUnsignedInt: DestValue->Val->UnsignedInteger = (unsigned int)FromInt; break;
case TypeUnsignedShort: DestValue->Val->UnsignedShortInteger = (unsigned short)FromInt; break;
case TypeUnsignedLong: DestValue->Val->UnsignedLongInteger = (uint64_t)FromInt; break;
case TypeUnsignedChar: DestValue->Val->UnsignedCharacter = (unsigned char)FromInt; break;
default: break;
}
return Result;
}
#ifndef NO_FP
/* assign a floating point value */
double ExpressionAssignFP(struct ParseState *Parser, struct Value *DestValue, double FromFP)
{
if (!DestValue->IsLValue)
ProgramFail(Parser, "can't assign to this");
DestValue->Val->FP = FromFP;
return FromFP;
}
#endif
/* push a node on to the expression stack */
void ExpressionStackPushValueNode(struct ParseState *Parser, struct ExpressionStack **StackTop, struct Value *ValueLoc)
{
struct ExpressionStack *StackNode = (struct ExpressionStack *) VariableAlloc(Parser->pc, Parser, sizeof(struct ExpressionStack), FALSE);
StackNode->Next = *StackTop;
StackNode->Val = ValueLoc;
*StackTop = StackNode;
#ifdef FANCY_ERROR_MESSAGES
StackNode->Line = Parser->Line;
StackNode->CharacterPos = Parser->CharacterPos;
#endif
#ifdef DEBUG_EXPRESSIONS
ExpressionStackShow(Parser->pc, *StackTop);
#endif
}
/* push a blank value on to the expression stack by type */
struct Value *ExpressionStackPushValueByType(struct ParseState *Parser, struct ExpressionStack **StackTop, struct ValueType *PushType)
{
struct Value *ValueLoc = VariableAllocValueFromType(Parser->pc, Parser, PushType, FALSE, NULL, FALSE);
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
return ValueLoc;
}
/* push a value on to the expression stack */
void ExpressionStackPushValue(struct ParseState *Parser, struct ExpressionStack **StackTop, struct Value *PushValue)
{
struct Value *ValueLoc = VariableAllocValueAndCopy(Parser->pc, Parser, PushValue, FALSE);
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
}
void ExpressionStackPushLValue(struct ParseState *Parser, struct ExpressionStack **StackTop, struct Value *PushValue, int Offset)
{
struct Value *ValueLoc = VariableAllocValueShared(Parser, PushValue);
ValueLoc->Val = (union AnyValue *)((char *)ValueLoc->Val + Offset);
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
}
void ExpressionStackPushDereference(struct ParseState *Parser, struct ExpressionStack **StackTop, struct Value *DereferenceValue)
{
struct Value *DerefVal;
struct Value *ValueLoc;
int Offset;
struct ValueType *DerefType;
int DerefIsLValue;
void *DerefDataLoc = VariableDereferencePointer(Parser, DereferenceValue, &DerefVal, &Offset, &DerefType, &DerefIsLValue);
if (DerefDataLoc == NULL)
ProgramFail(Parser, "NULL pointer dereference");
ValueLoc = VariableAllocValueFromExistingData(Parser, DerefType, (union AnyValue *)DerefDataLoc, DerefIsLValue, DerefVal);
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
}
void ExpressionPushLong(struct ParseState *Parser, struct ExpressionStack **StackTop, int64_t LongValue)
{
struct Value *ValueLoc = VariableAllocValueFromType(Parser->pc, Parser, &Parser->pc->LongType, FALSE, NULL, FALSE);
ValueLoc->Val->LongInteger = LongValue;
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
}
#ifndef NO_FP
void ExpressionPushFP(struct ParseState *Parser, struct ExpressionStack **StackTop, double FPValue)
{
struct Value *ValueLoc = VariableAllocValueFromType(Parser->pc, Parser, &Parser->pc->FPType, FALSE, NULL, FALSE);
ValueLoc->Val->FP = FPValue;
ExpressionStackPushValueNode(Parser, StackTop, ValueLoc);
}
#endif
/* assign to a pointer */
void ExpressionAssignToPointer(struct ParseState *Parser, struct Value *ToValue, struct Value *FromValue, const char *FuncName, int ParamNo, int AllowPointerCoercion)
{
struct ValueType *PointedToType = ToValue->Typ->FromType;
if (FromValue->Typ == ToValue->Typ || FromValue->Typ == Parser->pc->VoidPtrType || (ToValue->Typ == Parser->pc->VoidPtrType && FromValue->Typ->Base == TypePointer))
ToValue->Val->Pointer = FromValue->Val->Pointer; /* plain old pointer assignment */
else if (FromValue->Typ->Base == TypeArray && (PointedToType == FromValue->Typ->FromType || ToValue->Typ == Parser->pc->VoidPtrType))
{
/* the form is: blah *x = array of blah */
ToValue->Val->Pointer = (void *)&FromValue->Val->ArrayMem[0];
}
else if (FromValue->Typ->Base == TypePointer && FromValue->Typ->FromType->Base == TypeArray &&
(PointedToType == FromValue->Typ->FromType->FromType || ToValue->Typ == Parser->pc->VoidPtrType) )
{
/* the form is: blah *x = pointer to array of blah */
ToValue->Val->Pointer = VariableDereferencePointer(Parser, FromValue, NULL, NULL, NULL, NULL);
}
else if (IS_NUMERIC_COERCIBLE(FromValue) && ExpressionCoerceLong(FromValue) == 0)
{
/* null pointer assignment */
ToValue->Val->Pointer = NULL;
}
else if (AllowPointerCoercion && IS_NUMERIC_COERCIBLE(FromValue))
{
/* assign integer to native pointer */
ToValue->Val->Pointer = (void *)(uintptr_t)ExpressionCoerceUnsignedLong(FromValue);
}
else if (AllowPointerCoercion && FromValue->Typ->Base == TypePointer)
{
/* assign a pointer to a pointer to a different type */
ToValue->Val->Pointer = FromValue->Val->Pointer;
}
else
AssignFail(Parser, "%t from %t", ToValue->Typ, FromValue->Typ, 0, 0, FuncName, ParamNo);
}
/* assign any kind of value */
void ExpressionAssign(struct ParseState *Parser, struct Value *DestValue, struct Value *SourceValue, int Force, const char *FuncName, int ParamNo, int AllowPointerCoercion)
{
if (!DestValue->IsLValue && !Force)
AssignFail(Parser, "not an lvalue", NULL, NULL, 0, 0, FuncName, ParamNo);
if (IS_NUMERIC_COERCIBLE(DestValue) && !IS_NUMERIC_COERCIBLE_PLUS_POINTERS(SourceValue, AllowPointerCoercion))
AssignFail(Parser, "%t from %t", DestValue->Typ, SourceValue->Typ, 0, 0, FuncName, ParamNo);
switch (DestValue->Typ->Base)
{
case TypeInt: DestValue->Val->Integer = (int)ExpressionCoerceLong(SourceValue); break;
case TypeShort: DestValue->Val->ShortInteger = (short)ExpressionCoerceLong(SourceValue); break;
case TypeChar: DestValue->Val->Character = (char)ExpressionCoerceLong(SourceValue); break;
case TypeLong: DestValue->Val->LongInteger = ExpressionCoerceLong(SourceValue); break;
case TypeUnsignedInt: DestValue->Val->UnsignedInteger = (unsigned int) ExpressionCoerceUnsignedLong(SourceValue); break;
case TypeUnsignedShort: DestValue->Val->UnsignedShortInteger = (unsigned short)ExpressionCoerceUnsignedLong(SourceValue); break;
case TypeUnsignedLong: DestValue->Val->UnsignedLongInteger = ExpressionCoerceUnsignedLong(SourceValue); break;
case TypeUnsignedChar: DestValue->Val->UnsignedCharacter = (unsigned char)ExpressionCoerceUnsignedLong(SourceValue); break;
#ifndef NO_FP
case TypeFP:
if (!IS_NUMERIC_COERCIBLE_PLUS_POINTERS(SourceValue, AllowPointerCoercion))
AssignFail(Parser, "%t from %t", DestValue->Typ, SourceValue->Typ, 0, 0, FuncName, ParamNo);
DestValue->Val->FP = ExpressionCoerceFP(SourceValue);
break;
#endif
case TypePointer:
ExpressionAssignToPointer(Parser, DestValue, SourceValue, FuncName, ParamNo, AllowPointerCoercion);
break;
case TypeArray:
if (SourceValue->Typ->Base == TypeArray && DestValue->Typ->FromType == DestValue->Typ->FromType && DestValue->Typ->ArraySize == 0)
{
/* destination array is unsized - need to resize the destination array to the same size as the source array */
DestValue->Typ = SourceValue->Typ;
VariableRealloc(Parser, DestValue, TypeSizeValue(DestValue, FALSE));
if (DestValue->LValueFrom != NULL)
{
/* copy the resized value back to the LValue */
DestValue->LValueFrom->Val = DestValue->Val;
DestValue->LValueFrom->AnyValOnHeap = DestValue->AnyValOnHeap;
}
}
/* char array = "abcd" */
if (DestValue->Typ->FromType->Base == TypeChar && SourceValue->Typ->Base == TypePointer && SourceValue->Typ->FromType->Base == TypeChar)
{
if (DestValue->Typ->ArraySize == 0) /* char x[] = "abcd", x is unsized */
{
int Size = (int) strlen((char *) SourceValue->Val->Pointer) + 1;
#ifdef DEBUG_ARRAY_INITIALIZER
PRINT_SOURCE_POS;
fprintf(stderr, "str size: %d\n", Size);
#endif
DestValue->Typ = TypeGetMatching(Parser->pc, Parser, DestValue->Typ->FromType, DestValue->Typ->Base, Size, DestValue->Typ->Identifier, TRUE);
VariableRealloc(Parser, DestValue, TypeSizeValue(DestValue, FALSE));
}
/* else, it's char x[10] = "abcd" */
#ifdef DEBUG_ARRAY_INITIALIZER
PRINT_SOURCE_POS;
fprintf(stderr, "char[%d] from char* (len=%d)\n", DestValue->Typ->ArraySize, strlen(SourceValue->Val->Pointer));
#endif
memcpy((void *)DestValue->Val, SourceValue->Val->Pointer, TypeSizeValue(DestValue, FALSE));
break;
}
if (DestValue->Typ != SourceValue->Typ)
AssignFail(Parser, "%t from %t", DestValue->Typ, SourceValue->Typ, 0, 0, FuncName, ParamNo);
if (DestValue->Typ->ArraySize != SourceValue->Typ->ArraySize)
AssignFail(Parser, "from an array of size %d to one of size %d", NULL, NULL, DestValue->Typ->ArraySize, SourceValue->Typ->ArraySize, FuncName, ParamNo);
memcpy((void *)DestValue->Val, (void *)SourceValue->Val, TypeSizeValue(DestValue, FALSE));
break;
case TypeStruct:
case TypeUnion:
if (DestValue->Typ != SourceValue->Typ)
AssignFail(Parser, "%t from %t", DestValue->Typ, SourceValue->Typ, 0, 0, FuncName, ParamNo);
memcpy((void *)DestValue->Val, (void *)SourceValue->Val, TypeSizeValue(SourceValue, FALSE));
break;
default:
AssignFail(Parser, "%t", DestValue->Typ, NULL, 0, 0, FuncName, ParamNo);
break;
}
}
/* evaluate the first half of a ternary operator x ? y : z */
void ExpressionQuestionMarkOperator(struct ParseState *Parser, struct ExpressionStack **StackTop, struct Value *BottomValue, struct Value *TopValue)
{
if (!IS_NUMERIC_COERCIBLE(TopValue))
ProgramFail(Parser, "first argument to '?' should be a number");
if (ExpressionCoerceLong(TopValue))
{
/* the condition's true, return the BottomValue */
ExpressionStackPushValue(Parser, StackTop, BottomValue);
}
else
{
/* the condition's false, return void */
ExpressionStackPushValueByType(Parser, StackTop, &Parser->pc->VoidType);
}
}
/* evaluate the second half of a ternary operator x ? y : z */
void ExpressionColonOperator(struct ParseState *Parser, struct ExpressionStack **StackTop, struct Value *BottomValue, struct Value *TopValue)
{
if (TopValue->Typ->Base == TypeVoid)
{
/* invoke the "else" part - return the BottomValue */
ExpressionStackPushValue(Parser, StackTop, BottomValue);
}
else
{
/* it was a "then" - return the TopValue */
ExpressionStackPushValue(Parser, StackTop, TopValue);
}
}
/* evaluate a prefix operator */
void ExpressionPrefixOperator(struct ParseState *Parser, struct ExpressionStack **StackTop, enum LexToken Op, struct Value *TopValue)
{
struct Value *Result;
union AnyValue *ValPtr;
debugf("ExpressionPrefixOperator()\n");
switch (Op)
{
case TokenAmpersand:
if (!TopValue->IsLValue)
ProgramFail(Parser, "can't get the address of this");
ValPtr = TopValue->Val;
Result = VariableAllocValueFromType(Parser->pc, Parser, TypeGetMatching(Parser->pc, Parser, TopValue->Typ, TypePointer, 0, Parser->pc->StrEmpty, TRUE), FALSE, NULL, FALSE);
Result->Val->Pointer = (void *)ValPtr;
ExpressionStackPushValueNode(Parser, StackTop, Result);
break;
case TokenAsterisk:
ExpressionStackPushDereference(Parser, StackTop, TopValue);
break;
case TokenSizeof:
/* return the size of the argument */
if (TopValue->Typ == &Parser->pc->TypeType)
ExpressionPushLong(Parser, StackTop, TypeSize(TopValue->Val->Typ, TopValue->Val->Typ->ArraySize, TRUE));
else
ExpressionPushLong(Parser, StackTop, TypeSize(TopValue->Typ, TopValue->Typ->ArraySize, TRUE));
break;
default:
/* an arithmetic operator */
#ifndef NO_FP
if (TopValue->Typ == &Parser->pc->FPType)
{
/* floating point prefix arithmetic */
double ResultFP = 0.0;
switch (Op)
{
case TokenPlus: ResultFP = TopValue->Val->FP; break;
case TokenMinus: ResultFP = -TopValue->Val->FP; break;
case TokenIncrement: ResultFP = ExpressionAssignFP(Parser, TopValue, TopValue->Val->FP+1); break;
case TokenDecrement: ResultFP = ExpressionAssignFP(Parser, TopValue, TopValue->Val->FP-1); break;
case TokenUnaryNot: ResultFP = !TopValue->Val->FP; break;
default: ProgramFail(Parser, "invalid operation"); break;
}
ExpressionPushFP(Parser, StackTop, ResultFP);
}
else
#endif
if (IS_NUMERIC_COERCIBLE(TopValue))
{
/* integer prefix arithmetic */
int64_t ResultInt = 0;
int64_t TopInt = ExpressionCoerceLong(TopValue);
switch (Op)
{
case TokenPlus: ResultInt = TopInt; break;
case TokenMinus: ResultInt = -TopInt; break;
case TokenIncrement: ResultInt = ExpressionAssignInt(Parser, TopValue, TopInt+1, FALSE); break;
case TokenDecrement: ResultInt = ExpressionAssignInt(Parser, TopValue, TopInt-1, FALSE); break;
case TokenUnaryNot: ResultInt = !TopInt; break;
case TokenUnaryExor: ResultInt = ~TopInt; break;
default: ProgramFail(Parser, "invalid operation"); break;
}
ExpressionPushLong(Parser, StackTop, ResultInt);
}
else if (TopValue->Typ->Base == TypePointer)
{
/* pointer prefix arithmetic */
int Size = TypeSize(TopValue->Typ->FromType, 0, TRUE);
struct Value *StackValue;
void *ResultPtr;
if (TopValue->Val->Pointer == NULL)
ProgramFail(Parser, "invalid use of a NULL pointer");
if (!TopValue->IsLValue)
ProgramFail(Parser, "can't assign to this");
switch (Op)
{
case TokenIncrement: TopValue->Val->Pointer = (void *)((char *)TopValue->Val->Pointer + Size); break;
case TokenDecrement: TopValue->Val->Pointer = (void *)((char *)TopValue->Val->Pointer - Size); break;
default: ProgramFail(Parser, "invalid operation"); break;
}
ResultPtr = TopValue->Val->Pointer;
StackValue = ExpressionStackPushValueByType(Parser, StackTop, TopValue->Typ);
StackValue->Val->Pointer = ResultPtr;
}
else
ProgramFail(Parser, "invalid operation");
break;
}
}
/* evaluate a postfix operator */
void ExpressionPostfixOperator(struct ParseState *Parser, struct ExpressionStack **StackTop, enum LexToken Op, struct Value *TopValue)
{
debugf("ExpressionPostfixOperator()\n");
#ifndef NO_FP
if (TopValue->Typ == &Parser->pc->FPType)
{
/* floating point prefix arithmetic */
double ResultFP = 0.0;
switch (Op)
{
case TokenIncrement: ResultFP = ExpressionAssignFP(Parser, TopValue, TopValue->Val->FP+1); break;
case TokenDecrement: ResultFP = ExpressionAssignFP(Parser, TopValue, TopValue->Val->FP-1); break;
default: ProgramFail(Parser, "invalid operation"); break;
}
ExpressionPushFP(Parser, StackTop, ResultFP);
}
else
#endif
if (IS_NUMERIC_COERCIBLE(TopValue))
{
int64_t ResultInt = 0;
int64_t TopInt = ExpressionCoerceLong(TopValue);
switch (Op)
{
case TokenIncrement: ResultInt = ExpressionAssignInt(Parser, TopValue, TopInt+1, TRUE); break;
case TokenDecrement: ResultInt = ExpressionAssignInt(Parser, TopValue, TopInt-1, TRUE); break;
case TokenRightSquareBracket: ProgramFail(Parser, "not supported"); break; /* XXX */
case TokenCloseBracket: ProgramFail(Parser, "not supported"); break; /* XXX */
default: ProgramFail(Parser, "invalid operation"); break;
}
ExpressionPushLong(Parser, StackTop, ResultInt);
}
else if (TopValue->Typ->Base == TypePointer)
{
/* pointer postfix arithmetic */
int Size = TypeSize(TopValue->Typ->FromType, 0, TRUE);
struct Value *StackValue;
void *OrigPointer = TopValue->Val->Pointer;
if (TopValue->Val->Pointer == NULL)
ProgramFail(Parser, "invalid use of a NULL pointer");
if (!TopValue->IsLValue)
ProgramFail(Parser, "can't assign to this");
switch (Op)
{
case TokenIncrement: TopValue->Val->Pointer = (void *)((char *)TopValue->Val->Pointer + Size); break;
case TokenDecrement: TopValue->Val->Pointer = (void *)((char *)TopValue->Val->Pointer - Size); break;
default: ProgramFail(Parser, "invalid operation"); break;
}
StackValue = ExpressionStackPushValueByType(Parser, StackTop, TopValue->Typ);
StackValue->Val->Pointer = OrigPointer;
}
else
ProgramFail(Parser, "invalid operation");
}
/* evaluate an infix operator */
void ExpressionInfixOperator(struct ParseState *Parser, struct ExpressionStack **StackTop, enum LexToken Op, struct Value *BottomValue, struct Value *TopValue)
{
int64_t ResultInt = 0;
struct Value *StackValue;
void *Pointer;
debugf("ExpressionInfixOperator()\n");
if (BottomValue == NULL || TopValue == NULL)
ProgramFail(Parser, "invalid expression");
if (Op == TokenLeftSquareBracket)
{
/* array index */
int ArrayIndex;
struct Value *Result = NULL;
if (!IS_NUMERIC_COERCIBLE(TopValue))
ProgramFail(Parser, "array index must be an integer");
ArrayIndex = (int) ExpressionCoerceLong(TopValue);
/* make the array element result */
switch (BottomValue->Typ->Base)
{
case TypeArray: Result = VariableAllocValueFromExistingData(Parser, BottomValue->Typ->FromType, (union AnyValue *)(&BottomValue->Val->ArrayMem[0] + TypeSize(BottomValue->Typ, ArrayIndex, TRUE)), BottomValue->IsLValue, BottomValue->LValueFrom); break;
case TypePointer: Result = VariableAllocValueFromExistingData(Parser, BottomValue->Typ->FromType, (union AnyValue *)((char *)BottomValue->Val->Pointer + TypeSize(BottomValue->Typ->FromType, 0, TRUE) * ArrayIndex), BottomValue->IsLValue, BottomValue->LValueFrom); break;
default: ProgramFail(Parser, "this %t is not an array", BottomValue->Typ);
}
ExpressionStackPushValueNode(Parser, StackTop, Result);
}
else if (Op == TokenQuestionMark)
ExpressionQuestionMarkOperator(Parser, StackTop, TopValue, BottomValue);
else if (Op == TokenColon)
ExpressionColonOperator(Parser, StackTop, TopValue, BottomValue);
#ifndef NO_FP
else if ( (TopValue->Typ == &Parser->pc->FPType && BottomValue->Typ == &Parser->pc->FPType) ||
(TopValue->Typ == &Parser->pc->FPType && IS_NUMERIC_COERCIBLE(BottomValue)) ||
(IS_NUMERIC_COERCIBLE(TopValue) && BottomValue->Typ == &Parser->pc->FPType) )
{
/* floating point infix arithmetic */
int ResultIsInt = FALSE;
double ResultFP = 0.0;
double TopFP = (TopValue->Typ == &Parser->pc->FPType) ? TopValue->Val->FP : (double)ExpressionCoerceLong(TopValue);
double BottomFP = (BottomValue->Typ == &Parser->pc->FPType) ? BottomValue->Val->FP : (double)ExpressionCoerceLong(BottomValue);
switch (Op)
{
case TokenAssign: ASSIGN_FP_OR_INT(TopFP); break;
case TokenAddAssign: ASSIGN_FP_OR_INT(BottomFP + TopFP); break;
case TokenSubtractAssign: ASSIGN_FP_OR_INT(BottomFP - TopFP); break;
case TokenMultiplyAssign: ASSIGN_FP_OR_INT(BottomFP * TopFP); break;
case TokenDivideAssign: ASSIGN_FP_OR_INT(BottomFP / TopFP); break;
case TokenEqual: ResultInt = BottomFP == TopFP; ResultIsInt = TRUE; break;
case TokenNotEqual: ResultInt = BottomFP != TopFP; ResultIsInt = TRUE; break;
case TokenLessThan: ResultInt = BottomFP < TopFP; ResultIsInt = TRUE; break;
case TokenGreaterThan: ResultInt = BottomFP > TopFP; ResultIsInt = TRUE; break;
case TokenLessEqual: ResultInt = BottomFP <= TopFP; ResultIsInt = TRUE; break;
case TokenGreaterEqual: ResultInt = BottomFP >= TopFP; ResultIsInt = TRUE; break;
case TokenPlus: ResultFP = BottomFP + TopFP; break;
case TokenMinus: ResultFP = BottomFP - TopFP; break;
case TokenAsterisk: ResultFP = BottomFP * TopFP; break;
case TokenSlash: ResultFP = BottomFP / TopFP; break;
default: ProgramFail(Parser, "invalid operation"); break;
}
if (ResultIsInt)
ExpressionPushLong(Parser, StackTop, ResultInt);
else
ExpressionPushFP(Parser, StackTop, ResultFP);
}
#endif
else if (IS_NUMERIC_COERCIBLE(TopValue) && IS_NUMERIC_COERCIBLE(BottomValue))
{
/* integer operation */
int64_t TopInt = ExpressionCoerceLong(TopValue);
int64_t BottomInt = ExpressionCoerceLong(BottomValue);
switch (Op)
{
case TokenAssign: ResultInt = ExpressionAssignInt(Parser, BottomValue, TopInt, FALSE); break;
case TokenAddAssign: ResultInt = ExpressionAssignInt(Parser, BottomValue, BottomInt + TopInt, FALSE); break;
case TokenSubtractAssign: ResultInt = ExpressionAssignInt(Parser, BottomValue, BottomInt - TopInt, FALSE); break;
case TokenMultiplyAssign: ResultInt = ExpressionAssignInt(Parser, BottomValue, BottomInt * TopInt, FALSE); break;
case TokenDivideAssign: ResultInt = ExpressionAssignInt(Parser, BottomValue, BottomInt / TopInt, FALSE); break;
#ifndef NO_MODULUS
case TokenModulusAssign: ResultInt = ExpressionAssignInt(Parser, BottomValue, BottomInt % TopInt, FALSE); break;
#endif
case TokenShiftLeftAssign: ResultInt = ExpressionAssignInt(Parser, BottomValue, BottomInt << TopInt, FALSE); break;
case TokenShiftRightAssign:
if (BottomValue->Typ->Base == TypeUnsignedInt || BottomValue->Typ->Base == TypeUnsignedLong)
ResultInt = ExpressionAssignInt(Parser, BottomValue, (uint64_t) BottomInt >> TopInt, FALSE);
else
ResultInt = ExpressionAssignInt(Parser, BottomValue, BottomInt >> TopInt, FALSE);
break;
case TokenArithmeticAndAssign: ResultInt = ExpressionAssignInt(Parser, BottomValue, BottomInt & TopInt, FALSE); break;
case TokenArithmeticOrAssign: ResultInt = ExpressionAssignInt(Parser, BottomValue, BottomInt | TopInt, FALSE); break;
case TokenArithmeticExorAssign: ResultInt = ExpressionAssignInt(Parser, BottomValue, BottomInt ^ TopInt, FALSE); break;
case TokenLogicalOr: ResultInt = BottomInt || TopInt; break;
case TokenLogicalAnd: ResultInt = BottomInt && TopInt; break;
case TokenArithmeticOr: ResultInt = BottomInt | TopInt; break;
case TokenArithmeticExor: ResultInt = BottomInt ^ TopInt; break;
case TokenAmpersand: ResultInt = BottomInt & TopInt; break;
case TokenEqual: ResultInt = BottomInt == TopInt; break;
case TokenNotEqual: ResultInt = BottomInt != TopInt; break;
case TokenLessThan: ResultInt = BottomInt < TopInt; break;
case TokenGreaterThan: ResultInt = BottomInt > TopInt; break;
case TokenLessEqual: ResultInt = BottomInt <= TopInt; break;
case TokenGreaterEqual: ResultInt = BottomInt >= TopInt; break;
case TokenShiftLeft: ResultInt = BottomInt << TopInt; break;
case TokenShiftRight:
if (BottomValue->Typ->Base == TypeUnsignedInt || BottomValue->Typ->Base == TypeUnsignedLong)
ResultInt = (uint64_t) BottomInt >> TopInt;
else
ResultInt = BottomInt >> TopInt;
break;
case TokenPlus: ResultInt = BottomInt + TopInt; break;
case TokenMinus: ResultInt = BottomInt - TopInt; break;
case TokenAsterisk: ResultInt = BottomInt * TopInt; break;
case TokenSlash: ResultInt = BottomInt / TopInt; break;
#ifndef NO_MODULUS
case TokenModulus: ResultInt = BottomInt % TopInt; break;
#endif
default: ProgramFail(Parser, "invalid operation"); break;
}
ExpressionPushLong(Parser, StackTop, ResultInt);
}
else if (BottomValue->Typ->Base == TypePointer && IS_NUMERIC_COERCIBLE(TopValue))
{
/* pointer/integer infix arithmetic */
int64_t TopInt = ExpressionCoerceLong(TopValue);
if (Op == TokenEqual || Op == TokenNotEqual)
{
/* comparison to a NULL pointer */
if (TopInt != 0)
ProgramFail(Parser, "invalid operation");
if (Op == TokenEqual)
ExpressionPushLong(Parser, StackTop, BottomValue->Val->Pointer == NULL);
else
ExpressionPushLong(Parser, StackTop, BottomValue->Val->Pointer != NULL);
}
else if (Op == TokenPlus || Op == TokenMinus)
{
/* pointer arithmetic */
int Size = TypeSize(BottomValue->Typ->FromType, 0, TRUE);
Pointer = BottomValue->Val->Pointer;
if (Pointer == NULL)
ProgramFail(Parser, "invalid use of a NULL pointer");
if (Op == TokenPlus)
Pointer = (void *)((char *)Pointer + TopInt * Size);
else
Pointer = (void *)((char *)Pointer - TopInt * Size);
StackValue = ExpressionStackPushValueByType(Parser, StackTop, BottomValue->Typ);
StackValue->Val->Pointer = Pointer;
}
else if (Op == TokenAssign && TopInt == 0)
{
/* assign a NULL pointer */
HeapUnpopStack(Parser->pc, sizeof(struct Value));
ExpressionAssign(Parser, BottomValue, TopValue, FALSE, NULL, 0, FALSE);
ExpressionStackPushValueNode(Parser, StackTop, BottomValue);
}
else if (Op == TokenAddAssign || Op == TokenSubtractAssign)
{
/* pointer arithmetic */
int Size = TypeSize(BottomValue->Typ->FromType, 0, TRUE);
Pointer = BottomValue->Val->Pointer;
if (Pointer == NULL)
ProgramFail(Parser, "invalid use of a NULL pointer");
if (Op == TokenAddAssign)
Pointer = (void *)((char *)Pointer + TopInt * Size);
else
Pointer = (void *)((char *)Pointer - TopInt * Size);
HeapUnpopStack(Parser->pc, sizeof(struct Value));
BottomValue->Val->Pointer = Pointer;
ExpressionStackPushValueNode(Parser, StackTop, BottomValue);
}
else
ProgramFail(Parser, "invalid operation");
}
else if (BottomValue->Typ->Base == TypePointer && TopValue->Typ->Base == TypePointer && Op != TokenAssign)
{
/* pointer/pointer operations */
char *TopLoc = (char *)TopValue->Val->Pointer;
char *BottomLoc = (char *)BottomValue->Val->Pointer;
switch (Op)
{
case TokenEqual: ExpressionPushLong(Parser, StackTop, BottomLoc == TopLoc); break;
case TokenNotEqual: ExpressionPushLong(Parser, StackTop, BottomLoc != TopLoc); break;
case TokenMinus: ExpressionPushLong(Parser, StackTop, (int) (BottomLoc - TopLoc)); break;
default: ProgramFail(Parser, "invalid operation"); break;
}
}
else if (Op == TokenAssign)
{
/* assign a non-numeric type */
HeapUnpopStack(Parser->pc, sizeof(struct Value)); /* XXX - possible bug if lvalue is a temp value and takes more than sizeof(struct Value) */
ExpressionAssign(Parser, BottomValue, TopValue, FALSE, NULL, 0, FALSE);
ExpressionStackPushValueNode(Parser, StackTop, BottomValue);
}
else if (Op == TokenCast)
{
/* cast a value to a different type */ /* XXX - possible bug if the destination type takes more than sizeof(struct Value) + sizeof(struct ValueType *) */
struct Value *ValueLoc = ExpressionStackPushValueByType(Parser, StackTop, BottomValue->Val->Typ);
ExpressionAssign(Parser, ValueLoc, TopValue, TRUE, NULL, 0, TRUE);
}
else
ProgramFail(Parser, "invalid operation");
}
/* take the contents of the expression stack and compute the top until there's nothing greater than the given precedence */
void ExpressionStackCollapse(struct ParseState *Parser, struct ExpressionStack **StackTop, int Precedence, int *IgnorePrecedence)
{
int FoundPrecedence = Precedence;
struct Value *TopValue;
struct Value *BottomValue;
struct ExpressionStack *TopStackNode = *StackTop;
struct ExpressionStack *TopOperatorNode;
debugf("ExpressionStackCollapse(%d):\n", Precedence);
#ifdef DEBUG_EXPRESSIONS
ExpressionStackShow(Parser->pc, *StackTop);
#endif
while (TopStackNode != NULL && TopStackNode->Next != NULL && FoundPrecedence >= Precedence)
{
/* find the top operator on the stack */
if (TopStackNode->Order == OrderNone)
TopOperatorNode = TopStackNode->Next;
else
TopOperatorNode = TopStackNode;
FoundPrecedence = TopOperatorNode->Precedence;
/* does it have a high enough precedence? */
if (FoundPrecedence >= Precedence && TopOperatorNode != NULL)
{
/* execute this operator */
switch (TopOperatorNode->Order)
{
case OrderPrefix:
/* prefix evaluation */
debugf("prefix evaluation\n");
TopValue = TopStackNode->Val;
/* pop the value and then the prefix operator - assume they'll still be there until we're done */
HeapPopStack(Parser->pc, NULL, sizeof(struct ExpressionStack) + sizeof(struct Value) + TypeStackSizeValue(TopValue));
HeapPopStack(Parser->pc, TopOperatorNode, sizeof(struct ExpressionStack));
*StackTop = TopOperatorNode->Next;
/* do the prefix operation */
if (Parser->Mode == RunModeRun /* && FoundPrecedence < *IgnorePrecedence */)
{
/* run the operator */
ExpressionPrefixOperator(Parser, StackTop, TopOperatorNode->Op, TopValue);
}
else
{
/* we're not running it so just return 0 */
ExpressionPushLong(Parser, StackTop, 0);
}
break;
case OrderPostfix:
/* postfix evaluation */
debugf("postfix evaluation\n");
TopValue = TopStackNode->Next->Val;
/* pop the postfix operator and then the value - assume they'll still be there until we're done */
HeapPopStack(Parser->pc, NULL, sizeof(struct ExpressionStack));
HeapPopStack(Parser->pc, TopValue, sizeof(struct ExpressionStack) + sizeof(struct Value) + TypeStackSizeValue(TopValue));
*StackTop = TopStackNode->Next->Next;
/* do the postfix operation */
if (Parser->Mode == RunModeRun /* && FoundPrecedence < *IgnorePrecedence */)
{
/* run the operator */
ExpressionPostfixOperator(Parser, StackTop, TopOperatorNode->Op, TopValue);
}
else
{
/* we're not running it so just return 0 */
ExpressionPushLong(Parser, StackTop, 0);
}
break;
case OrderInfix:
/* infix evaluation */
debugf("infix evaluation\n");
TopValue = TopStackNode->Val;
if (TopValue != NULL)
{
BottomValue = TopOperatorNode->Next->Val;
/* pop a value, the operator and another value - assume they'll still be there until we're done */
HeapPopStack(Parser->pc, NULL, sizeof(struct ExpressionStack) + sizeof(struct Value) + TypeStackSizeValue(TopValue));
HeapPopStack(Parser->pc, NULL, sizeof(struct ExpressionStack));
HeapPopStack(Parser->pc, BottomValue, sizeof(struct ExpressionStack) + sizeof(struct Value) + TypeStackSizeValue(BottomValue));
*StackTop = TopOperatorNode->Next->Next;
/* do the infix operation */
if (Parser->Mode == RunModeRun /* && FoundPrecedence <= *IgnorePrecedence */)
{
/* run the operator */
ExpressionInfixOperator(Parser, StackTop, TopOperatorNode->Op, BottomValue, TopValue);
}
else
{
/* we're not running it so just return 0 */