docs: 补充注释与格式化

This commit is contained in:
2026-08-06 23:10:27 +08:00
parent e36a69976e
commit 31e28a03a6
156 changed files with 2393 additions and 1687 deletions
+32 -7
View File
@@ -1,10 +1,10 @@
//! 标签模板表达式 emit:`emit_tagged_template_expression` 构造 template object 并调用标签函数。
use crate::{CompileCtx, Emitter};
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
impl Emitter {
pub(crate) fn emit_tagged_template_expression(
&self, tt: &oxide_parser::TaggedTemplateExpression, ctx: &mut CompileCtx,
@@ -15,7 +15,12 @@ impl Emitter {
let tag_reg = self.emit_expression(&tt.tag, ctx)?;
let cooked_temp = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(cooked_temp), Operand::Imm(quasis.len() as u16), Operand::None));
ctx.inst(Inst::new(
OpCode::NEW_ARRAY,
Operand::Reg(cooked_temp),
Operand::Imm(quasis.len() as u16),
Operand::None,
));
for (i, quasi) in quasis.iter().enumerate() {
let s = quasi.value.cooked.as_ref().map(|c| c.to_string()).unwrap_or_default();
let const_idx = ctx.add_constant(Constant::String(s));
@@ -24,11 +29,21 @@ impl Emitter {
let idx_const = ctx.add_constant(Constant::Int(i as i32));
let idx_reg = ctx.alloc_reg();
ctx.inst(Inst::load_const(Operand::Reg(idx_reg), idx_const));
ctx.inst(Inst::new(OpCode::SET_ELEM, Operand::Reg(cooked_temp), Operand::Reg(idx_reg), Operand::Reg(str_reg)));
ctx.inst(Inst::new(
OpCode::SET_ELEM,
Operand::Reg(cooked_temp),
Operand::Reg(idx_reg),
Operand::Reg(str_reg),
));
}
let raw_temp = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(raw_temp), Operand::Imm(quasis.len() as u16), Operand::None));
ctx.inst(Inst::new(
OpCode::NEW_ARRAY,
Operand::Reg(raw_temp),
Operand::Imm(quasis.len() as u16),
Operand::None,
));
for (i, quasi) in quasis.iter().enumerate() {
let raw = quasi.value.raw.to_string();
let const_idx = ctx.add_constant(Constant::String(raw));
@@ -37,7 +52,12 @@ impl Emitter {
let idx_const = ctx.add_constant(Constant::Int(i as i32));
let idx_reg = ctx.alloc_reg();
ctx.inst(Inst::load_const(Operand::Reg(idx_reg), idx_const));
ctx.inst(Inst::new(OpCode::SET_ELEM, Operand::Reg(raw_temp), Operand::Reg(idx_reg), Operand::Reg(str_reg)));
ctx.inst(Inst::new(
OpCode::SET_ELEM,
Operand::Reg(raw_temp),
Operand::Reg(idx_reg),
Operand::Reg(str_reg),
));
}
let mut expr_temps = Vec::new();
@@ -52,7 +72,12 @@ impl Emitter {
expr_slots.push(ctx.alloc_reg());
}
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(cooked_slot), Operand::Reg(cooked_temp), Operand::None));
ctx.inst(Inst::new(
OpCode::LOAD_VAR,
Operand::Reg(cooked_slot),
Operand::Reg(cooked_temp),
Operand::None,
));
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(raw_slot), Operand::Reg(raw_temp), Operand::None));
for (slot, temp) in expr_slots.iter().zip(expr_temps.iter()) {
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(*slot), Operand::Reg(*temp), Operand::None));
@@ -2,9 +2,9 @@
//! 拼接字面量段与内插表达式。
use crate::{CompileCtx, Emitter};
use oxide_bytecode::module::Constant;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
impl Emitter {
pub(crate) fn emit_template_literal_expression(
@@ -42,12 +42,7 @@ impl Emitter {
}
}
ctx.inst(Inst::template_str(
Operand::Reg(r),
segment_count as u32,
total_len_hint as u16,
&parts,
));
ctx.inst(Inst::template_str(Operand::Reg(r), segment_count as u32, total_len_hint as u16, &parts));
Ok(r)
}