Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| lib:repl [2020/11/08 19:30] – sprowell | lib:repl [2020/11/15 23:14] (current) – Stopped at printing; more needs to be done. sprowell | ||
|---|---|---|---|
| Line 15: | Line 15: | ||
| The minimum that must be done is to make a new instance of the '' | The minimum that must be done is to make a new instance of the '' | ||
| - | The one wrinkle is that you must provide some data to the constructor. | + | The one wrinkle is that you must provide some data to the constructor. |
| <code Rust [enable_line_numbers=" | <code Rust [enable_line_numbers=" | ||
| use relision:: | use relision:: | ||
| - | use relision::main:: | + | use relision:: |
| fn main() { | fn main() { | ||
| configure_logging(); | configure_logging(); | ||
| Line 51: | Line 51: | ||
| Let's add a custom evaluator for lines. | Let's add a custom evaluator for lines. | ||
| - | To do this we need to create a function that takes a ''& | + | To do this we need to create a function that takes a ''& |
| - | * If the returned '' | + | * If the returned '' |
| * If the Boolean is '' | * If the Boolean is '' | ||
| Line 61: | Line 61: | ||
| use relision:: | use relision:: | ||
| - | fn echo(line: & | + | fn echo<U>(line: & |
| let answer = line.to_uppercase(); | let answer = line.to_uppercase(); | ||
| let cont = answer != " | let cont = answer != " | ||
| Line 68: | Line 68: | ||
| </ | </ | ||
| - | We install | + | The ''< |
| + | |||
| + | We install | ||
| <code Rust [enable_line_numbers=" | <code Rust [enable_line_numbers=" | ||
| use relision:: | use relision:: | ||
| + | use relision:: | ||
| + | |||
| + | fn echo< | ||
| + | let answer = line.to_uppercase(); | ||
| + | let cont = answer != " | ||
| + | (Some(answer), | ||
| + | } | ||
| fn main() { | fn main() { | ||
| - | let mut repl = repl:: | + | let mut repl = repl:: |
| repl.set_eval(echo); | repl.set_eval(echo); | ||
| repl.run(); | repl.run(); | ||
| Line 112: | Line 121: | ||
| use relision:: | use relision:: | ||
| - | fn noop(_line: & | + | fn noop<U>(_line: & |
| (None, true) | (None, true) | ||
| } | } | ||
| - | fn echo(line: & | + | fn echo<U>(line: & |
| let answer = line.to_uppercase(); | let answer = line.to_uppercase(); | ||
| let cont = answer != " | let cont = answer != " | ||
| Line 152: | Line 161: | ||
| </ | </ | ||
| - | Again, let's try this. | + | Again, let's try this. Note that this is '' |
| < | < | ||
| Line 179: | Line 188: | ||
| Our functions will probably want to do something a little more sophisticated, | Our functions will probably want to do something a little more sophisticated, | ||
| - | * The line editor (provided by the [[https:// | + | * The line editor (provided by the [[https:// |
| * The terminal (provided by the [[https:// | * The terminal (provided by the [[https:// | ||
| - | * The current value of the " | + | * The current value of the " |
| * Some user data, which can hold anything we want. | * Some user data, which can hold anything we want. | ||
| + | |||
| + | ==== Reading a Line ==== | ||
| + | |||
| + | You can read a line from the console using '' | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | Lines read in this way are not automatically added to the history; you can use '' | ||
| + | |||
| + | ==== Printing ==== | ||
| + | |||
| + | The context provides '' | ||
| + | |||
| + | Let's add a guessing game to the program. | ||
| + | |||
| + | <code Rust [enable_line_numbers=" | ||
| + | pub struct Counts { | ||
| + | pub win: u32, | ||
| + | pub lose: u32, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Now we will specialize '' | ||
| + | |||
| + | <code Rust [enable_line_numbers=" | ||
| + | fn guess(_line: | ||
| + | let limit = 10; | ||
| + | let high = 100; | ||
| + | // Tell the user the rules. | ||
| + | sayln!(context, | ||
| + | sayln!(context, | ||
| + | context.get().win + context.get().lose, | ||
| + | sayln!(context, | ||
| + | // Pick a random number. | ||
| + | let secret = rand:: | ||
| + | // Collect guesses. | ||
| + | let mut attempt = 1; | ||
| + | while attempt < limit { | ||
| + | // Get the next guess. | ||
| + | let line = context.editor.readline( | ||
| + | format!(" | ||
| + | match line { | ||
| + | Ok(text) => { | ||
| + | sayln!(context, | ||
| + | match text.trim().parse::< | ||
| + | Ok(number) => { | ||
| + | if number < secret { | ||
| + | sayln!(context, | ||
| + | } else if number > secret { | ||
| + | sayln!(context, | ||
| + | } else { | ||
| + | sayln!(context, | ||
| + | context.get_mut().win += 1; | ||
| + | return (None, true); | ||
| + | } | ||
| + | }, | ||
| + | Err(_) => { | ||
| + | sayln!(context, | ||
| + | } | ||
| + | } | ||
| + | }, | ||
| + | Err(ReadlineError:: | ||
| + | // This is a CTRL+C. | ||
| + | sayln!(context, | ||
| + | return (None, true); | ||
| + | }, | ||
| + | Err(ReadlineError:: | ||
| + | // This is end of file (CTRL+D). | ||
| + | sayln!(context, | ||
| + | return (None, true); | ||
| + | }, | ||
| + | Err(err) => { | ||
| + | // This is just an error. | ||
| + | error!(" | ||
| + | } | ||
| + | } | ||
| + | attempt += 1; | ||
| + | } // Collect guesses. | ||
| + | context.get_mut().lose += 1; | ||
| + | sayln!(context, | ||
| + | (None, true) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | There is a lot going on in this function. | ||
| + | |||
| + | If you wanted to add the line to the history, you would do the following. | ||
| + | |||
| + | <code Rust [enable_line_numbers=true]> | ||
| + | context.editor.add_history_entry(text.clone()); | ||
| + | </ | ||
| + | |||
| + | |||