mirror of https://github.com/rm-dr/daisy
Prettier output
parent
1f98645dd4
commit
0927cf25e4
22
src/main.rs
22
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<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),
|
||||
|
|
|
@ -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!()};
|
||||
|
||||
|
|
Loading…
Reference in New Issue