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;
|
|
|
|
pub mod evaluate;
|
2023-04-01 13:50:52 -07:00
|
|
|
pub mod quantity;
|
|
|
|
|
|
|
|
mod promptbuffer;
|
|
|
|
|
2023-03-27 09:47:02 -07:00
|
|
|
|
2023-03-24 19:42:23 -07:00
|
|
|
use crate::promptbuffer::PromptBuffer;
|
|
|
|
|
2023-04-01 13:50:52 -07:00
|
|
|
//use crate::tokens::Token;
|
2023-03-21 19:37:02 -07:00
|
|
|
//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-27 10:59:11 -07:00
|
|
|
\n {t}Daisy{r} {v}v{ver}{r}\r\n\n",
|
|
|
|
r = format!("{}{}", color::Fg(color::Reset), style::Reset),
|
|
|
|
|
|
|
|
// Icon colors
|
2023-03-25 12:14:05 -07:00
|
|
|
a = color::Fg(color::Magenta),
|
|
|
|
b = color::Fg(color::White),
|
2023-03-27 10:59:11 -07:00
|
|
|
|
|
|
|
// Title format
|
2023-03-25 12:14:05 -07:00
|
|
|
t = format!("{}{}", color::Fg(color::White), style::Bold),
|
|
|
|
|
2023-03-27 10:59:11 -07:00
|
|
|
// Version
|
|
|
|
v = format!("{}{}", color::Fg(color::White), style::Italic),
|
|
|
|
ver = env!("CARGO_PKG_VERSION"),
|
2023-03-25 12:14:05 -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();
|
|
|
|
|
2023-03-25 12:14:05 -07:00
|
|
|
draw_greeter(&mut stdout)?;
|
|
|
|
|
2023-04-07 09:42:50 -07:00
|
|
|
//let size = termion::terminal_size().unwrap();
|
2023-03-23 22:10:53 -07:00
|
|
|
//write!(stdout, "{:?}", size).unwrap();
|
|
|
|
|
2023-03-24 19:42:23 -07:00
|
|
|
let mut pb: PromptBuffer = PromptBuffer::new(64);
|
2023-03-23 22:10:53 -07:00
|
|
|
|
|
|
|
'outer: loop {
|
|
|
|
|
2023-03-27 22:13:14 -07:00
|
|
|
pb.write_prompt(&mut stdout)?;
|
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-29 10:40:33 -07:00
|
|
|
let in_str = pb.enter();
|
2023-03-27 09:47:02 -07:00
|
|
|
write!(stdout, "\r\n")?;
|
2023-03-29 10:40:33 -07:00
|
|
|
if in_str == "" { 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-29 10:40:33 -07:00
|
|
|
let g = parser::parse(&in_str);
|
2023-03-27 09:47:02 -07:00
|
|
|
#[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)?;
|
2023-03-29 10:40:33 -07:00
|
|
|
let out_str = g.print();
|
2023-03-28 14:36:53 -07:00
|
|
|
let g = evaluate::evaluate(g);
|
2023-03-27 09:47:02 -07:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
RawTerminal::activate_raw_mode(&stdout)?;
|
|
|
|
|
2023-03-29 10:40:33 -07:00
|
|
|
write!(
|
|
|
|
stdout, " {}{}=>{}{} {}\r\n",
|
|
|
|
style::Bold, color::Fg(color::Magenta),
|
|
|
|
style::Reset, color::Fg(color::Reset),
|
|
|
|
out_str
|
|
|
|
)?;
|
|
|
|
|
2023-03-28 14:36:53 -07:00
|
|
|
match g {
|
2023-03-29 19:03:53 -07:00
|
|
|
Ok(q) => {
|
2023-03-28 14:36:53 -07:00
|
|
|
write!(
|
2023-03-29 19:03:53 -07:00
|
|
|
stdout, "\n {}{}={} {}{}\r\n\n",
|
2023-03-28 14:36:53 -07:00
|
|
|
style::Bold,
|
|
|
|
color::Fg(color::Green),
|
|
|
|
style::Reset,
|
2023-03-29 19:03:53 -07:00
|
|
|
q.print(),
|
2023-03-28 14:36:53 -07:00
|
|
|
color::Fg(color::Reset)
|
|
|
|
)?;
|
|
|
|
},
|
|
|
|
|
|
|
|
Err(_) => {
|
|
|
|
write!(
|
2023-03-29 10:40:33 -07:00
|
|
|
stdout, "\n {}{}Mathematical Error: {}Failed to evaluate expression.{}\r\n\n",
|
2023-03-28 14:36:53 -07:00
|
|
|
style::Bold,
|
|
|
|
color::Fg(color::Red),
|
|
|
|
style::Reset,
|
|
|
|
color::Fg(color::Reset),
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
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-27 22:13:14 -07:00
|
|
|
Key::Left => { pb.cursor_left(); },
|
|
|
|
Key::Right => { pb.cursor_right(); },
|
2023-03-24 19:42:23 -07:00
|
|
|
Key::Up => { pb.hist_up(); },
|
|
|
|
Key::Down => { pb.hist_down(); },
|
2023-04-07 09:42:50 -07:00
|
|
|
|
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-27 22:13:14 -07:00
|
|
|
pb.write_prompt(&mut stdout)?;
|
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;
|
|
|
|
|
2023-04-07 09:42:50 -07:00
|
|
|
fn eval_to_str(s: &str) -> Result<String, ()> {
|
|
|
|
let g = match parser::parse(&String::from(s)) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(_) => return Err(())
|
|
|
|
};
|
|
|
|
//let out_str = g.print();
|
|
|
|
|
|
|
|
return match evaluate::evaluate(g) {
|
|
|
|
Ok(x) => Ok(x.print()),
|
|
|
|
Err(_) => Err(())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn good_expr(r: &str, s: &str) {
|
|
|
|
let out = eval_to_str(s).unwrap();
|
|
|
|
assert_eq!(r, out);
|
2023-03-27 10:54:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn bad_expr(s: &str) {
|
2023-04-07 09:42:50 -07:00
|
|
|
let out = eval_to_str(s);
|
|
|
|
|
|
|
|
match out {
|
2023-03-27 10:54:36 -07:00
|
|
|
Err(_) => { return },
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic_numbers() {
|
2023-04-07 09:42:50 -07:00
|
|
|
good_expr("1", "1");
|
|
|
|
good_expr("1", "1.0");
|
|
|
|
good_expr("1", "1.0000");
|
|
|
|
good_expr("1", "+1.0");
|
|
|
|
good_expr("1", "+1");
|
|
|
|
good_expr("3.5", "3.5");
|
|
|
|
good_expr("3.5", "3.50");
|
|
|
|
good_expr("3.5", "+3.50");
|
|
|
|
good_expr("0.2", "0.2");
|
|
|
|
good_expr("0.2", "+0.2 ");
|
|
|
|
good_expr("0.2", ".2");
|
|
|
|
good_expr("0.2", "+.2");
|
|
|
|
good_expr("-0.61", "-0.61");
|
|
|
|
good_expr("-0.61", "-.61");
|
|
|
|
good_expr("-0.61", "- .61");
|
|
|
|
good_expr("0.05", ".05");
|
|
|
|
good_expr("-123.45", "-123.45");
|
2023-03-27 10:54:36 -07:00
|
|
|
|
|
|
|
bad_expr("123..");
|
|
|
|
bad_expr("0..");
|
|
|
|
bad_expr(".0.");
|
|
|
|
bad_expr(".");
|
|
|
|
bad_expr(". 2");
|
|
|
|
bad_expr("..2");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn big_numbers() {
|
2023-04-07 09:42:50 -07:00
|
|
|
good_expr("1.2346e15", "1234567890000000");
|
|
|
|
good_expr("1.2346e15", "1234567890000000.0");
|
|
|
|
good_expr("1.2346e15", "+1234567890000000.0");
|
2023-03-27 10:54:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-04-07 09:42:50 -07:00
|
|
|
fn signs() {
|
|
|
|
good_expr( "5", "+++++5");
|
|
|
|
good_expr( "5", "++++5");
|
|
|
|
good_expr( "5", "+++5");
|
|
|
|
good_expr( "5", "++5");
|
|
|
|
good_expr( "5", "+5");
|
|
|
|
good_expr("-5", "-5");
|
|
|
|
good_expr( "5", "--5");
|
|
|
|
good_expr("-5", "---5");
|
|
|
|
good_expr( "5", "----5");
|
|
|
|
good_expr("-5", "-----5");
|
2023-03-27 10:54:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
}
|
|
|
|
|
2023-03-27 21:22:29 -07:00
|
|
|
#[test]
|
|
|
|
fn implicit_multiply() {
|
2023-04-07 09:42:50 -07:00
|
|
|
good_expr("15", "5(3)");
|
|
|
|
good_expr("15", "(5)3");
|
|
|
|
good_expr("15", "(5)(3)");
|
2023-03-27 21:22:29 -07:00
|
|
|
bad_expr("5 2");
|
|
|
|
}
|
|
|
|
|
2023-04-07 09:42:50 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn scientific() {
|
|
|
|
good_expr("100", "1e2");
|
|
|
|
good_expr("0.01", "1e-2");
|
|
|
|
good_expr("1", "1e0");
|
|
|
|
|
|
|
|
// In these expressions, `e` is euler's number
|
|
|
|
// under implicit multiplication
|
|
|
|
good_expr("5.4366", "1e(2)");
|
|
|
|
good_expr("14.778", "e2e");
|
|
|
|
|
|
|
|
bad_expr("2 2e2");
|
|
|
|
bad_expr("1e1.2");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-27 10:54:36 -07:00
|
|
|
#[test]
|
|
|
|
fn operators() {
|
2023-03-28 10:46:46 -07:00
|
|
|
|
2023-04-07 09:42:50 -07:00
|
|
|
good_expr("125", "5^(+3)");
|
|
|
|
good_expr("125", "+5^3");
|
|
|
|
good_expr("0.2148", "3 ^ (-1.4)");
|
|
|
|
|
|
|
|
// Should parse as ((2^3)^4)^5
|
|
|
|
good_expr("1.1529e18", "2^3^4^5");
|
|
|
|
|
|
|
|
// Should parse as 1/(2pi)
|
|
|
|
good_expr("0.15915", "1/2pi");
|
|
|
|
// Should parse as (1/2)*pi
|
|
|
|
good_expr("1.5708", "1/2*pi");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
good_expr("15", "5*3");
|
|
|
|
good_expr("15", "5 * 3 ");
|
|
|
|
good_expr("15", "( 5 ) * ( 3 )");
|
|
|
|
good_expr("15", "( 5 ) ( 3 )");
|
|
|
|
good_expr("15", "( ( 5 ) * ( 3 ) )");
|
|
|
|
good_expr("15", "((5)*(3");
|
|
|
|
good_expr("15", "( 5 * 3 )");
|
|
|
|
good_expr("15", "5(+3)");
|
|
|
|
good_expr("15", "+5*3");
|
|
|
|
|
|
|
|
good_expr("-15", "5*(-3)");
|
|
|
|
good_expr("-15", "5 * (-3)");
|
|
|
|
good_expr("-15", "( 5 ) * ( -3 )");
|
|
|
|
good_expr("-15", "( ( 5 ) * (-( 3 )) )");
|
|
|
|
good_expr("-15", "( 5 * (-3) )");
|
|
|
|
good_expr("-15", "+5*(-3)");
|
|
|
|
|
|
|
|
good_expr("2", "6/3");
|
|
|
|
good_expr("2", "5%3");
|
|
|
|
good_expr("8", "5+3");
|
|
|
|
good_expr("64", "4^3");
|
|
|
|
good_expr("64", "4 ^ 3");
|
|
|
|
good_expr("64", "4**3");
|
|
|
|
good_expr("-81", "-3^4");
|
|
|
|
good_expr("-81", "-(3^4)");
|
|
|
|
good_expr("0.5", "2^-1");
|
|
|
|
good_expr("0.25", "2^-2");
|
|
|
|
|
|
|
|
good_expr("2", "rt 4");
|
|
|
|
good_expr("2", "sqrt 4");
|
|
|
|
good_expr("6", "2 rt 9");
|
|
|
|
|
|
|
|
good_expr("7", "3!+1");
|
|
|
|
good_expr("18", "3!3");
|
|
|
|
bad_expr("3.1!");
|
|
|
|
bad_expr("pi!");
|
2023-03-27 10:54:36 -07:00
|
|
|
}
|
2023-03-18 22:16:26 -07:00
|
|
|
}
|