MCPcopy Index your code
hub / github.com/RustPython/RustPython / run

Function run

examples/mini_repl.rs:36–85  ·  view source on GitHub ↗
(vm: &vm::VirtualMachine)

Source from the content-addressed store, hash-verified

34}
35
36fn 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#"\
54def 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}

Callers

nothing calls this directly

Calls 13

new_functionMethod · 0.80
new_syntax_errorMethod · 0.80
run_code_objMethod · 0.80
print_exceptionMethod · 0.80
SomeClass · 0.50
set_itemMethod · 0.45
loadMethod · 0.45
clearMethod · 0.45
compileMethod · 0.45
to_ownedMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected