aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-01 17:59:47 -0500
committerMustafa Quraish <[email protected]>2022-02-01 17:59:47 -0500
commitabb5fd19ca12f51b6a0298d5fe8d019bd12445af (patch)
treecf6d9ff6018751e7bb4d796b0541f4dd30dec489 /src
parentAdd basic `defer` implementation. (diff)
downloadcup-abb5fd19ca12f51b6a0298d5fe8d019bd12445af.tar.xz
cup-abb5fd19ca12f51b6a0298d5fe8d019bd12445af.zip
Defer: Pop elements off the defer-stack when returning
We restore the count later, but this fix makes it so that the compiler doesn't get stuck in an infinite loop when you try to compile the following code: ``` fn main(): int { defer return 5; return 1; } ```
Diffstat (limited to 'src')
-rw-r--r--src/generator.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/src/generator.c b/src/generator.c
index 403bf4a..7de9d32 100644
--- a/src/generator.c
+++ b/src/generator.c
@@ -251,8 +251,10 @@ void generate_statement(Node *stmt, FILE *out)
fprintf(out, " push rax\n"); // Save the return value
// Run all the defer statements
- for (int i = defer_stack_count - 1; i >= 0; i--)
- generate_statement(defer_stack[i], out);
+ i64 old_count = defer_stack_count;
+ while (defer_stack_count > 0)
+ generate_statement(defer_stack[--defer_stack_count], out);
+ defer_stack_count = old_count;
// TODO: Only do this if we have local variables
fprintf(out, " pop rax\n");