Skip to content

Commit ecc3ea7

Browse files
Enhance return statement with loop state handling
Added loop state management in return statement.
1 parent 5d5ab8d commit ecc3ea7

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/basic/flow_control.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ void gosub_statement(struct basic_ctx* ctx)
199199

200200
if (ctx->call_stack_ptr < MAX_CALL_STACK_DEPTH) {
201201
ctx->call_stack[ctx->call_stack_ptr] = tokenizer_num(ctx, NUMBER);
202+
ctx->loop_state_stack[ctx->call_stack_ptr].for_stack_ptr = ctx->for_stack_ptr;
203+
ctx->loop_state_stack[ctx->call_stack_ptr].while_stack_ptr = ctx->while_stack_ptr;
204+
ctx->loop_state_stack[ctx->call_stack_ptr].repeat_stack_ptr = ctx->repeat_stack_ptr;
205+
202206
if (!new_stack_frame(ctx)) {
203207
return;
204208
}
@@ -212,9 +216,19 @@ void gosub_statement(struct basic_ctx* ctx)
212216
void return_statement(struct basic_ctx* ctx)
213217
{
214218
accept_or_return(RETURN, ctx);
219+
215220
if (ctx->call_stack_ptr > 0) {
216221
free_local_heap(ctx);
217222
pop_stack_frame(ctx);
223+
224+
while (ctx->for_stack_ptr > ctx->loop_state_stack[ctx->call_stack_ptr].for_stack_ptr) {
225+
ctx->for_stack_ptr--;
226+
buddy_free(ctx->allocator, ctx->for_stack[ctx->for_stack_ptr].for_variable);
227+
}
228+
229+
ctx->while_stack_ptr = ctx->loop_state_stack[ctx->call_stack_ptr].while_stack_ptr;
230+
ctx->repeat_stack_ptr = ctx->loop_state_stack[ctx->call_stack_ptr].repeat_stack_ptr;
231+
218232
jump_linenum(ctx->call_stack[ctx->call_stack_ptr], ctx);
219233
} else {
220234
tokenizer_error_print(ctx, "RETURN without GOSUB");

0 commit comments

Comments
 (0)