mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-25 11:11:11 +08:00
fix: 运行结束前将结果移至 r0 寄存器
This commit is contained in:
@@ -73,8 +73,15 @@ impl Compiler {
|
|||||||
ctx.max_regs = ctx.max_regs.max(1);
|
ctx.max_regs = ctx.max_regs.max(1);
|
||||||
ctx.reset_regs();
|
ctx.reset_regs();
|
||||||
|
|
||||||
|
let mut last_result = 0u8;
|
||||||
for stmt in &program.body {
|
for stmt in &program.body {
|
||||||
self.emit_statement(stmt, &mut ctx)?;
|
if let Some(r) = self.emit_statement(stmt, &mut ctx)? {
|
||||||
|
last_result = r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if last_result != 0 {
|
||||||
|
ctx.emit(opcode::encode(OpCode::LOAD_VAR, 0, last_result, 0));
|
||||||
}
|
}
|
||||||
ctx.emit(opcode::encode(OpCode::HALT, 0, 0, 0));
|
ctx.emit(opcode::encode(OpCode::HALT, 0, 0, 0));
|
||||||
|
|
||||||
@@ -191,35 +198,43 @@ impl Compiler {
|
|||||||
// ── Pass 2: Emitter ──
|
// ── Pass 2: Emitter ──
|
||||||
|
|
||||||
impl Compiler {
|
impl Compiler {
|
||||||
fn emit_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<(), String> {
|
fn emit_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
|
||||||
match stmt {
|
match stmt {
|
||||||
Statement::ExpressionStatement(es) => {
|
Statement::ExpressionStatement(es) => {
|
||||||
self.emit_expression(&es.expression, ctx)?;
|
Ok(Some(self.emit_expression(&es.expression, ctx)?))
|
||||||
}
|
}
|
||||||
Statement::VariableDeclaration(decl) => {
|
Statement::VariableDeclaration(decl) => {
|
||||||
|
let mut r = None;
|
||||||
for d in &decl.declarations {
|
for d in &decl.declarations {
|
||||||
if let Some(init) = &d.init {
|
if let Some(init) = &d.init {
|
||||||
self.emit_expression(init, ctx)?;
|
r = Some(self.emit_expression(init, ctx)?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(r)
|
||||||
}
|
}
|
||||||
Statement::ReturnStatement(ret) => match &ret.argument {
|
Statement::ReturnStatement(ret) => {
|
||||||
Some(expr) => {
|
match &ret.argument {
|
||||||
let r = self.emit_expression(expr, ctx)?;
|
Some(expr) => {
|
||||||
ctx.emit(opcode::encode(OpCode::RETURN, r, 0, 0));
|
let r = self.emit_expression(expr, ctx)?;
|
||||||
|
ctx.emit(opcode::encode(OpCode::RETURN, r, 0, 0));
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
ctx.emit(opcode::encode(OpCode::RETURN, 0, 0, 0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => {
|
Ok(None)
|
||||||
ctx.emit(opcode::encode(OpCode::RETURN, 0, 0, 0));
|
}
|
||||||
}
|
|
||||||
},
|
|
||||||
Statement::BlockStatement(block) => {
|
Statement::BlockStatement(block) => {
|
||||||
|
let mut r = None;
|
||||||
for s in &block.body {
|
for s in &block.body {
|
||||||
self.emit_statement(s, ctx)?;
|
if let Some(rr) = self.emit_statement(s, ctx)? {
|
||||||
|
r = Some(rr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Ok(r)
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => Ok(None),
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn emit_expression(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
|
fn emit_expression(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
|
||||||
|
|||||||
Reference in New Issue
Block a user