From 0927cf25e48aac7d9f9e577e633f84cfc31fb142 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 24 Mar 2023 13:28:14 -0700 Subject: [PATCH] Prettier output --- src/main.rs | 24 +++++++++++++++--------- src/parser.rs | 8 ++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 073be58..0a6797b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, s: &String) -> Result<(), std::io::Error> { write!( - stdout, - "\r{}{}==>{}{} {s} {}", + stdout, "\r{}{}==>{}{} {s} {}", style::Bold, color::Fg(color::Blue), color::Fg(color::Reset), @@ -56,16 +56,22 @@ fn main() -> Result<(), std::io::Error> { RawTerminal::activate_raw_mode(&stdout)?; match g { - Ok(g) => { - RawTerminal::suspend_raw_mode(&stdout)?; - writeln!(stdout, "Tokenized: {g:#?}")?; - RawTerminal::activate_raw_mode(&stdout)?; + Ok(g) => { + 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), diff --git a/src/parser.rs b/src/parser.rs index 485e796..d66470f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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!()};