python_ast/ast/tree/
if_stmt.rs

1use proc_macro2::TokenStream;
2use pyo3::{Bound, FromPyObject, PyAny, PyResult, types::PyAnyMethods};
3use quote::quote;
4use serde::{Deserialize, Serialize};
5
6use crate::{
7    CodeGen, CodeGenContext, ExprType, PythonOptions, SymbolTableScopes,
8    Node, impl_node_with_positions, PyAttributeExtractor, extract_list
9};
10
11use super::Statement;
12
13#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
14pub struct If {
15    pub test: ExprType,
16    pub body: Vec<Statement>,
17    pub orelse: Vec<Statement>,
18    pub lineno: Option<usize>,
19    pub col_offset: Option<usize>,
20    pub end_lineno: Option<usize>,
21    pub end_col_offset: Option<usize>,
22}
23
24impl<'a> FromPyObject<'a> for If {
25    fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
26        let test = ob.extract_attr_with_context("test", "if test condition")?;
27        let test = test.extract().expect("getting if test");
28        
29        let body: Vec<Statement> = extract_list(ob, "body", "if body statements")?;
30        let orelse: Vec<Statement> = extract_list(ob, "orelse", "if else statements")?;
31        
32        Ok(If {
33            test,
34            body,
35            orelse,
36            lineno: ob.lineno(),
37            col_offset: ob.col_offset(),
38            end_lineno: ob.end_lineno(),
39            end_col_offset: ob.end_col_offset(),
40        })
41    }
42}
43
44impl_node_with_positions!(If { lineno, col_offset, end_lineno, end_col_offset });
45
46impl CodeGen for If {
47    type Context = CodeGenContext;
48    type Options = PythonOptions;
49    type SymbolTable = SymbolTableScopes;
50
51    fn find_symbols(self, symbols: Self::SymbolTable) -> Self::SymbolTable {
52        let symbols = self.test.find_symbols(symbols);
53        let symbols = self.body.into_iter().fold(symbols, |acc, stmt| stmt.find_symbols(acc));
54        self.orelse.into_iter().fold(symbols, |acc, stmt| stmt.find_symbols(acc))
55    }
56
57    fn to_rust(
58        self,
59        ctx: Self::Context,
60        options: Self::Options,
61        symbols: Self::SymbolTable,
62    ) -> Result<TokenStream, Box<dyn std::error::Error>> {
63        let test = self.test.to_rust(ctx.clone(), options.clone(), symbols.clone())?;
64        
65        let body_stmts: Result<Vec<_>, _> = self.body
66            .into_iter()
67            .map(|stmt| stmt.to_rust(ctx.clone(), options.clone(), symbols.clone()))
68            .collect();
69        let body_stmts = body_stmts?;
70        
71        if self.orelse.is_empty() {
72            Ok(quote! {
73                if #test {
74                    #(#body_stmts)*
75                }
76            })
77        } else {
78            let else_stmts: Result<Vec<_>, _> = self.orelse
79                .into_iter()
80                .map(|stmt| stmt.to_rust(ctx.clone(), options.clone(), symbols.clone()))
81                .collect();
82            let else_stmts = else_stmts?;
83            
84            Ok(quote! {
85                if #test {
86                    #(#body_stmts)*
87                } else {
88                    #(#else_stmts)*
89                }
90            })
91        }
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    use crate::create_parse_test;
99
100    create_parse_test!(test_simple_if, "if x > 5:\n    print('big')", "if_test.py");
101    create_parse_test!(test_if_else, "if x > 5:\n    print('big')\nelse:\n    print('small')", "if_test.py");
102    create_parse_test!(test_if_elif, "if x > 10:\n    print('huge')\nelif x > 5:\n    print('big')\nelse:\n    print('small')", "if_test.py");
103}