-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathinterpreter.c
More file actions
891 lines (788 loc) · 29.6 KB
/
Copy pathinterpreter.c
File metadata and controls
891 lines (788 loc) · 29.6 KB
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
/* interpreter.c - Interpreter visitor implementation */
#include "interpreter.h"
#include "ast.h"
#include "stdrot.h"
#include "lib/mem.h"
#include <stdio.h>
extern void yyerror(const char *s);
extern String safe_strdup(const String *str);
extern void execute_func_call(const String func_name, ArgumentList *args);
/* External functions we need from the original implementation */
extern Variable *variable_new(String name);
extern void add_variable_to_scope(const String name, Variable *var);
extern Variable *get_variable(const String name);
extern Function *get_function(const String name);
extern bool is_builtin_function(const String name);
extern void execute_builtin_function(const String name, ArgumentList *args);
extern void execute_assignment(ASTNode *node);
extern void execute_for_statement(ASTNode *node);
extern void execute_while_statement(ASTNode *node);
extern void execute_do_while_statement(ASTNode *node);
extern void execute_switch_statement(ASTNode *node);
extern void handle_return_statement(ASTNode *expr);
extern Function *create_function(String name, VarType return_type,
Parameter *params, ASTNode *body);
extern void bruh(void);
/* For now, use the original evaluation system with visitor wrappers */
extern int evaluate_expression_int(ASTNode *node);
extern float evaluate_expression_float(ASTNode *node);
extern double evaluate_expression_double(ASTNode *node);
extern short evaluate_expression_short(ASTNode *node);
extern bool evaluate_expression_bool(ASTNode *node);
extern String evaluate_expression_string(ASTNode *node);
extern void *evaluate_multi_array_access(ASTNode *node);
extern void *handle_function_call(ASTNode *node);
extern size_t handle_sizeof(ASTNode *node);
/* Global pointer to current interpreter for function calls */
Interpreter *current_interpreter = NULL;
/* Create a new interpreter */
Interpreter *interpreter_new(void)
{
Interpreter *interp = SAFE_MALLOC(Interpreter);
if (!interp)
{
yyerror("Failed to allocate memory for interpreter");
return NULL;
}
/* Initialize visitor function pointers for expressions */
interp->base.visit_int_literal = interpreter_visit_int_literal;
interp->base.visit_float_literal = interpreter_visit_float_literal;
interp->base.visit_double_literal = interpreter_visit_double_literal;
interp->base.visit_char_literal = interpreter_visit_char_literal;
interp->base.visit_short_literal = interpreter_visit_short_literal;
interp->base.visit_boolean_literal = interpreter_visit_boolean_literal;
interp->base.visit_string_literal = interpreter_visit_string_literal;
interp->base.visit_identifier = interpreter_visit_identifier;
interp->base.visit_binary_operation = interpreter_visit_binary_operation;
interp->base.visit_unary_operation = interpreter_visit_unary_operation;
interp->base.visit_array_access = interpreter_visit_array_access;
interp->base.visit_function_call = interpreter_visit_function_call;
interp->base.visit_sizeof = interpreter_visit_sizeof;
/* Initialize visitor function pointers for statements */
interp->base.visit_declaration = interpreter_visit_declaration;
interp->base.visit_assignment = interpreter_visit_assignment;
interp->base.visit_if_statement = interpreter_visit_if_statement;
interp->base.visit_for_statement = interpreter_visit_for_statement;
interp->base.visit_while_statement = interpreter_visit_while_statement;
interp->base.visit_do_while_statement =
interpreter_visit_do_while_statement;
interp->base.visit_switch_statement = interpreter_visit_switch_statement;
interp->base.visit_break_statement = interpreter_visit_break_statement;
interp->base.visit_return_statement = interpreter_visit_return_statement;
interp->base.visit_function_definition =
interpreter_visit_function_definition;
interp->base.visit_statement_list = interpreter_visit_statement_list;
interp->base.visit_print_statement = interpreter_visit_print_statement;
interp->base.visit_error_statement = interpreter_visit_error_statement;
/* Initialize interpreter state */
interp->current_scope = current_scope;
interp->return_value.has_value = false;
interp->should_break = false;
interp->should_return = false;
return interp;
}
/* Free interpreter */
void interpreter_free(Interpreter *interp)
{
if (interp)
{
SAFE_FREE(interp);
}
}
/* Main interpretation function */
void interpret(ASTNode *root, Interpreter *interp)
{
if (!root || !interp)
return;
extern Scope *current_scope;
/* Set global interpreter pointer for function calls */
current_interpreter = interp;
/* Ensure there's a global scope for the visitor pattern */
if (!current_scope)
{
extern void enter_scope();
enter_scope();
}
/* Execute the AST using visitor pattern */
ast_accept(root, (Visitor *)interp);
/* Clear global interpreter pointer */
current_interpreter = NULL;
}
/* Expression visitor implementations */
void *interpreter_visit_int_literal(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return NULL;
/* For the interpreter visitor, we don't need to return allocated memory
* since we're using this for side effects, not expression evaluation */
return NULL;
}
void *interpreter_visit_float_literal(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return NULL;
/* For the interpreter visitor, we don't need to return allocated memory */
return NULL;
}
void *interpreter_visit_double_literal(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return NULL;
/* For the interpreter visitor, we don't need to return allocated memory */
return NULL;
}
void *interpreter_visit_char_literal(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return NULL;
/* For the interpreter visitor, we don't need to return allocated memory */
return NULL;
}
void *interpreter_visit_short_literal(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return NULL;
/* For the interpreter visitor, we don't need to return allocated memory */
return NULL;
}
void *interpreter_visit_boolean_literal(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return NULL;
/* For the interpreter visitor, we don't need to return allocated memory */
return NULL;
}
void *interpreter_visit_string_literal(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return NULL;
/* For the interpreter visitor, we don't need to return allocated memory */
return NULL;
}
void *interpreter_visit_identifier(Visitor *self, ASTNode *node)
{
(void)self;
if (!node || !node->data.name.data)
return NULL;
Variable *var = get_variable(node->data.name);
if (!var)
{
/* Not a variable -- an unscoped enum constant (e.g. bare `GREEN`
on the right-hand side of `favorite = GREEN;`) is expected here,
not an error; the actual value is resolved separately by
whichever evaluate_expression_* call handles this node. */
if (find_global_enum_constant(node->data.name) != NULL)
return NULL;
yyerror("Undefined variable");
return NULL;
}
/* For interpreter visitor, we don't need to return allocated memory
* since this is used for side effects, not expression evaluation */
return NULL;
}
void *interpreter_visit_binary_operation(Visitor *self, ASTNode *node)
{
(void)self;
if (!node || !node->data.op.left || !node->data.op.right)
return NULL;
/* For interpreter visitor, we don't need to return allocated memory
* since this is used for side effects, not expression evaluation */
return NULL;
}
void *interpreter_visit_unary_operation(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return NULL;
if (node->data.unary.op == OP_PRE_INC ||
node->data.unary.op == OP_PRE_DEC ||
node->data.unary.op == OP_POST_INC ||
node->data.unary.op == OP_POST_DEC)
{
evaluate_expression_int(node);
}
return NULL;
}
void *interpreter_visit_array_access(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return NULL;
/* WORKAROUND: If num_dimensions is 0 but we know this is array access,
* attempt recovery */
if (node->data.array.num_dimensions == 0)
{
/* Get the variable to determine expected dimensions */
Variable *var = get_variable(node->data.array.name);
if (!var || !var->is_array)
{
return NULL;
}
/* For now, assume single dimension access and try to find the index
* expression */
/* Create a temporary fixed node structure */
ASTNode temp_node = *node;
temp_node.data.array.num_dimensions = 1;
/* Try to recover the index expression from the old single-index field
*/
if (node->data.array.index)
{
temp_node.data.array.indices[0] = node->data.array.index;
}
else
{
/* Create a dummy index of 0 - this is the fallback */
ASTNode *zero_node = create_int_node(0);
temp_node.data.array.indices[0] = zero_node;
}
return evaluate_multi_array_access(&temp_node);
}
/* Use the existing array access implementation */
return evaluate_multi_array_access(node);
}
/* Executes a function call that is genuinely a bare statement -- the one
unambiguous point where a call's result is intentionally discarded and
the deprecated native write-back convention should still apply. Used
directly (bypassing ast_accept()/interpreter_visit_function_call()) at
every site where a bare NODE_FUNC_CALL has no other visitor coming along
to evaluate it for real: statement-list entries, and a for-loop's own
init/increment clause. See interpreter_visit_statement_list() and
interpreter_visit_for_statement(), the two callers. */
static void interpreter_execute_call_statement(ASTNode *node)
{
if (!node)
return;
const String func_name = node->data.func_call.function_name;
ArgumentList *args = node->data.func_call.arguments;
if (is_builtin_function(func_name))
{
execute_builtin_function(func_name, args);
}
else
{
/* Handle user-defined functions directly without return value
* allocation */
execute_function_call(func_name, args);
/* A call in statement position discards its result -- if that
* result was a struct, free the blob handle_return_statement
* allocated for it rather than leaving it to a later call's
* cleanup (or leaking it, if this was the last call). */
free_pending_struct_return_value();
}
}
/* ast_accept()'s generic pre-visit runs this on a NODE_FUNC_CALL reached as
part of a declaration/assignment/return/print/error statement's
right-hand expression, or (via visitor.c's NODE_DO_WHILE_STATEMENT case)
a do-while condition -- in every one of those cases, the statement's own
dedicated visitor (interpreter_visit_declaration et al., or this
interpreter's own per-iteration evaluate_expression_int() condition
check) is about to evaluate this exact node for real via
evaluate_expression_* / handle_function_call. That pre-visit exists so
shared visitors (e.g. the semantic analyzer, validating the call exists)
get a chance to look at it; it is not itself a place where executing the
call is correct. Doing so anyway is actively wrong, not just redundant:
for a self-referential declaration like `rizz n = slorp(n);`, the
pre-visit runs before interpreter_visit_declaration has created `n`, so
the argument silently evaluates to nothing and the (wrong) result gets
cached; for a do-while condition, the pre-visit runs before the loop
body has executed even once, so the first real check reads a stale
pre-loop value instead of re-evaluating. Do nothing here and let the
downstream evaluate_expression_*() call populate the memo cache itself,
at the right time. (Bare statement-position and for-loop init/incr
calls never reach here at all -- see interpreter_execute_call_statement()
above.) User-defined functions have no such cache and are still invoked
from both places -- a pre-existing gap, not introduced here, tracked
separately from native-call support. */
void *interpreter_visit_function_call(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return NULL;
if (!is_builtin_function(node->data.func_call.function_name))
{
execute_function_call(node->data.func_call.function_name,
node->data.func_call.arguments);
free_pending_struct_return_value();
}
return NULL;
}
/* ast_accept(), but a bare NODE_FUNC_CALL is executed directly via
interpreter_execute_call_statement() instead of going through
interpreter_visit_function_call()'s pre-visit no-op. Use this at any site
where a bare call has no other visitor coming along afterward to
evaluate it for real -- currently statement-list entries and a for
loop's own init/increment clause. A declaration/assignment (etc.)
wrapping a call is unaffected: it still goes through ast_accept()
normally, since interpreter_visit_declaration() et al. *are* that real
evaluation. */
static void interpreter_accept_or_execute_call(ASTNode *node, Visitor *self)
{
if (!node)
return;
if (node->type == NODE_FUNC_CALL)
interpreter_execute_call_statement(node);
else
ast_accept(node, self);
}
void *interpreter_visit_sizeof(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return NULL;
/* For the interpreter visitor, we don't need to return allocated memory
* The sizeof operation can be handled by the existing evaluation system */
handle_sizeof(node);
return NULL;
}
/* Statement visitor implementations */
void interpreter_visit_declaration(Visitor *self, ASTNode *node)
{
(void)self;
if (!node || !node->data.op.left || !node->data.op.left->data.name.data)
return;
String name = node->data.op.left->data.name;
/* Array declarations: create + populate storage here, at runtime, in
* whatever scope is current for this execution -- a function's own
* scope when this statement is inside a function body, or the shared
* global scope at top level. Doing this at runtime (instead of once at
* parse time, into whatever scope happened to be active while parsing)
* is what gives each call its own array instance and makes a
* function-local array visible inside the function that declares it. */
if (node->is_array && node->var_type != VAR_STRUCT)
{
if (node->modifiers.is_static && get_variable(name))
return;
Variable *var = variable_new(name);
var->pointer_level = node->pointer_level;
var->modifiers = node->modifiers;
add_variable_to_scope(name, var);
SAFE_FREE(var);
int dims[MAX_DIMENSIONS];
int num_dims = node->array_dimensions.num_dimensions;
for (int i = 0; i < num_dims; i++)
dims[i] = node->array_dimensions.dimensions[i];
if (!set_multi_array_variable(name, dims, num_dims, node->modifiers,
node->var_type))
{
yyerror("Failed to create array");
return;
}
if (node->pending_initializer)
{
populate_multi_array_variable(name, node->pending_initializer, dims,
num_dims);
}
return;
}
/* Struct/union declarations: same reasoning as arrays above -- create
* the Variable and its data blob here, at runtime, in the current
* scope, instead of at parse time. */
if (node->var_type == VAR_STRUCT ||
(node->data.op.right && node->data.op.right->type == NODE_STRUCT_DEF))
{
const String struct_type =
node->data.op.right ? node->data.op.right->data.struct_def.name
: (String){.data = NULL, .len = 0};
if (!struct_type.data)
return;
if (node->modifiers.is_static && get_variable(name))
return;
Variable *var = variable_new(name);
var->var_type = VAR_STRUCT;
var->pointer_level = node->pointer_level;
var->modifiers = node->modifiers;
var->struct_name = safe_strdup(&struct_type);
add_variable_to_scope(name, var);
SAFE_FREE(var);
Variable *sv = get_variable(name);
StructDef *def = get_struct_def(struct_type);
if (sv && def && !sv->value.array_data)
{
sv->value.array_data = calloc(1, def->total_size);
if (node->pending_initializer)
populate_struct_variable(name, node->pending_initializer);
else if (node->struct_init_expr)
{
ASTNode *src_expr = node->struct_init_expr;
if (src_expr->type == NODE_FUNC_CALL)
{
execute_function_call(
src_expr->data.func_call.function_name,
src_expr->data.func_call.arguments);
if (current_return_value.has_value &&
current_return_value.type == VAR_STRUCT)
{
void *blob = (void *)current_return_value.value.pvalue;
/* Guard against copying a differently-shaped struct
into this blob (e.g. `gang Big b = make_small();`)
-- struct_name identifies the *declared* return
type, which the semantic analyzer should already
have rejected if it mismatches struct_type, but
this is the last line of defense against an
out-of-bounds memcpy. */
if (blob && sv->value.array_data &&
current_return_value.struct_name.data &&
strcmp(current_return_value.struct_name.data,
struct_type.data) == 0)
{
memcpy(sv->value.array_data, blob, def->total_size);
}
else if (blob)
{
yyerror("Struct return type does not match "
"declared type");
}
/* Ownership transfers to us on return; always free
our copy of the temporary, whether or not the
type check above allowed the memcpy. */
free_pending_struct_return_value();
}
}
else if (src_expr->type == NODE_IDENTIFIER)
{
Variable *src = get_variable(src_expr->data.name);
if (src && src->var_type == VAR_STRUCT &&
src->value.array_data && sv->value.array_data &&
src->struct_name.data &&
strcmp(src->struct_name.data, struct_type.data) == 0)
{
memcpy(sv->value.array_data, src->value.array_data,
def->total_size);
}
else if (src && src->var_type == VAR_STRUCT)
{
yyerror("Cannot copy-initialize from a struct "
"variable of a different type");
}
}
}
}
return;
}
Variable *var = variable_new(name);
var->modifiers = node->modifiers;
var->var_type = node->var_type;
var->pointer_level = node->pointer_level;
/* If static and already exists in static map, skip entirely */
if (node->modifiers.is_static)
{
Variable *existing = get_variable(name);
if (existing)
{
SAFE_FREE(var);
return;
}
}
/* Must come after the static-already-exists check above: that early
return frees only the wrapper Variable, not enum_name's heap string. */
if (node->var_type == VAR_ENUM)
var->enum_name = safe_strdup(&node->enum_name);
/* Detect struct declaration: right node is a NODE_STRUCT_DEF */
if (node->data.op.right && node->data.op.right->type == NODE_STRUCT_DEF)
{
var->var_type = VAR_STRUCT;
var->struct_name =
safe_strdup(&node->data.op.right->data.struct_def.name);
}
add_variable_to_scope(name, var);
SAFE_FREE(var);
/* Handle initialization */
if (node->data.op.right)
{
Variable *scope_var = get_variable(name);
if (scope_var && scope_var->var_type == VAR_STRUCT)
{
if (!scope_var->value.array_data)
{
StructDef *def = get_struct_def(scope_var->struct_name);
if (def)
{
scope_var->value.array_data = calloc(1, def->total_size);
hm_put(current_scope->variables, name.data, name.len,
scope_var, sizeof(Variable));
}
}
return;
}
if (scope_var)
{
if (scope_var->pointer_level > 0)
{
scope_var->value.pvalue =
evaluate_expression_pointer(node->data.op.right);
return;
}
switch (scope_var->var_type)
{
case VAR_INT:
{
int int_value = evaluate_expression_int(node->data.op.right);
scope_var->value.ivalue = int_value;
break;
}
case VAR_FLOAT:
{
float float_value =
evaluate_expression_float(node->data.op.right);
scope_var->value.fvalue = float_value;
break;
}
case VAR_DOUBLE:
{
double double_value =
evaluate_expression_double(node->data.op.right);
scope_var->value.dvalue = double_value;
break;
}
case VAR_CHAR:
{
int int_value = evaluate_expression_int(node->data.op.right);
scope_var->value.ivalue = int_value;
break;
}
case VAR_SHORT:
{
short short_value =
evaluate_expression_short(node->data.op.right);
scope_var->value.svalue = short_value;
break;
}
case VAR_BOOL:
{
bool bool_value = evaluate_expression_bool(node->data.op.right);
scope_var->value.bvalue = bool_value;
break;
}
case VAR_STRING:
{
String string_value =
evaluate_expression_string(node->data.op.right);
scope_var->value.strvalue = string_value;
break;
}
case VAR_ENUM:
{
int int_value = evaluate_expression_int(node->data.op.right);
scope_var->value.ivalue = int_value;
break;
}
default:
break;
}
}
}
}
void interpreter_visit_assignment(Visitor *self, ASTNode *node)
{
(void)self;
if (!node || !node->data.op.left || !node->data.op.right)
return;
execute_assignment(node);
}
void interpreter_visit_if_statement(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return;
int condition = evaluate_expression_int(node->data.if_stmt.condition);
if (condition)
{
if (node->data.if_stmt.then_branch)
{
ast_accept(node->data.if_stmt.then_branch, (Visitor *)self);
}
}
else if (node->data.if_stmt.else_branch)
{
ast_accept(node->data.if_stmt.else_branch, (Visitor *)self);
}
}
void interpreter_visit_for_statement(Visitor *self, ASTNode *node)
{
if (!node)
return;
extern void enter_scope();
extern void exit_scope();
PUSH_JUMP_BUFFER();
if (setjmp(CURRENT_JUMP_BUFFER()) == 0)
{
enter_scope();
if (node->data.for_stmt.init)
{
interpreter_accept_or_execute_call(node->data.for_stmt.init, self);
}
while (1)
{
enter_scope();
if (node->data.for_stmt.cond)
{
int cond_result =
evaluate_expression_int(node->data.for_stmt.cond);
if (!cond_result)
{
exit_scope();
break;
}
}
if (node->data.for_stmt.body)
{
ast_accept(node->data.for_stmt.body, self);
}
if (node->data.for_stmt.incr)
{
interpreter_accept_or_execute_call(node->data.for_stmt.incr,
self);
}
exit_scope();
}
exit_scope();
}
POP_JUMP_BUFFER();
}
void interpreter_visit_while_statement(Visitor *self, ASTNode *node)
{
if (!node)
return;
extern void enter_scope();
extern void exit_scope();
PUSH_JUMP_BUFFER();
enter_scope();
while (evaluate_expression_int(node->data.while_stmt.cond) &&
setjmp(CURRENT_JUMP_BUFFER()) == 0)
{
enter_scope();
if (node->data.while_stmt.body)
{
ast_accept(node->data.while_stmt.body, self);
}
exit_scope();
}
exit_scope();
POP_JUMP_BUFFER();
}
void interpreter_visit_do_while_statement(Visitor *self, ASTNode *node)
{
if (!node)
return;
/* Add external function declarations */
extern void enter_scope();
extern void exit_scope();
/* Use setjmp/longjmp for break handling like the old code */
PUSH_JUMP_BUFFER();
enter_scope();
do
{
/* Enter new scope for each iteration */
enter_scope();
/* Execute the body using the visitor */
if (node->data.while_stmt.body)
{
ast_accept(node->data.while_stmt.body, self);
}
/* Exit scope before checking condition */
exit_scope();
} while (evaluate_expression_int(node->data.while_stmt.cond) &&
setjmp(CURRENT_JUMP_BUFFER()) == 0);
exit_scope();
POP_JUMP_BUFFER();
}
void interpreter_visit_switch_statement(Visitor *self, ASTNode *node)
{
(void)self;
execute_switch_statement(node);
}
void interpreter_visit_break_statement(Visitor *self, ASTNode *node)
{
(void)node;
(void)self;
/* Use bruh() which calls LONGJMP() to break out of the current loop */
bruh();
}
void interpreter_visit_return_statement(Visitor *self, ASTNode *node)
{
(void)self;
if (node && node->data.op.left)
{
handle_return_statement(node->data.op.left);
}
else
{
handle_return_statement(NULL);
}
}
void interpreter_visit_function_definition(Visitor *self, ASTNode *node)
{
(void)self;
if (!node)
return;
if (node->data.function_def.return_type == VAR_STRUCT &&
node->pointer_level > 0)
{
/* Pointer-to-struct returns are rejected at parse time (see
create_function_def_node_struct) and deliberately left
unregistered there. create_function()/create_function_ex()
below have no knowledge of that rejection and would happily
register the function anyway -- the unconditional call a few
lines down registers it with return_pointer_level 0 regardless,
and the pointer_level > 0 branch after it would then "fix" that
up to the real pointer_level, undoing the rejection and letting
the function run with a silently-wrong by-value return. Skip
registration entirely so get_function() stays NULL and any call
site reports a clear "Undefined function" instead. */
return;
}
Function *func = create_function(
node->data.function_def.name, node->data.function_def.return_type,
node->data.function_def.parameters, node->data.function_def.body);
if (node->pointer_level > 0)
{
func = create_function_ex(
node->data.function_def.name, node->data.function_def.return_type,
node->pointer_level, node->data.function_def.parameters,
node->data.function_def.body);
}
if (!func)
{
yyerror("Failed to create function");
exit(1);
}
}
void interpreter_visit_statement_list(Visitor *self, ASTNode *node)
{
if (!node)
return;
/* Manually traverse all statements in the list */
StatementList *stmt = node->data.statements;
while (stmt)
{
if (stmt->statement)
interpreter_accept_or_execute_call(stmt->statement, self);
stmt = stmt->next;
}
}
void interpreter_visit_print_statement(Visitor *self, ASTNode *node)
{
(void)self;
if (!node || !node->data.op.left)
return;
ASTNode *expr = node->data.op.left;
ArgumentList args = {expr, NULL};
execute_func_call((String){.data = "yapping", .len = sizeof("yapping")},
&args);
}
void interpreter_visit_error_statement(Visitor *self, ASTNode *node)
{
(void)self;
if (!node || !node->data.op.left)
return;
ASTNode *expr = node->data.op.left;
ArgumentList args = {expr, NULL};
execute_func_call((String){.data = "baka", .len = sizeof("baka")}, &args);
}