forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.rs
27 lines (24 loc) · 888 Bytes
/
eval.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use crate::{compiler, scope::Scope, PyResult, VirtualMachine};
pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str) -> PyResult {
match vm.compile(source, compiler::Mode::Eval, source_path.to_owned()) {
Ok(bytecode) => {
debug!("Code object: {:?}", bytecode);
vm.run_code_obj(bytecode, scope)
}
Err(err) => Err(vm.new_syntax_error(&err, Some(source))),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Interpreter;
#[test]
fn test_print_42() {
Interpreter::without_stdlib(Default::default()).enter(|vm| {
let source = String::from("print('Hello world')");
let vars = vm.new_scope_with_builtins();
let result = eval(vm, &source, vars, "<unittest>").expect("this should pass");
assert!(vm.is_none(&result));
})
}
}