forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.rs
More file actions
45 lines (39 loc) · 1.15 KB
/
eval.rs
File metadata and controls
45 lines (39 loc) · 1.15 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
extern crate rustpython_parser;
use std::error::Error;
use crate::compile;
use crate::frame::ScopeRef;
use crate::pyobject::PyResult;
use crate::vm::VirtualMachine;
pub fn eval(vm: &mut VirtualMachine, source: &str, scope: ScopeRef, source_path: &str) -> PyResult {
match compile::compile(
source,
&compile::Mode::Eval,
source_path.to_string(),
vm.ctx.code_type(),
) {
Ok(bytecode) => {
debug!("Code object: {:?}", bytecode);
vm.run_code_obj(bytecode, scope)
}
Err(err) => {
let syntax_error = vm.context().exceptions.syntax_error.clone();
Err(vm.new_exception(syntax_error, err.description().to_string()))
}
}
}
#[cfg(test)]
mod tests {
use super::eval;
use super::VirtualMachine;
#[test]
fn test_print_42() {
let source = String::from("print('Hello world')\n");
let mut vm = VirtualMachine::new();
let vars = vm.context().new_scope(None);
let _result = eval(&mut vm, &source, vars, "<unittest>");
// TODO: check result?
//assert_eq!(
// parse_ast,
// );
}
}