Made some neatment updates

This commit is contained in:
Sofia 2020-06-24 17:37:53 +03:00
parent a5b8561d1f
commit cf0f4fc0b9
3 changed files with 34 additions and 8 deletions

View File

@ -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,

View File

@ -108,4 +108,5 @@ pub enum RuntimePanic {
RegistryNotDefined,
InvalidHeapAddress,
ValueNotInitialized,
InvalidTypeAssign,
}

View File

@ -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,
}
}
}