Made some neatment updates
This commit is contained in:
parent
a5b8561d1f
commit
cf0f4fc0b9
@ -2,6 +2,7 @@ use std::collections::HashMap;
|
||||
|
||||
use super::errors::CompilerError;
|
||||
use super::parser::{Expression, LiteralPattern, ParsedReid, Pattern, Position, Statement};
|
||||
use super::vm::VariableType;
|
||||
|
||||
type Variable = (HeapID, VariableType);
|
||||
pub type HeapID = usize;
|
||||
@ -19,11 +20,6 @@ pub enum Command {
|
||||
StringLit(String), // Bring String Literal to Stack
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum VariableType {
|
||||
TypeString,
|
||||
}
|
||||
|
||||
pub struct Compiler {
|
||||
parsed: ParsedReid,
|
||||
root_scope: Scope,
|
||||
|
@ -108,4 +108,5 @@ pub enum RuntimePanic {
|
||||
RegistryNotDefined,
|
||||
InvalidHeapAddress,
|
||||
ValueNotInitialized,
|
||||
InvalidTypeAssign,
|
||||
}
|
||||
|
35
src/vm.rs
35
src/vm.rs
@ -1,6 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::compiler::{Command, CompiledReid, HeapID, VariableType};
|
||||
use super::compiler::{Command, CompiledReid, HeapID};
|
||||
use super::errors::RuntimePanic;
|
||||
|
||||
pub struct VirtualMachine {
|
||||
@ -103,7 +103,7 @@ impl VirtualMachine {
|
||||
Command::AssignVariable(heapid, regid) => {
|
||||
if let Some(reg) = &self.registry[regid] {
|
||||
if let Some(var) = self.heap.get_mut(&heapid) {
|
||||
var.1 = Some(reg.clone());
|
||||
var.try_set(Some(reg.clone()))?;
|
||||
dbg!("Variable assigned", heapid, regid, &self.heap);
|
||||
Ok(())
|
||||
} else {
|
||||
@ -142,7 +142,36 @@ impl VirtualMachine {
|
||||
#[derive(Clone, Debug)]
|
||||
struct AllocatedVar(VariableType, Option<Value>);
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user