From 8d4620e1da43835baa83823f4c4acf3a96ccfbb4 Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Sat, 6 Jun 2026 13:30:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=97=A0=20else=20?= =?UTF-8?q?=E5=9D=97=E6=97=B6=E5=86=97=E4=BD=99=E6=8C=87=E4=BB=A4=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxide_compiler/src/counter.rs | 6 ++++-- crates/oxide_compiler/src/emitter.rs | 9 ++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/crates/oxide_compiler/src/counter.rs b/crates/oxide_compiler/src/counter.rs index 1238aa11..863229cd 100644 --- a/crates/oxide_compiler/src/counter.rs +++ b/crates/oxide_compiler/src/counter.rs @@ -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); diff --git a/crates/oxide_compiler/src/emitter.rs b/crates/oxide_compiler/src/emitter.rs index 92cb5e9f..43345955 100644 --- a/crates/oxide_compiler/src/emitter.rs +++ b/crates/oxide_compiler/src/emitter.rs @@ -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)?;