mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-18 18:45:23 +00:00
Add annotation setup
This commit is contained in:
parent
e1999cb698
commit
fae42a0533
1 changed files with 14 additions and 23 deletions
|
@ -984,15 +984,14 @@ impl<O: OutputStream> Compiler<O> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn option_stmt_to_bool(&mut self, stmts: &Option<ast::Suite>) -> bool {
|
fn find_ann(&self, body: &[ast::Statement]) -> bool {
|
||||||
match &stmts {
|
use ast::StatementType::*;
|
||||||
|
let option_stmt_to_bool = |suit: &Option<ast::Suite>| -> bool {
|
||||||
|
match suit {
|
||||||
Some(stmts) => self.find_ann(stmts),
|
Some(stmts) => self.find_ann(stmts),
|
||||||
None => false,
|
None => false,
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
fn find_ann(&mut self, body: &[ast::Statement]) -> bool {
|
|
||||||
use ast::StatementType::*;
|
|
||||||
|
|
||||||
for statement in body {
|
for statement in body {
|
||||||
let res = match &statement.node {
|
let res = match &statement.node {
|
||||||
|
@ -1007,17 +1006,17 @@ impl<O: OutputStream> Compiler<O> {
|
||||||
iter: _,
|
iter: _,
|
||||||
body,
|
body,
|
||||||
orelse,
|
orelse,
|
||||||
} => self.find_ann(body) || self.option_stmt_to_bool(orelse),
|
} => self.find_ann(body) || option_stmt_to_bool(orelse),
|
||||||
If {
|
If {
|
||||||
test: _,
|
test: _,
|
||||||
body,
|
body,
|
||||||
orelse,
|
orelse,
|
||||||
} => self.find_ann(body) || self.option_stmt_to_bool(orelse),
|
} => self.find_ann(body) || option_stmt_to_bool(orelse),
|
||||||
While {
|
While {
|
||||||
test: _,
|
test: _,
|
||||||
body,
|
body,
|
||||||
orelse,
|
orelse,
|
||||||
} => self.find_ann(body) || self.option_stmt_to_bool(orelse),
|
} => self.find_ann(body) || option_stmt_to_bool(orelse),
|
||||||
With {
|
With {
|
||||||
is_async: _,
|
is_async: _,
|
||||||
items: _,
|
items: _,
|
||||||
|
@ -1029,9 +1028,9 @@ impl<O: OutputStream> Compiler<O> {
|
||||||
orelse,
|
orelse,
|
||||||
finalbody,
|
finalbody,
|
||||||
} => {
|
} => {
|
||||||
self.find_ann(body)
|
self.find_ann(&body)
|
||||||
|| self.option_stmt_to_bool(orelse)
|
|| option_stmt_to_bool(orelse)
|
||||||
|| self.option_stmt_to_bool(finalbody)
|
|| option_stmt_to_bool(finalbody)
|
||||||
}
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
|
@ -1097,15 +1096,7 @@ impl<O: OutputStream> Compiler<O> {
|
||||||
});
|
});
|
||||||
// setup annotations
|
// setup annotations
|
||||||
if self.find_ann(body) {
|
if self.find_ann(body) {
|
||||||
self.emit(Instruction::BuildMap {
|
self.emit(Instruction::SetupAnnotation);
|
||||||
size: 0,
|
|
||||||
unpack: false,
|
|
||||||
for_call: false,
|
|
||||||
});
|
|
||||||
self.emit(Instruction::StoreName {
|
|
||||||
name: "__annotations__".to_owned(),
|
|
||||||
scope: bytecode::NameScope::Free,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
self.compile_statements(new_body)?;
|
self.compile_statements(new_body)?;
|
||||||
self.emit(Instruction::LoadConst {
|
self.emit(Instruction::LoadConst {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue