diff options
| author | Mustafa Quraish <[email protected]> | 2022-02-01 17:59:47 -0500 |
|---|---|---|
| committer | Mustafa Quraish <[email protected]> | 2022-02-01 17:59:47 -0500 |
| commit | abb5fd19ca12f51b6a0298d5fe8d019bd12445af (patch) | |
| tree | cf6d9ff6018751e7bb4d796b0541f4dd30dec489 /src/generator.c | |
| parent | Add basic `defer` implementation. (diff) | |
| download | cup-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/generator.c')
| -rw-r--r-- | src/generator.c | 6 |
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"); |