This commit is contained in:
Stephen Chung 2022-01-29 15:59:24 +08:00
commit 2cfd426aaf
3 changed files with 58 additions and 22 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ Cargo.lock
benches/results benches/results
before* before*
after* after*
.rhai-repl-history.txt

View File

@ -15,12 +15,22 @@ include = ["**/*.rs", "scripts/*.rhai", "**/*.md", "Cargo.toml"]
keywords = ["scripting", "scripting-engine", "scripting-language", "embedded"] keywords = ["scripting", "scripting-engine", "scripting-language", "embedded"]
categories = ["no-std", "embedded", "wasm", "parser-implementations"] categories = ["no-std", "embedded", "wasm", "parser-implementations"]
[lib]
name = "rhai"
path = "src/lib.rs"
[[bin]]
name = "rhai-repl"
path = "src/bin/rhai-repl.rs"
required-features = ["rustyline"]
[dependencies] [dependencies]
smallvec = { version = "1.7", default-features = false, features = ["union", "const_new" ] } smallvec = { version = "1.7", default-features = false, features = ["union", "const_new" ] }
ahash = { version = "0.7", default-features = false } ahash = { version = "0.7", default-features = false }
num-traits = { version = "0.2", default-features = false } num-traits = { version = "0.2", default-features = false }
smartstring = { version = "0.2.8", default-features = false } smartstring = { version = "0.2.8", default-features = false }
rhai_codegen = { version = "1.2", path = "codegen", default-features = false } rhai_codegen = { version = "1.2", path = "codegen", default-features = false }
rustyline = { versoin = "9", optional = true }
[dev-dependencies] [dev-dependencies]
serde_bytes = "0.11" serde_bytes = "0.11"

View File

@ -1,3 +1,5 @@
use rustyline::error::ReadlineError;
use rustyline::Editor;
use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST, INT}; use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST, INT};
use std::{ use std::{
@ -191,6 +193,12 @@ fn main() {
// Create scope // Create scope
let mut scope = Scope::new(); let mut scope = Scope::new();
// REPL line editor setup
let mut rl = Editor::<()>::new();
if rl.load_history(".rhai-repl-history.txt").is_err() {
println!("No previous history.");
}
// REPL loop // REPL loop
let mut input = String::new(); let mut input = String::new();
let mut main_ast = AST::empty(); let mut main_ast = AST::empty();
@ -200,33 +208,48 @@ fn main() {
print_help(); print_help();
'main_loop: loop { 'main_loop: loop {
print!("rhai-repl> ");
stdout().flush().expect("couldn't flush stdout");
input.clear(); input.clear();
loop { let readline = rl.readline("rhai-repl> ");
match stdin().read_line(&mut input) {
Ok(0) => break 'main_loop, match readline {
Ok(_) => (), Ok(line) => {
Err(err) => panic!("input error: {}", err), rl.add_history_entry(line.as_str());
input = line;
},
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => {
break 'main_loop
},
Err(err) => {
eprintln!("Error: {:?}", err);
break 'main_loop
} }
let line = input.as_str().trim_end();
// Allow line continuation
if line.ends_with('\\') {
let len = line.len();
input.truncate(len - 1);
input.push('\n');
} else {
break;
}
print!("> ");
stdout().flush().expect("couldn't flush stdout");
} }
//loop {
// match stdin().read_line(&mut input) {
// Ok(0) => break 'main_loop,
// Ok(_) => (),
// Err(err) => panic!("input error: {}", err),
// }
// let line = input.as_str().trim_end();
// // Allow line continuation
// if line.ends_with('\\') {
// let len = line.len();
// input.truncate(len - 1);
// input.push('\n');
// } else {
// break;
// }
// print!("> ");
// stdout().flush().expect("couldn't flush stdout");
//}
let script = input.trim(); let script = input.trim();
if script.is_empty() { if script.is_empty() {
@ -341,4 +364,6 @@ fn main() {
// Throw away all the statements, leaving only the functions // Throw away all the statements, leaving only the functions
main_ast.clear_statements(); main_ast.clear_statements();
} }
rl.save_history(".rhai-repl-history.txt").unwrap();
} }