mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-01-08 06:28:56 +01:00
Removed the struct MathParser. The regular Parser struct does does its job now.
This commit is contained in:
parent
dfd8e4ff64
commit
2cdade5d05
@ -5,17 +5,17 @@ use crate::parser::{Expr, Stmt, Unit};
|
|||||||
use crate::prelude::{self, Prelude};
|
use crate::prelude::{self, Prelude};
|
||||||
use crate::visitor::Visitor;
|
use crate::visitor::Visitor;
|
||||||
|
|
||||||
pub struct Interpreter {
|
pub struct Interpreter<'a> {
|
||||||
pub symbol_table: HashMap<String, Stmt>,
|
symbol_table: &'a mut HashMap<String, Stmt>,
|
||||||
angle_unit: Unit,
|
angle_unit: Unit,
|
||||||
prelude: Prelude,
|
prelude: Prelude,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Interpreter {
|
impl<'a> Interpreter<'a> {
|
||||||
pub fn new(angle_unit: Unit) -> Self {
|
pub fn new(angle_unit: Unit, symbol_table: &'a mut HashMap<String, Stmt>) -> Self {
|
||||||
let mut hashmap: HashMap<String, Stmt> = HashMap::new();
|
//let mut hashmap: HashMap<String, Stmt> = HashMap::new();
|
||||||
for constant in prelude::CONSTANTS {
|
for constant in prelude::CONSTANTS {
|
||||||
hashmap.insert(
|
symbol_table.insert(
|
||||||
constant.0.to_string(),
|
constant.0.to_string(),
|
||||||
Stmt::VarDecl(
|
Stmt::VarDecl(
|
||||||
constant.0.to_string(),
|
constant.0.to_string(),
|
||||||
@ -25,9 +25,9 @@ impl Interpreter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Interpreter {
|
Interpreter {
|
||||||
angle_unit,
|
angle_unit: angle_unit.clone(),
|
||||||
symbol_table: hashmap,
|
symbol_table,
|
||||||
prelude: Prelude::new(),
|
prelude: Prelude::new(angle_unit),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,11 +44,6 @@ impl Interpreter {
|
|||||||
|
|
||||||
return None;
|
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 {
|
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 {
|
fn visit_stmt(&mut self, stmt: &Stmt) -> f64 {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::VarDecl(identifier, _) => {
|
Stmt::VarDecl(identifier, _) => {
|
||||||
|
20
src/main.rs
20
src/main.rs
@ -2,12 +2,10 @@ use std::{env, process};
|
|||||||
|
|
||||||
mod interpreter;
|
mod interpreter;
|
||||||
mod lexer;
|
mod lexer;
|
||||||
mod math_parser;
|
|
||||||
mod parser;
|
mod parser;
|
||||||
mod prelude;
|
mod prelude;
|
||||||
mod visitor;
|
mod visitor;
|
||||||
use math_parser::MathParser;
|
use parser::{Parser, Unit};
|
||||||
use parser::Unit;
|
|
||||||
|
|
||||||
use rustyline::error::ReadlineError;
|
use rustyline::error::ReadlineError;
|
||||||
use rustyline::Editor;
|
use rustyline::Editor;
|
||||||
@ -15,12 +13,12 @@ use rustyline::Editor;
|
|||||||
#[allow(unused_assignments)] // The compiler gives a warning that is not valid.
|
#[allow(unused_assignments)] // The compiler gives a warning that is not valid.
|
||||||
fn main() {
|
fn main() {
|
||||||
let angle_unit = get_angle_unit();
|
let angle_unit = get_angle_unit();
|
||||||
let mut math_parser = MathParser::new();
|
let mut parser = Parser::new();
|
||||||
math_parser.set_angle_unit(angle_unit);
|
parser.angle_unit = angle_unit;
|
||||||
|
|
||||||
// Command line argument input, execute it and exit.
|
// Command line argument input, execute it and exit.
|
||||||
if let Some(expr) = env::args().skip(1).next() {
|
if let Some(expr) = env::args().skip(1).next() {
|
||||||
eval(&mut math_parser, &expr);
|
eval(&mut parser, &expr);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,7 +31,7 @@ fn main() {
|
|||||||
match readline {
|
match readline {
|
||||||
Ok(input) => {
|
Ok(input) => {
|
||||||
rl.add_history_entry(input.as_str());
|
rl.add_history_entry(input.as_str());
|
||||||
eval_repl(&mut math_parser, &input);
|
eval_repl(&mut parser, &input);
|
||||||
}
|
}
|
||||||
Err(ReadlineError::Interrupted) => break,
|
Err(ReadlineError::Interrupted) => break,
|
||||||
_ => 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 {
|
match input {
|
||||||
"" => eprint!(""),
|
"" => eprint!(""),
|
||||||
"clear" => print!("\x1B[2J"),
|
"clear" => print!("\x1B[2J"),
|
||||||
"exit" => process::exit(0),
|
"exit" => process::exit(0),
|
||||||
_ => eval(math_parser, input),
|
_ => eval(parser, input),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eval(math_parser: &mut MathParser, input: &str) {
|
fn eval(parser: &mut Parser, input: &str) {
|
||||||
if let Some(result) = math_parser.parse(input) {
|
if let Some(result) = parser.parse(input) {
|
||||||
println!("{}", result);
|
println!("{}", result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -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)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Stmt {
|
pub enum Stmt {
|
||||||
@ -27,8 +31,10 @@ pub enum Unit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Parser {
|
pub struct Parser {
|
||||||
|
pub angle_unit: Unit,
|
||||||
tokens: Vec<Token>,
|
tokens: Vec<Token>,
|
||||||
pos: usize,
|
pos: usize,
|
||||||
|
symbol_table: HashMap<String, Stmt>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TokenKind {
|
impl TokenKind {
|
||||||
@ -49,11 +55,13 @@ impl Parser {
|
|||||||
Parser {
|
Parser {
|
||||||
tokens: Vec::new(),
|
tokens: Vec::new(),
|
||||||
pos: 0,
|
pos: 0,
|
||||||
|
symbol_table: HashMap::new(),
|
||||||
|
angle_unit: prelude::DEFAULT_ANGLE_UNIT,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(&mut self, tokens: Vec<Token>) -> Vec<Stmt> {
|
pub fn parse(&mut self, input: &str) -> Option<f64> {
|
||||||
self.tokens = tokens;
|
self.tokens = Lexer::lex(input);
|
||||||
self.pos = 0;
|
self.pos = 0;
|
||||||
|
|
||||||
let mut statements: Vec<Stmt> = Vec::new();
|
let mut statements: Vec<Stmt> = Vec::new();
|
||||||
@ -61,7 +69,7 @@ impl Parser {
|
|||||||
statements.push(self.parse_stmt());
|
statements.push(self.parse_stmt());
|
||||||
}
|
}
|
||||||
|
|
||||||
statements
|
Interpreter::new(self.angle_unit.clone(), &mut self.symbol_table).interpret(statements)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_stmt(&mut self) -> Stmt {
|
fn parse_stmt(&mut self) -> Stmt {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use crate::{math_parser, parser::Unit};
|
use crate::parser::Unit;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
pub const DEFAULT_ANGLE_UNIT: Unit = Unit::Radians;
|
||||||
pub const CONSTANTS: &[(&str, &str)] = &[
|
pub const CONSTANTS: &[(&str, &str)] = &[
|
||||||
("pi", "3.14159265"),
|
("pi", "3.14159265"),
|
||||||
("π", "3.14159265"),
|
("π", "3.14159265"),
|
||||||
@ -66,15 +67,15 @@ fn from_angle_unit(x: f64, angle_unit: &Unit) -> f64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Prelude {
|
pub struct Prelude {
|
||||||
pub angle_unit: Unit,
|
angle_unit: Unit,
|
||||||
unary: HashMap<String, UnaryFuncInfo>,
|
unary: HashMap<String, UnaryFuncInfo>,
|
||||||
binary: HashMap<String, BinaryFuncInfo>,
|
binary: HashMap<String, BinaryFuncInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Prelude {
|
impl Prelude {
|
||||||
pub fn new() -> Self {
|
pub fn new(angle_unit: Unit) -> Self {
|
||||||
Prelude {
|
Prelude {
|
||||||
angle_unit: math_parser::DEFAULT_ANGLE_UNIT,
|
angle_unit,
|
||||||
unary: HashMap::new(),
|
unary: HashMap::new(),
|
||||||
binary: HashMap::new(),
|
binary: HashMap::new(),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user