(vm: &vm::VirtualMachine)
| 34 | } |
| 35 | |
| 36 | fn run(vm: &vm::VirtualMachine) -> vm::PyResult<()> { |
| 37 | let mut input = String::with_capacity(50); |
| 38 | let stdin = std::io::stdin(); |
| 39 | |
| 40 | let scope: vm::scope::Scope = vm.new_scope_with_builtins(); |
| 41 | |
| 42 | // typing `quit()` is too long, let's make `on(False)` work instead. |
| 43 | scope |
| 44 | .globals |
| 45 | .set_item("on", vm.new_function("on", on).into(), vm)?; |
| 46 | |
| 47 | // let's include a fibonacci function, but let's be lazy and write it in Python |
| 48 | add_python_function!( |
| 49 | scope, |
| 50 | vm, |
| 51 | // a fun line to test this with is |
| 52 | // ''.join( l * fib(i) for i, l in enumerate('supercalifragilistic') ) |
| 53 | r#"\ |
| 54 | def fib(n): |
| 55 | return n if n <= 1 else fib(n - 1) + fib(n - 2) |
| 56 | "# |
| 57 | )?; |
| 58 | |
| 59 | while ON.load(Ordering::Relaxed) { |
| 60 | input.clear(); |
| 61 | stdin |
| 62 | .read_line(&mut input) |
| 63 | .expect("Failed to read line of input"); |
| 64 | |
| 65 | // this line also automatically prints the output |
| 66 | // (note that this is only the case when compiler::Mode::Single is passed to vm.compile) |
| 67 | match vm |
| 68 | .compile(&input, vm::compiler::Mode::Single, "<embedded>".to_owned()) |
| 69 | .map_err(|err| vm.new_syntax_error(&err, Some(&input))) |
| 70 | .and_then(|code_obj| vm.run_code_obj(code_obj, scope.clone())) |
| 71 | { |
| 72 | Ok(output) => { |
| 73 | // store the last value in the "last" variable |
| 74 | if !vm.is_none(&output) { |
| 75 | scope.globals.set_item("last", output, vm)?; |
| 76 | } |
| 77 | } |
| 78 | Err(exc) => { |
| 79 | vm.print_exception(exc); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Ok(()) |
| 85 | } |
nothing calls this directly
no test coverage detected