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(()) Ok(())
} }
fn option_stmt_to_bool(&mut self, stmts: &Option<ast::Suite>) -> bool { fn find_ann(&self, body: &[ast::Statement]) -> bool {
match &stmts {
Some(stmts) => self.find_ann(stmts),
None => false,
}
}
fn find_ann(&mut self, body: &[ast::Statement]) -> bool {
use ast::StatementType::*; 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 { 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 {