|
| 1 | +///! This example show cases a very simple REPL. |
| 2 | +///! While a much better REPL can be found in ../src/shell, |
| 3 | +///! This much smaller REPL is still a useful example because it showcases inserting |
| 4 | +///! values and functions into the Python runtime's scope, and showcases use |
| 5 | +///! of the compilation mode "Single". |
| 6 | +///! Note that in particular this REPL does a horrible job of showing users their errors |
| 7 | +///! (instead it simply crashes). |
| 8 | +use rustpython_compiler::compile; |
| 9 | +use rustpython_vm::{pyobject::PyResult, PySettings, VirtualMachine}; |
| 10 | +// this needs to be in scope in order to insert things into scope.globals |
| 11 | +use rustpython_vm::pyobject::ItemProtocol; |
| 12 | + |
| 13 | +fn main() -> PyResult<()> { |
| 14 | + let mut on = true; |
| 15 | + |
| 16 | + let mut input = String::with_capacity(50); |
| 17 | + let stdin = std::io::stdin(); |
| 18 | + |
| 19 | + let vm = VirtualMachine::new(PySettings::default()); |
| 20 | + let scope = vm.new_scope_with_builtins(); |
| 21 | + |
| 22 | + // typing `quit()` is too long, let's make `on(False)` work instead. |
| 23 | + scope |
| 24 | + .globals |
| 25 | + .set_item( |
| 26 | + "on", |
| 27 | + vm.context().new_rustfunc({ |
| 28 | + let on: *mut bool = &mut on; |
| 29 | + move |b: bool, _: &VirtualMachine| unsafe { *on = b } |
| 30 | + }), |
| 31 | + &vm, |
| 32 | + ) |
| 33 | + .unwrap(); |
| 34 | + |
| 35 | + while on { |
| 36 | + input.clear(); |
| 37 | + stdin.read_line(&mut input).unwrap(); |
| 38 | + |
| 39 | + let code_obj = vm |
| 40 | + .compile(&input, compile::Mode::Single, "<embedded>".to_string()) |
| 41 | + .map_err(|err| vm.new_syntax_error(&err))?; |
| 42 | + |
| 43 | + // this line also automatically prints the output |
| 44 | + let output = vm.run_code_obj(code_obj, scope.clone())?; |
| 45 | + |
| 46 | + // store the last value in the "last" variable |
| 47 | + if !vm.is_none(&output) { |
| 48 | + scope.globals.set_item("last", output, &vm).unwrap(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + Ok(()) |
| 53 | +} |
0 commit comments