mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-19 19:15:43 +00:00
Add constant optimization test
This commit is contained in:
parent
e9ad1f64ee
commit
a01853fad5
2 changed files with 26 additions and 2 deletions
|
@ -117,7 +117,7 @@ pub(crate) type Label = usize;
|
|||
|
||||
impl<O> Default for Compiler<O>
|
||||
where
|
||||
O: OutputStream + Default,
|
||||
O: OutputStream,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Compiler::new(0)
|
||||
|
@ -1916,7 +1916,7 @@ mod tests {
|
|||
use rustpython_parser::parser;
|
||||
|
||||
fn compile_exec(source: &str) -> CodeObject {
|
||||
let mut compiler = Compiler::default();
|
||||
let mut compiler: Compiler = Default::default();
|
||||
compiler.source_path = Some("source_path".to_string());
|
||||
compiler.push_new_code_object("<module>".to_string());
|
||||
let ast = parser::parse_program(&source.to_string()).unwrap();
|
||||
|
@ -2003,4 +2003,24 @@ mod tests {
|
|||
code.instructions
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_constant_optimization() {
|
||||
let code = compile_exec("1 + 2 + 3 + 4\n1.5 * 2.5");
|
||||
assert_eq!(
|
||||
code.instructions,
|
||||
vec![
|
||||
LoadConst {
|
||||
value: Integer { value: 10.into() }
|
||||
},
|
||||
Pop,
|
||||
LoadConst {
|
||||
value: Float { value: 3.75 }
|
||||
},
|
||||
Pop,
|
||||
LoadConst { value: None },
|
||||
ReturnValue,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue