feat: VM 支持逻辑运算并添加集成测试

This commit is contained in:
爱特每羊队
2026-06-06 12:45:09 +08:00
parent 1b59378a69
commit 2b7b8d2ac7
3 changed files with 193 additions and 4 deletions
+12 -4
View File
@@ -151,15 +151,23 @@ impl Compiler {
ctx.max_regs = ctx.max_regs.max(1);
ctx.reset_regs();
let mut last_result = 0u8;
let mut last_result: Option<u8> = None;
for stmt in &program.body {
if let Some(r) = self.emit_statement(stmt, &mut ctx)? {
last_result = r;
last_result = Some(r);
}
}
if last_result != 0 {
ctx.emit(opcode::encode(OpCode::LOAD_VAR, 0, last_result, 0));
if let Some(r) = last_result {
ctx.emit(opcode::encode(OpCode::LOAD_VAR, 0, r, 0));
} else {
let undef_idx = ctx.add_constant(Constant::Undefined);
ctx.emit(opcode::encode(
OpCode::LOAD_CONST,
0,
(undef_idx & 0xFF) as u8,
((undef_idx >> 8) & 0xFF) as u8,
));
}
ctx.emit(opcode::encode(OpCode::HALT, 0, 0, 0));