Fix some warnings, make commands nicer

This commit is contained in:
Sofia 2020-06-22 22:12:40 +03:00
parent b0a4f9dc7e
commit 267d3aabf0
2 changed files with 23 additions and 21 deletions

View File

@ -1 +1,2 @@
let otus = "gotus";
let otus = "gotus";
let dotus = otus;

View File

@ -3,19 +3,20 @@ use std::collections::HashMap;
use super::errors::CompilerError;
use super::parser::{Expression, LiteralPattern, ParsedReid, Pattern, Position, Statement};
type Variable = (VariableID, VariableType);
type VariableID = u32;
type Variable = (HeapID, VariableType);
type HeapID = u32;
type RegID = u32;
#[derive(Debug)]
pub enum Command {
InitializeVariable(VariableID, VariableType),
BeginScope,
EndScope,
Pop(u32), // Pop into registery u32
Push(u32), // Push out of registery 32
AssignVariable(VariableID, u32), // Assign variable from registery u32
VarToReg(VariableID, u32), // Bring Variable to registery u32
StringLit(String), // Bring String Literal to Heap
InitializeVariable(HeapID, VariableType), // Initializes new variable to HeapID at VariableType
BeginScope, // Begins new Scope
EndScope, // Ends Scope
Pop(RegID), // Pop into registery at RegID
Push(RegID), // Push out of registery at RegID
AssignVariable(HeapID, RegID), // Assign variable from registery at RegID
VarToReg(HeapID, RegID), // Bring Variable to registery at RegID
StringLit(String), // Bring String Literal to Stack
}
#[derive(Debug, Copy, Clone)]
@ -109,7 +110,7 @@ impl Compiler {
Err(err) => Err(CompilerError::LetFailed(pos, Box::new(err))),
}
}
Statement::ExprStatement(pos, expr) => self.handle_expression(expr),
Statement::ExprStatement(_, expr) => self.handle_expression(expr),
}
}
@ -123,16 +124,16 @@ impl Compiler {
#[derive(Default)]
pub struct Scope {
counter: VariableID,
counter: HeapID,
variables: HashMap<String, Variable>,
innerScope: Option<Box<Scope>>,
inner_scope: Option<Box<Scope>>,
}
impl Scope {
fn get(&self, variable: String) -> Option<Variable> {
if let Some(val) = self.variables.get(&variable) {
Some(*val)
} else if let Some(inner) = &self.innerScope {
} else if let Some(inner) = &self.inner_scope {
if let Some(val) = inner.get(variable) {
Some((val.0 + self.counter, val.1))
} else {
@ -151,7 +152,7 @@ impl Scope {
) -> Result<(), CompilerError> {
if self.variables.contains_key(&variable) {
Err(CompilerError::VariableExists(pos, variable))
} else if let Some(inner) = &mut self.innerScope {
} else if let Some(inner) = &mut self.inner_scope {
inner.add_var(pos, variable, vtype)
} else {
self.variables.insert(variable, (self.counter, vtype));
@ -161,19 +162,19 @@ impl Scope {
}
fn begin_scope(&mut self) {
if let Some(inner) = &mut self.innerScope {
if let Some(inner) = &mut self.inner_scope {
inner.begin_scope();
} else {
self.innerScope = Some(Box::default());
self.inner_scope = Some(Box::default());
}
}
fn end_scope(&mut self, pos: Position) -> Result<(), CompilerError> {
if let Some(inner) = &mut self.innerScope {
if inner.innerScope.is_some() {
if let Some(inner) = &mut self.inner_scope {
if inner.inner_scope.is_some() {
inner.end_scope(pos)?;
} else {
self.innerScope = None;
self.inner_scope = None;
}
Ok(())
} else {