Add annotation setup

This commit is contained in:
lynskylate 2020-07-08 01:06:12 +08:00
parent e1999cb698
commit fae42a0533

View file

@ -984,15 +984,14 @@ impl<O: OutputStream> Compiler<O> {
Ok(())
}
fn option_stmt_to_bool(&mut self, stmts: &Option<ast::Suite>) -> bool {
match &stmts {
Some(stmts) => self.find_ann(stmts),
None => false,
}
}
fn find_ann(&mut self, body: &[ast::Statement]) -> bool {
fn find_ann(&self, body: &[ast::Statement]) -> bool {
use ast::StatementType::*;
let option_stmt_to_bool = |suit: &Option<ast::Suite>| -> bool {
match suit {
Some(stmts) => self.find_ann(stmts),
None => false,
}
};
for statement in body {
let res = match &statement.node {
@ -1007,17 +1006,17 @@ impl<O: OutputStream> Compiler<O> {
iter: _,
body,
orelse,
} => self.find_ann(body) || self.option_stmt_to_bool(orelse),
} => self.find_ann(body) || option_stmt_to_bool(orelse),
If {
test: _,
body,
orelse,
} => self.find_ann(body) || self.option_stmt_to_bool(orelse),
} => self.find_ann(body) || option_stmt_to_bool(orelse),
While {
test: _,
body,
orelse,
} => self.find_ann(body) || self.option_stmt_to_bool(orelse),
} => self.find_ann(body) || option_stmt_to_bool(orelse),
With {
is_async: _,
items: _,
@ -1029,9 +1028,9 @@ impl<O: OutputStream> Compiler<O> {
orelse,
finalbody,
} => {
self.find_ann(body)
|| self.option_stmt_to_bool(orelse)
|| self.option_stmt_to_bool(finalbody)
self.find_ann(&body)
|| option_stmt_to_bool(orelse)
|| option_stmt_to_bool(finalbody)
}
_ => false,
};
@ -1097,15 +1096,7 @@ impl<O: OutputStream> Compiler<O> {
});
// setup annotations
if self.find_ann(body) {
self.emit(Instruction::BuildMap {
size: 0,
unpack: false,
for_call: false,
});
self.emit(Instruction::StoreName {
name: "__annotations__".to_owned(),
scope: bytecode::NameScope::Free,
});
self.emit(Instruction::SetupAnnotation);
}
self.compile_statements(new_body)?;
self.emit(Instruction::LoadConst {