pull/2/head
Mark 2023-06-11 10:39:46 -07:00
parent 3c0952c3a3
commit dedb192d54
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
1 changed files with 79 additions and 74 deletions

View File

@ -30,24 +30,39 @@ fn do_expression(
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout)?; RawTerminal::activate_raw_mode(&stdout)?;
match g { // Check for parse errors
Ok(g) => { if let Err((l, e)) = g {
#[cfg(debug_assertions)] write!(
RawTerminal::suspend_raw_mode(&stdout)?; stdout, "{}{}{} {}{}\r\n",
let out_str = g.to_string(); color::Fg(color::Red),
let g = g.evaluate(); " ".repeat(l.pos + 4),
#[cfg(debug_assertions)] "^".repeat(l.len),
RawTerminal::activate_raw_mode(&stdout)?; e.to_message(),
color::Fg(color::Reset),
)?;
return Ok(());
}
let Ok(g) = g else {panic!()};
// Display parsed string
write!( write!(
stdout, " {}{}=>{}{} {}\r\n", stdout, " {}{}=>{}{} {}\r\n",
style::Bold, color::Fg(color::Magenta), style::Bold, color::Fg(color::Magenta),
style::Reset, color::Fg(color::Reset), style::Reset, color::Fg(color::Reset),
out_str g.to_string()
)?; )?;
match g { // Evaluate expression
Ok(q) => { #[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout)?;
let g = g.evaluate();
#[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout)?;
// Show output
if let Ok(q) = g {
write!( write!(
stdout, "\n {}{}={} {}{}\r\n\n", stdout, "\n {}{}={} {}{}\r\n\n",
style::Bold, style::Bold,
@ -56,7 +71,9 @@ fn do_expression(
q.to_string_outer(), q.to_string_outer(),
color::Fg(color::Reset) color::Fg(color::Reset)
)?; )?;
}, } else {
match g {
Ok(_) => panic!(),
Err(EvalError::TooBig) => { Err(EvalError::TooBig) => {
write!( write!(
@ -98,20 +115,8 @@ fn do_expression(
)?; )?;
} }
} }
},
// Show parse error
Err((l, e)) => {
write!(
stdout, "{}{}{} {}{}\r\n",
color::Fg(color::Red),
" ".repeat(l.pos + 4),
"^".repeat(l.len),
e.to_message(),
color::Fg(color::Reset),
)?;
} }
};
return Ok(()); return Ok(());
} }