diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-10-09 21:01:43 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-10-09 21:01:43 -0400 |
| commit | a2490f4a500fca4b84e5652d31fc621f9f20742b (patch) | |
| tree | e86060a5d71a2808675ef1e94609951484851225 /compile.c | |
| parent | b85f76f6c0a9efd66149348fb24aa81cd4d47663 (diff) | |
Clean up codegen for `when` statements
Diffstat (limited to 'compile.c')
| -rw-r--r-- | compile.c | 20 |
1 files changed, 18 insertions, 2 deletions
@@ -437,10 +437,26 @@ CORD compile_statement(env_t *env, ast_t *ast) field = field->next; } } - code = CORD_all(code, compile_statement(scope, clause->body), "\nbreak;\n}\n"); + if (clause->body->tag == Block) { + ast_list_t *statements = Match(clause->body, Block)->statements; + if (!statements || (statements->ast->tag == Pass && !statements->next)) + code = CORD_all(code, "break;\n}\n"); + else + code = CORD_all(code, compile_inline_block(scope, clause->body), "\nbreak;\n}\n"); + } else { + code = CORD_all(code, compile_statement(scope, clause->body), "\nbreak;\n}\n"); + } } if (when->else_body) { - code = CORD_all(code, "default: {\n", compile_statement(env, when->else_body), "\nbreak;\n}"); + if (when->else_body->tag == Block) { + ast_list_t *statements = Match(when->else_body, Block)->statements; + if (!statements || (statements->ast->tag == Pass && !statements->next)) + code = CORD_all(code, "default: break;"); + else + code = CORD_all(code, "default: {\n", compile_inline_block(env, when->else_body), "\nbreak;\n}\n"); + } else { + code = CORD_all(code, "default: {\n", compile_statement(env, when->else_body), "\nbreak;\n}\n"); + } } else { code = CORD_all(code, "default: errx(1, \"Invalid tag!\");\n"); } |
