mirror of
https://github.com/rm-dr/daisy
synced 2025-07-05 01:59:30 -07:00
Compare commits
2 Commits
4aba24ec69
...
v1.1.0
Author | SHA1 | Date | |
---|---|---|---|
bab305e11d | |||
a6c3ffa68d |
108
Cargo.lock
generated
108
Cargo.lock
generated
@ -14,12 +14,6 @@ version = "1.3.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "bumpalo"
|
|
||||||
version = "3.14.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
@ -34,7 +28,6 @@ dependencies = [
|
|||||||
"num",
|
"num",
|
||||||
"termion",
|
"termion",
|
||||||
"toml",
|
"toml",
|
||||||
"wasm-bindgen",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -59,12 +52,6 @@ version = "0.2.147"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "log"
|
|
||||||
version = "0.4.20"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.5.0"
|
version = "2.5.0"
|
||||||
@ -153,30 +140,6 @@ version = "0.1.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
|
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "once_cell"
|
|
||||||
version = "1.18.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "proc-macro2"
|
|
||||||
version = "1.0.67"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328"
|
|
||||||
dependencies = [
|
|
||||||
"unicode-ident",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "quote"
|
|
||||||
version = "1.0.33"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.2.16"
|
version = "0.2.16"
|
||||||
@ -210,17 +173,6 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "syn"
|
|
||||||
version = "2.0.37"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"unicode-ident",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "termion"
|
name = "termion"
|
||||||
version = "2.0.1"
|
version = "2.0.1"
|
||||||
@ -267,66 +219,6 @@ dependencies = [
|
|||||||
"winnow",
|
"winnow",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "unicode-ident"
|
|
||||||
version = "1.0.12"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wasm-bindgen"
|
|
||||||
version = "0.2.87"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"wasm-bindgen-macro",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wasm-bindgen-backend"
|
|
||||||
version = "0.2.87"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd"
|
|
||||||
dependencies = [
|
|
||||||
"bumpalo",
|
|
||||||
"log",
|
|
||||||
"once_cell",
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
"wasm-bindgen-shared",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wasm-bindgen-macro"
|
|
||||||
version = "0.2.87"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d"
|
|
||||||
dependencies = [
|
|
||||||
"quote",
|
|
||||||
"wasm-bindgen-macro-support",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wasm-bindgen-macro-support"
|
|
||||||
version = "0.2.87"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
"wasm-bindgen-backend",
|
|
||||||
"wasm-bindgen-shared",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wasm-bindgen-shared"
|
|
||||||
version = "0.2.87"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winnow"
|
name = "winnow"
|
||||||
version = "0.4.6"
|
version = "0.4.6"
|
||||||
|
17
Cargo.toml
17
Cargo.toml
@ -13,11 +13,6 @@ readme = "README.md"
|
|||||||
name = "daisy"
|
name = "daisy"
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
[lib]
|
|
||||||
name = "daisycalc"
|
|
||||||
path = "src/lib.rs"
|
|
||||||
crate-type = ["cdylib", "rlib"]
|
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
debug = 0
|
debug = 0
|
||||||
@ -30,19 +25,11 @@ panic = "abort"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cfg-if = "1.0.0"
|
cfg-if = "1.0.0"
|
||||||
num = "0.4.1"
|
|
||||||
#astro-float = "0.7.1"
|
|
||||||
|
|
||||||
[package.metadata.wasm-pack.profile.release]
|
|
||||||
# wasm-opt doesn't work, need to figure out why
|
|
||||||
wasm-opt = false
|
|
||||||
|
|
||||||
[target.'cfg(target_family = "unix")'.dependencies]
|
[target.'cfg(target_family = "unix")'.dependencies]
|
||||||
termion = "2.0.1"
|
termion = "2.0.1"
|
||||||
|
num = "0.4.1"
|
||||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
#astro-float = "0.7.1"
|
||||||
wasm-bindgen = "0.2"
|
|
||||||
|
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
toml = "0.7.4"
|
toml = "0.7.4"
|
37
index.html
37
index.html
@ -1,37 +0,0 @@
|
|||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
|
|
||||||
|
|
||||||
<div id="terminal">
|
|
||||||
<div id="output"></div>
|
|
||||||
<div id="input">
|
|
||||||
<span id="prompt">$ </span>
|
|
||||||
<input type="text" id="cmd">
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Note the usage of `type=module` here as this is an ES6 module -->
|
|
||||||
<script type="module">
|
|
||||||
// See wasm-pack docs
|
|
||||||
// Build with `wasm-pack build --release --target web`
|
|
||||||
// Works with wasm-pack 0.9.1. Some other versions give a segfault.
|
|
||||||
import init, { dostr } from './pkg/daisycalc.js';
|
|
||||||
await init();
|
|
||||||
|
|
||||||
$(document).ready(function() {
|
|
||||||
$("#cmd").focus();
|
|
||||||
$("#cmd").keypress(function(e) {
|
|
||||||
if(e.which == 13) {
|
|
||||||
var cmd = $("#cmd").val();
|
|
||||||
$("#output").append("<div>$ " + cmd + "</div>");
|
|
||||||
$("#output").append("<div>$ " + dostr(cmd) + "</div>");
|
|
||||||
$("#cmd").val("");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
13
src/entrypoint/mod.rs
Normal file
13
src/entrypoint/mod.rs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
|
||||||
|
// Select main script for target system
|
||||||
|
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> {
|
||||||
|
unimplemented!("Not yet implemented.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
src/entrypoint/unix/mod.rs
Normal file
3
src/entrypoint/unix/mod.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
mod unix;
|
||||||
|
mod promptbuffer;
|
||||||
|
pub use self::unix::main;
|
@ -1,9 +1,9 @@
|
|||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use termion::raw::RawTerminal;
|
use termion::raw::RawTerminal;
|
||||||
use daisycalc::FormattedText;
|
use crate::formattedtext;
|
||||||
use daisycalc::parser::substitute_cursor;
|
use crate::parser::substitute_cursor;
|
||||||
use daisycalc::Context;
|
use crate::context::Context;
|
||||||
|
|
||||||
const PROMPT_STR: &str = "==> ";
|
const PROMPT_STR: &str = "==> ";
|
||||||
|
|
||||||
@ -55,8 +55,8 @@ impl PromptBuffer {
|
|||||||
|
|
||||||
write!(
|
write!(
|
||||||
stdout, "\r{}{PROMPT_STR}{}{}",
|
stdout, "\r{}{PROMPT_STR}{}{}",
|
||||||
FormattedText::format_map('p', context).unwrap(),
|
formattedtext::format_map('p', context).unwrap(),
|
||||||
FormattedText::format_map('n', context).unwrap(),
|
formattedtext::format_map('n', context).unwrap(),
|
||||||
s
|
s
|
||||||
)?;
|
)?;
|
||||||
|
|
115
src/entrypoint/unix/unix.rs
Normal file
115
src/entrypoint/unix/unix.rs
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
use std::io::stdout;
|
||||||
|
use std::io::stdin;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
use termion::{
|
||||||
|
event::Key,
|
||||||
|
input::TermRead,
|
||||||
|
raw::IntoRawMode,
|
||||||
|
color::DetectColors
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::promptbuffer::PromptBuffer;
|
||||||
|
use crate::command;
|
||||||
|
use crate::context::Context;
|
||||||
|
use crate::FormattedText;
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn main() -> Result<(), std::io::Error> {
|
||||||
|
let mut stdout = stdout().into_raw_mode().unwrap();
|
||||||
|
let mut pb: PromptBuffer = PromptBuffer::new(64);
|
||||||
|
let mut context = Context::new();
|
||||||
|
|
||||||
|
// Set color compatibilty
|
||||||
|
let term_colors = stdout.available_colors().unwrap_or(0);
|
||||||
|
if term_colors >= 256 {
|
||||||
|
context.config.term_color_type = 2;
|
||||||
|
} else if term_colors >= 8 {
|
||||||
|
context.config.term_color_type = 1;
|
||||||
|
} else {
|
||||||
|
context.config.term_color_type = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
context.config.check();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Handle command-line arguments
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
if args.iter().any(|s| s == "--help") {
|
||||||
|
let t = command::do_command(&mut context, &String::from("help"));
|
||||||
|
t.write(&context, &mut stdout)?;
|
||||||
|
return Ok(());
|
||||||
|
} else if args.iter().any(|s| s == "--version") {
|
||||||
|
let t = FormattedText::new(format!(
|
||||||
|
"Daisy v{}\n", env!("CARGO_PKG_VERSION")
|
||||||
|
));
|
||||||
|
t.write(&context, &mut stdout)?;
|
||||||
|
return Ok(());
|
||||||
|
} else if args.iter().any(|s| s == "--debug") {
|
||||||
|
let t = FormattedText::new(format!(
|
||||||
|
concat!(
|
||||||
|
"Daisy v{}\n",
|
||||||
|
"Your terminal supports {} colors.\n"
|
||||||
|
),
|
||||||
|
env!("CARGO_PKG_VERSION"),
|
||||||
|
term_colors
|
||||||
|
));
|
||||||
|
t.write(&context, &mut stdout)?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
'outer: loop {
|
||||||
|
|
||||||
|
pb.write_prompt(&mut context, &mut stdout)?;
|
||||||
|
|
||||||
|
let stdin = stdin();
|
||||||
|
for c in stdin.keys() {
|
||||||
|
if let Key::Char(q) = c.as_ref().unwrap() {
|
||||||
|
match q {
|
||||||
|
'\n' => {
|
||||||
|
// Print again without cursor, in case we pressed enter
|
||||||
|
// while inside a substitution
|
||||||
|
pb.write_prompt_nocursor(&mut context, &mut stdout)?;
|
||||||
|
let in_str = pb.enter();
|
||||||
|
FormattedText::newline(&mut stdout)?;
|
||||||
|
if in_str == "" { break; }
|
||||||
|
|
||||||
|
if in_str.trim() == "quit" {
|
||||||
|
break 'outer;
|
||||||
|
} else {
|
||||||
|
let r = crate::do_string(&mut context, &in_str);
|
||||||
|
|
||||||
|
match r {
|
||||||
|
Ok(t) | Err(t) => {
|
||||||
|
t.write(&context, &mut stdout).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 context, &mut stdout)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FormattedText::newline(&mut stdout)?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
@ -1,12 +1,34 @@
|
|||||||
use super::FormattedText;
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use crate::context::Context;
|
|
||||||
|
|
||||||
use termion::raw::RawTerminal;
|
use termion::raw::RawTerminal;
|
||||||
use termion::color;
|
use termion::color;
|
||||||
use termion::style;
|
use termion::style;
|
||||||
use termion::clear;
|
use termion::clear;
|
||||||
use termion::cursor;
|
use termion::cursor;
|
||||||
|
use std::ops::Add;
|
||||||
|
use crate::context::Context;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct FormattedText {
|
||||||
|
text: String
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToString for FormattedText {
|
||||||
|
fn to_string(&self) -> String { return self.text.clone(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn format_map_none(c: char) -> Option<String> {
|
||||||
|
Some(match c {
|
||||||
|
'n'|'i'|'t'|'a'|
|
||||||
|
'e'|'c'|'s'|'r'|
|
||||||
|
'p'
|
||||||
|
=> { "".to_string() },
|
||||||
|
_ => { return None }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fn format_map_ansi(c: char) -> Option<String> {
|
fn format_map_ansi(c: char) -> Option<String> {
|
||||||
Some(match c {
|
Some(match c {
|
||||||
@ -43,17 +65,6 @@ fn format_map_ansi(c: char) -> Option<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn format_map_none(c: char) -> Option<String> {
|
|
||||||
Some(match c {
|
|
||||||
'n'|'i'|'t'|'a'|
|
|
||||||
'e'|'c'|'s'|'r'|
|
|
||||||
'p'
|
|
||||||
=> { "".to_string() },
|
|
||||||
_ => { return None }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// style::reset also resets color.
|
// style::reset also resets color.
|
||||||
// Make sure color comes AFTER style reset.
|
// Make sure color comes AFTER style reset.
|
||||||
fn format_map_full(c: char) -> Option<String> {
|
fn format_map_full(c: char) -> Option<String> {
|
||||||
@ -91,20 +102,35 @@ fn format_map_full(c: char) -> Option<String> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FormattedText {
|
pub fn format_map(c: char, context: &Context) -> Option<String> {
|
||||||
pub fn newline(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
|
||||||
write!(stdout, "\n")?;
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn format_map(c: char, context: &Context) -> Option<String> {
|
|
||||||
match context.config.term_color_type {
|
match context.config.term_color_type {
|
||||||
0 => format_map_none(c),
|
0 => format_map_none(c),
|
||||||
1 => format_map_ansi(c),
|
1 => format_map_ansi(c),
|
||||||
2 => format_map_full(c),
|
2 => format_map_full(c),
|
||||||
_ => unreachable!("Invalid term_color_type")
|
_ => unreachable!("Invalid term_color_type")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl FormattedText {
|
||||||
|
pub fn newline(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||||
|
write!(stdout, "\n")?;
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl FormattedText {
|
||||||
|
pub fn new(s: String) -> FormattedText {
|
||||||
|
return FormattedText {
|
||||||
|
text: s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push(&mut self, s: &str) {
|
||||||
|
self.text.push_str(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn write(&self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
pub fn write(&self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||||
|
|
||||||
@ -127,7 +153,7 @@ impl FormattedText {
|
|||||||
'[' => {
|
'[' => {
|
||||||
let a = chars.next().unwrap();
|
let a = chars.next().unwrap();
|
||||||
|
|
||||||
// Treat double [[ as escaped [
|
// Handle double [[ as escaped [
|
||||||
if a == '[' { s.push('['); }
|
if a == '[' { s.push('['); }
|
||||||
|
|
||||||
let b = chars.next().unwrap();
|
let b = chars.next().unwrap();
|
||||||
@ -135,7 +161,7 @@ impl FormattedText {
|
|||||||
match (a, b) {
|
match (a, b) {
|
||||||
(c, ']') => { // Normal text
|
(c, ']') => { // Normal text
|
||||||
|
|
||||||
let q = Self::format_map(c, context);
|
let q = format_map(c, context);
|
||||||
|
|
||||||
if q.is_some() {
|
if q.is_some() {
|
||||||
s.push_str(&q.unwrap());
|
s.push_str(&q.unwrap());
|
||||||
@ -162,3 +188,12 @@ impl FormattedText {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Add for FormattedText {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, other: Self) -> Self::Output {
|
||||||
|
return FormattedText::new(format!("{}{}", self.text, other.text));
|
||||||
|
}
|
||||||
|
}
|
@ -1,33 +0,0 @@
|
|||||||
use std::ops::Add;
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct FormattedText {
|
|
||||||
pub(super) text: String
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ToString for FormattedText {
|
|
||||||
fn to_string(&self) -> String { return self.text.clone(); }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FormattedText {
|
|
||||||
pub fn new(s: String) -> FormattedText {
|
|
||||||
return FormattedText {
|
|
||||||
text: s
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn push(&mut self, s: &str) {
|
|
||||||
self.text.push_str(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl Add for FormattedText {
|
|
||||||
type Output = Self;
|
|
||||||
|
|
||||||
fn add(self, other: Self) -> Self::Output {
|
|
||||||
return FormattedText::new(format!("{}{}", self.text, other.text));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
mod formattedtext;
|
|
||||||
pub use formattedtext::FormattedText;
|
|
||||||
|
|
||||||
|
|
||||||
// Select write implementation by target system
|
|
||||||
cfg_if::cfg_if! {
|
|
||||||
if #[cfg(target_family = "unix")] {
|
|
||||||
mod unix_backend;
|
|
||||||
} else if #[cfg(target_arch = "wasm32")] {
|
|
||||||
mod wasm_backend;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
use super::FormattedText;
|
|
||||||
|
|
||||||
impl FormattedText {
|
|
||||||
pub fn newline() -> Result<(), ()> {
|
|
||||||
print!("\n");
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write(&self) -> String {
|
|
||||||
return self.text.clone();
|
|
||||||
}
|
|
||||||
}
|
|
301
src/lib.rs
301
src/lib.rs
@ -1,301 +0,0 @@
|
|||||||
pub mod parser;
|
|
||||||
pub mod command;
|
|
||||||
pub mod quantity;
|
|
||||||
|
|
||||||
use crate::parser::substitute;
|
|
||||||
use crate::parser::LineLocation;
|
|
||||||
|
|
||||||
|
|
||||||
mod context;
|
|
||||||
mod formattedtext;
|
|
||||||
mod errors;
|
|
||||||
mod evaluate;
|
|
||||||
|
|
||||||
pub use crate::formattedtext::FormattedText;
|
|
||||||
pub use crate::context::Context;
|
|
||||||
pub use crate::errors::DaisyError;
|
|
||||||
pub use crate::evaluate::evaluate;
|
|
||||||
|
|
||||||
|
|
||||||
cfg_if::cfg_if! {
|
|
||||||
if #[cfg(target_arch = "wasm32")] {
|
|
||||||
use wasm_bindgen::prelude::*;
|
|
||||||
|
|
||||||
#[wasm_bindgen]
|
|
||||||
pub fn dostr(x: String) -> String{
|
|
||||||
let mut context = Context::new();
|
|
||||||
let x = x.trim().to_string();
|
|
||||||
let r = do_string(&mut context, &x);
|
|
||||||
|
|
||||||
match r {
|
|
||||||
Ok(t) | Err(t) => {
|
|
||||||
return t.write();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn do_string(
|
|
||||||
context: &mut Context,
|
|
||||||
s: &String
|
|
||||||
) -> Result<FormattedText, FormattedText> {
|
|
||||||
|
|
||||||
let r: (LineLocation, DaisyError);
|
|
||||||
if command::is_command(s) {
|
|
||||||
return Ok(command::do_command(context, s));
|
|
||||||
} else if s.contains("=") {
|
|
||||||
let x = do_assignment(context, s);
|
|
||||||
match x {
|
|
||||||
Ok(t) => { return Ok(t) },
|
|
||||||
Err(t) => { r = t }
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
let x = do_expression(context, s);
|
|
||||||
match x {
|
|
||||||
Ok((t, e)) => { context.push_hist(e); return Ok(t) },
|
|
||||||
Err(t) => { r = t }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
let (l, e) = r;
|
|
||||||
let mut t = FormattedText::new("".to_string());
|
|
||||||
if l.zero() {
|
|
||||||
t.push(&format!(
|
|
||||||
"\n {}\n\n",
|
|
||||||
e.text().to_string(),
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
t.push(&format!(
|
|
||||||
concat!(
|
|
||||||
"{}[e]{}[n]\n",
|
|
||||||
" {}\n\n"
|
|
||||||
),
|
|
||||||
" ".repeat(l.pos + 4),
|
|
||||||
"^".repeat(l.len),
|
|
||||||
e.text().to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Err(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle a simple evaluation string.
|
|
||||||
// Returns a FormattedText with output that should be printed.
|
|
||||||
#[inline(always)]
|
|
||||||
fn do_expression(
|
|
||||||
context: &mut Context,
|
|
||||||
s: &String
|
|
||||||
) -> Result<(FormattedText, parser::Expression), (LineLocation, DaisyError)> {
|
|
||||||
|
|
||||||
let mut output = FormattedText::new("".to_string());
|
|
||||||
|
|
||||||
let g = parser::parse(context, &s)?;
|
|
||||||
let g_evaluated = evaluate::evaluate(context, &g)?;
|
|
||||||
|
|
||||||
// Display parsed string
|
|
||||||
output.push(&format!(
|
|
||||||
" [s]=>[n] {}\n\n",
|
|
||||||
g.display(context)
|
|
||||||
));
|
|
||||||
|
|
||||||
// Display result
|
|
||||||
output.push(&format!(
|
|
||||||
" [r]=[n] {}\n\n",
|
|
||||||
g_evaluated.display_outer(context),
|
|
||||||
));
|
|
||||||
|
|
||||||
return Ok((output, g_evaluated));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Handle a variable or function definition string.
|
|
||||||
// Returns a FormattedText with output that should be printed.
|
|
||||||
#[inline(always)]
|
|
||||||
fn do_assignment(
|
|
||||||
context: &mut Context,
|
|
||||||
s: &String
|
|
||||||
) -> Result<FormattedText, (LineLocation, DaisyError)> {
|
|
||||||
|
|
||||||
let mut output = FormattedText::new("".to_string());
|
|
||||||
|
|
||||||
let parts = s.split("=").collect::<Vec<&str>>();
|
|
||||||
if parts.len() != 2 {
|
|
||||||
return Err((
|
|
||||||
LineLocation::new_zero(),
|
|
||||||
DaisyError::Syntax
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Index of first non-whitespace character in left
|
|
||||||
// (relative to whole prompt)
|
|
||||||
let starting_left = parts[0]
|
|
||||||
.char_indices()
|
|
||||||
.find(|(_, ch)| !(ch.is_whitespace() && *ch != '\n'))
|
|
||||||
.map(|(i, _)| i)
|
|
||||||
.unwrap_or_else(|| parts[0].len());
|
|
||||||
|
|
||||||
// Index of first non-whitespace character in right
|
|
||||||
// (relative to whole prompt)
|
|
||||||
// +1 accounts for equals sign
|
|
||||||
let starting_right = parts[0].chars().count() + 1 +
|
|
||||||
parts[1]
|
|
||||||
.char_indices()
|
|
||||||
.find(|(_, ch)| !(ch.is_whitespace() && *ch != '\n'))
|
|
||||||
.map(|(i, _)| i)
|
|
||||||
.unwrap_or_else(|| parts[0].len());
|
|
||||||
|
|
||||||
|
|
||||||
let left = substitute(context, &parts[0].trim().to_string());
|
|
||||||
let right = substitute(context, &parts[1].trim().to_string());
|
|
||||||
let is_function = left.contains("(");
|
|
||||||
|
|
||||||
// The order of methods below is a bit odd.
|
|
||||||
// This is intentional, since we want to check a definition's
|
|
||||||
// variable name before even attempting to parse its content.
|
|
||||||
if is_function {
|
|
||||||
let mut mode = 0;
|
|
||||||
let mut name = String::new();
|
|
||||||
let mut args = String::new();
|
|
||||||
for c in left.chars() {
|
|
||||||
match mode {
|
|
||||||
|
|
||||||
// Mode 0: reading function name
|
|
||||||
0 => {
|
|
||||||
if c == '(' {
|
|
||||||
mode = 1; continue;
|
|
||||||
} else { name.push(c); }
|
|
||||||
},
|
|
||||||
|
|
||||||
// Mode 1: reading arguments
|
|
||||||
1 => {
|
|
||||||
if c == ')' {
|
|
||||||
mode = 2; continue;
|
|
||||||
} else { args.push(c); }
|
|
||||||
},
|
|
||||||
|
|
||||||
// Mode 2: we should be done by now.
|
|
||||||
// That close paren should've been the last character.
|
|
||||||
2 => {
|
|
||||||
return Err((
|
|
||||||
LineLocation{ pos: starting_left, len: left.chars().count() },
|
|
||||||
DaisyError::Syntax
|
|
||||||
));
|
|
||||||
},
|
|
||||||
|
|
||||||
_ => unreachable!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
let args = args
|
|
||||||
.split(",").collect::<Vec<&str>>()
|
|
||||||
.iter().map(|x| x.trim().to_string()).collect::<Vec<String>>();
|
|
||||||
|
|
||||||
if name.len() == 0 {
|
|
||||||
return Err((
|
|
||||||
LineLocation{ pos: starting_left, len: left.chars().count() },
|
|
||||||
DaisyError::Syntax
|
|
||||||
));
|
|
||||||
};
|
|
||||||
|
|
||||||
if !context.valid_function(&name) {
|
|
||||||
return Err((
|
|
||||||
LineLocation{ pos: starting_left, len: left.chars().count() },
|
|
||||||
DaisyError::BadFunction
|
|
||||||
));
|
|
||||||
};
|
|
||||||
|
|
||||||
if args.iter().find(|x| &x[..] == "").is_some() {
|
|
||||||
return Err((
|
|
||||||
LineLocation{ pos: starting_left, len: left.chars().count() },
|
|
||||||
DaisyError::Syntax
|
|
||||||
));
|
|
||||||
};
|
|
||||||
|
|
||||||
for a in &args {
|
|
||||||
if !context.valid_varible(a) {
|
|
||||||
return Err((
|
|
||||||
LineLocation{ pos: starting_left, len: left.chars().count() },
|
|
||||||
DaisyError::BadVariable
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse right hand side
|
|
||||||
let g = parser::parse(context, &right);
|
|
||||||
let Ok(g) = g else {
|
|
||||||
let Err((l, e)) = g else { unreachable!() };
|
|
||||||
return Err((
|
|
||||||
LineLocation{ pos: l.pos + starting_right, len: l.len},
|
|
||||||
e
|
|
||||||
));
|
|
||||||
};
|
|
||||||
|
|
||||||
// Display parsed string
|
|
||||||
output.push(&format!(
|
|
||||||
" [s]=>[n] {left} = {}\n\n",
|
|
||||||
g.display(context)
|
|
||||||
));
|
|
||||||
|
|
||||||
// Evaluate expression with shadow variables
|
|
||||||
for a in &args { context.add_shadow(a.to_string(), None);}
|
|
||||||
let g_evaluated = evaluate::evaluate(context, &g);
|
|
||||||
context.clear_shadow();
|
|
||||||
let Ok(_g_evaluated) = g_evaluated else {
|
|
||||||
let Err((l, e)) = g_evaluated else { unreachable!() };
|
|
||||||
return Err((
|
|
||||||
LineLocation{ pos: l.pos + starting_right, len: l.len},
|
|
||||||
e
|
|
||||||
));
|
|
||||||
};
|
|
||||||
|
|
||||||
// We could push g_evaluated instead, but an un-evaluated string
|
|
||||||
// makes the 'vars' command prettier.
|
|
||||||
//
|
|
||||||
// We still need to evaluate g above, though, to make sure it works.
|
|
||||||
context.push_function(name, args, g).unwrap();
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if !context.valid_varible(&left) {
|
|
||||||
return Err((
|
|
||||||
LineLocation{ pos: starting_left, len: left.chars().count() },
|
|
||||||
DaisyError::BadVariable
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse right hand side
|
|
||||||
let g = parser::parse(context, &right);
|
|
||||||
let Ok(g) = g else {
|
|
||||||
let Err((l, e)) = g else { unreachable!() };
|
|
||||||
return Err((
|
|
||||||
LineLocation{ pos: l.pos + starting_right, len: l.len},
|
|
||||||
e
|
|
||||||
));
|
|
||||||
};
|
|
||||||
|
|
||||||
// Display parsed string
|
|
||||||
output.push(&format!(
|
|
||||||
" [t]=>[n] {left} = {}\n\n",
|
|
||||||
g.display(context)
|
|
||||||
));
|
|
||||||
|
|
||||||
// Evaluate expression
|
|
||||||
let g_evaluated = evaluate::evaluate(context, &g);
|
|
||||||
let Ok(g_evaluated) = g_evaluated else {
|
|
||||||
let Err((l, e)) = g_evaluated else { unreachable!() };
|
|
||||||
return Err((
|
|
||||||
LineLocation{ pos: l.pos + starting_right, len: l.len},
|
|
||||||
e
|
|
||||||
));
|
|
||||||
};
|
|
||||||
|
|
||||||
context.push_variable(left.to_string(), g_evaluated).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(output);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
373
src/main.rs
373
src/main.rs
@ -1,124 +1,289 @@
|
|||||||
pub mod promptbuffer;
|
pub mod parser;
|
||||||
|
pub mod command;
|
||||||
|
pub mod quantity;
|
||||||
|
pub mod evaluate;
|
||||||
|
pub mod context;
|
||||||
|
pub mod errors;
|
||||||
|
pub mod formattedtext;
|
||||||
|
|
||||||
use std::io::stdout;
|
use crate::parser::substitute;
|
||||||
use std::io::stdin;
|
use crate::errors::DaisyError;
|
||||||
use std::env;
|
use crate::formattedtext::FormattedText;
|
||||||
|
use crate::context::Context;
|
||||||
|
use crate::parser::LineLocation;
|
||||||
|
|
||||||
use termion::{
|
|
||||||
event::Key,
|
|
||||||
input::TermRead,
|
|
||||||
raw::IntoRawMode,
|
|
||||||
color::DetectColors
|
|
||||||
};
|
|
||||||
|
|
||||||
use promptbuffer::PromptBuffer;
|
// Run main script for target system
|
||||||
use daisycalc::command;
|
mod entrypoint;
|
||||||
use daisycalc::Context;
|
use crate::entrypoint::main_e;
|
||||||
use daisycalc::FormattedText;
|
|
||||||
|
|
||||||
use daisycalc::do_string;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
|
fn main() -> Result<(), std::io::Error> {
|
||||||
|
return main_e();
|
||||||
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn main() -> Result<(), std::io::Error> {
|
pub fn do_string(
|
||||||
let mut stdout = stdout().into_raw_mode().unwrap();
|
context: &mut Context,
|
||||||
let mut pb: PromptBuffer = PromptBuffer::new(64);
|
s: &String
|
||||||
let mut context = Context::new();
|
) -> Result<FormattedText, FormattedText> {
|
||||||
|
|
||||||
// Set color compatibilty
|
let r: (LineLocation, DaisyError);
|
||||||
let term_colors = stdout.available_colors().unwrap_or(0);
|
if command::is_command(s) {
|
||||||
if term_colors >= 256 {
|
return Ok(command::do_command(context, s));
|
||||||
context.config.term_color_type = 2;
|
} else if s.contains("=") {
|
||||||
} else if term_colors >= 8 {
|
let x = do_assignment(context, s);
|
||||||
context.config.term_color_type = 1;
|
match x {
|
||||||
|
Ok(t) => { return Ok(t) },
|
||||||
|
Err(t) => { r = t }
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
context.config.term_color_type = 0;
|
let x = do_expression(context, s);
|
||||||
|
match x {
|
||||||
|
Ok((t, e)) => { context.push_hist(e); return Ok(t) },
|
||||||
|
Err(t) => { r = t }
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
context.config.check();
|
let (l, e) = r;
|
||||||
|
let mut t = FormattedText::new("".to_string());
|
||||||
|
if l.zero() {
|
||||||
|
t.push(&format!(
|
||||||
// Handle command-line arguments
|
"\n {}\n\n",
|
||||||
let args: Vec<String> = env::args().collect();
|
e.text().to_string(),
|
||||||
if args.iter().any(|s| s == "--help") {
|
|
||||||
let t = command::do_command(&mut context, &String::from("help"));
|
|
||||||
t.write(&context, &mut stdout)?;
|
|
||||||
return Ok(());
|
|
||||||
} else if args.iter().any(|s| s == "--version") {
|
|
||||||
let t = FormattedText::new(format!(
|
|
||||||
"Daisy v{}\n", env!("CARGO_PKG_VERSION")
|
|
||||||
));
|
));
|
||||||
t.write(&context, &mut stdout)?;
|
} else {
|
||||||
return Ok(());
|
t.push(&format!(
|
||||||
} else if args.iter().any(|s| s == "--debug") {
|
|
||||||
let t = FormattedText::new(format!(
|
|
||||||
concat!(
|
concat!(
|
||||||
"Daisy v{}\n",
|
"{}[e]{}[n]\n",
|
||||||
"Your terminal supports {} colors.\n"
|
" {}\n\n"
|
||||||
),
|
),
|
||||||
env!("CARGO_PKG_VERSION"),
|
" ".repeat(l.pos + 4),
|
||||||
term_colors
|
"^".repeat(l.len),
|
||||||
|
e.text().to_string(),
|
||||||
));
|
));
|
||||||
t.write(&context, &mut stdout)?;
|
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
'outer: loop {
|
return Err(t);
|
||||||
|
|
||||||
pb.write_prompt(&mut context, &mut stdout)?;
|
|
||||||
|
|
||||||
let stdin = stdin();
|
|
||||||
for c in stdin.keys() {
|
|
||||||
if let Key::Char(q) = c.as_ref().unwrap() {
|
|
||||||
match q {
|
|
||||||
'\n' => {
|
|
||||||
// Print again without cursor, in case we pressed enter
|
|
||||||
// while inside a substitution
|
|
||||||
pb.write_prompt_nocursor(&mut context, &mut stdout)?;
|
|
||||||
let in_str = pb.enter();
|
|
||||||
FormattedText::newline(&mut stdout)?;
|
|
||||||
if in_str == "" { break; }
|
|
||||||
|
|
||||||
if in_str.trim() == "quit" {
|
|
||||||
break 'outer;
|
|
||||||
} else {
|
|
||||||
let r = crate::do_string(&mut context, &in_str);
|
|
||||||
|
|
||||||
match r {
|
|
||||||
Ok(t) | Err(t) => {
|
|
||||||
t.write(&context, &mut stdout).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 context, &mut stdout)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
FormattedText::newline(&mut stdout)?;
|
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle a simple evaluation string.
|
||||||
|
// Returns a FormattedText with output that should be printed.
|
||||||
|
#[inline(always)]
|
||||||
|
fn do_expression(
|
||||||
|
context: &mut Context,
|
||||||
|
s: &String
|
||||||
|
) -> Result<(FormattedText, parser::Expression), (LineLocation, DaisyError)> {
|
||||||
|
|
||||||
|
let mut output = FormattedText::new("".to_string());
|
||||||
|
|
||||||
|
let g = parser::parse(context, &s)?;
|
||||||
|
let g_evaluated = evaluate::evaluate(context, &g)?;
|
||||||
|
|
||||||
|
// Display parsed string
|
||||||
|
output.push(&format!(
|
||||||
|
" [s]=>[n] {}\n\n",
|
||||||
|
g.display(context)
|
||||||
|
));
|
||||||
|
|
||||||
|
// Display result
|
||||||
|
output.push(&format!(
|
||||||
|
" [r]=[n] {}\n\n",
|
||||||
|
g_evaluated.display_outer(context),
|
||||||
|
));
|
||||||
|
|
||||||
|
return Ok((output, g_evaluated));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Handle a variable or function definition string.
|
||||||
|
// Returns a FormattedText with output that should be printed.
|
||||||
|
#[inline(always)]
|
||||||
|
fn do_assignment(
|
||||||
|
context: &mut Context,
|
||||||
|
s: &String
|
||||||
|
) -> Result<FormattedText, (LineLocation, DaisyError)> {
|
||||||
|
|
||||||
|
let mut output = FormattedText::new("".to_string());
|
||||||
|
|
||||||
|
let parts = s.split("=").collect::<Vec<&str>>();
|
||||||
|
if parts.len() != 2 {
|
||||||
|
return Err((
|
||||||
|
LineLocation::new_zero(),
|
||||||
|
DaisyError::Syntax
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Index of first non-whitespace character in left
|
||||||
|
// (relative to whole prompt)
|
||||||
|
let starting_left = parts[0]
|
||||||
|
.char_indices()
|
||||||
|
.find(|(_, ch)| !(ch.is_whitespace() && *ch != '\n'))
|
||||||
|
.map(|(i, _)| i)
|
||||||
|
.unwrap_or_else(|| parts[0].len());
|
||||||
|
|
||||||
|
// Index of first non-whitespace character in right
|
||||||
|
// (relative to whole prompt)
|
||||||
|
// +1 accounts for equals sign
|
||||||
|
let starting_right = parts[0].chars().count() + 1 +
|
||||||
|
parts[1]
|
||||||
|
.char_indices()
|
||||||
|
.find(|(_, ch)| !(ch.is_whitespace() && *ch != '\n'))
|
||||||
|
.map(|(i, _)| i)
|
||||||
|
.unwrap_or_else(|| parts[0].len());
|
||||||
|
|
||||||
|
|
||||||
|
let left = substitute(context, &parts[0].trim().to_string());
|
||||||
|
let right = substitute(context, &parts[1].trim().to_string());
|
||||||
|
let is_function = left.contains("(");
|
||||||
|
|
||||||
|
// The order of methods below is a bit odd.
|
||||||
|
// This is intentional, since we want to check a definition's
|
||||||
|
// variable name before even attempting to parse its content.
|
||||||
|
if is_function {
|
||||||
|
let mut mode = 0;
|
||||||
|
let mut name = String::new();
|
||||||
|
let mut args = String::new();
|
||||||
|
for c in left.chars() {
|
||||||
|
match mode {
|
||||||
|
|
||||||
|
// Mode 0: reading function name
|
||||||
|
0 => {
|
||||||
|
if c == '(' {
|
||||||
|
mode = 1; continue;
|
||||||
|
} else { name.push(c); }
|
||||||
|
},
|
||||||
|
|
||||||
|
// Mode 1: reading arguments
|
||||||
|
1 => {
|
||||||
|
if c == ')' {
|
||||||
|
mode = 2; continue;
|
||||||
|
} else { args.push(c); }
|
||||||
|
},
|
||||||
|
|
||||||
|
// Mode 2: we should be done by now.
|
||||||
|
// That close paren should've been the last character.
|
||||||
|
2 => {
|
||||||
|
return Err((
|
||||||
|
LineLocation{ pos: starting_left, len: left.chars().count() },
|
||||||
|
DaisyError::Syntax
|
||||||
|
));
|
||||||
|
},
|
||||||
|
|
||||||
|
_ => unreachable!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let args = args
|
||||||
|
.split(",").collect::<Vec<&str>>()
|
||||||
|
.iter().map(|x| x.trim().to_string()).collect::<Vec<String>>();
|
||||||
|
|
||||||
|
if name.len() == 0 {
|
||||||
|
return Err((
|
||||||
|
LineLocation{ pos: starting_left, len: left.chars().count() },
|
||||||
|
DaisyError::Syntax
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
if !context.valid_function(&name) {
|
||||||
|
return Err((
|
||||||
|
LineLocation{ pos: starting_left, len: left.chars().count() },
|
||||||
|
DaisyError::BadFunction
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
if args.iter().find(|x| &x[..] == "").is_some() {
|
||||||
|
return Err((
|
||||||
|
LineLocation{ pos: starting_left, len: left.chars().count() },
|
||||||
|
DaisyError::Syntax
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
for a in &args {
|
||||||
|
if !context.valid_varible(a) {
|
||||||
|
return Err((
|
||||||
|
LineLocation{ pos: starting_left, len: left.chars().count() },
|
||||||
|
DaisyError::BadVariable
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse right hand side
|
||||||
|
let g = parser::parse(context, &right);
|
||||||
|
let Ok(g) = g else {
|
||||||
|
let Err((l, e)) = g else { unreachable!() };
|
||||||
|
return Err((
|
||||||
|
LineLocation{ pos: l.pos + starting_right, len: l.len},
|
||||||
|
e
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
// Display parsed string
|
||||||
|
output.push(&format!(
|
||||||
|
" [s]=>[n] {left} = {}\n\n",
|
||||||
|
g.display(context)
|
||||||
|
));
|
||||||
|
|
||||||
|
// Evaluate expression with shadow variables
|
||||||
|
for a in &args { context.add_shadow(a.to_string(), None);}
|
||||||
|
let g_evaluated = evaluate::evaluate(context, &g);
|
||||||
|
context.clear_shadow();
|
||||||
|
let Ok(_g_evaluated) = g_evaluated else {
|
||||||
|
let Err((l, e)) = g_evaluated else { unreachable!() };
|
||||||
|
return Err((
|
||||||
|
LineLocation{ pos: l.pos + starting_right, len: l.len},
|
||||||
|
e
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
// We could push g_evaluated instead, but an un-evaluated string
|
||||||
|
// makes the 'vars' command prettier.
|
||||||
|
//
|
||||||
|
// We still need to evaluate g above, though, to make sure it works.
|
||||||
|
context.push_function(name, args, g).unwrap();
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if !context.valid_varible(&left) {
|
||||||
|
return Err((
|
||||||
|
LineLocation{ pos: starting_left, len: left.chars().count() },
|
||||||
|
DaisyError::BadVariable
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse right hand side
|
||||||
|
let g = parser::parse(context, &right);
|
||||||
|
let Ok(g) = g else {
|
||||||
|
let Err((l, e)) = g else { unreachable!() };
|
||||||
|
return Err((
|
||||||
|
LineLocation{ pos: l.pos + starting_right, len: l.len},
|
||||||
|
e
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
// Display parsed string
|
||||||
|
output.push(&format!(
|
||||||
|
" [t]=>[n] {left} = {}\n\n",
|
||||||
|
g.display(context)
|
||||||
|
));
|
||||||
|
|
||||||
|
// Evaluate expression
|
||||||
|
let g_evaluated = evaluate::evaluate(context, &g);
|
||||||
|
let Ok(g_evaluated) = g_evaluated else {
|
||||||
|
let Err((l, e)) = g_evaluated else { unreachable!() };
|
||||||
|
return Err((
|
||||||
|
LineLocation{ pos: l.pos + starting_right, len: l.len},
|
||||||
|
e
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
context.push_variable(left.to_string(), g_evaluated).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(output);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
// Many of these have been borrowed from insect.
|
// Many of these have been borrowed from insect.
|
||||||
use daisycalc::parser;
|
use crate::parser;
|
||||||
use daisycalc::evaluate;
|
use crate::evaluate::evaluate;
|
||||||
use daisycalc::Context;
|
use crate::context::Context;
|
||||||
|
|
||||||
fn eval_to_str(s: &str) -> Result<String, ()> {
|
fn eval_to_str(s: &str) -> Result<String, ()> {
|
||||||
let g = match parser::parse_no_context(&String::from(s)) {
|
let g = match parser::parse_no_context(&String::from(s)) {
|
||||||
|
Reference in New Issue
Block a user