Skip to content

Commit

Permalink
Add editing support to the REPL, using the rustyline crate.
Browse files Browse the repository at this point in the history
This as an optional default feature, "repl-edit".
  • Loading branch information
jimblandy committed Aug 2, 2017
1 parent 650c648 commit 0a67163
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 24 deletions.
8 changes: 7 additions & 1 deletion lisp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ name = "lisp"
version = "0.1.0"
workspace = ".."

[features]
default = ["repl-edit"]

# Provide interactive editing and history in the lisp REPL.
repl-edit = ["rustyline"]

[dependencies]
nom = { git = "https://github.com/Geal/nom", branch = "master" }
lazy_static = "0.2.8"
error-chain = "0.10"
rustyline = { optional = true, version = "1.0.0" }

[dependencies.cell-gc]
path = ".."

[dependencies.cell-gc-derive]
path = "../cell-gc-derive"

11 changes: 11 additions & 0 deletions lisp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ extern crate nom;
#[macro_use]
extern crate error_chain;

#[cfg(feature = "repl-edit")]
extern crate rustyline;

pub mod errors {
error_chain!{}
}
Expand All @@ -18,3 +21,11 @@ pub mod parse;
pub mod repl;
pub mod value;
pub mod vm;

#[cfg(feature = "repl-edit")]
#[path = "rustyline-prompt.rs"]
mod prompt;

#[cfg(not(feature = "repl-edit"))]
#[path = "plain-prompt.rs"]
mod prompt;
32 changes: 32 additions & 0 deletions lisp/src/plain-prompt.rs
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));
}
}
44 changes: 21 additions & 23 deletions lisp/src/repl.rs
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;
}

// Print
if !result.is_unspecified() {
println!("{}", result);
// Print
if !result.is_unspecified() {
println!("{}", result);
}
},
None => return Ok(()),
}

// Loop...
}

Ok(())
})
}
38 changes: 38 additions & 0 deletions lisp/src/rustyline-prompt.rs
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)),
}
}
}
}

0 comments on commit 0a67163

Please sign in to comment.