(&mut self, statement: &ast::Stmt)
| 2252 | } |
| 2253 | |
| 2254 | fn compile_statement(&mut self, statement: &ast::Stmt) -> CompileResult<()> { |
| 2255 | trace!("Compiling {statement:?}"); |
| 2256 | let prev_source_range = self.current_source_range; |
| 2257 | self.set_source_range(statement.range()); |
| 2258 | |
| 2259 | match &statement { |
| 2260 | // we do this here because `from __future__` still executes that `from` statement at runtime, |
| 2261 | // we still need to compile the ImportFrom down below |
| 2262 | ast::Stmt::ImportFrom(ast::StmtImportFrom { module, names, .. }) |
| 2263 | if module.as_ref().map(|id| id.as_str()) == Some("__future__") => |
| 2264 | { |
| 2265 | self.compile_future_features(names)? |
| 2266 | } |
| 2267 | // ignore module-level doc comments |
| 2268 | ast::Stmt::Expr(ast::StmtExpr { value, .. }) |
| 2269 | if matches!(&**value, ast::Expr::StringLiteral(..)) |
| 2270 | && matches!(self.done_with_future_stmts, DoneWithFuture::No) => |
| 2271 | { |
| 2272 | self.done_with_future_stmts = DoneWithFuture::DoneWithDoc |
| 2273 | } |
| 2274 | // if we find any other statement, stop accepting future statements |
| 2275 | _ => self.done_with_future_stmts = DoneWithFuture::Yes, |
| 2276 | } |
| 2277 | |
| 2278 | match &statement { |
| 2279 | ast::Stmt::Import(ast::StmtImport { names, .. }) => { |
| 2280 | // import a, b, c as d |
| 2281 | for name in names { |
| 2282 | let name = &name; |
| 2283 | self.emit_load_const(ConstantData::Integer { |
| 2284 | value: num_traits::Zero::zero(), |
| 2285 | }); |
| 2286 | self.emit_load_const(ConstantData::None); |
| 2287 | let namei = self.name(&name.name); |
| 2288 | emit!(self, Instruction::ImportName { namei }); |
| 2289 | if let Some(alias) = &name.asname { |
| 2290 | let parts: Vec<&str> = name.name.split('.').skip(1).collect(); |
| 2291 | for (i, part) in parts.iter().enumerate() { |
| 2292 | let namei = self.name(part); |
| 2293 | emit!(self, Instruction::ImportFrom { namei }); |
| 2294 | if i < parts.len() - 1 { |
| 2295 | emit!(self, Instruction::Swap { i: 2 }); |
| 2296 | emit!(self, Instruction::PopTop); |
| 2297 | } |
| 2298 | } |
| 2299 | self.store_name(alias.as_str())?; |
| 2300 | if !parts.is_empty() { |
| 2301 | emit!(self, Instruction::PopTop); |
| 2302 | } |
| 2303 | } else { |
| 2304 | self.store_name(name.name.split('.').next().unwrap())? |
| 2305 | } |
| 2306 | } |
| 2307 | } |
| 2308 | ast::Stmt::ImportFrom(ast::StmtImportFrom { |
| 2309 | level, |
| 2310 | module, |
| 2311 | names, |
no test coverage detected