-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add editing support to the REPL, using the rustyline crate.
This as an optional default feature, "repl-edit".
- Loading branch information
Showing
5 changed files
with
109 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//! Types and functions for reading input interactively from the user. | ||
//! | ||
//! If you build this crate with the `repl-edit` feature enabled (the default), | ||
//! then the read-eval-print loop provides shell-like editing commands and | ||
//! history for your expressions, using the `rustyline` crate. Otherwise, it | ||
//! just reads from the standard input directly. Both versions define a `Prompt` | ||
//! type, with the same interface. | ||
use std::io::{self, Write}; | ||
|
||
pub struct Prompt; | ||
|
||
impl Prompt { | ||
/// Return a new `Prompt` value. | ||
pub fn new() -> Prompt { | ||
Prompt | ||
} | ||
|
||
/// Read an expression from the user. Return `Ok(Some(input))` if we read | ||
/// the input successfully, `Ok(None)` on end-of-file, or `Err(e)` otherwise. | ||
pub fn read_expr(&mut self, prompt: &str) -> io::Result<Option<String>> { | ||
print!("{}", prompt); | ||
io::stdout().flush()?; | ||
|
||
let mut source = String::new(); | ||
io::stdin().read_line(&mut source)?; | ||
if source.is_empty() { | ||
return Ok(None); | ||
} | ||
return Ok(Some(source)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,40 @@ | ||
use cell_gc; | ||
use parse; | ||
use std::io::{self, Write}; | ||
use prompt; | ||
use std::io; | ||
use value::Value; | ||
use vm; | ||
|
||
pub fn repl() -> io::Result<()> { | ||
let mut prompt = prompt::Prompt::new(); | ||
|
||
cell_gc::with_heap(|hs| { | ||
let env = vm::Environment::default_env(hs); | ||
|
||
loop { | ||
print!("lisp> "); | ||
io::stdout().flush()?; | ||
|
||
// Read | ||
let mut source = String::new(); | ||
io::stdin().read_line(&mut source)?; | ||
if source.is_empty() { | ||
break; | ||
} | ||
let exprs = parse::parse(hs, &source) | ||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.description()))?; | ||
match prompt.read_expr("lisp> ")? { | ||
Some(source) => { | ||
let exprs = parse::parse(hs, &source) | ||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.description()))?; | ||
|
||
// Eval | ||
let mut result = Value::Unspecified; | ||
for expr in exprs { | ||
let val = vm::eval(hs, expr, env.clone()) | ||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.description()))?; | ||
result = val; | ||
} | ||
// Eval | ||
let mut result = Value::Unspecified; | ||
for expr in exprs { | ||
let val = vm::eval(hs, expr, env.clone()) | ||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.description()))?; | ||
result = val; | ||
} | ||
|
||
if !result.is_unspecified() { | ||
println!("{}", result); | ||
if !result.is_unspecified() { | ||
println!("{}", result); | ||
} | ||
}, | ||
None => return Ok(()), | ||
} | ||
|
||
// Loop... | ||
} | ||
|
||
Ok(()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//! Types and functions for reading input interactively from the user. | ||
//! | ||
//! If you build this crate with the `repl-edit` feature enabled (the default), | ||
//! then the read-eval-print loop provides shell-like editing commands and | ||
//! history for your expressions, using the `rustyline` crate. Otherwise, it | ||
//! just reads from the standard input directly. Both versions define a `Prompt` | ||
//! type, with the same interface. | ||
use std::io; | ||
use rustyline; | ||
use rustyline::error::ReadlineError; | ||
|
||
pub struct Prompt(rustyline::Editor<()>); | ||
|
||
impl Prompt { | ||
/// Return a new `Prompt` value. | ||
pub fn new() -> Prompt { | ||
Prompt(rustyline::Editor::<()>::new()) | ||
} | ||
|
||
/// Read an expression from the user. Return `Ok(Some(input))` if we read | ||
/// the input successfully, `Ok(None)` on end-of-file, or `Err(e)` otherwise. | ||
pub fn read_expr(&mut self, prompt: &str) -> io::Result<Option<String>> { | ||
loop { | ||
match self.0.readline(prompt) { | ||
Ok(source) => { | ||
self.0.add_history_entry(&source); | ||
return Ok(Some(source)) | ||
}, | ||
Err(ReadlineError::Eof) => return Ok(None), | ||
Err(ReadlineError::Interrupted) => { | ||
println!("Interrupted."); | ||
}, | ||
Err(other) => return Err(io::Error::new(io::ErrorKind::Other, other)), | ||
} | ||
} | ||
} | ||
} |