]> rethought.computer Git - sorel-lang.git/commitdiff
function pointers
authorBryan English <bryan@rethought.computer>
Thu, 12 Feb 2026 14:19:06 +0000 (09:19 -0500)
committerBryan English <bryan@rethought.computer>
Thu, 12 Feb 2026 14:19:29 +0000 (09:19 -0500)
sorel-ir/src/lib.rs
sorelc/src/ir.rs
sorelc/src/riscv_asm_codegen.rs
tests/test1.sorel

index d4e8e8fecc4f6716c78f8b9e689b03b450109bd5..35970e98a6b60e39e44c9bd3c99526c078ce81b2 100644 (file)
@@ -5,6 +5,8 @@ use serde_derive::{Serialize, Deserialize};
 pub enum IR {
     Label(String),
     Call(String),
+    WordPointer(String),
+    CallPtr,
     Ret,
     StackPush(u64),
     StackPushString(String), // refers to string label, not the string itself
index 7324af7b1ed0b5c61615d897b699c5e29a3dd75c..7b931c460c4898a7aef0d7c768be380fdb2e7839 100644 (file)
@@ -181,6 +181,7 @@ impl ImportTree {
                             "endif" => IR::EndIf,
                             "loop" => IR::Loop,
                             "endloop" => IR::EndLoop,
+                            "call" => IR::CallPtr,
                             "=" => IR::Equals,
                             ">" => IR::GreaterThan,
                             "<" => IR::LessThan,
@@ -198,7 +199,14 @@ impl ImportTree {
                             "sys5" => IR::Sys5,
                             "sys6" => IR::Sys6,
                             // TODO num type specfic math like `+:i32`, etc.
-                            _ =>  IR::Call(String::from(*word))
+                            _ =>  {
+                                if word.starts_with("'") {
+                                    let actual_word = &word[1..];
+                                    IR::WordPointer(String::from(actual_word))
+                                } else {
+                                    IR::Call(String::from(*word))
+                                }
+                            }
                         }
                     },
                     Token::String(text) => {
@@ -284,6 +292,9 @@ impl ImportTree {
                 IR::Call(name) => {
                     IR::Call(module.get_label_for_call(name))
                 },
+                IR::WordPointer(name) => {
+                    IR::WordPointer(module.get_label_for_call(name))
+                },
                 _ => instruction.clone()
             };
             self.text.push(new_instruction);
index 23fba90984ed6a2b9b960137a99bc82221dda042..e9b0e86c8e3f87ef24acc74ba6b410041055ede5 100644 (file)
@@ -161,6 +161,17 @@ impl<'a> CodeGen<'a> {
                     self.label(format!("# call {}", mangled));
                     self.line(format!("call {}", mangled));    
                 },
+                IR::WordPointer(name) => {
+                    let mangled = mangle(name);
+                    self.label(format!("# '{} (word pointer)", mangled));
+                    self.line(format!("la t0, {}", mangled));
+                    self.push_from("t0");
+                },
+                IR::CallPtr => {
+                    self.label("# callptr");
+                    self.pop_to("t0");
+                    self.line("jalr t0");
+                },
                 IR::Ret => {
                     if last_label == "main" {
                         self.label("# exit 0 syscall");
index ca868a4c183c690eb4992728c282ffc05ab050c5..dd01bd7478e6e2982dd73d3ec7e47e75c1088424 100644 (file)
@@ -32,3 +32,9 @@ puts \ ( ptr )
 free \ ( )
 
 42 43 44 "soup" putstack
+
+: hello_world_again
+  "Hello World, Again!\n" puts drop
+;
+
+'hello_world_again call