diff options
| author | Mustafa Quraish <[email protected]> | 2022-02-05 21:26:56 -0500 |
|---|---|---|
| committer | Mustafa Quraish <[email protected]> | 2022-02-05 21:26:56 -0500 |
| commit | 0a0050695254411047122a760ca22ad31c6bd9f8 (patch) | |
| tree | 412cf1c7eac92f08ebb38d35fc815687568106a7 /compiler/codegen.cup | |
| parent | Update `run.sh2` to not re-make the C compiler (diff) | |
| download | cup-0a0050695254411047122a760ca22ad31c6bd9f8.tar.xz cup-0a0050695254411047122a760ca22ad31c6bd9f8.zip | |
[compiler.cup] Support for+while loops
Diffstat (limited to 'compiler/codegen.cup')
| -rw-r--r-- | compiler/codegen.cup | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/compiler/codegen.cup b/compiler/codegen.cup index 975844d..ffe7c1f 100644 --- a/compiler/codegen.cup +++ b/compiler/codegen.cup @@ -290,6 +290,32 @@ fn generate_statement(node: Node*) { generate_block(node.d.conditional.els); emit_asm(".if"); emit_num(label); emit_asm(":\n"); + } else if (node.typ == AST_WHILE) { + let label = ++gen_label_counter; + emit_asm(".loop_s"); emit_num(label); emit_asm(":\n"); + generate_expr_into_rax(node.d.looop.cond); + emit_asm(" cmp rax, 0\n"); + emit_asm(" je .loop_e"); emit_num(label); emit_asm("\n"); + generate_statement(node.d.looop.body); + emit_asm(" jmp .loop_s"); emit_num(label); emit_asm("\n"); + emit_asm(".loop_e"); emit_num(label); emit_asm(":\n"); + + } else if (node.typ == AST_FOR) { + let label = ++gen_label_counter; + if (node.d.looop.init) + generate_statement(node.d.looop.init); + emit_asm(".loop_s"); emit_num(label); emit_asm(":\n"); + if (node.d.looop.cond) { + generate_expr_into_rax(node.d.looop.cond); + emit_asm(" cmp rax, 0\n"); + emit_asm(" je .loop_e"); emit_num(label); emit_asm("\n"); + } + generate_statement(node.d.looop.body); + if (node.d.looop.step) + generate_statement(node.d.looop.step); + emit_asm(" jmp .loop_s"); emit_num(label); emit_asm("\n"); + emit_asm(".loop_e"); emit_num(label); emit_asm(":\n"); + } else { // Default to a simple expression statement generate_expr_into_rax(node); |