-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathast.c
2202 lines (2041 loc) · 66.7 KB
/
ast.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
/* ast.c */
#include "ast.h"
#include <stdbool.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#include <stdint.h>
#include <stdio.h>
static JumpBuffer *jump_buffer = {0};
TypeModifiers current_modifiers = {false, false, false, false, false};
extern VarType current_var_type;
Variable symbol_table[MAX_VARS];
int var_count = 0;
// Symbol table functions
bool set_variable(const char *name, void *value, VarType type, TypeModifiers mods)
{
// Search for an existing variable
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, name) == 0)
{
symbol_table[i].var_type = type;
symbol_table[i].modifiers = mods;
switch (type)
{
case VAR_INT:
symbol_table[i].value.ivalue = *(int *)value;
break;
case VAR_SHORT:
symbol_table[i].value.svalue = *(short *)value;
break;
case VAR_FLOAT:
symbol_table[i].value.fvalue = *(float *)value;
break;
case VAR_DOUBLE:
symbol_table[i].value.dvalue = *(double *)value;
break;
case VAR_BOOL:
symbol_table[i].value.bvalue = *(bool *)value;
break;
case VAR_CHAR:
symbol_table[i].value.ivalue = *(char *)value;
break;
}
return true;
}
}
// Add a new variable if it doesn't exist
if (var_count < MAX_VARS)
{
symbol_table[var_count].name = strdup(name);
symbol_table[var_count].var_type = type;
symbol_table[var_count].modifiers = mods;
switch (type)
{
case VAR_INT:
symbol_table[var_count].value.ivalue = *(int *)value;
break;
case VAR_SHORT:
symbol_table[var_count].value.svalue = *(short *)value;
break;
case VAR_FLOAT:
symbol_table[var_count].value.fvalue = *(float *)value;
break;
case VAR_DOUBLE:
symbol_table[var_count].value.dvalue = *(double *)value;
break;
case VAR_BOOL:
symbol_table[var_count].value.bvalue = *(bool *)value;
break;
case VAR_CHAR:
symbol_table[var_count].value.ivalue = *(char *)value;
break;
default:
break;
}
var_count++;
return true;
}
return false; // Symbol table is full
}
bool set_int_variable(const char *name, int value, TypeModifiers mods)
{
return set_variable(name, &value, VAR_INT, mods);
}
bool set_short_variable(const char *name, short value, TypeModifiers mods)
{
return set_variable(name, &value, VAR_SHORT, mods);
}
bool set_float_variable(const char *name, float value, TypeModifiers mods)
{
return set_variable(name, &value, VAR_FLOAT, mods);
}
bool set_double_variable(const char *name, double value, TypeModifiers mods)
{
return set_variable(name, &value, VAR_DOUBLE, mods);
}
bool set_bool_variable(const char *name, bool value, TypeModifiers mods)
{
return set_variable(name, &value, VAR_BOOL, mods);
}
void reset_modifiers(void)
{
current_modifiers.is_volatile = false;
current_modifiers.is_signed = false;
current_modifiers.is_unsigned = false;
current_modifiers.is_const = false;
}
TypeModifiers get_current_modifiers(void)
{
TypeModifiers mods = current_modifiers;
reset_modifiers(); // Reset for next declaration
return mods;
}
/* Include the symbol table functions */
extern int get_variable(char *name);
extern void yyerror(const char *s);
extern void ragequit(int exit_code);
extern void chill(unsigned int seconds);
extern void yapping(const char *format, ...);
extern void yappin(const char *format, ...);
extern void baka(const char *format, ...);
extern TypeModifiers get_variable_modifiers(const char *name);
extern int yylineno;
/* Function implementations */
bool check_and_mark_identifier(ASTNode *node, const char *contextErrorMessage)
{
if (!node->alreadyChecked)
{
node->alreadyChecked = true;
node->isValidSymbol = false;
// Do the table lookup
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, node->data.name) == 0)
{
node->isValidSymbol = true;
break;
}
}
if (!node->isValidSymbol)
{
yylineno = yylineno - 2;
yyerror(contextErrorMessage);
}
}
return node->isValidSymbol;
}
void execute_switch_statement(ASTNode *node)
{
int switch_value = evaluate_expression(node->data.switch_stmt.expression);
CaseNode *current_case = node->data.switch_stmt.cases;
int matched = 0;
PUSH_JUMP_BUFFER();
if (setjmp(CURRENT_JUMP_BUFFER()) == 0)
{
while (current_case)
{
if (current_case->value)
{
int case_value = evaluate_expression(current_case->value);
if (case_value == switch_value || matched)
{
matched = 1;
execute_statements(current_case->statements);
}
}
else
{
// Default case
if (matched || !matched)
{
execute_statements(current_case->statements);
break;
}
}
current_case = current_case->next;
}
}
else
{
// Break encountered; do nothing
}
POP_JUMP_BUFFER();
}
static ASTNode *create_node(NodeType type, VarType var_type, TypeModifiers modifiers)
{
ASTNode *node = malloc(sizeof(ASTNode));
if (!node)
{
yyerror("Error: Memory allocation failed for ASTNode.\n");
exit(EXIT_FAILURE);
}
node->type = type;
node->var_type = var_type;
node->modifiers = modifiers;
node->alreadyChecked = false;
node->isValidSymbol = false;
return node;
}
ASTNode *create_int_node(int value)
{
ASTNode *node = create_node(NODE_INT, VAR_INT, current_modifiers);
SET_DATA_INT(node, value);
return node;
}
ASTNode *create_short_node(short value)
{
ASTNode *node = create_node(NODE_SHORT, VAR_SHORT, current_modifiers);
SET_DATA_SHORT(node, value);
return node;
}
ASTNode *create_float_node(float value)
{
ASTNode *node = create_node(NODE_FLOAT, VAR_FLOAT, current_modifiers);
SET_DATA_FLOAT(node, value);
return node;
}
ASTNode *create_char_node(char value)
{
ASTNode *node = create_node(NODE_CHAR, VAR_CHAR, current_modifiers);
SET_DATA_INT(node, value); // Store char as integer
return node;
}
ASTNode *create_boolean_node(bool value)
{
ASTNode *node = create_node(NODE_BOOLEAN, VAR_BOOL, current_modifiers);
SET_DATA_BOOL(node, value);
return node;
}
ASTNode *create_identifier_node(char *name)
{
ASTNode *node = create_node(NODE_IDENTIFIER, NONE, current_modifiers);
SET_DATA_NAME(node, name);
return node;
}
ASTNode *create_assignment_node(char *name, ASTNode *expr)
{
ASTNode *node = create_node(NODE_ASSIGNMENT, NONE, get_current_modifiers());
SET_DATA_OP(node, create_identifier_node(name), expr, OP_ASSIGN);
return node;
}
ASTNode *create_operation_node(OperatorType op, ASTNode *left, ASTNode *right)
{
ASTNode *node = create_node(NODE_OPERATION, NONE, current_modifiers);
SET_DATA_OP(node, left, right, op);
return node;
}
ASTNode *create_unary_operation_node(OperatorType op, ASTNode *operand)
{
ASTNode *node = create_node(NODE_UNARY_OPERATION, NONE, current_modifiers);
SET_DATA_UNARY_OP(node, operand, op);
return node;
}
ASTNode *create_for_statement_node(ASTNode *init, ASTNode *cond, ASTNode *incr, ASTNode *body)
{
ASTNode *node = create_node(NODE_FOR_STATEMENT, NONE, current_modifiers);
SET_DATA_FOR(node, init, cond, incr, body);
return node;
}
ASTNode *create_while_statement_node(ASTNode *cond, ASTNode *body)
{
ASTNode *node = create_node(NODE_WHILE_STATEMENT, NONE, current_modifiers);
SET_DATA_WHILE(node, cond, body);
return node;
}
ASTNode *create_do_while_statement_node(ASTNode *cond, ASTNode *body)
{
ASTNode *node = create_node(NODE_DO_WHILE_STATEMENT, NONE, current_modifiers);
SET_DATA_WHILE(node, cond, body);
return node;
}
ASTNode *create_function_call_node(char *func_name, ArgumentList *args)
{
ASTNode *node = create_node(NODE_FUNC_CALL, NONE, current_modifiers);
SET_DATA_FUNC_CALL(node, func_name, args);
return node;
}
ASTNode *create_double_node(double value)
{
ASTNode *node = create_node(NODE_DOUBLE, VAR_DOUBLE, current_modifiers);
SET_DATA_DOUBLE(node, value);
return node;
}
ASTNode *create_sizeof_node(char *name)
{
ASTNode *node = create_node(NODE_SIZEOF, NONE, current_modifiers);
SET_DATA_NAME(node, name);
return node;
}
// @param promotion: 0 for no promotion, 1 for promotion to double 2 for promotion to float
void *handle_identifier(ASTNode *node, const char *contextErrorMessage, int promote)
{
if (!check_and_mark_identifier(node, contextErrorMessage))
exit(1);
char *name = node->data.name;
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, name) == 0)
{
static Value promoted_value; // Static variable for holding promoted value.
if (promote == 1)
{
switch (symbol_table[i].var_type)
{
case VAR_DOUBLE:
return &symbol_table[i].value.dvalue;
case VAR_FLOAT:
promoted_value.dvalue = (double)symbol_table[i].value.fvalue;
return &promoted_value;
case VAR_INT:
case VAR_CHAR:
case VAR_SHORT:
promoted_value.dvalue = (double)symbol_table[i].value.svalue;
return &promoted_value;
case VAR_BOOL:
promoted_value.dvalue = (double)symbol_table[i].value.ivalue;
return &promoted_value;
default:
yyerror("Unsupported variable type");
return NULL;
}
}
else if (promote == 2)
{
switch (symbol_table[i].var_type)
{
case VAR_DOUBLE:
promoted_value.fvalue = (float)symbol_table[i].value.dvalue;
return &promoted_value;
case VAR_FLOAT:
return &symbol_table[i].value.fvalue;
case VAR_INT:
case VAR_CHAR:
case VAR_SHORT:
promoted_value.fvalue = (float)symbol_table[i].value.svalue;
case VAR_BOOL:
promoted_value.fvalue = (float)symbol_table[i].value.ivalue;
return &promoted_value;
default:
yyerror("Unsupported variable type");
return NULL;
}
}
else
{
switch (symbol_table[i].var_type)
{
case VAR_DOUBLE:
return &symbol_table[i].value.dvalue;
case VAR_FLOAT:
return &symbol_table[i].value.fvalue;
case VAR_INT:
case VAR_CHAR:
case VAR_SHORT:
return &symbol_table[i].value.svalue;
case VAR_BOOL:
return &symbol_table[i].value.ivalue;
default:
yyerror("Unsupported variable type");
return NULL;
}
}
}
}
yyerror("Undefined variable");
return NULL;
}
int get_expression_type(ASTNode *node)
{
if (!node)
{
yyerror("Null node in get_expression_type");
return NONE; // Return an unknown type if the node is null
}
switch (node->type)
{
case NODE_INT:
return VAR_INT;
case NODE_SHORT:
return VAR_SHORT;
case NODE_FLOAT:
return VAR_FLOAT;
case NODE_DOUBLE:
return VAR_DOUBLE;
case NODE_BOOLEAN:
return VAR_BOOL;
case NODE_CHAR:
return VAR_INT;
case NODE_IDENTIFIER:
{
// Look up the variable type in the symbol table
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, node->data.name) == 0)
{
return symbol_table[i].var_type;
}
}
yyerror("Undefined variable in get_expression_type");
return NONE;
}
case NODE_OPERATION:
{
// For binary operations, evaluate both operands to determine the highest type
int left_type = get_expression_type(node->data.op.left);
int right_type = get_expression_type(node->data.op.right);
// Promote to the highest type (int -> float -> double)
if (left_type == VAR_DOUBLE || right_type == VAR_DOUBLE)
return VAR_DOUBLE;
else if (left_type == VAR_FLOAT || right_type == VAR_FLOAT)
return VAR_FLOAT;
else
return VAR_INT;
}
case NODE_UNARY_OPERATION:
{
return get_expression_type(node->data.unary.operand);
}
default:
yyerror("Unknown node type in get_expression_type");
return NONE;
}
}
void *handle_binary_operation(ASTNode *node, int result_type)
{
if (!node || node->type != NODE_OPERATION)
{
yyerror("Invalid binary operation node");
return NULL;
}
// Evaluate left and right operands.
void *left_value = NULL;
void *right_value = NULL;
// Determine the actual types of the operands.
int left_type = get_expression_type(node->data.op.left);
int right_type = get_expression_type(node->data.op.right);
// Promote types if necessary (short -> int -> float -> double).
int promoted_type = VAR_INT;
if (left_type == VAR_DOUBLE || right_type == VAR_DOUBLE)
promoted_type = VAR_DOUBLE;
else if (left_type == VAR_FLOAT || right_type == VAR_FLOAT)
promoted_type = VAR_FLOAT;
// Allocate and evaluate operands based on promoted type.
switch (promoted_type)
{
case VAR_INT:
left_value = calloc(1, sizeof(int));
right_value = calloc(1, sizeof(int));
*(int *)left_value = evaluate_expression_int(node->data.op.left);
*(int *)right_value = evaluate_expression_int(node->data.op.right);
break;
case VAR_FLOAT:
left_value = calloc(1, sizeof(float));
right_value = calloc(1, sizeof(float));
*(float *)left_value = (left_type == VAR_INT)
? (float)evaluate_expression_int(node->data.op.left)
: evaluate_expression_float(node->data.op.left);
*(float *)right_value = (right_type == VAR_INT)
? (float)evaluate_expression_int(node->data.op.right)
: evaluate_expression_float(node->data.op.right);
break;
case VAR_DOUBLE:
left_value = calloc(1, sizeof(double));
right_value = calloc(1, sizeof(double));
*(double *)left_value = (left_type == VAR_INT)
? (double)evaluate_expression_int(node->data.op.left)
: (left_type == VAR_FLOAT)
? (double)evaluate_expression_float(node->data.op.left)
: evaluate_expression_double(node->data.op.left);
*(double *)right_value = (right_type == VAR_INT)
? (double)evaluate_expression_int(node->data.op.right)
: (right_type == VAR_FLOAT)
? (double)evaluate_expression_float(node->data.op.right)
: evaluate_expression_double(node->data.op.right);
break;
default:
yyerror("Unsupported type promotion");
return NULL;
}
// Perform the operation and allocate the result.
void *result = calloc(1, (promoted_type == VAR_DOUBLE) ? sizeof(double)
: (promoted_type == VAR_FLOAT) ? sizeof(float)
: (promoted_type == VAR_SHORT) ? sizeof(short)
: sizeof(int));
switch (node->data.op.op)
{
case OP_PLUS:
if (promoted_type == VAR_INT)
*(int *)result = *(int *)left_value + *(int *)right_value;
else if (promoted_type == VAR_FLOAT)
{
*(float *)result = *(float *)left_value + *(float *)right_value;
}
else if (promoted_type == VAR_DOUBLE)
*(double *)result = *(double *)left_value + *(double *)right_value;
break;
case OP_MINUS:
if (promoted_type == VAR_INT)
*(int *)result = *(int *)left_value - *(int *)right_value;
else if (promoted_type == VAR_FLOAT)
{
*(float *)result = *(float *)left_value - *(float *)right_value;
}
else if (promoted_type == VAR_DOUBLE)
{
volatile double res = *(double *)left_value - *(double *)right_value;
*(double *)result = res;
}
break;
case OP_TIMES:
if (promoted_type == VAR_INT)
*(int *)result = *(int *)left_value * *(int *)right_value;
else if (promoted_type == VAR_FLOAT)
*(float *)result = *(float *)left_value * *(float *)right_value;
else if (promoted_type == VAR_DOUBLE)
*(double *)result = *(double *)left_value * *(double *)right_value;
break;
case OP_DIVIDE:
if (promoted_type == VAR_INT)
{
if (*(int *)right_value == 0)
{
yyerror("Division by zero");
*(int *)result = 0; // Define a fallback behavior for int division by zero
}
else
{
*(int *)result = *(int *)left_value / *(int *)right_value;
}
}
else if (promoted_type == VAR_FLOAT)
{
float right = *(float *)right_value;
float left = *(float *)left_value;
if (fabsf(right) < __FLT_MIN__)
{
if (fabsf(left) < __FLT_MIN__)
{
*(float *)result = 0.0f / 0.0f; // NaN
}
else
{
*(float *)result = left > 0 ? __FLT_MAX__ : -__FLT_MAX__;
}
}
else
{
*(float *)result = left / right;
}
}
else if (promoted_type == VAR_DOUBLE)
{
double right = *(double *)right_value;
double left = *(double *)left_value;
if (fabs(right) < __DBL_MIN__)
{
if (fabs(left) < __DBL_MIN__)
{
*(double *)result = 0.0 / 0.0; // NaN
}
else
{
*(double *)result = left > 0 ? __DBL_MAX__ : -__DBL_MAX__;
}
}
else
{
*(double *)result = left / right;
}
}
break;
case OP_MOD:
if (promoted_type == VAR_INT)
{
int left = *(int *)left_value;
int right = *(int *)right_value;
if (right == 0)
{
yyerror("Modulo by zero");
*(int *)result = 0; // Define fallback for modulo by zero
}
else if (node->modifiers.is_unsigned)
{
// Explicitly handle unsigned modulo
unsigned int ul = (unsigned int)left;
unsigned int ur = (unsigned int)right;
*(int *)result = (int)(ul % ur);
}
else
{
*(int *)result = left % right;
}
}
else
{
yyerror("Modulo operation is only supported for integers");
*(int *)result = 0;
}
break;
case OP_LT:
if (promoted_type == VAR_INT)
*(int *)result = *(int *)left_value < *(int *)right_value;
else if (promoted_type == VAR_FLOAT)
*(float *)result = *(float *)left_value < *(float *)right_value;
else if (promoted_type == VAR_DOUBLE)
*(double *)result = *(double *)left_value < *(double *)right_value;
break;
case OP_GT:
if (promoted_type == VAR_INT)
*(int *)result = *(int *)left_value > *(int *)right_value;
else if (promoted_type == VAR_FLOAT)
*(float *)result = *(float *)left_value > *(float *)right_value;
else if (promoted_type == VAR_DOUBLE)
*(double *)result = *(double *)left_value > *(double *)right_value;
break;
case OP_LE:
if (promoted_type == VAR_INT)
*(int *)result = *(int *)left_value <= *(int *)right_value;
else if (promoted_type == VAR_FLOAT)
*(float *)result = *(float *)left_value <= *(float *)right_value;
else if (promoted_type == VAR_DOUBLE)
*(double *)result = *(double *)left_value <= *(double *)right_value;
break;
case OP_GE:
if (promoted_type == VAR_INT)
*(int *)result = *(int *)left_value >= *(int *)right_value;
else if (promoted_type == VAR_FLOAT)
*(float *)result = *(float *)left_value >= *(float *)right_value;
else if (promoted_type == VAR_DOUBLE)
*(double *)result = *(double *)left_value >= *(double *)right_value;
break;
case OP_EQ:
if (promoted_type == VAR_INT)
*(int *)result = *(int *)left_value == *(int *)right_value;
else if (promoted_type == VAR_FLOAT)
*(float *)result = *(float *)left_value == *(float *)right_value;
else if (promoted_type == VAR_DOUBLE)
*(double *)result = *(double *)left_value == *(double *)right_value;
break;
case OP_NE:
if (promoted_type == VAR_INT)
*(int *)result = *(int *)left_value != *(int *)right_value;
else if (promoted_type == VAR_FLOAT)
*(float *)result = *(float *)left_value != *(float *)right_value;
else if (promoted_type == VAR_DOUBLE)
*(double *)result = *(double *)left_value != *(double *)right_value;
break;
default:
yyerror("Unsupported binary operator");
free(result);
result = NULL;
}
free(left_value);
free(right_value);
return result;
}
void *handle_unary_expression(ASTNode *node, void *operand_value, int operand_type)
{
switch (node->data.unary.op)
{
case OP_NEG:
if (operand_type == VAR_INT)
{
int *result = malloc(sizeof(int));
*result = -(*(int *)operand_value);
return result;
}
else if (operand_type == VAR_SHORT)
{
short *result = malloc(sizeof(short));
*result = !(*(short *)operand_value);
return result;
}
else if (operand_type == VAR_FLOAT)
{
float *result = malloc(sizeof(float));
*result = -(*(float *)operand_value);
return result;
}
else if (operand_type == VAR_DOUBLE)
{
double *result = malloc(sizeof(double));
*result = -(*(double *)operand_value);
return result;
}
else if (operand_type == VAR_BOOL)
{
bool *result = malloc(sizeof(bool));
*result = !(*(bool *)operand_value);
return result;
}
else
{
yyerror("Invalid type for unary negation");
return NULL;
}
case OP_PRE_INC:
if (operand_type == VAR_INT)
{
int old_value = *(int *)operand_value;
set_int_variable(node->data.unary.operand->data.name, old_value + 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else if (operand_type == VAR_SHORT)
{
short old_value = *(short *)operand_value;
set_short_variable(node->data.unary.operand->data.name, old_value + 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else if (operand_type == VAR_FLOAT)
{
float old_value = *(float *)operand_value;
set_float_variable(node->data.unary.operand->data.name, old_value + 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else if (operand_type == VAR_DOUBLE)
{
double old_value = *(double *)operand_value;
set_double_variable(node->data.unary.operand->data.name, old_value + 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else
{
yyerror("Invalid type for pre-increment");
return NULL;
}
case OP_PRE_DEC:
if (operand_type == VAR_INT)
{
int old_value = *(int *)operand_value;
set_int_variable(node->data.unary.operand->data.name, old_value - 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else if (operand_type == VAR_SHORT)
{
short old_value = *(short *)operand_value;
set_short_variable(node->data.unary.operand->data.name, old_value - 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else if (operand_type == VAR_FLOAT)
{
float old_value = *(float *)operand_value;
set_float_variable(node->data.unary.operand->data.name, old_value - 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else if (operand_type == VAR_DOUBLE)
{
double old_value = *(double *)operand_value;
set_double_variable(node->data.unary.operand->data.name, old_value - 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else
{
yyerror("Invalid type for pre-decrement");
return NULL;
}
case OP_POST_INC:
if (operand_type == VAR_INT)
{
int old_value = *(int *)operand_value;
set_int_variable(node->data.unary.operand->data.name, old_value + 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else if (operand_type == VAR_SHORT)
{
short old_value = *(short *)operand_value;
set_short_variable(node->data.unary.operand->data.name, old_value + 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else if (operand_type == VAR_FLOAT)
{
float old_value = *(float *)operand_value;
set_float_variable(node->data.unary.operand->data.name, old_value + 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else if (operand_type == VAR_DOUBLE)
{
double old_value = *(double *)operand_value;
set_double_variable(node->data.unary.operand->data.name, old_value + 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else
{
yyerror("Invalid type for post-increment");
return NULL;
}
case OP_POST_DEC:
if (operand_type == VAR_INT)
{
int old_value = *(int *)operand_value;
set_int_variable(node->data.unary.operand->data.name, old_value - 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else if (operand_type == VAR_SHORT)
{
short old_value = *(short *)operand_value;
set_short_variable(node->data.unary.operand->data.name, old_value - 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else if (operand_type == VAR_FLOAT)
{
float old_value = *(float *)operand_value;
set_float_variable(node->data.unary.operand->data.name, old_value - 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else if (operand_type == VAR_DOUBLE)
{
double old_value = *(double *)operand_value;
set_double_variable(node->data.unary.operand->data.name, old_value - 1, get_variable_modifiers(node->data.unary.operand->data.name));
return operand_value;
}
else
{
yyerror("Invalid type for post-decrement");
return NULL;
}
default:
yyerror("Unknown unary operator");
return NULL;
}
}
float evaluate_expression_float(ASTNode *node)
{
if (!node)
return 0.0f;
switch (node->type)
{
case NODE_FLOAT:
return node->data.fvalue;
case NODE_DOUBLE:
return (float)node->data.dvalue;
case NODE_INT:
return (float)node->data.ivalue;
case NODE_IDENTIFIER:
{
return *(float *)handle_identifier(node, "Undefined variable", 1);
}
case NODE_OPERATION:
{
int result_type = get_expression_type(node);
void *result = handle_binary_operation(node, result_type);
float result_float = 0.0f;
result_float = (result_type == VAR_INT)
? (float)(*(int *)result)
: (result_type == VAR_FLOAT)
? *(float *)result
: (float)(*(double *)result);
free(result);
return result_float;
}
case NODE_UNARY_OPERATION:
{
float operand = evaluate_expression_float(node->data.unary.operand);
float *result = (float *)handle_unary_expression(node, &operand, VAR_FLOAT);
return *result;
}
default:
yyerror("Invalid float expression");
return 0.0f;
}
}
double evaluate_expression_double(ASTNode *node)
{
if (!node)
return 0.0L;
switch (node->type)
{
case NODE_DOUBLE:
return node->data.dvalue;
case NODE_FLOAT:
return (double)node->data.fvalue;
case NODE_INT:
return (double)node->data.ivalue;
case NODE_IDENTIFIER:
{
return *(double *)handle_identifier(node, "Undefined variable", 1);
}
case NODE_OPERATION:
{
int result_type = get_expression_type(node);
void *result = handle_binary_operation(node, result_type);
double result_double = 0.0L;
result_double = (result_type == VAR_INT)
? (double)(*(int *)result)
: (result_type == VAR_FLOAT)
? (double)(*(float *)result)
: *(double *)result;
free(result);
return result_double;
}
case NODE_UNARY_OPERATION:
{
double operand = evaluate_expression_double(node->data.unary.operand);
double *result = (double *)handle_unary_expression(node, &operand, VAR_DOUBLE);
return *result;
}
default:
yyerror("Invalid double expression");
return 0.0L;
}
}
short evaluate_expression_short(ASTNode *node)
{
if (!node)
return 0;
switch (node->type)
{
case NODE_INT:
return (short)node->data.ivalue;
case NODE_BOOLEAN:
return (short)node->data.bvalue; // Already 1 or 0
case NODE_CHAR: // Add explicit handling for characters
return (short)node->data.ivalue;
case NODE_SHORT:
return node->data.svalue;
case NODE_FLOAT:
yyerror("Cannot use float in integer context");
return (short)node->data.fvalue;
case NODE_DOUBLE:
yyerror("Cannot use double in integer context");
return (short)node->data.dvalue;