summaryrefslogtreecommitdiffstats
path: root/crates
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 /crates
parent7c0409d1fc7b1816b97eb8da178efc98eb01ac15 (diff)
downloadrebel-0565ef1692f2fc4712cc55ebe25dc88f99d57c38.tar
rebel-0565ef1692f2fc4712cc55ebe25dc88f99d57c38.zip
rebel-lang: replace eval-string example with simple REPL
Diffstat (limited to 'crates')
-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
2 files changed, 29 insertions, 51 deletions
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(())
}