2023-03-23 22:10:53 -07:00
|
|
|
|
use std::io::{Write, stdout, stdin};
|
|
|
|
|
use termion::event::Key;
|
|
|
|
|
use termion::input::TermRead;
|
|
|
|
|
use termion::raw::IntoRawMode;
|
|
|
|
|
use termion::raw::RawTerminal;
|
|
|
|
|
use termion::{color, style};
|
2023-03-18 22:16:26 -07:00
|
|
|
|
|
2023-03-19 20:32:49 -07:00
|
|
|
|
mod parser;
|
2023-03-24 13:28:14 -07:00
|
|
|
|
use crate::parser::Token;
|
2023-03-21 19:37:02 -07:00
|
|
|
|
//use crate::parser::ParserError;
|
|
|
|
|
use crate::parser::LineLocation;
|
2023-03-24 13:28:14 -07:00
|
|
|
|
use crate::parser::Eval;
|
2023-03-18 22:16:26 -07:00
|
|
|
|
|
|
|
|
|
|
2023-03-23 22:10:53 -07:00
|
|
|
|
fn draw_line(stdout: &mut RawTerminal<std::io::Stdout>, s: &String) -> Result<(), std::io::Error> {
|
|
|
|
|
write!(
|
2023-03-24 13:28:14 -07:00
|
|
|
|
stdout, "\r{}{}==>{}{} {s} {}",
|
2023-03-23 22:10:53 -07:00
|
|
|
|
style::Bold,
|
|
|
|
|
color::Fg(color::Blue),
|
|
|
|
|
color::Fg(color::Reset),
|
|
|
|
|
style::Reset,
|
2023-03-18 22:16:26 -07:00
|
|
|
|
|
2023-03-23 22:10:53 -07:00
|
|
|
|
// Our string can change by at most one character each iteration.
|
|
|
|
|
// Clearing is done by inserting an extra space, then moving the cursor left.
|
|
|
|
|
termion::cursor::Left(1)
|
|
|
|
|
)?;
|
|
|
|
|
stdout.flush()?;
|
2023-03-18 22:16:26 -07:00
|
|
|
|
|
2023-03-23 22:10:53 -07:00
|
|
|
|
return Ok(());
|
2023-03-18 22:16:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<(), std::io::Error> {
|
2023-03-23 22:10:53 -07:00
|
|
|
|
let mut stdout = stdout().into_raw_mode().unwrap();
|
|
|
|
|
|
|
|
|
|
//let size = termion::terminal_size().unwrap();
|
|
|
|
|
//write!(stdout, "{:?}", size).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut s: String = String::with_capacity(64);
|
|
|
|
|
|
|
|
|
|
'outer: loop {
|
|
|
|
|
|
|
|
|
|
s.clear();
|
|
|
|
|
draw_line(&mut stdout, &s)?;
|
|
|
|
|
|
|
|
|
|
let stdin = stdin();
|
|
|
|
|
for c in stdin.keys() {
|
|
|
|
|
if let Key::Char(q) = c.as_ref().unwrap() {
|
|
|
|
|
match q {
|
|
|
|
|
'\n' => {
|
|
|
|
|
let s = s.trim().to_string();
|
|
|
|
|
if s == "" { write!(stdout, "\r\n")?; break; }
|
|
|
|
|
|
|
|
|
|
RawTerminal::suspend_raw_mode(&stdout)?;
|
|
|
|
|
write!(stdout, "\n")?;
|
|
|
|
|
let g = parser::parse(&s);
|
|
|
|
|
RawTerminal::activate_raw_mode(&stdout)?;
|
|
|
|
|
|
|
|
|
|
match g {
|
2023-03-24 13:28:14 -07:00
|
|
|
|
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!(); }
|
2023-03-23 22:10:53 -07:00
|
|
|
|
},
|
|
|
|
|
Err((l, e)) => {
|
|
|
|
|
let LineLocation{pos, len} = l;
|
|
|
|
|
write!(
|
2023-03-24 13:28:14 -07:00
|
|
|
|
stdout, "{}{}{} {e:?}{}\r\n",
|
2023-03-23 22:10:53 -07:00
|
|
|
|
color::Fg(color::Red),
|
|
|
|
|
" ".repeat(pos + 4),
|
|
|
|
|
"^".repeat(len),
|
|
|
|
|
color::Fg(color::Reset),
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
},
|
|
|
|
|
'/' => { s.push('÷'); },
|
|
|
|
|
'*' => { s.push('×'); },
|
|
|
|
|
_ => { s.push(*q); }
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
match c.unwrap() {
|
|
|
|
|
Key::Backspace => { s.pop(); },
|
|
|
|
|
Key::Delete => { s.pop(); },
|
|
|
|
|
Key::Left => {},
|
|
|
|
|
Key::Right => {},
|
|
|
|
|
Key::Up => {},
|
|
|
|
|
Key::Down => {},
|
|
|
|
|
|
|
|
|
|
Key::Ctrl('d') |
|
|
|
|
|
Key::Ctrl('c') => { break 'outer; },
|
|
|
|
|
_ => {}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
draw_line(&mut stdout, &s)?;
|
2023-03-18 22:16:26 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 22:10:53 -07:00
|
|
|
|
write!(stdout, "\r\n")?;
|
|
|
|
|
return Ok(());
|
2023-03-18 22:16:26 -07:00
|
|
|
|
}
|