Working system

This commit is contained in:
2026-03-14 00:29:52 +01:00
parent 13a9c59364
commit 1cf1503d3a
2 changed files with 33 additions and 10 deletions
+2
View File
@@ -5,6 +5,8 @@ use archforge::runtime::Runtime;
fn main() { fn main() {
let mut runtime = Runtime::new(); let mut runtime = Runtime::new();
runtime.register_opcode("print", |params| println!("{params:?}"));
runtime.execute_script(Path::new("example.forge")); runtime.execute_script(Path::new("example.forge"));
runtime.call_function("main", vec![]); runtime.call_function("main", vec![]);
+31 -10
View File
@@ -1,10 +1,16 @@
use std::{collections::HashMap, path::Path}; use std::{collections::HashMap, path::Path};
use crate::parser::{self, ast::Statement}; use crate::parser::{
self,
ast::{Operand, Statement},
};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Registry {} pub struct Registry {
pub registry: HashMap<String, String>,
}
#[derive(Debug, Clone)]
pub struct Function { pub struct Function {
pub registry: Registry, pub registry: Registry,
pub body: Vec<Statement>, pub body: Vec<Statement>,
@@ -14,12 +20,14 @@ pub struct Function {
pub struct Runtime { pub struct Runtime {
pub registry: Registry, pub registry: Registry,
pub functions: HashMap<String, Function>, pub functions: HashMap<String, Function>,
pub opcodes: HashMap<String, Box<dyn Fn(Vec<String>)>>, pub opcodes: HashMap<String, Box<dyn Fn(Vec<Operand>)>>,
} }
impl Registry { impl Registry {
pub fn new() -> Self { pub fn new() -> Self {
Self {} Self {
registry: HashMap::new(),
}
} }
} }
@@ -58,21 +66,34 @@ impl Runtime {
let op = self let op = self
.opcodes .opcodes
.get(&inst.opcode.to_lowercase()) .get(&inst.opcode.to_lowercase())
.expect("Invalid opcode"); .expect(&format!("Invalid opcode: {}", inst.opcode));
(op)(vec![]); (op)(inst.operands);
} }
_ => {} _ => {}
} }
} }
} }
pub fn call_function(&mut self, fn_name: &str, params: Vec<u8>) { pub fn call_function(&mut self, fn_name: &str, params: Vec<Operand>) {
let f = self let f = self
.functions .functions
.get(fn_name) .get_mut(fn_name)
.expect("Function does not exist"); .expect(&format!("Function does not exist: {}", fn_name))
.clone();
self.execute(f.body.clone(), f.registry.clone()); self.execute(f.body, f.registry);
}
pub fn register_opcode(&mut self, opcode: &str, f: impl Fn(Vec<Operand>) + 'static) {
self.opcodes.insert(opcode.to_lowercase(), Box::new(f));
}
}
impl Registry {
pub fn params(&mut self, params: Vec<String>) {
for (v, p) in self.registry.values_mut().zip(params) {
*v = p;
}
} }
} }