Working ish runtime
This commit is contained in:
@@ -1,13 +1,7 @@
|
|||||||
use archforge::parser::parse_program;
|
use archforge::runtime::Runtime;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let p = parse_program("increment(x) {
|
let mut runtime = Runtime::new();
|
||||||
ADD $counter, %x, $counter
|
|
||||||
}
|
|
||||||
|
|
||||||
main() {
|
runtime.call_function("main", vec![]);
|
||||||
increment(5)
|
|
||||||
}").unwrap();
|
|
||||||
|
|
||||||
println!("{p:?}");
|
|
||||||
}
|
}
|
||||||
+7
-7
@@ -1,43 +1,43 @@
|
|||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Program {
|
pub struct Program {
|
||||||
pub functions: Vec<Function>,
|
pub functions: Vec<Function>,
|
||||||
pub statements: Vec<Statement>,
|
pub statements: Vec<Statement>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Function {
|
pub struct Function {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub params: Vec<String>,
|
pub params: Vec<String>,
|
||||||
pub body: Vec<Statement>,
|
pub body: Vec<Statement>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Statement {
|
pub enum Statement {
|
||||||
Instruction(Instruction),
|
Instruction(Instruction),
|
||||||
PointerAssign { left: Reg, right: Reg },
|
PointerAssign { left: Reg, right: Reg },
|
||||||
FunctionCall(FunctionCall),
|
FunctionCall(FunctionCall),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Instruction {
|
pub struct Instruction {
|
||||||
pub opcode: String,
|
pub opcode: String,
|
||||||
pub operands: Vec<Operand>,
|
pub operands: Vec<Operand>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct FunctionCall {
|
pub struct FunctionCall {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub args: Vec<Operand>,
|
pub args: Vec<Operand>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Operand {
|
pub enum Operand {
|
||||||
Reg(Reg),
|
Reg(Reg),
|
||||||
Number(i64),
|
Number(i64),
|
||||||
Pointer(Box<Operand>),
|
Pointer(Box<Operand>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Reg {
|
pub enum Reg {
|
||||||
Global(String),
|
Global(String),
|
||||||
Local(String),
|
Local(String),
|
||||||
|
|||||||
@@ -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<Statement>,
|
||||||
|
pub params: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Runtime {
|
||||||
|
pub registry: Registry,
|
||||||
|
pub functions: HashMap<String, Function>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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<Statement>, reg: Registry) {
|
||||||
|
for statement in statements {
|
||||||
|
match statement {
|
||||||
|
Statement::Instruction(inst) => {
|
||||||
|
println!("{inst:#?}");
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_function(&mut self, fn_name: &str, params: Vec<u8>) {
|
||||||
|
let f = self
|
||||||
|
.functions
|
||||||
|
.get(fn_name)
|
||||||
|
.expect("Function does not exist");
|
||||||
|
|
||||||
|
self.execute(f.body.clone(), f.registry.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user