python_ast/ast/tree/
if_stmt.rs1use 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())?;
65
66 let body_stmts: Result<Vec<_>, _> = self.body
67 .into_iter()
68 .map(|stmt| stmt.to_rust(ctx.clone(), options.clone(), symbols.clone()))
69 .collect();
70 let body_stmts = body_stmts?;
71
72 if self.orelse.is_empty() {
73 Ok(quote! {
74 if #test {
75 #(#body_stmts)*
76 }
77 })
78 } else {
79 let else_stmts: Result<Vec<_>, _> = self.orelse
80 .into_iter()
81 .map(|stmt| stmt.to_rust(ctx.clone(), options.clone(), symbols.clone()))
82 .collect();
83 let else_stmts = else_stmts?;
84
85 Ok(quote! {
86 if #test {
87 #(#body_stmts)*
88 } else {
89 #(#else_stmts)*
90 }
91 })
92 }
93 }
94}
95
96#[cfg(test)]
97mod tests {
98 use super::*;
99 use crate::create_parse_test;
100
101 create_parse_test!(test_simple_if, "if x > 5:\n print('big')", "if_test.py");
102 create_parse_test!(test_if_else, "if x > 5:\n print('big')\nelse:\n print('small')", "if_test.py");
103 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");
104}