refactor: 完成语法域入口点迁移,收紧内部方法可见性

This commit is contained in:
2026-06-25 21:53:26 +08:00
parent f7bcfd3846
commit 804a516e38
33 changed files with 403 additions and 222 deletions
@@ -1,9 +1,7 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_call_expression(
&self, call: &oxide_parser::CallExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
fn emit_call_expression(&self, call: &oxide_parser::CallExpression, ctx: &mut CompileCtx) -> Result<u8, String> {
if matches!(&call.callee, Expression::Super(_)) {
if !ctx.in_derived_constructor {
return Err("super() only supported in derived constructors".into());
@@ -94,4 +92,11 @@ impl Compiler {
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, 0, 0));
Ok(result_reg)
}
pub(in crate::emitter) fn emit_call_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
match expr {
Expression::CallExpression(call) => self.emit_call_expression(call, ctx),
_ => self.emit_unsupported_expression(expr, ctx),
}
}
}
@@ -1,7 +1,7 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_arrow_function_expression(
fn emit_arrow_function_expression(
&self, arrow: &oxide_parser::ArrowFunctionExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
// Rest params not yet supported
@@ -42,9 +42,7 @@ impl Compiler {
Ok(r)
}
pub(in crate::emitter) fn emit_function_expression(
&self, fe: &oxide_parser::Function, ctx: &mut CompileCtx,
) -> Result<u8, String> {
fn emit_function_expression(&self, fe: &oxide_parser::Function, ctx: &mut CompileCtx) -> Result<u8, String> {
// FunctionExpression: compile body, emit LOAD_CONST(BytecodeFunc)
let mut param_names = Vec::new();
for (idx, param) in fe.params.items.iter().enumerate() {
@@ -76,13 +74,11 @@ impl Compiler {
Ok(r)
}
pub(in crate::emitter) fn emit_class_expression(&self, class: &Class, ctx: &mut CompileCtx) -> Result<u8, String> {
fn emit_class_expression(&self, class: &Class, ctx: &mut CompileCtx) -> Result<u8, String> {
self.emit_class(class, ctx)
}
pub(in crate::emitter) fn emit_new_expression(
&self, ne: &oxide_parser::NewExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
fn emit_new_expression(&self, ne: &oxide_parser::NewExpression, ctx: &mut CompileCtx) -> Result<u8, String> {
let constructor_reg = self.emit_expression(&ne.callee, ctx)?;
let mut arg_regs = Vec::new();
for arg in &ne.arguments {
@@ -96,4 +92,16 @@ impl Compiler {
ctx.emit(arg_regs.len() as u32);
Ok(r)
}
pub(in crate::emitter) fn emit_function_domain(
&self, expr: &Expression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
match expr {
Expression::ArrowFunctionExpression(arrow) => self.emit_arrow_function_expression(arrow, ctx),
Expression::FunctionExpression(fe) => self.emit_function_expression(fe, ctx),
Expression::ClassExpression(class) => self.emit_class_expression(class, ctx),
Expression::NewExpression(ne) => self.emit_new_expression(ne, ctx),
_ => self.emit_unsupported_expression(expr, ctx),
}
}
}
@@ -15,7 +15,7 @@ impl Compiler {
}
}
pub(in crate::emitter) fn emit_numeric_literal_expression(
fn emit_numeric_literal_expression(
&self, n: &oxide_parser::NumericLiteral, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let idx = if is_int_literal(n.value) {
@@ -28,7 +28,7 @@ impl Compiler {
Ok(r)
}
pub(in crate::emitter) fn emit_string_literal_expression(
fn emit_string_literal_expression(
&self, s: &oxide_parser::StringLiteral, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let idx = ctx.add_constant(Constant::String(s.value.to_string()));
@@ -37,7 +37,7 @@ impl Compiler {
Ok(r)
}
pub(in crate::emitter) fn emit_boolean_literal_expression(
fn emit_boolean_literal_expression(
&self, b: &oxide_parser::BooleanLiteral, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let idx = ctx.add_constant(Constant::Boolean(b.value));
@@ -46,7 +46,7 @@ impl Compiler {
Ok(r)
}
pub(in crate::emitter) fn emit_null_literal_expression(&self, ctx: &mut CompileCtx) -> Result<u8, String> {
fn emit_null_literal_expression(&self, ctx: &mut CompileCtx) -> Result<u8, String> {
let idx = ctx.add_constant(Constant::Null);
let r = ctx.alloc_reg();
ctx.emit_load_const(r, idx);
@@ -75,7 +75,7 @@ impl Compiler {
Ok(r)
}
pub(in crate::emitter) fn emit_reg_exp_literal_expression(
fn emit_reg_exp_literal_expression(
&self, lit: &oxide_parser::RegExpLiteral, ctx: &mut CompileCtx,
) -> Result<u8, String> {
if let Some(raw) = &lit.raw {
@@ -1,7 +1,7 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_static_member_expression(
fn emit_static_member_expression(
&self, member: &oxide_parser::StaticMemberExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
if matches!(&member.object, Expression::Super(_)) {
@@ -35,7 +35,7 @@ impl Compiler {
Ok(obj_reg)
}
pub(in crate::emitter) fn emit_computed_member_expression(
fn emit_computed_member_expression(
&self, member: &oxide_parser::ComputedMemberExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let obj_reg = self.emit_expression(&member.object, ctx)?;
@@ -45,7 +45,7 @@ impl Compiler {
Ok(r)
}
pub(in crate::emitter) fn emit_private_field_expression(
fn emit_private_field_expression(
&self, member: &oxide_parser::PrivateFieldExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let obj_reg = self.emit_expression(&member.object, ctx)?;
@@ -55,9 +55,7 @@ impl Compiler {
Ok(r)
}
pub(in crate::emitter) fn emit_chain_expression(
&self, chain: &oxide_parser::ChainExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
fn emit_chain_expression(&self, chain: &oxide_parser::ChainExpression, ctx: &mut CompileCtx) -> Result<u8, String> {
let id = ctx.next_label_id();
let short_label = Label::TernaryElse(id);
let end_label = Label::TernaryEnd(id);
@@ -72,4 +70,14 @@ impl Compiler {
ctx.emit_load_const(result_reg, undefined_idx);
Ok(result_reg)
}
pub(in crate::emitter) fn emit_member_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
match expr {
Expression::StaticMemberExpression(member) => self.emit_static_member_expression(member, ctx),
Expression::ComputedMemberExpression(member) => self.emit_computed_member_expression(member, ctx),
Expression::PrivateFieldExpression(member) => self.emit_private_field_expression(member, ctx),
Expression::ChainExpression(chain) => self.emit_chain_expression(chain, ctx),
_ => self.emit_unsupported_expression(expr, ctx),
}
}
}
@@ -1,9 +1,7 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_object_expression(
&self, obj: &oxide_parser::ObjectExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
fn emit_object_expression(&self, obj: &oxide_parser::ObjectExpression, ctx: &mut CompileCtx) -> Result<u8, String> {
let obj_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::NEW_OBJECT, obj_reg, 0, 0));
let prop_checkpoint = ctx.reg_checkpoint();
@@ -53,9 +51,7 @@ impl Compiler {
Ok(obj_reg)
}
pub(in crate::emitter) fn emit_array_expression(
&self, arr: &oxide_parser::ArrayExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
fn emit_array_expression(&self, arr: &oxide_parser::ArrayExpression, ctx: &mut CompileCtx) -> Result<u8, String> {
let arr_reg = ctx.alloc_reg();
let n = arr.elements.len() as u16;
ctx.emit(opcode::encode(OpCode::NEW_ARRAY, arr_reg, (n & 0xFF) as u8, ((n >> 8) & 0xFF) as u8));
@@ -73,4 +69,12 @@ impl Compiler {
}
Ok(arr_reg)
}
pub(in crate::emitter) fn emit_object_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
match expr {
Expression::ObjectExpression(obj) => self.emit_object_expression(obj, ctx),
Expression::ArrayExpression(arr) => self.emit_array_expression(arr, ctx),
_ => self.emit_unsupported_expression(expr, ctx),
}
}
}
@@ -1,7 +1,7 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_private_in_expression(
fn emit_private_in_expression(
&self, pin: &oxide_parser::PrivateInExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let obj_reg = self.emit_expression(&pin.right, ctx)?;
@@ -11,9 +11,7 @@ impl Compiler {
Ok(result_reg)
}
pub(in crate::emitter) fn emit_binary_expression(
&self, bin: &oxide_parser::BinaryExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
fn emit_binary_expression(&self, bin: &oxide_parser::BinaryExpression, ctx: &mut CompileCtx) -> Result<u8, String> {
let checkpoint = ctx.reg_checkpoint();
let left = self.emit_expression(&bin.left, ctx)?;
let right = self.emit_expression(&bin.right, ctx)?;
@@ -48,9 +46,7 @@ impl Compiler {
Ok(left)
}
pub(in crate::emitter) fn emit_unary_expression(
&self, un: &oxide_parser::UnaryExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
fn emit_unary_expression(&self, un: &oxide_parser::UnaryExpression, ctx: &mut CompileCtx) -> Result<u8, String> {
if matches!(un.operator, UnaryOperator::Delete) {
return match &un.argument {
Expression::Identifier(_) => {
@@ -154,7 +150,7 @@ impl Compiler {
}
}
pub(in crate::emitter) fn emit_conditional_expression(
fn emit_conditional_expression(
&self, cond: &oxide_parser::ConditionalExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let id = ctx.next_label_id();
@@ -183,7 +179,7 @@ impl Compiler {
Ok(result_reg)
}
pub(in crate::emitter) fn emit_logical_expression(
fn emit_logical_expression(
&self, log: &oxide_parser::LogicalExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
use oxide_parser::LogicalOperator;
@@ -251,7 +247,7 @@ impl Compiler {
Ok(dup_reg)
}
pub(in crate::emitter) fn emit_update_expression(
fn emit_update_expression(
&self, update: &oxide_parser::UpdateExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
match &update.argument {
@@ -304,4 +300,16 @@ impl Compiler {
_ => Err("member update not yet supported".into()),
}
}
pub(in crate::emitter) fn emit_operator(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result<u8, String> {
match expr {
Expression::BinaryExpression(bin) => self.emit_binary_expression(bin, ctx),
Expression::PrivateInExpression(pin) => self.emit_private_in_expression(pin, ctx),
Expression::UnaryExpression(un) => self.emit_unary_expression(un, ctx),
Expression::ConditionalExpression(cond) => self.emit_conditional_expression(cond, ctx),
Expression::LogicalExpression(log) => self.emit_logical_expression(log, ctx),
Expression::UpdateExpression(update) => self.emit_update_expression(update, ctx),
_ => self.emit_unsupported_expression(expr, ctx),
}
}
}
@@ -1,7 +1,7 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_template_literal_expression(
fn emit_template_literal_expression(
&self, tl: &oxide_parser::TemplateLiteral, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let r = ctx.alloc_reg();
@@ -52,7 +52,7 @@ impl Compiler {
Ok(r)
}
pub(in crate::emitter) fn emit_tagged_template_expression(
fn emit_tagged_template_expression(
&self, tt: &oxide_parser::TaggedTemplateExpression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let quasis = &tt.quasi.quasis;
@@ -125,4 +125,14 @@ impl Compiler {
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, 0, 0));
Ok(result_reg)
}
pub(in crate::emitter) fn emit_template_domain(
&self, expr: &Expression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
match expr {
Expression::TemplateLiteral(tl) => self.emit_template_literal_expression(tl, ctx),
Expression::TaggedTemplateExpression(tt) => self.emit_tagged_template_expression(tt, ctx),
_ => self.emit_unsupported_expression(expr, ctx),
}
}
}