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,88 +30,93 @@ 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(());
}
write!( let Ok(g) = g else {panic!()};
stdout, " {}{}=>{}{} {}\r\n",
style::Bold, color::Fg(color::Magenta),
style::Reset, color::Fg(color::Reset),
out_str
)?;
match g {
Ok(q) => {
write!(
stdout, "\n {}{}={} {}{}\r\n\n",
style::Bold,
color::Fg(color::Green),
style::Reset,
q.to_string_outer(),
color::Fg(color::Reset)
)?;
},
Err(EvalError::TooBig) => { // Display parsed string
write!( write!(
stdout, "\n {}{}Mathematical Error: {}Number too big{}\r\n\n", stdout, " {}{}=>{}{} {}\r\n",
style::Bold, style::Bold, color::Fg(color::Magenta),
color::Fg(color::Red), style::Reset, color::Fg(color::Reset),
style::Reset, g.to_string()
color::Fg(color::Reset), )?;
)?;
},
Err(EvalError::ZeroDivision) => { // Evaluate expression
write!( #[cfg(debug_assertions)]
stdout, "\n {}{}Mathematical Error: {}Division by zero{}\r\n\n", RawTerminal::suspend_raw_mode(&stdout)?;
style::Bold, let g = g.evaluate();
color::Fg(color::Red), #[cfg(debug_assertions)]
style::Reset, RawTerminal::activate_raw_mode(&stdout)?;
color::Fg(color::Reset),
)?;
},
Err(EvalError::BadMath) => { // Show output
write!( if let Ok(q) = g {
stdout, "\n {}{}Mathematical Error: {}Failed to evaluate expression{}\r\n\n", write!(
style::Bold, stdout, "\n {}{}={} {}{}\r\n\n",
color::Fg(color::Red), style::Bold,
style::Reset, color::Fg(color::Green),
color::Fg(color::Reset), style::Reset,
)?; q.to_string_outer(),
}, color::Fg(color::Reset)
)?;
} else {
match g {
Ok(_) => panic!(),
Err(EvalError::IncompatibleUnit) => { Err(EvalError::TooBig) => {
write!( write!(
stdout, "\n {}{}Evaluation Error: {}Incompatible units{}\r\n\n", stdout, "\n {}{}Mathematical Error: {}Number too big{}\r\n\n",
style::Bold, style::Bold,
color::Fg(color::Red), color::Fg(color::Red),
style::Reset, style::Reset,
color::Fg(color::Reset), color::Fg(color::Reset),
)?; )?;
} },
Err(EvalError::ZeroDivision) => {
write!(
stdout, "\n {}{}Mathematical Error: {}Division by zero{}\r\n\n",
style::Bold,
color::Fg(color::Red),
style::Reset,
color::Fg(color::Reset),
)?;
},
Err(EvalError::BadMath) => {
write!(
stdout, "\n {}{}Mathematical Error: {}Failed to evaluate expression{}\r\n\n",
style::Bold,
color::Fg(color::Red),
style::Reset,
color::Fg(color::Reset),
)?;
},
Err(EvalError::IncompatibleUnit) => {
write!(
stdout, "\n {}{}Evaluation Error: {}Incompatible units{}\r\n\n",
style::Bold,
color::Fg(color::Red),
style::Reset,
color::Fg(color::Reset),
)?;
} }
},
// 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(());
} }