mirror of
https://github.com/rm-dr/daisy
synced 2025-07-14 00:14:48 -07:00
Restructured code
This commit is contained in:
@ -1,79 +0,0 @@
|
||||
use crate::parser::Token;
|
||||
use crate::parser::LineLocation;
|
||||
use crate::parser::ParserError;
|
||||
|
||||
#[inline(always)]
|
||||
fn get_at_coords<'a>(g: &'a mut Token, coords: &Vec<usize>) -> &'a mut Token {
|
||||
let mut h = &mut *g;
|
||||
|
||||
for t in coords.iter() {
|
||||
let inner = h.get_args().unwrap();
|
||||
h = &mut inner[*t];
|
||||
}
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
pub fn p_evaluate(
|
||||
mut g: Token,
|
||||
) -> Result<Token, (LineLocation, ParserError)> {
|
||||
let mut coords: Vec<usize> = Vec::with_capacity(16);
|
||||
coords.push(0);
|
||||
|
||||
'outer: loop {
|
||||
|
||||
let mut h = &mut g;
|
||||
for t in coords.iter() {
|
||||
let inner = h.get_args();
|
||||
|
||||
if inner.is_none() || *t >= inner.as_ref().unwrap().len() {
|
||||
coords.pop();
|
||||
if coords.len() == 0 { break 'outer; }
|
||||
|
||||
|
||||
let p = get_at_coords(&mut g, &coords);
|
||||
let e = p.eval();
|
||||
*p = e;
|
||||
|
||||
let l = coords.pop().unwrap();
|
||||
coords.push(l + 1);
|
||||
continue 'outer;
|
||||
}
|
||||
|
||||
h = &mut inner.unwrap()[*t];
|
||||
}
|
||||
|
||||
match h {
|
||||
Token::Multiply(_) |
|
||||
Token::Divide(_) |
|
||||
Token::Add(_) |
|
||||
Token::Factorial(_) |
|
||||
Token::Negative(_) |
|
||||
Token::Power(_) |
|
||||
Token::Modulo(_)
|
||||
=> {
|
||||
coords.push(0);
|
||||
continue 'outer;
|
||||
},
|
||||
|
||||
Token::Constant(_,_,_) |
|
||||
Token::Number(_,_) => {
|
||||
let l = coords.pop().unwrap();
|
||||
coords.push(l + 1);
|
||||
continue 'outer;
|
||||
}
|
||||
|
||||
Token::PreNumber(_,_) |
|
||||
Token::PreWord(_,_) |
|
||||
Token::PreOperator(_,_) |
|
||||
Token::PreGroup(_,_) |
|
||||
Token::PreGroupStart(_) |
|
||||
Token::PreGroupEnd(_)
|
||||
=> panic!()
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
return Ok(g);
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::parser::Operator;
|
||||
use crate::parser::Token;
|
||||
use crate::parser::LineLocation;
|
||||
use crate::tokens::Token;
|
||||
use crate::tokens::Operator;
|
||||
use crate::tokens::LineLocation;
|
||||
|
||||
|
||||
pub fn p_find_subs(
|
||||
@ -60,6 +60,7 @@ pub fn p_find_subs(
|
||||
"chi" => {Some("χ")},
|
||||
"psi" => {Some("ψ")},
|
||||
"omega" => {Some("ω")},
|
||||
"sqrt" => {Some("√")},
|
||||
|
||||
_ => {None}
|
||||
};
|
||||
|
@ -1,9 +1,10 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::parser::Token;
|
||||
use crate::parser::LineLocation;
|
||||
use crate::tokens::Token;
|
||||
use crate::tokens::Operator;
|
||||
use crate::tokens::LineLocation;
|
||||
|
||||
use crate::parser::ParserError;
|
||||
use crate::parser::Operator;
|
||||
|
||||
/// Looks backwards at the elements of g.
|
||||
/// - Inserts ImplicitMultiply
|
||||
|
84
src/parser/mod.rs
Normal file
84
src/parser/mod.rs
Normal file
@ -0,0 +1,84 @@
|
||||
mod tokenize;
|
||||
mod treeify;
|
||||
mod groupify;
|
||||
mod find_subs;
|
||||
|
||||
|
||||
use crate::parser::tokenize::p_tokenize;
|
||||
use crate::parser::groupify::p_groupify;
|
||||
use crate::parser::treeify::p_treeify;
|
||||
use crate::parser::find_subs::p_find_subs;
|
||||
|
||||
use crate::tokens;
|
||||
|
||||
/// Types of parser errors.
|
||||
/// If we cannot parse a string, one of these is returned.
|
||||
#[derive(Debug)]
|
||||
pub enum ParserError {
|
||||
//MissingCloseParen,
|
||||
ExtraCloseParen,
|
||||
EmptyGroup,
|
||||
Syntax,
|
||||
Undefined(String),
|
||||
BadNumber
|
||||
}
|
||||
|
||||
impl ParserError {
|
||||
pub fn to_message(&self) -> String {
|
||||
match self {
|
||||
//ParserError::MissingCloseParen => {
|
||||
// String::from("This group is never closed")
|
||||
//},
|
||||
ParserError::ExtraCloseParen => {
|
||||
String::from("Extra close parenthesis")
|
||||
},
|
||||
ParserError::EmptyGroup => {
|
||||
String::from("Groups can't be empty")
|
||||
},
|
||||
ParserError::Syntax => {
|
||||
String::from("Syntax")
|
||||
},
|
||||
ParserError::Undefined(s) => {
|
||||
format!("\"{s}\" isn't defined")
|
||||
},
|
||||
ParserError::BadNumber => {
|
||||
String::from("Invalid number")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub fn parse(
|
||||
s: &String
|
||||
) -> Result<
|
||||
tokens::Token,
|
||||
(tokens::LineLocation, ParserError)
|
||||
> {
|
||||
|
||||
let tokens = p_tokenize(s);
|
||||
let (_, tokens) = p_find_subs(tokens);
|
||||
let mut g = p_groupify(tokens)?;
|
||||
g = p_treeify(g)?;
|
||||
|
||||
return Ok(g);
|
||||
}
|
||||
|
||||
|
||||
pub fn substitute(s: &String) -> String{
|
||||
if s == "" { return s.clone() }
|
||||
let mut new_s = s.clone();
|
||||
|
||||
let tokens = p_tokenize(s);
|
||||
let (subs, _) = p_find_subs(tokens);
|
||||
|
||||
for r in subs.iter() {
|
||||
new_s.replace_range(
|
||||
r.0.pos..r.0.pos+r.0.len,
|
||||
&r.1[..]
|
||||
)
|
||||
}
|
||||
|
||||
return new_s;
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::parser::Token;
|
||||
use crate::parser::LineLocation;
|
||||
use crate::parser::Operator;
|
||||
use crate::tokens::Token;
|
||||
use crate::tokens::Operator;
|
||||
use crate::tokens::LineLocation;
|
||||
|
||||
/// Updates the length of a Token's LineLocation.
|
||||
/// Run whenever a token is finished.
|
||||
|
@ -1,9 +1,10 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::parser::Token;
|
||||
use crate::parser::LineLocation;
|
||||
use crate::tokens::Token;
|
||||
use crate::tokens::Operator;
|
||||
use crate::tokens::LineLocation;
|
||||
|
||||
use crate::parser::ParserError;
|
||||
use crate::parser::Operator;
|
||||
|
||||
fn treeify_binary(
|
||||
i: usize,
|
||||
|
Reference in New Issue
Block a user