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::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();
}
}

View File

@ -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;
@ -265,3 +267,21 @@ pub fn evaluate(s: &String) -> Result<Token, (LineLocation, ParserError)> {
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
// 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},