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

View File

@ -51,6 +51,14 @@ pub enum Token {
impl Eval for Token { impl Eval for Token {
fn eval(&self) -> Token { fn eval(&self) -> Token {
match self { 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) => { Token::Negative(ref v) => {
if v.len() != 1 {panic!()}; if v.len() != 1 {panic!()};