MCPcopy Index your code
hub / github.com/RustPython/RustPython / ast_to_object

Function ast_to_object

crates/vm/src/stdlib/_ast/elif_else_clause.rs:4–49  ·  view source on GitHub ↗
(
    clause: ast::ElifElseClause,
    mut rest: alloc::vec::IntoIter<ast::ElifElseClause>,
    vm: &VirtualMachine,
    source_file: &SourceFile,
)

Source from the content-addressed store, hash-verified

2use rustpython_compiler_core::SourceFile;
3
4pub(super) fn ast_to_object(
5 clause: ast::ElifElseClause,
6 mut rest: alloc::vec::IntoIter<ast::ElifElseClause>,
7 vm: &VirtualMachine,
8 source_file: &SourceFile,
9) -> PyObjectRef {
10 let ast::ElifElseClause {
11 node_index: _,
12 range,
13 test,
14 body,
15 } = clause;
16 let Some(test) = test else {
17 assert!(rest.len() == 0);
18 return body.ast_to_object(vm, source_file);
19 };
20 let node = NodeAst
21 .into_ref_with_type(vm, pyast::NodeStmtIf::static_type().to_owned())
22 .unwrap();
23 let dict = node.as_object().dict().unwrap();
24
25 dict.set_item("test", test.ast_to_object(vm, source_file), vm)
26 .unwrap();
27 dict.set_item("body", body.ast_to_object(vm, source_file), vm)
28 .unwrap();
29
30 let orelse = if let Some(next) = rest.next() {
31 if next.test.is_some() {
32 let next = ast::ElifElseClause {
33 range: TextRange::new(next.range.start(), range.end()),
34 ..next
35 };
36 vm.ctx
37 .new_list(vec![ast_to_object(next, rest, vm, source_file)])
38 .into()
39 } else {
40 next.body.ast_to_object(vm, source_file)
41 }
42 } else {
43 vm.ctx.new_list(vec![]).into()
44 };
45 dict.set_item("orelse", orelse, vm).unwrap();
46
47 node_add_location(&dict, range, vm, source_file);
48 node.into()
49}
50
51pub(super) fn ast_from_object(
52 vm: &VirtualMachine,

Callers 2

ast_to_objectMethod · 0.85
ast_to_objectMethod · 0.85

Calls 13

newFunction · 0.85
node_add_locationFunction · 0.85
into_ref_with_typeMethod · 0.80
new_listMethod · 0.80
ast_to_objectMethod · 0.45
unwrapMethod · 0.45
to_ownedMethod · 0.45
dictMethod · 0.45
as_objectMethod · 0.45
set_itemMethod · 0.45
nextMethod · 0.45
startMethod · 0.45

Tested by

no test coverage detected