fix: 修复无 else 块时冗余指令跳转问题

This commit is contained in:
爱特每羊队
2026-06-06 13:30:41 +08:00
parent fc953a53fc
commit a3f6db1da7
2 changed files with 10 additions and 5 deletions
+4 -2
View File
@@ -33,7 +33,9 @@ impl Compiler {
self.count_statement(&ifs.consequent, ctx);
ctx.alloc_reg(); // result register
ctx.projected_pc += 1; // LOAD_VAR result <- consequent
ctx.projected_pc += 1; // JMP (skip else)
if ifs.alternate.is_some() {
ctx.projected_pc += 1; // JMP (skip else)
}
ctx.label_map.insert(else_label, ctx.projected_pc);
if let Some(alt_stmt) = &ifs.alternate {
self.count_statement(alt_stmt, ctx);
@@ -173,7 +175,7 @@ impl Compiler {
LogicalOperator::And => Label::TernaryEnd(id),
LogicalOperator::Or => Label::TernaryElse(id),
LogicalOperator::Coalesce => {
return;
unreachable!()
}
};
ctx.label_map.insert(skip_label, ctx.projected_pc);
+6 -3
View File
@@ -93,9 +93,12 @@ impl Compiler {
));
}
let end_pos = ctx.label_map[&end_label];
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
ctx.emit(opcode::encode_jmp(offset as i16));
let has_alt = ifs.alternate.is_some();
if has_alt {
let end_pos = ctx.label_map[&end_label];
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
ctx.emit(opcode::encode_jmp(offset as i16));
}
if let Some(alt) = &ifs.alternate {
let alt_reg = self.emit_statement(alt, ctx)?;