daisy/src/main.rs

362 lines
7.8 KiB
Rust
Raw Normal View History

2023-03-23 22:10:53 -07:00
use std::io::{Write, stdout, stdin};
2023-03-24 19:42:23 -07:00
2023-03-23 22:10:53 -07:00
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-27 09:47:02 -07:00
pub mod tokens;
2023-03-27 10:54:36 -07:00
pub mod parser;
2023-03-24 19:42:23 -07:00
mod promptbuffer;
2023-03-27 10:54:36 -07:00
pub mod evaluate;
2023-03-27 09:47:02 -07:00
use crate::tokens::Token;
2023-03-24 19:42:23 -07:00
use crate::promptbuffer::PromptBuffer;
//use crate::parser::ParserError;
2023-03-25 10:32:51 -07:00
//use crate::parser::LineLocation;
2023-03-18 22:16:26 -07:00
2023-03-25 12:14:05 -07:00
/*
###### @@@@@@
# ##@@ @
## #@ @@
2023-03-25 21:38:59 -07:00
@@@@@@@@@@@@@
2023-03-25 12:14:05 -07:00
@@ @# ##
@ @@## #
@@@@@@ ######
2023-03-25 21:38:59 -07:00
Daisy 0.0.1
2023-03-25 12:14:05 -07:00
*/
#[inline(always)]
fn draw_greeter(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
write!(
stdout,
"\n \
{a} ###### {b} @@@@@@\r\n \
{a}# ##{b}@@ @\r\n \
{a}## #{b}@ @@\r\n \
2023-03-25 21:38:59 -07:00
{a} {b}@@@@@@@@@@@@@{a}\r\n \
2023-03-25 12:14:05 -07:00
{b}@@ @{a}# ##\r\n \
{b}@ @@{a}## #\r\n \
{b} @@@@@@ {a} ###### {r}\r\n \
2023-03-25 21:38:59 -07:00
\n {t}Daisy{r} {v}v0.0.0{r}\r\n\n",
2023-03-25 12:14:05 -07:00
a = color::Fg(color::Magenta),
b = color::Fg(color::White),
t = format!("{}{}", color::Fg(color::White), style::Bold),
v = format!("{}{}", color::Fg(color::White), style::Italic),
r = format!("{}{}", color::Fg(color::Reset), style::Reset),
)?;
return Ok(());
}
2023-03-24 19:42:23 -07:00
fn draw_line(
stdout: &mut RawTerminal<std::io::Stdout>,
s: &String,
clear_len: usize
) -> Result<(), std::io::Error> {
2023-03-23 22:10:53 -07:00
write!(
2023-03-24 19:42:23 -07:00
stdout, "\r{}{}==>{}{} {}",
2023-03-23 22:10:53 -07:00
style::Bold,
color::Fg(color::Blue),
color::Fg(color::Reset),
style::Reset,
2023-03-24 19:42:23 -07:00
s
2023-03-23 22:10:53 -07:00
)?;
2023-03-24 19:42:23 -07:00
// If this string is shorter, clear the remaining old one.
if clear_len != 0 {
write!(
stdout, "{}{}",
" ".repeat(clear_len as usize),
termion::cursor::Left(clear_len as u16)
)?;
}
2023-03-23 22:10:53 -07:00
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
}
2023-03-27 09:47:02 -07:00
/*
#[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout)?;
#[cfg(debug_assertions)]
write!(stdout, "\n")?;
#[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout)?;
*/
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();
2023-03-25 12:14:05 -07:00
draw_greeter(&mut stdout)?;
2023-03-23 22:10:53 -07:00
//let size = termion::terminal_size().unwrap();
//write!(stdout, "{:?}", size).unwrap();
2023-03-24 19:42:23 -07:00
let mut pb: PromptBuffer = PromptBuffer::new(64);
let mut last_len: usize = 0;
2023-03-23 22:10:53 -07:00
'outer: loop {
2023-03-25 10:32:51 -07:00
let s = parser::substitute(&pb.get_contents());
2023-03-24 19:42:23 -07:00
draw_line(
2023-03-25 10:32:51 -07:00
&mut stdout, &s,
if s.chars().count() >= last_len
{ 0 } else {last_len - s.chars().count()}
2023-03-24 19:42:23 -07:00
)?;
2023-03-25 10:32:51 -07:00
last_len = s.chars().count();
2023-03-23 22:10:53 -07:00
let stdin = stdin();
for c in stdin.keys() {
if let Key::Char(q) = c.as_ref().unwrap() {
match q {
'\n' => {
2023-03-24 19:42:23 -07:00
let s = pb.enter();
2023-03-27 09:47:02 -07:00
write!(stdout, "\r\n")?;
if s == "" { break; }
2023-03-23 22:10:53 -07:00
2023-03-27 09:47:02 -07:00
#[cfg(debug_assertions)]
2023-03-23 22:10:53 -07:00
RawTerminal::suspend_raw_mode(&stdout)?;
2023-03-27 09:47:02 -07:00
let g = parser::parse(&s);
#[cfg(debug_assertions)]
2023-03-23 22:10:53 -07:00
RawTerminal::activate_raw_mode(&stdout)?;
match g {
2023-03-24 19:42:23 -07:00
Ok(g) => {
2023-03-27 09:47:02 -07:00
#[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout)?;
let g = evaluate::evaluate(g).unwrap();
2023-03-24 13:28:14 -07:00
let n = g.eval();
2023-03-27 09:47:02 -07:00
#[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout)?;
2023-03-24 13:28:14 -07:00
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
},
2023-03-27 09:47:02 -07:00
// Show parse error
2023-03-23 22:10:53 -07:00
Err((l, e)) => {
write!(
2023-03-25 20:47:33 -07:00
stdout, "{}{}{} {}{}\r\n",
2023-03-23 22:10:53 -07:00
color::Fg(color::Red),
2023-03-25 09:54:07 -07:00
" ".repeat(l.pos + 4),
"^".repeat(l.len),
2023-03-25 20:47:33 -07:00
e.to_message(),
2023-03-23 22:10:53 -07:00
color::Fg(color::Reset),
)?;
}
};
break;
},
2023-03-24 19:42:23 -07:00
_ => { pb.add_char(*q); }
2023-03-23 22:10:53 -07:00
};
} else {
match c.unwrap() {
2023-03-24 19:42:23 -07:00
Key::Backspace => { pb.backspace(); },
Key::Delete => { pb.delete(); },
2023-03-23 22:10:53 -07:00
Key::Left => {},
Key::Right => {},
2023-03-24 19:42:23 -07:00
Key::Up => { pb.hist_up(); },
Key::Down => { pb.hist_down(); },
2023-03-23 22:10:53 -07:00
Key::Ctrl('d') |
Key::Ctrl('c') => { break 'outer; },
_ => {}
};
};
2023-03-24 20:21:48 -07:00
2023-03-25 10:32:51 -07:00
let s = parser::substitute(&pb.get_contents());
2023-03-24 19:42:23 -07:00
draw_line(
2023-03-25 10:32:51 -07:00
&mut stdout, &s,
if s.chars().count() >= last_len
{ 0 } else {last_len - s.chars().count()}
2023-03-24 19:42:23 -07:00
)?;
2023-03-25 10:32:51 -07:00
last_len = s.chars().count();
2023-03-18 22:16:26 -07:00
}
}
2023-03-23 22:10:53 -07:00
write!(stdout, "\r\n")?;
return Ok(());
2023-03-27 10:54:36 -07:00
}
#[cfg(test)]
mod tests {
// Many of these have been borrowed from insect.
use crate::parser;
use crate::evaluate;
use crate::tokens;
fn good_expr(r: f64, s: &str) {
let s = String::from(s);
let g = parser::parse(&s).unwrap();
let g = evaluate::evaluate(g).unwrap();
let n = g.eval();
let tokens::Token::Number(_, v) = n else {panic!()};
assert_eq!(v, r);
}
fn bad_expr(s: &str) {
let s = String::from(s);
match parser::parse(&s) {
Err(_) => { return },
_ => {}
};
panic!()
}
#[test]
fn basic_numbers() {
good_expr(1f64, "1");
good_expr(1f64, "1.0");
good_expr(1f64, "1.0000");
//good_expr(1f64, "+1.0");
//good_expr(1f64, "+1");
good_expr(3.5f64, "3.5");
good_expr(3.5f64, "3.50");
//good_expr(3.5f64, "+3.50");
good_expr(0.2f64, "0.2");
//good_expr(0.2f64, "+0.2 ");
good_expr(0.2f64, ".2");
//good_expr(0.2f64, "+.2");
good_expr(-0.61f64, "-0.61");
good_expr(-0.61f64, "-.61");
good_expr(-0.61f64, "- .61");
good_expr(0.05f64, ".05");
good_expr(-123.45f64, "-123.45");
bad_expr("123..");
bad_expr("0..");
bad_expr(".0.");
bad_expr(".");
bad_expr(". 2");
bad_expr("..2");
}
#[test]
fn big_numbers() {
good_expr(1234567890000000f64, "1234567890000000");
good_expr(1234567890000000f64, "1234567890000000.0");
//good_expr(1234567890000000f64, "+1234567890000000.0");
}
#[test]
fn negatives() {
good_expr(-5f64, "-5");
good_expr(5f64, "--5");
good_expr(-5f64, "---5");
good_expr(5f64, "----5");
good_expr(-5f64, "-----5");
}
#[test]
fn bad_expressions() {
bad_expr("2^");
bad_expr("^2");
bad_expr("5*");
bad_expr("5/");
bad_expr("5%");
bad_expr("%2");
bad_expr("3 + ");
bad_expr("3 + @");
bad_expr("3 - ");
2023-03-27 10:56:48 -07:00
bad_expr("()");
bad_expr("3+2)");
2023-03-27 10:54:36 -07:00
}
#[test]
fn operators() {
good_expr(125f64, "5^3");
good_expr(125f64, "5 ^ 3");
good_expr(125f64, "( 5 ) ^ ( 3 )");
good_expr(125f64, "( ( 5 ) ^ ( 3 ) )");
2023-03-27 10:56:48 -07:00
good_expr(125f64, "( ( 5 ^ ( 3");
2023-03-27 10:54:36 -07:00
good_expr(125f64, "( 5 ^ 3 )");
//good_expr(125f64, "5^(+3)");
//good_expr(125f64, "+5^3");
good_expr(64f64, "4^3");
good_expr(64f64, "4 ^ 3");
//good_expr(64f64, "4**3");
//good_expr(64f64, "4 ** 3");
good_expr(-81f64, "-3^4");
good_expr(-81f64, "-3 ^ 4");
//good_expr(-81f64, "-3**4");
//good_expr(-81f64, "-3 ** 4");
good_expr(-81f64, "-(3^4)");
//good_expr(f64, "3 ^ (-1.4)");
//good_expr(f64, "3 ** (-1.4)");
//good_expr(f64, "2^3^4^5");
good_expr(0.5f64, "2^-1");
good_expr(0.25f64, "2^-2");
good_expr(15f64, "5*3");
good_expr(15f64, "5 * 3 ");
good_expr(15f64, "( 5 ) * ( 3 )");
good_expr(15f64, "( 5 ) ( 3 )");
good_expr(15f64, "( ( 5 ) * ( 3 ) )");
good_expr(15f64, "( 5 * 3 )");
good_expr(15f64, "5(3)");
good_expr(15f64, "(5)3");
//good_expr(15f64, "5(+3)");
//good_expr(15f64, "+5*3");
good_expr(-15f64, "5*(-3)");
good_expr(-15f64, "5 * (-3)");
good_expr(-15f64, "( 5 ) * ( -3 )");
good_expr(-15f64, "( ( 5 ) * (-( 3 )) )");
good_expr(-15f64, "( 5 * (-3) )");
//good_expr(-15f64, "+5*(-3)");
good_expr(2f64, "6/3");
good_expr(2f64, "6 / 3");
good_expr(2f64, "( 6 ) / ( 3 )");
good_expr(2f64, "( ( 6 ) / ( 3 ) )");
good_expr(2f64, "( 6 / 3 )");
good_expr(2f64, "5%3");
good_expr(2f64, "5 % 3");
good_expr(2f64, "( 5 ) % ( 3 )");
good_expr(2f64, "( ( 5 ) % ( 3 ) )");
good_expr(2f64, "( 5 % 3 )");
good_expr(8f64, "5+3");
good_expr(8f64, "5 + 3");
good_expr(8f64, "( 5 ) + ( 3 )");
good_expr(8f64, "( ( 5 ) + ( 3 ) )");
good_expr(8f64, "( 5 + 3 )");
good_expr(2f64, "5-3");
good_expr(2f64, "5 - 3");
good_expr(2f64, "( 5 ) - ( 3 )");
good_expr(2f64, "( ( 5 ) - ( 3 ) )");
good_expr(2f64, "( 5 - 3 )");
}
2023-03-18 22:16:26 -07:00
}