From 5ad89874fb2e7faf14838295c328b87e9ffe5eea Mon Sep 17 00:00:00 2001 From: Erin Date: Thu, 21 Jul 2022 17:44:00 +0200 Subject: [PATCH] added repl --- Cargo.lock | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 1 + src/lib.rs | 1 + src/main.rs | 17 +---- src/repl.rs | 26 ++++++++ 5 files changed, 203 insertions(+), 22 deletions(-) create mode 100644 src/repl.rs diff --git a/Cargo.lock b/Cargo.lock index 4a000d8..f47755d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -139,7 +139,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf2eec61efe56aa1e813f5126959296933cf0700030e4314786c48779a66ab82" dependencies = [ "log", - "nix", + "nix 0.22.3", ] [[package]] @@ -387,6 +387,27 @@ dependencies = [ "syn", ] +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if 1.0.0", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "dispatch" version = "0.2.0" @@ -479,6 +500,12 @@ dependencies = [ "bytemuck", ] +[[package]] +name = "endian-type" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" + [[package]] name = "epaint" version = "0.18.1" @@ -494,6 +521,27 @@ dependencies = [ "parking_lot 0.12.1", ] +[[package]] +name = "errno" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "error-code" version = "2.3.1" @@ -504,6 +552,17 @@ dependencies = [ "str-buf", ] +[[package]] +name = "fd-lock" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e11dcc7e4d79a8c89b9ab4c6f5c30b1fc4a83c420792da3542fd31179ed5f517" +dependencies = [ + "cfg-if 1.0.0", + "rustix", + "windows-sys", +] + [[package]] name = "fnv" version = "1.0.7" @@ -680,6 +739,12 @@ dependencies = [ "web-sys", ] +[[package]] +name = "io-lifetimes" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24c3f4eff5495aee4c0399d7b6a0dc2b6e81be84242ffbfcf253ebacccc1d0cb" + [[package]] name = "jni" version = "0.19.0" @@ -737,6 +802,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "linux-raw-sys" +version = "0.0.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4d2456c373231a208ad294c33dc5bff30051eafd954cd4caae83a712b12854d" + [[package]] name = "lock_api" version = "0.4.7" @@ -926,6 +997,15 @@ dependencies = [ "jni-sys", ] +[[package]] +name = "nibble_vec" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" +dependencies = [ + "smallvec", +] + [[package]] name = "nix" version = "0.22.3" @@ -939,6 +1019,17 @@ dependencies = [ "memoffset", ] +[[package]] +name = "nix" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "195cdbc1741b8134346d515b3a56a1c94b0912758009cfd53f99ea0f57b065fc" +dependencies = [ + "bitflags", + "cfg-if 1.0.0", + "libc", +] + [[package]] name = "nohash-hasher" version = "0.2.0" @@ -1147,6 +1238,16 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radix_trie" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd" +dependencies = [ + "endian-type", + "nibble_vec", +] + [[package]] name = "raw-window-handle" version = "0.4.3" @@ -1165,12 +1266,60 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall", + "thiserror", +] + [[package]] name = "regex-syntax" version = "0.6.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64" +[[package]] +name = "rustix" +version = "0.35.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d51cc38aa10f6bbb377ed28197aa052aa4e2b762c22be9d3153d01822587e787" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "rustyline" +version = "10.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1cd5ae51d3f7bf65d7969d579d502168ef578f289452bd8ccc91de28fda20e" +dependencies = [ + "bitflags", + "cfg-if 1.0.0", + "clipboard-win", + "dirs-next", + "fd-lock", + "libc", + "log", + "memchr", + "nix 0.24.2", + "radix_trie", + "scopeguard", + "unicode-segmentation", + "unicode-width", + "utf8parse", + "winapi", +] + [[package]] name = "same-file" version = "1.0.6" @@ -1235,7 +1384,7 @@ dependencies = [ "lazy_static", "log", "memmap2", - "nix", + "nix 0.22.3", "pkg-config", "wayland-client", "wayland-cursor", @@ -1377,6 +1526,18 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99" + +[[package]] +name = "unicode-width" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" + [[package]] name = "url" version = "2.2.2" @@ -1389,6 +1550,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8parse" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372" + [[package]] name = "version_check" version = "0.9.4" @@ -1487,7 +1654,7 @@ dependencies = [ "bitflags", "downcast-rs", "libc", - "nix", + "nix 0.22.3", "scoped-tls", "wayland-commons", "wayland-scanner", @@ -1500,7 +1667,7 @@ version = "0.29.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94f6e5e340d7c13490eca867898c4cec5af56c27a5ffe5c80c6fc4708e22d33e" dependencies = [ - "nix", + "nix 0.22.3", "once_cell", "smallvec", "wayland-sys", @@ -1512,7 +1679,7 @@ version = "0.29.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c52758f13d5e7861fc83d942d3d99bf270c83269575e52ac29e5b73cb956a6bd" dependencies = [ - "nix", + "nix 0.22.3", "wayland-client", "xcursor", ] @@ -1570,6 +1737,7 @@ dependencies = [ "eframe", "logos", "ordered-float", + "rustyline", ] [[package]] @@ -1736,7 +1904,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e99be55648b3ae2a52342f9a870c0e138709a3493261ce9b469afe6e4df6d8a" dependencies = [ "gethostname", - "nix", + "nix 0.22.3", "winapi", "winapi-wsapoll", ] diff --git a/Cargo.toml b/Cargo.toml index decf283..25de363 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ chumsky = "0.8" eframe = "*" logos = "*" ordered-float = "3.0" +rustyline = "10.0" diff --git a/src/lib.rs b/src/lib.rs index b1e3461..d015d07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,5 @@ pub mod error; pub mod lexer; pub mod list; pub mod parser; +pub mod repl; pub mod value; diff --git a/src/main.rs b/src/main.rs index 612b582..3ce2655 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,7 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use web_lisp::{error::Error, parser}; - fn main() -> Result<(), Box> { - let src = std::fs::read_to_string(std::env::args().nth(1).ok_or("no filename provided")?)?; - - match parser::read(&src) { - Ok(vals) => { - for val in vals { - println!("{val}"); - } - } - Err(errs) => { - for err in errs { - Error::Parse(err).report(&src)?; - } - } - } + web_lisp::repl::repl()?; Ok(()) } diff --git a/src/repl.rs b/src/repl.rs new file mode 100644 index 0000000..d3b9bef --- /dev/null +++ b/src/repl.rs @@ -0,0 +1,26 @@ +use rustyline::{error::ReadlineError, Editor}; + +use crate::{error::Error, parser::read}; + +pub fn repl() -> rustyline::Result<()> { + let prompt = "#=> "; + let mut rl = Editor::<()>::new()?; + loop { + match rl.readline(prompt) { + Ok(line) => match read(&line) { + Ok(values) => values.iter().for_each(|e| println!("{e}")), + Err(e) => e.into_iter().map(Error::Parse).for_each(|e| { + if let Err(e) = e.report(&line) { + eprintln!("Failed to generate error report\n{e:?}") + } + }), + }, + Err(ReadlineError::Eof) => { + println!("Bye!"); + break Ok(()); + } + Err(ReadlineError::Interrupted) => (), + Err(e) => return Err(e), + } + } +}