This is an old revision of the document!
Using the REPL
The Relision library provides a generic read, evaluate, print loop (a REPL) that is available through the `relision::repl::Repl` struct. This provides the following services.
- Loading and saving of command history
- Reading lines from the prompt with history
- Detecting and executing commands, including providing help
- Word wrapping of the output
- Coloring of error and other message types.
The REPL support a concept of commands. These are indicated by a leading colon, such as :help.
Simplest Use
The minimum that must be done is to make a new instance of the Repl struct and then invoke its run method. This will not load or save any history, and it will provide the default set of commands and will, by default, just echo other lines.
use relision::repl; fn main() { let mut repl = repl::Repl::new(); repl.run(()); }
Adding Line Evaluation
Let's add custom evaluation of lines. Our “evaluation” will consist of just echoing the line, converted to all upper case and, if the line is “QUIT” then we will also exit the REPL.
To do this we need to create a function that takes a &String and a relision::repl::ReplContext. The &String is the line to evaluate, and the ReplContext packages information from the REPL that we might need. The return is an Option<String> and a Boolean. If the returned Option<String> is not None then it is output. If the Boolean is true then the REPL continues, and if it is false the REPL terminates.
The function we want looks like this.
fn echo(line: &String, _context: ReplContext) -> (Option<String>, bool) { let answer = line.to_uppercase(); let cont = answer != "QUIT"; (Some(answer), cont) }
We install it as the evaluation function after creating the instance and before invoking run.
fn main() { let mut repl = repl::Repl::new(); repl.set_eval(echo); repl.run(); }