From 267d3aabf0af54c7f0aa24fd08467a9e7c271b1f Mon Sep 17 00:00:00 2001 From: Teascade Date: Mon, 22 Jun 2020 22:12:40 +0300 Subject: [PATCH] Fix some warnings, make commands nicer --- reid_src/test.reid | 3 ++- src/compiler.rs | 41 +++++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/reid_src/test.reid b/reid_src/test.reid index 4a9fbbd..9a63f69 100644 --- a/reid_src/test.reid +++ b/reid_src/test.reid @@ -1 +1,2 @@ -let otus = "gotus"; \ No newline at end of file +let otus = "gotus"; +let dotus = otus; \ No newline at end of file diff --git a/src/compiler.rs b/src/compiler.rs index 1648889..5ec938e 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -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, - innerScope: Option>, + inner_scope: Option>, } impl Scope { fn get(&self, variable: String) -> Option { 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 {