Added prompt replacement

pull/2/head
Mark 2023-03-25 10:32:51 -07:00
parent 3e1fa97ce8
commit f3b86c3c7a
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 100 additions and 18 deletions

View File

@ -12,7 +12,7 @@ use crate::promptbuffer::PromptBuffer;
use crate::parser::Token; use crate::parser::Token;
//use crate::parser::ParserError; //use crate::parser::ParserError;
use crate::parser::LineLocation; //use crate::parser::LineLocation;
fn draw_line( fn draw_line(
@ -54,14 +54,13 @@ fn main() -> Result<(), std::io::Error> {
'outer: loop { 'outer: loop {
let s = parser::substitute(&pb.get_contents());
draw_line( draw_line(
&mut stdout, &mut stdout, &s,
pb.get_contents(), if s.chars().count() >= last_len
if pb.get_contents().len() >= last_len { 0 } else {last_len - s.chars().count()}
{ 0 } else {last_len - pb.get_contents().len()}
)?; )?;
last_len = pb.get_contents().len(); last_len = s.chars().count();
let stdin = stdin(); let stdin = stdin();
for c in stdin.keys() { for c in stdin.keys() {
@ -102,8 +101,6 @@ fn main() -> Result<(), std::io::Error> {
break; break;
}, },
'/' => { pb.add_char('÷'); },
'*' => { pb.add_char('×'); },
_ => { pb.add_char(*q); } _ => { pb.add_char(*q); }
}; };
} else { } else {
@ -121,13 +118,13 @@ fn main() -> Result<(), std::io::Error> {
}; };
}; };
let s = parser::substitute(&pb.get_contents());
draw_line( draw_line(
&mut stdout, &mut stdout, &s,
pb.get_contents(), if s.chars().count() >= last_len
if pb.get_contents().len() >= last_len { 0 } else {last_len - s.chars().count()}
{ 0 } else {last_len - pb.get_contents().len()}
)?; )?;
last_len = pb.get_contents().len(); last_len = s.chars().count();
} }
} }

View File

@ -2,11 +2,13 @@ mod tokenize;
mod treeify; mod treeify;
mod groupify; mod groupify;
mod evaluate; mod evaluate;
mod find_subs;
use crate::parser::tokenize::p_tokenize; use crate::parser::tokenize::p_tokenize;
use crate::parser::groupify::p_groupify; use crate::parser::groupify::p_groupify;
use crate::parser::treeify::p_treeify; use crate::parser::treeify::p_treeify;
use crate::parser::evaluate::p_evaluate; use crate::parser::evaluate::p_evaluate;
use crate::parser::find_subs::p_find_subs;
use std::collections::VecDeque; use std::collections::VecDeque;
@ -264,4 +266,22 @@ pub fn evaluate(s: &String) -> Result<Token, (LineLocation, ParserError)> {
g = p_evaluate(g)?; g = p_evaluate(g)?;
return Ok(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;
} }

66
src/parser/find_subs.rs Normal file
View File

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

View File

@ -82,10 +82,9 @@ pub fn p_tokenize(input: &String) -> VecDeque<Token> {
// Operator // Operator
// Always one character // Always one character
'*'|'×'| '*'|'/'|'+'|
'/'|'÷'| '^'|'!'|'%'
'+'|'%'| => {
'^'|'!' => {
if t.is_some() { g.push_back(update_line_location(t.unwrap(), i)); } if t.is_some() { g.push_back(update_line_location(t.unwrap(), i)); }
t = Some(Token::PreOperator( t = Some(Token::PreOperator(
LineLocation{pos: i, len: 0}, LineLocation{pos: i, len: 0},