From: Bryan English Date: Sat, 31 Jan 2026 03:52:58 +0000 (-0500) Subject: basic loops X-Git-Url: https://rethought.computer/gitweb//gitweb//git?a=commitdiff_plain;h=31fd3dc724405c665a1942616d6b7242a699f1c4;p=sorel-lang.git basic loops --- diff --git a/rel-lang/rel-ir/src/lib.rs b/rel-lang/rel-ir/src/lib.rs index 4eb4542..09b5d73 100644 --- a/rel-lang/rel-ir/src/lib.rs +++ b/rel-lang/rel-ir/src/lib.rs @@ -35,6 +35,8 @@ pub enum IR { If, Else, EndIf, + Loop, + EndLoop, // System calls Sys0, diff --git a/rel-lang/relc/src/ir.rs b/rel-lang/relc/src/ir.rs index 9ab9831..ccff396 100644 --- a/rel-lang/relc/src/ir.rs +++ b/rel-lang/relc/src/ir.rs @@ -123,6 +123,8 @@ impl ImportTree { "if" => IR::If, "else" => IR::Else, "endif" => IR::EndIf, + "loop" => IR::Loop, + "endloop" => IR::EndLoop, "=" => IR::Equals, ">" => IR::GreaterThan, "+" => IR::AddU64, diff --git a/rel-lang/relc/src/riscv_asm_codegen.rs b/rel-lang/relc/src/riscv_asm_codegen.rs index 8999ef5..14972aa 100644 --- a/rel-lang/relc/src/riscv_asm_codegen.rs +++ b/rel-lang/relc/src/riscv_asm_codegen.rs @@ -126,6 +126,8 @@ impl<'a> CodeGen<'a> { let mut if_block_count = 0; let mut if_stack = vec![]; + let mut loop_count = 0; + let mut loop_stack = vec![]; let mut seen_else = HashSet::new(); let mut last_label = ""; @@ -284,7 +286,21 @@ impl<'a> CodeGen<'a> { seen_else.remove(&if_counter); } stack.pop(); - } + }, + IR::Loop => { // keep looping as long as top of stack is true/0 + self.label(format!("_loop_{}:", loop_count)); + self.pop_to("t0"); + self.line(format!("bnez t0, _endloop_{}", loop_count)); + loop_stack.push(loop_count); + loop_count += 1; + }, + IR::EndLoop => { + let stack = &mut loop_stack; + let loop_counter = stack.last().unwrap().clone(); + self.line(format!("j _loop_{}", loop_counter)); + self.label(format!("_endloop_{}:", loop_counter)); + stack.pop(); + }, _ => bail!("not implemented yet"), } }