diff --git a/src/main.rs b/src/main.rs index 4c60520..2361ce3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use crate::promptbuffer::PromptBuffer; use crate::parser::Token; //use crate::parser::ParserError; -use crate::parser::LineLocation; +//use crate::parser::LineLocation; fn draw_line( @@ -54,14 +54,13 @@ fn main() -> Result<(), std::io::Error> { 'outer: loop { + let s = parser::substitute(&pb.get_contents()); draw_line( - &mut stdout, - pb.get_contents(), - if pb.get_contents().len() >= last_len - { 0 } else {last_len - pb.get_contents().len()} + &mut stdout, &s, + if s.chars().count() >= last_len + { 0 } else {last_len - s.chars().count()} )?; - last_len = pb.get_contents().len(); - + last_len = s.chars().count(); let stdin = stdin(); for c in stdin.keys() { @@ -102,8 +101,6 @@ fn main() -> Result<(), std::io::Error> { break; }, - '/' => { pb.add_char('÷'); }, - '*' => { pb.add_char('×'); }, _ => { pb.add_char(*q); } }; } else { @@ -121,13 +118,13 @@ fn main() -> Result<(), std::io::Error> { }; }; + let s = parser::substitute(&pb.get_contents()); draw_line( - &mut stdout, - pb.get_contents(), - if pb.get_contents().len() >= last_len - { 0 } else {last_len - pb.get_contents().len()} + &mut stdout, &s, + if s.chars().count() >= last_len + { 0 } else {last_len - s.chars().count()} )?; - last_len = pb.get_contents().len(); + last_len = s.chars().count(); } } diff --git a/src/parser.rs b/src/parser.rs index 7ce0c88..ee79905 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2,11 +2,13 @@ mod tokenize; mod treeify; mod groupify; mod evaluate; +mod find_subs; use crate::parser::tokenize::p_tokenize; use crate::parser::groupify::p_groupify; use crate::parser::treeify::p_treeify; use crate::parser::evaluate::p_evaluate; +use crate::parser::find_subs::p_find_subs; use std::collections::VecDeque; @@ -264,4 +266,22 @@ pub fn evaluate(s: &String) -> Result { g = p_evaluate(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; } \ No newline at end of file diff --git a/src/parser/find_subs.rs b/src/parser/find_subs.rs new file mode 100644 index 0000000..7a2e0ac --- /dev/null +++ b/src/parser/find_subs.rs @@ -0,0 +1,66 @@ +use std::collections::VecDeque; + +use crate::parser::Operator; +use crate::parser::Token; +use crate::parser::LineLocation; + + +pub fn p_find_subs( + mut g: VecDeque, +) -> Vec<(LineLocation, String)> { + + let mut r: Vec<(LineLocation, String)> = Vec::with_capacity(8); + + while g.len() > 0 { + // Read in reverse. Very important! + let t = g.pop_back().unwrap(); + + match &t { + + Token::PreOperator(l, o) => { + match o { + Operator::Multiply => { r.push((l.clone(), String::from("×"))); }, + Operator::Divide => { r.push((l.clone(), String::from("÷"))); }, + _ => {} + } + }, + + Token::PreWord(l, s) => { + match &s[..] { + + // Greek letters + "alpha" => { r.push((l.clone(), String::from("α"))); }, + "beta" => { r.push((l.clone(), String::from("β"))); }, + "gamma" => { r.push((l.clone(), String::from("γ"))); }, + "delta" => { r.push((l.clone(), String::from("δ"))); }, + "epsilon" => { r.push((l.clone(), String::from("ε"))); }, + "zeta" => { r.push((l.clone(), String::from("ζ"))); }, + "eta" => { r.push((l.clone(), String::from("η"))); }, + "theta" => { r.push((l.clone(), String::from("θ"))); }, + "iota" => { r.push((l.clone(), String::from("ι"))); }, + //"kappa" => { r.push((l.clone(), String::from("κ"))); }, + "lambda" => { r.push((l.clone(), String::from("λ"))); }, + "mu" => { r.push((l.clone(), String::from("μ"))); }, + "nu" => { r.push((l.clone(), String::from("ν"))); }, + "xi" => { r.push((l.clone(), String::from("ξ"))); }, + //"omicron" => { r.push((l.clone(), String::from("ο"))); }, + "pi" => { r.push((l.clone(), String::from("π"))); }, + "rho" => { r.push((l.clone(), String::from("ρ"))); }, + "sigma" => { r.push((l.clone(), String::from("σ"))); }, + "tau" => { r.push((l.clone(), String::from("τ"))); }, + //"upsilon" => { r.push((l.clone(), String::from("υ"))); }, + "phi" => { r.push((l.clone(), String::from("φ"))); }, + "chi" => { r.push((l.clone(), String::from("χ"))); }, + "psi" => { r.push((l.clone(), String::from("ψ"))); }, + "omega" => { r.push((l.clone(), String::from("ω"))); } + + _ => {} + } + + } + _ => {} + } + } + + return r; +} \ No newline at end of file diff --git a/src/parser/tokenize.rs b/src/parser/tokenize.rs index a08debb..56035c7 100644 --- a/src/parser/tokenize.rs +++ b/src/parser/tokenize.rs @@ -82,10 +82,9 @@ pub fn p_tokenize(input: &String) -> VecDeque { // Operator // Always one character - '*'|'×'| - '/'|'÷'| - '+'|'%'| - '^'|'!' => { + '*'|'/'|'+'| + '^'|'!'|'%' + => { if t.is_some() { g.push_back(update_line_location(t.unwrap(), i)); } t = Some(Token::PreOperator( LineLocation{pos: i, len: 0},