summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2024-04-27 22:10:03 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2024-04-27 22:10:03 +0200
commit0565ef1692f2fc4712cc55ebe25dc88f99d57c38 (patch)
tree76f8e50e3ee4c98c791e432317800b4673a88191
parent7c0409d1fc7b1816b97eb8da178efc98eb01ac15 (diff)
downloadrebel-0565ef1692f2fc4712cc55ebe25dc88f99d57c38.tar
rebel-0565ef1692f2fc4712cc55ebe25dc88f99d57c38.zip
rebel-lang: replace eval-string example with simple REPL
-rw-r--r--Cargo.lock101
-rw-r--r--crates/rebel-lang/Cargo.toml1
-rw-r--r--crates/rebel-lang/examples/repl.rs (renamed from crates/rebel-lang/examples/eval-string.rs)79
3 files changed, 130 insertions, 51 deletions
diff --git a/Cargo.lock b/Cargo.lock
index eebaa39..8bf3d88 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -177,6 +177,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
[[package]]
+name = "clipboard-win"
+version = "5.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "79f4473f5144e20d9aceaf2972478f06ddf687831eafeeb434fbaf0acc4144ad"
+dependencies = [
+ "error-code",
+]
+
+[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -256,6 +265,12 @@ dependencies = [
]
[[package]]
+name = "endian-type"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d"
+
+[[package]]
name = "enum-kinds"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -283,6 +298,23 @@ dependencies = [
]
[[package]]
+name = "error-code"
+version = "3.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a0474425d51df81997e2f90a21591180b38eccf27292d755f3e30750225c175b"
+
+[[package]]
+name = "fd-lock"
+version = "4.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7e5768da2206272c81ef0b5e951a41862938a6070da63bcea197899942d3b947"
+dependencies = [
+ "cfg-if",
+ "rustix",
+ "windows-sys 0.52.0",
+]
+
+[[package]]
name = "filetime"
version = "0.2.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -340,6 +372,15 @@ dependencies = [
]
[[package]]
+name = "home"
+version = "0.5.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5"
+dependencies = [
+ "windows-sys 0.52.0",
+]
+
+[[package]]
name = "indexmap"
version = "2.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -392,6 +433,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
[[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.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -511,6 +561,16 @@ dependencies = [
]
[[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 = "rebel"
version = "0.1.0"
dependencies = [
@@ -544,6 +604,7 @@ dependencies = [
"enum-kinds",
"rebel-common",
"rebel-parse",
+ "rustyline",
]
[[package]]
@@ -615,6 +676,28 @@ dependencies = [
]
[[package]]
+name = "rustyline"
+version = "14.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7803e8936da37efd9b6d4478277f4b2b9bb5cdb37a113e8d63222e58da647e63"
+dependencies = [
+ "bitflags 2.5.0",
+ "cfg-if",
+ "clipboard-win",
+ "fd-lock",
+ "home",
+ "libc",
+ "log",
+ "memchr",
+ "nix",
+ "radix_trie",
+ "unicode-segmentation",
+ "unicode-width",
+ "utf8parse",
+ "windows-sys 0.52.0",
+]
+
+[[package]]
name = "ryu"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -685,6 +768,12 @@ dependencies = [
]
[[package]]
+name = "smallvec"
+version = "1.13.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
+
+[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -817,6 +906,18 @@ dependencies = [
]
[[package]]
+name = "unicode-segmentation"
+version = "1.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
+
+[[package]]
+name = "unicode-width"
+version = "0.1.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "68f5e5f3158ecfd4b8ff6fe086db7c8467a2dfdac97fe420f2b7c4aa97af66d6"
+
+[[package]]
name = "unsafe-libyaml"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/crates/rebel-lang/Cargo.toml b/crates/rebel-lang/Cargo.toml
index 44183be..0c2f684 100644
--- a/crates/rebel-lang/Cargo.toml
+++ b/crates/rebel-lang/Cargo.toml
@@ -15,3 +15,4 @@ enum-kinds = "0.5.1"
[dev-dependencies]
clap = { version = "4.0.0", features = ["derive"] }
+rustyline = "14.0.0"
diff --git a/crates/rebel-lang/examples/eval-string.rs b/crates/rebel-lang/examples/repl.rs
index a9c0b01..0163c6b 100644
--- a/crates/rebel-lang/examples/eval-string.rs
+++ b/crates/rebel-lang/examples/repl.rs
@@ -1,7 +1,3 @@
-use std::{fmt::Debug, process, time::Instant};
-
-use clap::Parser;
-
use rebel_lang::{
func::{Func, FuncDef, FuncType},
typing::{ArrayLen, Type, TypeFamily},
@@ -9,11 +5,6 @@ use rebel_lang::{
};
use rebel_parse::{recipe, tokenize};
-#[derive(Clone, Debug, Parser)]
-struct Opts {
- input: String,
-}
-
fn intrinsic_array_len(params: &[Value]) -> Result<Value> {
assert!(params.len() == 1);
let Value::Array(array) = &params[0] else {
@@ -31,36 +22,7 @@ fn intrinsic_string_len(params: &[Value]) -> Result<Value> {
))
}
-fn main() {
- let opts: Opts = Opts::parse();
- let input = opts.input.trim();
-
- let start = Instant::now();
- let result = tokenize::token_stream(input);
- let dur = Instant::now().duration_since(start);
- println!("Tokenization took {} µs", dur.as_micros());
-
- let tokens = match result {
- Ok(value) => value,
- Err(err) => {
- println!("{err}");
- process::exit(1);
- }
- };
-
- let start = Instant::now();
- let result = recipe::expr(&tokens);
- let dur = Instant::now().duration_since(start);
- println!("Parsing took {} µs", dur.as_micros());
-
- let expr = match result {
- Ok(value) => value,
- Err(err) => {
- println!("{err}");
- process::exit(1);
- }
- };
-
+fn main() -> rustyline::Result<()> {
let mut ctx = Context::default();
ctx.methods.entry(TypeFamily::Array).or_default().insert(
@@ -84,18 +46,33 @@ fn main() {
},
);
- let start = Instant::now();
- let result = ctx.eval(&expr);
- let dur = Instant::now().duration_since(start);
- println!("Eval took {} µs", dur.as_micros());
+ let mut rl = rustyline::DefaultEditor::new()?;
- let value = match result {
- Ok(value) => value,
- Err(err) => {
- println!("{err:?}");
- process::exit(1);
- }
- };
+ while let Ok(line) = rl.readline("> ") {
+ rl.add_history_entry(line.as_str())?;
+ let tokens = match tokenize::token_stream(&line) {
+ Ok(value) => value,
+ Err(err) => {
+ println!("Tokenize error: {err}");
+ continue;
+ }
+ };
+ let expr = match recipe::expr(&tokens) {
+ Ok(value) => value,
+ Err(err) => {
+ println!("Parse error: {err}");
+ continue;
+ }
+ };
+ let value = match ctx.eval(&expr) {
+ Ok(value) => value,
+ Err(err) => {
+ println!("Eval error: {err:?}");
+ continue;
+ }
+ };
+ println!("{value}");
+ }
- println!("{value}");
+ Ok(())
}