chore: reduce convertor wrapping

This commit is contained in:
Shunsuke Shibayama 2024-02-11 18:56:47 +09:00
parent 7a158aace8
commit 3928e5522d
2 changed files with 25 additions and 1 deletions

View file

@ -3098,6 +3098,10 @@ impl PyCodeGenerator {
fn emit_dict(&mut self, dict: crate::hir::Dict) {
let init_stack_len = self.stack_len();
if !self.cfg.no_std {
self.emit_push_null();
self.emit_load_name_instr(Identifier::public("Dict"));
}
match dict {
crate::hir::Dict::Normal(dic) => {
let len = dic.kvs.len();
@ -3115,6 +3119,10 @@ impl PyCodeGenerator {
}
other => todo!("{other}"),
}
if !self.cfg.no_std {
self.emit_call_instr(1, Name);
self.stack_dec();
}
debug_assert_eq!(self.stack_len(), init_stack_len + 1);
}
@ -3304,8 +3312,9 @@ impl PyCodeGenerator {
fn emit_expr(&mut self, expr: Expr) {
log!(info "entered {} ({expr})", fn_name!());
self.push_lnotab(&expr);
let init_stack_len = self.stack_len();
let mut wrapped = true;
if !self.cfg.no_std {
if !self.cfg.no_std && expr.should_wrap() {
match expr.ref_t().derefine() {
Bool => {
self.emit_push_null();
@ -3353,6 +3362,8 @@ impl PyCodeGenerator {
}
},
}
} else {
wrapped = false;
}
match expr {
Expr::Literal(lit) => self.emit_load_const(lit.value),
@ -3380,6 +3391,7 @@ impl PyCodeGenerator {
self.emit_call_instr(1, Name);
self.stack_dec();
}
debug_assert_eq!(self.stack_len(), init_stack_len + 1);
}
/// forブロックなどで使う

View file

@ -2850,6 +2850,18 @@ impl Expr {
}
}
pub fn should_wrap(&self) -> bool {
match self {
Self::Literal(_)
| Self::Accessor(_)
| Self::Call(_)
| Self::BinOp(_)
| Self::UnaryOp(_) => true,
Self::TypeAsc(t) => t.expr.should_wrap(),
_ => false,
}
}
pub fn need_to_be_closed(&self) -> bool {
match self {
Self::BinOp(_) | Self::UnaryOp(_) | Self::Lambda(_) | Self::TypeAsc(_) => true,