-
Notifications
You must be signed in to change notification settings - Fork 22
/
ast.c
1007 lines (927 loc) · 25.5 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 <setjmp.h>
#include <string.h>
static jmp_buf break_env;
TypeModifiers current_modifiers = {false, false, false, false, false};
variable symbol_table[MAX_VARS];
int var_count = 0;
// Symbol table functions
bool set_int_variable(char *name, int value, TypeModifiers mods)
{
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, name) == 0)
{
symbol_table[i].is_float = false;
symbol_table[i].value.ivalue = value;
symbol_table[i].modifiers = mods;
return true;
}
}
if (var_count < MAX_VARS)
{
symbol_table[var_count].name = strdup(name);
symbol_table[var_count].is_float = false;
symbol_table[var_count].value.ivalue = value;
symbol_table[var_count].modifiers = mods;
var_count++;
return true;
}
return false;
}
bool set_float_variable(char *name, float value, TypeModifiers mods)
{
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, name) == 0)
{
symbol_table[i].is_float = true;
symbol_table[i].value.fvalue = value;
symbol_table[i].modifiers = mods;
return true;
}
}
if (var_count < MAX_VARS)
{
symbol_table[var_count].name = strdup(name);
symbol_table[var_count].is_float = true;
symbol_table[var_count].value.fvalue = value;
symbol_table[var_count].modifiers = mods;
var_count++;
return true;
}
return false;
}
void reset_modifiers(void)
{
current_modifiers.is_volatile = false;
current_modifiers.is_signed = false;
current_modifiers.is_unsigned = false;
}
TypeModifiers get_current_modifiers(void)
{
TypeModifiers mods = current_modifiers;
reset_modifiers(); // Reset for next declaration
return mods;
}
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;
if (setjmp(break_env) == 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
}
}
/* Include the symbol table functions */
extern bool set_variable(char *name, int value, TypeModifiers mod);
extern int get_variable(char *name);
extern void yyerror(const char *s);
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);
/* Function implementations */
ASTNode *create_number_node(int value)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_NUMBER;
node->data.value = value;
node->modifiers.is_unsigned = current_modifiers.is_unsigned;
return node;
}
ASTNode *create_float_node(float value)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_FLOAT;
node->data.fvalue = value;
return node;
}
float evaluate_expression_float(ASTNode *node)
{
if (!node)
return 0.0f;
switch (node->type)
{
case NODE_FLOAT:
return node->data.fvalue;
case NODE_NUMBER:
return (float)node->data.value;
case NODE_IDENTIFIER:
{
char *name = node->data.name;
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, name) == 0)
{
return symbol_table[i].is_float ? symbol_table[i].value.fvalue : (float)symbol_table[i].value.ivalue;
}
}
yyerror("Undefined variable");
return 0.0f;
}
case NODE_OPERATION:
{
float left = evaluate_expression_float(node->data.op.left);
float right = evaluate_expression_float(node->data.op.right);
switch (node->data.op.op)
{
case OP_PLUS:
return left + right;
case OP_MINUS:
return left - right;
case OP_TIMES:
return left * right;
case OP_DIVIDE:
if (right == 0.0f)
{
yyerror("Division by zero");
return 0.0f;
}
return left / right;
case OP_LT:
return left < right ? 1.0f : 0.0f;
case OP_GT:
return left > right ? 1.0f : 0.0f;
case OP_LE:
return left <= right ? 1.0f : 0.0f;
case OP_GE:
return left >= right ? 1.0f : 0.0f;
case OP_EQ:
return left == right ? 1.0f : 0.0f;
case OP_NE:
return left != right ? 1.0f : 0.0f;
default:
yyerror("Invalid operator for float operation");
return 0.0f;
}
}
case NODE_UNARY_OPERATION:
{
float operand = evaluate_expression_float(node->data.unary.operand);
switch (node->data.unary.op)
{
case OP_NEG:
return -operand;
default:
yyerror("Unknown unary operator for float");
return 0.0f;
}
}
default:
yyerror("Invalid float expression");
return 0.0f;
}
}
int evaluate_expression_int(ASTNode *node)
{
if (!node)
return 0;
switch (node->type)
{
case NODE_NUMBER:
return node->data.value;
case NODE_BOOLEAN:
return node->data.value; // Already 1 or 0
case NODE_CHAR: // Add explicit handling for characters
return node->data.value;
case NODE_FLOAT:
yyerror("Cannot use float in integer context");
return (int)node->data.fvalue;
case NODE_SIZEOF:
{
char *name = node->data.name;
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, name) == 0)
{
if (symbol_table[i].is_float)
{
return sizeof(float);
}
else if (symbol_table[i].modifiers.is_unsigned)
{
return sizeof(unsigned int);
}
else if (symbol_table[i].modifiers.is_boolean)
{
return sizeof(bool);
}
else
{
return sizeof(int);
}
}
}
yyerror("Undefined variable in sizeof");
return 0;
}
case NODE_IDENTIFIER:
{
char *name = node->data.name;
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, name) == 0)
{
if (symbol_table[i].is_float)
{
yyerror("Cannot use float variable in integer context");
return (int)symbol_table[i].value.fvalue;
}
return symbol_table[i].value.ivalue;
}
}
yyerror("Undefined variable");
return 0;
}
case NODE_OPERATION:
{
// Special handling for logical operations
if (node->data.op.op == OP_AND || node->data.op.op == OP_OR)
{
int left = evaluate_expression_int(node->data.op.left);
int right = evaluate_expression_int(node->data.op.right);
switch (node->data.op.op)
{
case OP_AND:
return left && right;
case OP_OR:
return left || right;
default:
break;
}
}
// Regular integer operations
int left = evaluate_expression_int(node->data.op.left);
int right = evaluate_expression_int(node->data.op.right);
switch (node->data.op.op)
{
case OP_PLUS:
return left + right;
case OP_MINUS:
return left - right;
case OP_TIMES:
return left * right;
case OP_DIVIDE:
if (right == 0)
{
yyerror("Division by zero");
return 0;
}
return left / right;
case OP_MOD:
if (right == 0)
{
yyerror("Division by zero");
return 0;
}
// Explicitly handle unsigned modulo
if (node->modifiers.is_unsigned)
{
unsigned int ul = (unsigned int)left;
unsigned int ur = (unsigned int)right;
return ul % ur;
}
return left % right;
case OP_LT:
return left < right;
case OP_GT:
return left > right;
case OP_LE:
return left <= right;
case OP_GE:
return left >= right;
case OP_EQ:
return left == right;
case OP_NE:
return left != right;
default:
yyerror("Unknown operator");
return 0;
}
}
case NODE_UNARY_OPERATION:
{
int operand = evaluate_expression_int(node->data.unary.operand);
switch (node->data.unary.op)
{
case OP_NEG:
return -operand;
default:
yyerror("Unknown unary operator");
return 0;
}
}
default:
yyerror("Invalid integer expression");
return 0;
}
}
ASTNode *create_char_node(char value)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_CHAR;
node->data.value = value;
return node;
}
ASTNode *create_boolean_node(int value)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_BOOLEAN;
node->data.value = value ? 1 : 0;
node->modifiers.is_boolean = true;
return node;
}
ASTNode *create_sizeof_node(char *identifier)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_SIZEOF;
node->data.name = strdup(identifier);
return node;
}
ASTNode *create_identifier_node(char *name)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_IDENTIFIER;
node->data.name = strdup(name);
return node;
}
ASTNode *create_assignment_node(char *name, ASTNode *expr)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_ASSIGNMENT;
node->modifiers = get_current_modifiers();
if (expr->type == NODE_BOOLEAN)
{
node->modifiers.is_boolean = true;
}
node->data.op.left = create_identifier_node(name);
node->data.op.right = expr;
node->data.op.op = '=';
return node;
}
ASTNode *create_operation_node(OperatorType op, ASTNode *left, ASTNode *right)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_OPERATION;
node->data.op.left = left;
node->data.op.right = right;
node->data.op.op = op;
node->modifiers.is_unsigned = left->modifiers.is_unsigned || right->modifiers.is_unsigned;
node->modifiers.is_signed = false;
node->modifiers.is_volatile = left->modifiers.is_volatile || right->modifiers.is_volatile;
return node;
}
ASTNode *create_unary_operation_node(OperatorType op, ASTNode *operand)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_UNARY_OPERATION;
node->data.unary.operand = operand;
node->data.unary.op = op;
return node;
}
ASTNode *create_for_statement_node(ASTNode *init, ASTNode *cond, ASTNode *incr, ASTNode *body)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_FOR_STATEMENT;
node->data.for_stmt.init = init;
node->data.for_stmt.cond = cond;
node->data.for_stmt.incr = incr;
node->data.for_stmt.body = body;
return node;
}
ASTNode *create_while_statement_node(ASTNode *cond, ASTNode *body)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_WHILE_STATEMENT;
node->data.while_stmt.cond = cond;
node->data.while_stmt.body = body;
return node;
}
ASTNode *create_function_call_node(char *func_name, ArgumentList *args)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_FUNC_CALL;
node->data.func_call.function_name = strdup(func_name);
node->data.func_call.arguments = args;
return node;
}
ArgumentList *create_argument_list(ASTNode *expr, ArgumentList *existing_list)
{
ArgumentList *new_node = malloc(sizeof(ArgumentList));
new_node->expr = expr;
new_node->next = NULL;
if (!existing_list)
{
return new_node;
}
else
{
/* Append to the end of existing_list */
ArgumentList *temp = existing_list;
while (temp->next)
{
temp = temp->next;
}
temp->next = new_node;
return existing_list;
}
}
ASTNode *create_print_statement_node(ASTNode *expr)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_PRINT_STATEMENT;
node->data.op.left = expr;
return node;
}
ASTNode *create_error_statement_node(ASTNode *expr)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_ERROR_STATEMENT;
node->data.op.left = expr;
return node;
}
ASTNode *create_statement_list(ASTNode *statement, ASTNode *existing_list)
{
if (!existing_list)
{
// If there's no existing list, create a new one
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_STATEMENT_LIST;
node->data.statements = malloc(sizeof(StatementList));
node->data.statements->statement = statement;
node->data.statements->next = NULL;
return node;
}
else
{
// Append at the end of existing_list
StatementList *sl = existing_list->data.statements;
while (sl->next)
{
sl = sl->next;
}
// Now sl is the last element; append the new statement
StatementList *new_item = malloc(sizeof(StatementList));
new_item->statement = statement;
new_item->next = NULL;
sl->next = new_item;
return existing_list;
}
}
bool is_float_expression(ASTNode *node)
{
if (!node)
return false;
switch (node->type)
{
case NODE_FLOAT:
return true;
case NODE_NUMBER:
return false;
case NODE_IDENTIFIER:
{
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, node->data.name) == 0)
{
return symbol_table[i].is_float;
}
}
yyerror("Undefined variable in type check");
return false;
}
case NODE_OPERATION:
{
// If either operand is float, result is float
return is_float_expression(node->data.op.left) ||
is_float_expression(node->data.op.right);
}
default:
return false;
}
}
int evaluate_expression(ASTNode *node)
{
if (is_float_expression(node))
{
return (int)evaluate_expression_float(node);
}
return evaluate_expression_int(node);
}
void execute_assignment(ASTNode *node)
{
if (node->type != NODE_ASSIGNMENT)
{
yyerror("Expected assignment node");
return;
}
char *name = node->data.op.left->data.name;
ASTNode *value_node = node->data.op.right;
TypeModifiers mods = node->modifiers;
// Check if the right-hand side is a float expression
if (is_float_expression(value_node))
{
float value = evaluate_expression_float(value_node);
if (!set_float_variable(name, value, mods))
{
yyerror("Failed to set float variable");
}
}
else
{
int value = evaluate_expression_int(value_node);
if (!set_int_variable(name, value, mods))
{
yyerror("Failed to set integer variable");
}
}
}
void execute_statement(ASTNode *node)
{
if (!node)
return;
switch (node->type)
{
case NODE_ASSIGNMENT:
{
char *name = node->data.op.left->data.name;
ASTNode *value_node = node->data.op.right;
TypeModifiers mods = node->modifiers;
if (value_node->type == NODE_CHAR)
{
// Handle character assignments directly
if (!set_int_variable(name, value_node->data.value, mods))
{
yyerror("Failed to set character variable");
}
}
else if (is_float_expression(value_node))
{
float value = evaluate_expression_float(value_node);
if (!set_float_variable(name, value, mods))
{
yyerror("Failed to set float variable");
}
}
else
{
int value = evaluate_expression_int(value_node);
if (!set_int_variable(name, value, mods))
{
yyerror("Failed to set integer variable");
}
}
break;
}
case NODE_OPERATION:
case NODE_UNARY_OPERATION:
case NODE_NUMBER:
case NODE_CHAR:
case NODE_IDENTIFIER:
evaluate_expression(node);
break;
case NODE_FUNC_CALL:
if (strcmp(node->data.func_call.function_name, "yapping") == 0)
{
execute_yapping_call(node->data.func_call.arguments);
}
else if (strcmp(node->data.func_call.function_name, "yappin") == 0)
{
execute_yappin_call(node->data.func_call.arguments);
}
else if (strcmp(node->data.func_call.function_name, "baka") == 0)
{
execute_baka_call(node->data.func_call.arguments);
}
break;
case NODE_FOR_STATEMENT:
execute_for_statement(node);
break;
case NODE_WHILE_STATEMENT:
execute_while_statement(node);
break;
case NODE_PRINT_STATEMENT:
{
ASTNode *expr = node->data.op.left;
if (expr->type == NODE_STRING_LITERAL)
{
yapping("%s\n", expr->data.name);
}
else
{
int value = evaluate_expression(expr);
yapping("%d\n", value);
}
break;
}
case NODE_ERROR_STATEMENT:
{
ASTNode *expr = node->data.op.left;
if (expr->type == NODE_STRING_LITERAL)
{
baka("%s\n", expr->data.name);
}
else
{
int value = evaluate_expression(expr);
baka("%d\n", value);
}
break;
}
case NODE_STATEMENT_LIST:
execute_statements(node);
break;
case NODE_IF_STATEMENT:
if (evaluate_expression(node->data.if_stmt.condition))
{
execute_statement(node->data.if_stmt.then_branch);
}
else if (node->data.if_stmt.else_branch)
{
execute_statement(node->data.if_stmt.else_branch);
}
break;
case NODE_SWITCH_STATEMENT:
execute_switch_statement(node);
break;
case NODE_BREAK_STATEMENT:
// Signal to break out of the current loop/switch
longjmp(break_env, 1);
break;
default:
yyerror("Unknown statement type");
break;
}
}
void execute_statements(ASTNode *node)
{
if (!node)
return;
if (node->type != NODE_STATEMENT_LIST)
{
execute_statement(node);
return;
}
StatementList *current = node->data.statements;
while (current)
{
execute_statement(current->statement);
current = current->next;
}
}
void execute_for_statement(ASTNode *node)
{
// Execute initialization once
if (node->data.for_stmt.init)
{
execute_statement(node->data.for_stmt.init);
}
while (1)
{
// Evaluate condition
if (node->data.for_stmt.cond)
{
int cond_result = evaluate_expression(node->data.for_stmt.cond);
if (!cond_result)
{
break;
}
}
// Execute body
if (node->data.for_stmt.body)
{
execute_statement(node->data.for_stmt.body);
}
// Execute increment
if (node->data.for_stmt.incr)
{
execute_statement(node->data.for_stmt.incr);
}
}
}
void execute_while_statement(ASTNode *node)
{
while (evaluate_expression(node->data.while_stmt.cond))
{
execute_statement(node->data.while_stmt.body);
}
}
ASTNode *create_if_statement_node(ASTNode *condition, ASTNode *then_branch, ASTNode *else_branch)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_IF_STATEMENT;
node->data.if_stmt.condition = condition;
node->data.if_stmt.then_branch = then_branch;
node->data.if_stmt.else_branch = else_branch;
return node;
}
ASTNode *create_string_literal_node(char *string)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_STRING_LITERAL;
node->data.name = string;
return node;
}
ASTNode *create_switch_statement_node(ASTNode *expression, CaseNode *cases)
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_SWITCH_STATEMENT;
node->data.switch_stmt.expression = expression;
node->data.switch_stmt.cases = cases;
return node;
}
CaseNode *create_case_node(ASTNode *value, ASTNode *statements)
{
CaseNode *node = malloc(sizeof(CaseNode));
node->value = value;
node->statements = statements;
node->next = NULL;
return node;
}
CaseNode *create_default_case_node(ASTNode *statements)
{
return create_case_node(NULL, statements); // NULL value indicates default case
}
CaseNode *append_case_list(CaseNode *list, CaseNode *case_node)
{
if (!list)
return case_node;
CaseNode *current = list;
while (current->next)
current = current->next;
current->next = case_node;
return list;
}
ASTNode *create_break_node()
{
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_BREAK_STATEMENT;
node->data.break_stmt = NULL;
return node;
}
void execute_yapping_call(ArgumentList *args)
{
if (!args)
{
yapping("\n");
return;
}
ASTNode *formatNode = args->expr;
if (formatNode->type != NODE_STRING_LITERAL)
{
yyerror("First argument to yapping must be a string literal");
return;
}
ArgumentList *cur = args->next;
if (!cur)
{
yapping("%s", formatNode->data.name);
return;
}
ASTNode *expr = cur->expr;
// Handle float expressions
if (is_float_expression(expr))
{
float val = evaluate_expression_float(expr);
yapping(formatNode->data.name, val);
return;
}
// Check if we're dealing with an unsigned value
bool is_unsigned = false;
bool is_bool = false;
if (expr->type == NODE_BOOLEAN)
{
is_bool = true;
}
if (expr->type == NODE_IDENTIFIER)
{
TypeModifiers mods = get_variable_modifiers(expr->data.name);
is_unsigned = mods.is_unsigned;
is_bool = mods.is_boolean;
}
else
{
// Check if the node itself has unsigned modifier
is_unsigned = expr->modifiers.is_unsigned;
}
if (is_bool)
{
int val = evaluate_expression_int(expr);
// If format specifier is present, handle differently
if (strstr(formatNode->data.name, "%") != NULL)
{
yapping(formatNode->data.name, val);
}
else
{
yapping("%s", val ? "yes" : "no");
}
return;
}
if (is_unsigned)
{
unsigned int val = (unsigned int)evaluate_expression_int(expr);
if (strstr(formatNode->data.name, "%lu") != NULL)
{
yapping(formatNode->data.name, (unsigned long)val);
}
else if (strstr(formatNode->data.name, "%u") != NULL)
{
yapping(formatNode->data.name, val);
}
else
{
yapping("%u", val);
}
return;
}
// Handle regular integers
int val = evaluate_expression_int(expr);
yapping(formatNode->data.name, val);
}
void execute_yappin_call(ArgumentList *args)
{
if (!args)
{
yappin("\n");
return;
}
ASTNode *formatNode = args->expr;
if (formatNode->type != NODE_STRING_LITERAL)
{
yyerror("First argument to yappin must be a string literal");
return;
}
ArgumentList *cur = args->next;
if (!cur)
{
yappin("%s", formatNode->data.name);
return;
}
ASTNode *expr = cur->expr;
// Check if it's a boolean value
if (expr->type == NODE_BOOLEAN ||
(expr->type == NODE_IDENTIFIER && get_variable_modifiers(expr->data.name).is_boolean))
{
int val = evaluate_expression_int(expr);
if (strstr(formatNode->data.name, "%d") != NULL)
{
yappin(formatNode->data.name, val);
}
else
{
yappin(val ? "yes" : "no");
}
return;
}
// Handle float expressions
if (is_float_expression(expr))
{
float val = evaluate_expression_float(expr);
yappin(formatNode->data.name, val);
return;
}
// Handle integer expressions
int val = evaluate_expression_int(expr);
yappin(formatNode->data.name, val);
}
void execute_baka_call(ArgumentList *args)
{
if (!args)
{