From 1cf1503d3ac1ce231ff7a5aa58a55de068e08801 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sat, 14 Mar 2026 00:29:52 +0100 Subject: [PATCH] Working system --- examples/emulator.rs | 2 ++ src/runtime.rs | 41 +++++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/examples/emulator.rs b/examples/emulator.rs index 17172b9..d6a0ab6 100644 --- a/examples/emulator.rs +++ b/examples/emulator.rs @@ -5,6 +5,8 @@ use archforge::runtime::Runtime; fn main() { let mut runtime = Runtime::new(); + runtime.register_opcode("print", |params| println!("{params:?}")); + runtime.execute_script(Path::new("example.forge")); runtime.call_function("main", vec![]); diff --git a/src/runtime.rs b/src/runtime.rs index 4409b42..756ff44 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -1,10 +1,16 @@ use std::{collections::HashMap, path::Path}; -use crate::parser::{self, ast::Statement}; +use crate::parser::{ + self, + ast::{Operand, Statement}, +}; #[derive(Debug, Clone)] -pub struct Registry {} +pub struct Registry { + pub registry: HashMap, +} +#[derive(Debug, Clone)] pub struct Function { pub registry: Registry, pub body: Vec, @@ -14,12 +20,14 @@ pub struct Function { pub struct Runtime { pub registry: Registry, pub functions: HashMap, - pub opcodes: HashMap)>>, + pub opcodes: HashMap)>>, } impl Registry { pub fn new() -> Self { - Self {} + Self { + registry: HashMap::new(), + } } } @@ -58,21 +66,34 @@ impl Runtime { let op = self .opcodes .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) { + pub fn call_function(&mut self, fn_name: &str, params: Vec) { let f = self .functions - .get(fn_name) - .expect("Function does not exist"); + .get_mut(fn_name) + .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) + 'static) { + self.opcodes.insert(opcode.to_lowercase(), Box::new(f)); + } +} + +impl Registry { + pub fn params(&mut self, params: Vec) { + for (v, p) in self.registry.values_mut().zip(params) { + *v = p; + } } }