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