From 0ad745c6d52612b31688873774938717aad94549 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 13 Mar 2026 23:44:58 +0100 Subject: [PATCH] Working ish runtime --- examples/emulator.rs | 14 +++------ src/parser/ast.rs | 14 ++++----- src/runtime.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 17 deletions(-) diff --git a/examples/emulator.rs b/examples/emulator.rs index 6469ccf..263bca8 100644 --- a/examples/emulator.rs +++ b/examples/emulator.rs @@ -1,13 +1,7 @@ -use archforge::parser::parse_program; +use archforge::runtime::Runtime; fn main() { - let p = parse_program("increment(x) { - ADD $counter, %x, $counter - } - - main() { - increment(5) - }").unwrap(); + let mut runtime = Runtime::new(); - println!("{p:?}"); -} \ No newline at end of file + runtime.call_function("main", vec![]); +} diff --git a/src/parser/ast.rs b/src/parser/ast.rs index b41c6aa..7224206 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -1,43 +1,43 @@ -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Program { pub functions: Vec, pub statements: Vec, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Function { pub name: String, pub params: Vec, pub body: Vec, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Statement { Instruction(Instruction), PointerAssign { left: Reg, right: Reg }, FunctionCall(FunctionCall), } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Instruction { pub opcode: String, pub operands: Vec, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct FunctionCall { pub name: String, pub args: Vec, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Operand { Reg(Reg), Number(i64), Pointer(Box), } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Reg { Global(String), Local(String), diff --git a/src/runtime.rs b/src/runtime.rs index e69de29..34cb9b2 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -0,0 +1,71 @@ +use std::{collections::HashMap, path::Path}; + +use crate::parser::{self, ast::Statement}; + +#[derive(Debug, Clone)] +pub struct Registry {} + +pub struct Function { + pub registry: Registry, + pub body: Vec, + pub params: Vec, +} + +pub struct Runtime { + pub registry: Registry, + pub functions: HashMap, +} + +impl Registry { + pub fn new() -> Self { + Self {} + } +} + +impl Runtime { + pub fn new() -> Self { + Self { + registry: Registry::new(), + functions: HashMap::new(), + } + } +} + +impl Runtime { + pub fn execute_script(&mut self, path: &Path) { + let src = std::fs::read_to_string(path).unwrap(); + + let ast = parser::parse_program(&src).unwrap(); + + for func in ast.functions { + self.functions.insert( + func.name, + Function { + registry: Registry::new(), + body: func.body, + params: func.params, + }, + ); + } + } + + pub fn execute(&mut self, statements: Vec, reg: Registry) { + for statement in statements { + match statement { + Statement::Instruction(inst) => { + println!("{inst:#?}"); + } + _ => {} + } + } + } + + pub fn call_function(&mut self, fn_name: &str, params: Vec) { + let f = self + .functions + .get(fn_name) + .expect("Function does not exist"); + + self.execute(f.body.clone(), f.registry.clone()); + } +}