Added conditional compilation for os type

pull/2/head
Mark 2023-04-07 20:36:55 -07:00
parent f8e2c32f9a
commit e0f36ce112
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
7 changed files with 192 additions and 149 deletions

7
Cargo.lock generated
View File

@ -14,10 +14,17 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "daisy"
version = "0.0.1"
dependencies = [
"cfg-if",
"rug",
"termion",
]

View File

@ -12,7 +12,11 @@ lto = "fat"
codegen-units = 1
panic = "abort"
[dependencies]
signal-hook = "0.3.15"
rug = "1.19.2"
cfg-if = "1.0.0"
[target.'cfg(target_family = "unix")'.dependencies]
termion = "2.0.1"
rug = "1.19.2"

12
src/entry/mod.rs Normal file
View File

@ -0,0 +1,12 @@
cfg_if::cfg_if! {
if #[cfg(target_family = "unix")] {
mod unix;
pub use unix::main as main_e;
} else {
pub fn main_e () -> Result<(), std::io::Error> {
println!("Not yet implemented.");
Ok(())
}
}
}

3
src/entry/unix/mod.rs Normal file
View File

@ -0,0 +1,3 @@
mod unix;
pub(in crate::entry::unix) mod promptbuffer;
pub use crate::entry::unix::unix::main;

158
src/entry/unix/unix.rs Normal file
View File

@ -0,0 +1,158 @@
use std::io::Write;
use std::io::stdout;
use std::io::stdin;
use termion::{
event::Key,
input::TermRead,
raw::IntoRawMode,
raw::RawTerminal,
color,
style,
};
use super::promptbuffer::PromptBuffer;
use crate::parser;
use crate::evaluate;
#[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 \
{a} {b}@@@@@@@@@@@@@{a}\r\n \
{b}@@ @{a}# ##\r\n \
{b}@ @@{a}## #\r\n \
{b} @@@@@@ {a} ###### {r}\r\n \
\n {t}Daisy{r} {v}v{ver}{r}\r\n\n",
r = format!("{}{}", color::Fg(color::Reset), style::Reset),
// Icon colors
a = color::Fg(color::Magenta),
b = color::Fg(color::White),
// Title format
t = format!("{}{}", color::Fg(color::White), style::Bold),
// Version
v = format!("{}{}", color::Fg(color::White), style::Italic),
ver = env!("CARGO_PKG_VERSION"),
)?;
return Ok(());
}
#[inline(always)]
pub fn main() -> Result<(), std::io::Error> {
let mut stdout = stdout().into_raw_mode().unwrap();
draw_greeter(&mut stdout)?;
//let size = termion::terminal_size().unwrap();
//write!(stdout, "{:?}", size).unwrap();
let mut pb: PromptBuffer = PromptBuffer::new(64);
'outer: loop {
pb.write_prompt(&mut stdout)?;
let stdin = stdin();
for c in stdin.keys() {
if let Key::Char(q) = c.as_ref().unwrap() {
match q {
'\n' => {
let in_str = pb.enter();
write!(stdout, "\r\n")?;
if in_str == "" { break; }
#[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout)?;
let g = parser::parse(&in_str);
#[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout)?;
match g {
Ok(g) => {
#[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout)?;
let out_str = g.print();
let g = evaluate::evaluate(g);
#[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout)?;
write!(
stdout, " {}{}=>{}{} {}\r\n",
style::Bold, color::Fg(color::Magenta),
style::Reset, color::Fg(color::Reset),
out_str
)?;
match g {
Ok(q) => {
write!(
stdout, "\n {}{}={} {}{}\r\n\n",
style::Bold,
color::Fg(color::Green),
style::Reset,
q.print(),
color::Fg(color::Reset)
)?;
},
Err(_) => {
write!(
stdout, "\n {}{}Mathematical Error: {}Failed to evaluate expression.{}\r\n\n",
style::Bold,
color::Fg(color::Red),
style::Reset,
color::Fg(color::Reset),
)?;
}
}
},
// Show parse error
Err((l, e)) => {
write!(
stdout, "{}{}{} {}{}\r\n",
color::Fg(color::Red),
" ".repeat(l.pos + 4),
"^".repeat(l.len),
e.to_message(),
color::Fg(color::Reset),
)?;
}
};
break;
},
_ => { pb.add_char(*q); }
};
} else {
match c.unwrap() {
Key::Backspace => { pb.backspace(); },
Key::Delete => { pb.delete(); },
Key::Left => { pb.cursor_left(); },
Key::Right => { pb.cursor_right(); },
Key::Up => { pb.hist_up(); },
Key::Down => { pb.hist_down(); },
Key::Ctrl('d') |
Key::Ctrl('c') => { break 'outer; },
_ => {}
};
};
pb.write_prompt(&mut stdout)?;
}
}
write!(stdout, "\r\n")?;
return Ok(());
}

View File

@ -1,27 +1,16 @@
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};
pub mod tokens;
pub mod parser;
pub mod evaluate;
pub mod quantity;
mod promptbuffer;
use crate::promptbuffer::PromptBuffer;
//use crate::tokens::Token;
//use crate::parser::ParserError;
//use crate::parser::LineLocation;
/*
Greeter ascii art:
###### @@@@@@
# ##@@ @
## #@ @@
@ -30,146 +19,16 @@ use crate::promptbuffer::PromptBuffer;
@ @@## #
@@@@@@ ######
Daisy 0.0.1
Daisy 0.0.0
*/
#[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 \
{a} {b}@@@@@@@@@@@@@{a}\r\n \
{b}@@ @{a}# ##\r\n \
{b}@ @@{a}## #\r\n \
{b} @@@@@@ {a} ###### {r}\r\n \
\n {t}Daisy{r} {v}v{ver}{r}\r\n\n",
r = format!("{}{}", color::Fg(color::Reset), style::Reset),
// Icon colors
a = color::Fg(color::Magenta),
b = color::Fg(color::White),
// Title format
t = format!("{}{}", color::Fg(color::White), style::Bold),
// Version
v = format!("{}{}", color::Fg(color::White), style::Italic),
ver = env!("CARGO_PKG_VERSION"),
)?;
return Ok(());
}
mod entry;
use crate::entry::main_e;
fn main() -> Result<(), std::io::Error> {
let mut stdout = stdout().into_raw_mode().unwrap();
draw_greeter(&mut stdout)?;
//let size = termion::terminal_size().unwrap();
//write!(stdout, "{:?}", size).unwrap();
let mut pb: PromptBuffer = PromptBuffer::new(64);
'outer: loop {
pb.write_prompt(&mut stdout)?;
let stdin = stdin();
for c in stdin.keys() {
if let Key::Char(q) = c.as_ref().unwrap() {
match q {
'\n' => {
let in_str = pb.enter();
write!(stdout, "\r\n")?;
if in_str == "" { break; }
#[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout)?;
let g = parser::parse(&in_str);
#[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout)?;
match g {
Ok(g) => {
#[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout)?;
let out_str = g.print();
let g = evaluate::evaluate(g);
#[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout)?;
write!(
stdout, " {}{}=>{}{} {}\r\n",
style::Bold, color::Fg(color::Magenta),
style::Reset, color::Fg(color::Reset),
out_str
)?;
match g {
Ok(q) => {
write!(
stdout, "\n {}{}={} {}{}\r\n\n",
style::Bold,
color::Fg(color::Green),
style::Reset,
q.print(),
color::Fg(color::Reset)
)?;
},
Err(_) => {
write!(
stdout, "\n {}{}Mathematical Error: {}Failed to evaluate expression.{}\r\n\n",
style::Bold,
color::Fg(color::Red),
style::Reset,
color::Fg(color::Reset),
)?;
}
}
},
// Show parse error
Err((l, e)) => {
write!(
stdout, "{}{}{} {}{}\r\n",
color::Fg(color::Red),
" ".repeat(l.pos + 4),
"^".repeat(l.len),
e.to_message(),
color::Fg(color::Reset),
)?;
}
};
break;
},
_ => { pb.add_char(*q); }
};
} else {
match c.unwrap() {
Key::Backspace => { pb.backspace(); },
Key::Delete => { pb.delete(); },
Key::Left => { pb.cursor_left(); },
Key::Right => { pb.cursor_right(); },
Key::Up => { pb.hist_up(); },
Key::Down => { pb.hist_down(); },
Key::Ctrl('d') |
Key::Ctrl('c') => { break 'outer; },
_ => {}
};
};
pb.write_prompt(&mut stdout)?;
}
return main_e();
}
write!(stdout, "\r\n")?;
return Ok(());
}
#[cfg(test)]
mod tests;