Reid/src/vm.rs

178 lines
5.5 KiB
Rust

use std::collections::HashMap;
use super::compiler::{Command, CompiledReid, HeapID};
use super::errors::RuntimePanic;
pub struct VirtualMachine {
registry_stack: Vec<[Option<Value>; 8]>,
registry: [Option<Value>; 8],
stack: Vec<Value>,
heap: HashMap<HeapID, AllocatedVar>,
position: usize,
compiled: CompiledReid,
}
impl VirtualMachine {
pub fn from(compiled: CompiledReid) -> VirtualMachine {
VirtualMachine {
registry_stack: Vec::new(),
registry: Default::default(),
stack: Vec::new(),
heap: HashMap::new(),
position: 0,
compiled,
}
}
pub fn run(&mut self) -> Result<(), RuntimePanic> {
let mut error = None;
while error.is_none() {
if let Some(command) = self.compiled.list.get(self.position).map(|v| v.clone()) {
self.position += 1;
if let Err(err) = self.execute(command.clone()) {
error = Some(err);
}
if self.remaining() == 0 {
break;
}
} else {
error = Some(RuntimePanic::InvalidCommandIdx(self.position));
}
}
if let Some(error) = error {
Err(error)
} else {
Ok(())
}
}
fn remaining(&self) -> usize {
let len = self.compiled.list.len();
len - self.position.max(0).min(len)
}
fn execute(&mut self, command: Command) -> Result<(), RuntimePanic> {
match command {
Command::InitializeVariable(heapid, vtype) => {
self.heap.insert(heapid, AllocatedVar(vtype, None));
dbg!("Initialized new variable to heap", &self.heap);
Ok(())
}
Command::BeginScope => {
self.registry_stack.push(self.registry.clone());
self.registry = Default::default();
dbg!("Begun new scope");
Ok(())
}
Command::EndScope => {
if let Some(reg) = self.registry_stack.pop() {
self.registry = reg;
dbg!("Scope ended");
Ok(())
} else {
Err(RuntimePanic::ScopeStackUnderflow)
}
}
Command::Pop(regid) => {
if let Some(val) = self.stack.pop() {
self.registry[regid] = Some(val);
dbg!("Registry popped", regid, &self.stack, &self.registry);
Ok(())
} else {
Err(RuntimePanic::StackUnderflow)
}
}
Command::Push(regid) => {
if let Some(reg) = &self.registry[regid] {
if self.stack.len() < usize::MAX {
self.stack.push(reg.clone());
dbg!("Registry pushed", regid, &self.stack);
Ok(())
} else {
Err(RuntimePanic::StackOverflow)
}
} else {
Err(RuntimePanic::RegistryNotDefined)
}
}
Command::AssignVariable(heapid, regid) => {
if let Some(reg) = &self.registry[regid] {
if let Some(var) = self.heap.get_mut(&heapid) {
var.try_set(Some(reg.clone()))?;
dbg!("Variable assigned", heapid, regid, &self.heap);
Ok(())
} else {
Err(RuntimePanic::InvalidHeapAddress)
}
} else {
Err(RuntimePanic::RegistryNotDefined)
}
}
Command::VarToReg(heapid, regid) => {
if let Some(var) = self.heap.get(&heapid) {
if let Some(val) = &var.1 {
self.registry[regid] = Some(val.clone());
dbg!("Variable pushed to registry", heapid, regid, &self.registry);
Ok(())
} else {
Err(RuntimePanic::ValueNotInitialized)
}
} else {
Err(RuntimePanic::InvalidHeapAddress)
}
}
Command::StringLit(string) => {
if self.stack.len() < usize::MAX {
self.stack.push(Value::StringVal(string.clone()));
dbg!("String literal added to stack", string, &self.stack);
Ok(())
} else {
Err(RuntimePanic::StackOverflow)
}
}
}
}
}
#[derive(Clone, Debug)]
struct AllocatedVar(VariableType, Option<Value>);
impl AllocatedVar {
fn try_set(&mut self, new_val: Option<Value>) -> Result<(), RuntimePanic> {
if let Some(val) = new_val {
if val.get_type() == self.0 {
self.1 = Some(val);
Ok(())
} else {
Err(RuntimePanic::InvalidTypeAssign)
}
} else {
self.1 = None;
Ok(())
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum VariableType {
TypeString,
}
#[derive(Clone, Debug, PartialEq, Eq)]
enum Value {
StringVal(String),
}
impl Value {
fn get_type(&self) -> VariableType {
match self {
Value::StringVal(_) => VariableType::TypeString,
}
}
}