Prettier output

pull/2/head
Mark 2023-03-24 13:28:14 -07:00
parent 1f98645dd4
commit 0927cf25e4
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 23 additions and 9 deletions

View File

@ -6,15 +6,15 @@ use termion::raw::RawTerminal;
use termion::{color, style};
mod parser;
//use crate::parser::Token;
use crate::parser::Token;
//use crate::parser::ParserError;
use crate::parser::LineLocation;
use crate::parser::Eval;
fn draw_line(stdout: &mut RawTerminal<std::io::Stdout>, s: &String) -> Result<(), std::io::Error> {
write!(
stdout,
"\r{}{}==>{}{} {s} {}",
stdout, "\r{}{}==>{}{} {s} {}",
style::Bold,
color::Fg(color::Blue),
color::Fg(color::Reset),
@ -57,15 +57,21 @@ fn main() -> Result<(), std::io::Error> {
match g {
Ok(g) => {
RawTerminal::suspend_raw_mode(&stdout)?;
writeln!(stdout, "Tokenized: {g:#?}")?;
RawTerminal::activate_raw_mode(&stdout)?;
let n = g.eval();
if let Token::Number(_, v) = n {
write!(
stdout, "\r\n {}{}={} {v}{}\r\n\n",
style::Bold,
color::Fg(color::Green),
style::Reset,
color::Fg(color::Reset)
)?;
} else { panic!(); }
},
Err((l, e)) => {
let LineLocation{pos, len} = l;
write!(
stdout,
"{}{}{} {e:?}{}\r\n",
stdout, "{}{}{} {e:?}{}\r\n",
color::Fg(color::Red),
" ".repeat(pos + 4),
"^".repeat(len),

View File

@ -51,6 +51,14 @@ pub enum Token {
impl Eval for Token {
fn eval(&self) -> Token {
match self {
Token::Root(ref v) => {
if v.len() != 1 {panic!()};
if let Token::Number(l, v) = v[0] {
Token::Number(l, v)
} else { panic!(); }
},
Token::Negative(ref v) => {
if v.len() != 1 {panic!()};