]> rethought.computer Git - sorel-lang.git/commitdiff
basic loops keep/31fd3dc724405c665a1942616d6b7242a699f1c4
authorBryan English <bryan@rethought.computer>
Sat, 31 Jan 2026 03:52:58 +0000 (22:52 -0500)
committerBryan English <bryan@rethought.computer>
Tue, 10 Feb 2026 04:08:54 +0000 (04:08 +0000)
rel-lang/rel-ir/src/lib.rs
rel-lang/relc/src/ir.rs
rel-lang/relc/src/riscv_asm_codegen.rs

index 4eb45427c2f13acbcf7d0a779a5e54342973975e..09b5d731b3bef8dbfb72921f2af552e1120fa971 100644 (file)
@@ -35,6 +35,8 @@ pub enum IR {
     If,
     Else,
     EndIf,
+    Loop,
+    EndLoop,
 
     // System calls
     Sys0,
index 9ab9831ff231ed30cd48fdf10edef51ed58b5530..ccff396b5265f4b06d79fdcc8bbe74e607cb93bd 100644 (file)
@@ -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,
index 8999ef580e7e9e7c6b9c60ea43fc178be41177b4..14972aa8f1113ce1350e37e9d276ab158d029234 100644 (file)
@@ -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"),
             }
         }