From dfc14cfce9afe79964bf45a42d319fa6346e719a Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Wed, 5 Aug 2026 01:10:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E7=AE=80?= =?UTF-8?q?=E5=8D=95=E8=A1=A8=E8=BE=BE=E5=BC=8F=E8=AE=A1=E6=95=B0=E8=BE=85?= =?UTF-8?q?=E5=8A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxide_compiler/src/compiler.rs | 19 +-------- crates/oxide_compiler/src/expr/identifier.rs | 28 ------------- crates/oxide_compiler/src/expr/literal.rs | 21 ---------- .../oxide_compiler/src/expr/object/array.rs | 20 --------- crates/oxide_compiler/src/expr/object/mod.rs | 8 ---- .../src/expr/object/object_literal.rs | 24 ----------- .../oxide_compiler/src/expr/parenthesized.rs | 7 ---- .../oxide_compiler/src/expr/template/mod.rs | 8 ---- .../src/expr/template/tagged.rs | 41 ------------------- .../src/expr/template/template_literal.rs | 13 ------ crates/oxide_compiler/src/expr/this.rs | 5 --- 11 files changed, 1 insertion(+), 193 deletions(-) diff --git a/crates/oxide_compiler/src/compiler.rs b/crates/oxide_compiler/src/compiler.rs index 9bf3ccf5..30c68114 100644 --- a/crates/oxide_compiler/src/compiler.rs +++ b/crates/oxide_compiler/src/compiler.rs @@ -383,11 +383,6 @@ impl CompileCtx { self.count_instr(); } - pub(crate) fn count_template_str(&mut self, segment_count: usize) { - self.alloc_reg(); - self.count_instr_with_ext(1 + segment_count); - } - pub(crate) fn count_jump(&mut self) { self.count_instr(); } @@ -1307,25 +1302,13 @@ impl Compiler { | Expression::SequenceExpression(_) | Expression::LogicalExpression(_) => self.count_conditional_chain(expr, ctx), Expression::ChainExpression(_) => self.count_chain_expression(expr, ctx), - Expression::ObjectExpression(_) | Expression::ArrayExpression(_) => self.count_object_domain(expr, ctx), - Expression::TemplateLiteral(_) | Expression::TaggedTemplateExpression(_) => { - self.count_template_domain(expr, ctx) - } Expression::ArrowFunctionExpression(_) | Expression::FunctionExpression(_) | Expression::ClassExpression(_) => self.count_function_domain(expr, ctx), Expression::StaticMemberExpression(_) | Expression::ComputedMemberExpression(_) | Expression::PrivateFieldExpression(_) => self.count_member_domain(expr, ctx), - Expression::ParenthesizedExpression(_) => self.count_parenthesized_expression(expr, ctx), - Expression::ThisExpression(_) => self.count_this_expression(ctx), - Expression::Identifier(_) => self.count_identifier_expression(expr, ctx), - Expression::NumericLiteral(_) - | Expression::StringLiteral(_) - | Expression::BooleanLiteral(_) - | Expression::NullLiteral(_) - | Expression::RegExpLiteral(_) => self.count_literal(expr, ctx), - _ => self.count_default_expression(ctx), + _ => {} } } diff --git a/crates/oxide_compiler/src/expr/identifier.rs b/crates/oxide_compiler/src/expr/identifier.rs index 1cc41580..42d2888a 100644 --- a/crates/oxide_compiler/src/expr/identifier.rs +++ b/crates/oxide_compiler/src/expr/identifier.rs @@ -1,35 +1,7 @@ use crate::compiler::{CompileCtx, Compiler}; use oxide_bytecode::opcode::{self, OpCode}; -use oxide_parser::Expression; impl Compiler { - pub(crate) fn count_identifier_expression(&self, expr: &Expression, ctx: &mut CompileCtx) { - let Expression::Identifier(ident) = expr else { - return; - }; - let name = ident.name.as_str(); - - for up in &ctx.current_upvalue_captures { - if up.name == name { - ctx.alloc_reg(); - ctx.count_instr(); - return; - } - } - - if ctx.scopes.symbols.lookup_is_captured(name) { - ctx.alloc_reg(); - ctx.count_instr(); - return; - } - - if CompileCtx::is_known_builtin(name) { - let _ = ctx.lookup_or_builtin(name); - } - ctx.alloc_reg(); - ctx.projected_pc += 1; - } - pub(crate) fn emit_identifier_expression( &self, ident: &oxide_parser::IdentifierReference, ctx: &mut CompileCtx, ) -> Result { diff --git a/crates/oxide_compiler/src/expr/literal.rs b/crates/oxide_compiler/src/expr/literal.rs index 6a121313..50313b68 100644 --- a/crates/oxide_compiler/src/expr/literal.rs +++ b/crates/oxide_compiler/src/expr/literal.rs @@ -4,27 +4,6 @@ use oxide_bytecode::opcode::{self, OpCode}; use oxide_parser::Expression; impl Compiler { - pub(crate) fn count_literal(&self, expr: &Expression, ctx: &mut CompileCtx) { - match expr { - Expression::RegExpLiteral(_) => self.count_regexp_literal(ctx), - _ => self.count_default_expression(ctx), - } - } - - fn count_regexp_literal(&self, ctx: &mut CompileCtx) { - ctx.alloc_reg(); - ctx.projected_pc += 1; - ctx.alloc_reg(); - ctx.projected_pc += 1; - ctx.alloc_reg(); - ctx.projected_pc += 1; - } - - pub(crate) fn count_default_expression(&self, ctx: &mut CompileCtx) { - ctx.alloc_reg(); - ctx.projected_pc += 1; - } - pub(crate) fn emit_literal(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { match expr { Expression::NumericLiteral(n) => self.emit_numeric_literal_expression(n, ctx), diff --git a/crates/oxide_compiler/src/expr/object/array.rs b/crates/oxide_compiler/src/expr/object/array.rs index 2e5d840d..4b52fe84 100644 --- a/crates/oxide_compiler/src/expr/object/array.rs +++ b/crates/oxide_compiler/src/expr/object/array.rs @@ -3,27 +3,7 @@ use oxide_bytecode::{ module::Constant, opcode::{self, OpCode}, }; -use oxide_parser::Expression; - impl Compiler { - pub(crate) fn count_array_expression(&self, expr: &Expression, ctx: &mut CompileCtx) { - let Expression::ArrayExpression(arr) = expr else { - return; - }; - ctx.alloc_reg(); - ctx.projected_pc += 1; - let elem_checkpoint = ctx.reg_checkpoint(); - for elem in &arr.elements { - if let Some(e) = elem.as_expression() { - self.count_expression(e, ctx); - ctx.alloc_reg(); - ctx.projected_pc += 1; - ctx.projected_pc += 1; - ctx.restore_reg_checkpoint(elem_checkpoint); - } - } - } - pub(crate) fn emit_array_expression( &self, arr: &oxide_parser::ArrayExpression, ctx: &mut CompileCtx, ) -> Result { diff --git a/crates/oxide_compiler/src/expr/object/mod.rs b/crates/oxide_compiler/src/expr/object/mod.rs index 3ce19588..733b77e3 100644 --- a/crates/oxide_compiler/src/expr/object/mod.rs +++ b/crates/oxide_compiler/src/expr/object/mod.rs @@ -5,14 +5,6 @@ mod array; mod object_literal; impl Compiler { - pub(crate) fn count_object_domain(&self, expr: &Expression, ctx: &mut CompileCtx) { - match expr { - Expression::ObjectExpression(_) => self.count_object_expression(expr, ctx), - Expression::ArrayExpression(_) => self.count_array_expression(expr, ctx), - _ => {} - } - } - pub(crate) fn emit_object_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { match expr { Expression::ObjectExpression(obj) => self.emit_object_expression(obj, ctx), diff --git a/crates/oxide_compiler/src/expr/object/object_literal.rs b/crates/oxide_compiler/src/expr/object/object_literal.rs index 1d8b6d67..69a09bcb 100644 --- a/crates/oxide_compiler/src/expr/object/object_literal.rs +++ b/crates/oxide_compiler/src/expr/object/object_literal.rs @@ -6,30 +6,6 @@ use oxide_bytecode::{ use oxide_parser::{Expression, ObjectPropertyKind, PropertyKey, PropertyKind}; impl Compiler { - pub(crate) fn count_object_expression(&self, expr: &Expression, ctx: &mut CompileCtx) { - let Expression::ObjectExpression(obj) = expr else { - return; - }; - ctx.alloc_reg(); - ctx.projected_pc += 1; - let prop_checkpoint = ctx.reg_checkpoint(); - for prop in &obj.properties { - if let ObjectPropertyKind::ObjectProperty(p) = prop { - if matches!(p.kind, PropertyKind::Get | PropertyKind::Set) { - self.count_expression(&p.value, ctx); - ctx.count_load_const(); - ctx.count_define_accessor(); - } else { - ctx.alloc_reg(); - ctx.projected_pc += 1; - self.count_expression(&p.value, ctx); - ctx.projected_pc += 1; - } - ctx.restore_reg_checkpoint(prop_checkpoint); - } - } - } - pub(crate) fn emit_object_expression( &self, obj: &oxide_parser::ObjectExpression, ctx: &mut CompileCtx, ) -> Result { diff --git a/crates/oxide_compiler/src/expr/parenthesized.rs b/crates/oxide_compiler/src/expr/parenthesized.rs index a3744e71..2f8c5981 100644 --- a/crates/oxide_compiler/src/expr/parenthesized.rs +++ b/crates/oxide_compiler/src/expr/parenthesized.rs @@ -1,13 +1,6 @@ use crate::compiler::{CompileCtx, Compiler}; -use oxide_parser::Expression; impl Compiler { - pub(crate) fn count_parenthesized_expression(&self, expr: &Expression, ctx: &mut CompileCtx) { - if let Expression::ParenthesizedExpression(p) = expr { - self.count_expression(&p.expression, ctx); - } - } - pub(crate) fn emit_parenthesized_expression( &self, p: &oxide_parser::ParenthesizedExpression, ctx: &mut CompileCtx, ) -> Result { diff --git a/crates/oxide_compiler/src/expr/template/mod.rs b/crates/oxide_compiler/src/expr/template/mod.rs index 7a6735d6..70ac5585 100644 --- a/crates/oxide_compiler/src/expr/template/mod.rs +++ b/crates/oxide_compiler/src/expr/template/mod.rs @@ -5,14 +5,6 @@ mod tagged; mod template_literal; impl Compiler { - pub(crate) fn count_template_domain(&self, expr: &Expression, ctx: &mut CompileCtx) { - match expr { - Expression::TemplateLiteral(_) => self.count_template_literal(expr, ctx), - Expression::TaggedTemplateExpression(_) => self.count_tagged_template_expression(expr, ctx), - _ => {} - } - } - pub(crate) fn emit_template_domain(&self, expr: &Expression, ctx: &mut CompileCtx) -> Result { match expr { Expression::TemplateLiteral(tl) => self.emit_template_literal_expression(tl, ctx), diff --git a/crates/oxide_compiler/src/expr/template/tagged.rs b/crates/oxide_compiler/src/expr/template/tagged.rs index 1263ed9b..8e68a09e 100644 --- a/crates/oxide_compiler/src/expr/template/tagged.rs +++ b/crates/oxide_compiler/src/expr/template/tagged.rs @@ -3,48 +3,7 @@ use oxide_bytecode::{ module::Constant, opcode::{self, OpCode}, }; -use oxide_parser::Expression; - impl Compiler { - pub(crate) fn count_tagged_template_expression(&self, expr: &Expression, ctx: &mut CompileCtx) { - let Expression::TaggedTemplateExpression(tt) = expr else { - return; - }; - self.count_expression(&tt.tag, ctx); - let quasi_count = tt.quasi.quasis.len(); - - for _ in 0..quasi_count { - ctx.count_load_const(); - ctx.count_load_const(); - ctx.count_instr(); - } - ctx.alloc_reg(); - ctx.count_instr(); - - for _ in 0..quasi_count { - ctx.count_load_const(); - ctx.count_load_const(); - ctx.count_instr(); - } - ctx.alloc_reg(); - ctx.count_instr(); - - for expr in &tt.quasi.expressions { - self.count_expression(expr, ctx); - } - - ctx.alloc_reg(); - ctx.alloc_reg(); - for _ in &tt.quasi.expressions { - ctx.alloc_reg(); - ctx.count_instr(); - } - ctx.count_words(2); - ctx.count_load_const(); - ctx.count_call_instr_with_arg_ext(); - ctx.count_load_var(); - } - pub(crate) fn emit_tagged_template_expression( &self, tt: &oxide_parser::TaggedTemplateExpression, ctx: &mut CompileCtx, ) -> Result { diff --git a/crates/oxide_compiler/src/expr/template/template_literal.rs b/crates/oxide_compiler/src/expr/template/template_literal.rs index f0c7cbdc..691e585a 100644 --- a/crates/oxide_compiler/src/expr/template/template_literal.rs +++ b/crates/oxide_compiler/src/expr/template/template_literal.rs @@ -3,20 +3,7 @@ use oxide_bytecode::{ module::Constant, opcode::{self, OpCode}, }; -use oxide_parser::Expression; - impl Compiler { - pub(crate) fn count_template_literal(&self, expr: &Expression, ctx: &mut CompileCtx) { - let Expression::TemplateLiteral(tl) = expr else { - return; - }; - for expr in &tl.expressions { - self.count_expression(expr, ctx); - } - let segment_count = tl.quasis.len() + tl.expressions.len(); - ctx.count_template_str(segment_count); - } - pub(crate) fn emit_template_literal_expression( &self, tl: &oxide_parser::TemplateLiteral, ctx: &mut CompileCtx, ) -> Result { diff --git a/crates/oxide_compiler/src/expr/this.rs b/crates/oxide_compiler/src/expr/this.rs index b575fb2a..9e0603b0 100644 --- a/crates/oxide_compiler/src/expr/this.rs +++ b/crates/oxide_compiler/src/expr/this.rs @@ -2,11 +2,6 @@ use crate::compiler::{CompileCtx, Compiler}; use oxide_bytecode::opcode::{self, OpCode}; impl Compiler { - pub(crate) fn count_this_expression(&self, ctx: &mut CompileCtx) { - ctx.alloc_reg(); - ctx.projected_pc += 1; - } - pub(crate) fn emit_this_expression(&self, ctx: &mut CompileCtx) -> Result { let r = ctx.alloc_reg(); let src = ctx.static_block_this_reg.unwrap_or(254);