Removed the struct MathParser. The regular Parser struct does does its job now.

This commit is contained in:
PaddiM8 2020-05-29 19:30:43 +02:00
parent dfd8e4ff64
commit 2cdade5d05
5 changed files with 37 additions and 65 deletions

View File

@ -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<String, Stmt>,
pub struct Interpreter<'a> {
symbol_table: &'a mut HashMap<String, Stmt>,
angle_unit: Unit,
prelude: Prelude,
}
impl Interpreter {
pub fn new(angle_unit: Unit) -> Self {
let mut hashmap: HashMap<String, Stmt> = HashMap::new();
impl<'a> Interpreter<'a> {
pub fn new(angle_unit: Unit, symbol_table: &'a mut HashMap<String, Stmt>) -> Self {
//let mut hashmap: HashMap<String, Stmt> = 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<f64, f64> for Interpreter {
impl<'a> Visitor<f64, f64> for Interpreter<'a> {
fn visit_stmt(&mut self, stmt: &Stmt) -> f64 {
match stmt {
Stmt::VarDecl(identifier, _) => {

View File

@ -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);
}
}

View File

@ -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<f64> {
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);
}
}

View File

@ -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<Token>,
pos: usize,
symbol_table: HashMap<String, Stmt>,
}
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<Token>) -> Vec<Stmt> {
self.tokens = tokens;
pub fn parse(&mut self, input: &str) -> Option<f64> {
self.tokens = Lexer::lex(input);
self.pos = 0;
let mut statements: Vec<Stmt> = 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 {

View File

@ -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<String, UnaryFuncInfo>,
binary: HashMap<String, BinaryFuncInfo>,
}
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(),
}