From 2cdade5d050388bd52cbb4efab7a85cb418ecdf2 Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Fri, 29 May 2020 19:30:43 +0200 Subject: [PATCH] Removed the struct MathParser. The regular Parser struct does does its job now. --- src/interpreter.rs | 25 ++++++++++--------------- src/main.rs | 20 +++++++++----------- src/math_parser.rs | 30 ------------------------------ src/parser.rs | 18 +++++++++++++----- src/prelude.rs | 9 +++++---- 5 files changed, 37 insertions(+), 65 deletions(-) delete mode 100644 src/math_parser.rs diff --git a/src/interpreter.rs b/src/interpreter.rs index daa1d73..2704715 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -5,17 +5,17 @@ use crate::parser::{Expr, Stmt, Unit}; use crate::prelude::{self, Prelude}; use crate::visitor::Visitor; -pub struct Interpreter { - pub symbol_table: HashMap, +pub struct Interpreter<'a> { + symbol_table: &'a mut HashMap, angle_unit: Unit, prelude: Prelude, } -impl Interpreter { - pub fn new(angle_unit: Unit) -> Self { - let mut hashmap: HashMap = HashMap::new(); +impl<'a> Interpreter<'a> { + pub fn new(angle_unit: Unit, symbol_table: &'a mut HashMap) -> Self { + //let mut hashmap: HashMap = HashMap::new(); for constant in prelude::CONSTANTS { - hashmap.insert( + symbol_table.insert( constant.0.to_string(), Stmt::VarDecl( constant.0.to_string(), @@ -25,9 +25,9 @@ impl Interpreter { } Interpreter { - angle_unit, - symbol_table: hashmap, - prelude: Prelude::new(), + angle_unit: angle_unit.clone(), + symbol_table, + prelude: Prelude::new(angle_unit), } } @@ -44,11 +44,6 @@ impl Interpreter { return None; } - - pub fn set_angle_unit(&mut self, angle_unit: Unit) { - self.prelude.angle_unit = angle_unit.clone(); - self.angle_unit = angle_unit; - } } impl TokenKind { @@ -68,7 +63,7 @@ impl Unit { } } -impl Visitor for Interpreter { +impl<'a> Visitor for Interpreter<'a> { fn visit_stmt(&mut self, stmt: &Stmt) -> f64 { match stmt { Stmt::VarDecl(identifier, _) => { diff --git a/src/main.rs b/src/main.rs index 4f5d1b5..961ca28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,10 @@ use std::{env, process}; mod interpreter; mod lexer; -mod math_parser; mod parser; mod prelude; mod visitor; -use math_parser::MathParser; -use parser::Unit; +use parser::{Parser, Unit}; use rustyline::error::ReadlineError; use rustyline::Editor; @@ -15,12 +13,12 @@ use rustyline::Editor; #[allow(unused_assignments)] // The compiler gives a warning that is not valid. fn main() { let angle_unit = get_angle_unit(); - let mut math_parser = MathParser::new(); - math_parser.set_angle_unit(angle_unit); + let mut parser = Parser::new(); + parser.angle_unit = angle_unit; // Command line argument input, execute it and exit. if let Some(expr) = env::args().skip(1).next() { - eval(&mut math_parser, &expr); + eval(&mut parser, &expr); return; } @@ -33,7 +31,7 @@ fn main() { match readline { Ok(input) => { rl.add_history_entry(input.as_str()); - eval_repl(&mut math_parser, &input); + eval_repl(&mut parser, &input); } Err(ReadlineError::Interrupted) => break, _ => break, @@ -41,17 +39,17 @@ fn main() { } } -fn eval_repl(math_parser: &mut MathParser, input: &str) { +fn eval_repl(parser: &mut Parser, input: &str) { match input { "" => eprint!(""), "clear" => print!("\x1B[2J"), "exit" => process::exit(0), - _ => eval(math_parser, input), + _ => eval(parser, input), } } -fn eval(math_parser: &mut MathParser, input: &str) { - if let Some(result) = math_parser.parse(input) { +fn eval(parser: &mut Parser, input: &str) { + if let Some(result) = parser.parse(input) { println!("{}", result); } } diff --git a/src/math_parser.rs b/src/math_parser.rs deleted file mode 100644 index 65f4886..0000000 --- a/src/math_parser.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::interpreter::Interpreter; -use crate::lexer::Lexer; -use crate::parser::{Parser, Unit}; - -pub const DEFAULT_ANGLE_UNIT: Unit = Unit::Radians; - -pub struct MathParser { - parser: Parser, - interpreter: Interpreter, -} - -impl MathParser { - pub fn new() -> Self { - MathParser { - parser: Parser::new(), - interpreter: Interpreter::new(DEFAULT_ANGLE_UNIT), - } - } - - pub fn parse(&mut self, source: &str) -> Option { - let tokens = Lexer::lex(source); - let statements = self.parser.parse(tokens); - - self.interpreter.interpret(statements) - } - - pub fn set_angle_unit(&mut self, unit: Unit) { - self.interpreter.set_angle_unit(unit); - } -} diff --git a/src/parser.rs b/src/parser.rs index 05a21d5..d841fb1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,6 +1,10 @@ -use std::mem; +use std::{collections::HashMap, mem}; -use crate::lexer::{Token, TokenKind}; +use crate::{ + interpreter::Interpreter, + lexer::{Lexer, Token, TokenKind}, + prelude, +}; #[derive(Debug, Clone)] pub enum Stmt { @@ -27,8 +31,10 @@ pub enum Unit { } pub struct Parser { + pub angle_unit: Unit, tokens: Vec, pos: usize, + symbol_table: HashMap, } impl TokenKind { @@ -49,11 +55,13 @@ impl Parser { Parser { tokens: Vec::new(), pos: 0, + symbol_table: HashMap::new(), + angle_unit: prelude::DEFAULT_ANGLE_UNIT, } } - pub fn parse(&mut self, tokens: Vec) -> Vec { - self.tokens = tokens; + pub fn parse(&mut self, input: &str) -> Option { + self.tokens = Lexer::lex(input); self.pos = 0; let mut statements: Vec = Vec::new(); @@ -61,7 +69,7 @@ impl Parser { statements.push(self.parse_stmt()); } - statements + Interpreter::new(self.angle_unit.clone(), &mut self.symbol_table).interpret(statements) } fn parse_stmt(&mut self) -> Stmt { diff --git a/src/prelude.rs b/src/prelude.rs index abdd5cd..7234d75 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,6 +1,7 @@ -use crate::{math_parser, parser::Unit}; +use crate::parser::Unit; use std::collections::HashMap; +pub const DEFAULT_ANGLE_UNIT: Unit = Unit::Radians; pub const CONSTANTS: &[(&str, &str)] = &[ ("pi", "3.14159265"), ("π", "3.14159265"), @@ -66,15 +67,15 @@ fn from_angle_unit(x: f64, angle_unit: &Unit) -> f64 { } pub struct Prelude { - pub angle_unit: Unit, + angle_unit: Unit, unary: HashMap, binary: HashMap, } impl Prelude { - pub fn new() -> Self { + pub fn new(angle_unit: Unit) -> Self { Prelude { - angle_unit: math_parser::DEFAULT_ANGLE_UNIT, + angle_unit, unary: HashMap::new(), binary: HashMap::new(), }