From 198bb7d428b8db540deafe8525579a98c718b4c8 Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Wed, 5 Aug 2026 01:03:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E8=AF=AD?= =?UTF-8?q?=E5=8F=A5=E8=AE=A1=E6=95=B0=E8=BE=85=E5=8A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxide_compiler/src/compiler.rs | 12 +++------- .../src/stmt/basic/expression.rs | 6 ----- crates/oxide_compiler/src/stmt/basic/mod.rs | 2 -- .../src/stmt/basic/return_stmt.rs | 7 ------ crates/oxide_compiler/src/stmt/block.rs | 12 ---------- .../src/stmt/declaration/class_decl.rs | 6 ----- .../src/stmt/declaration/function_decl.rs | 13 +---------- .../src/stmt/declaration/mod.rs | 9 -------- .../src/stmt/declaration/var.rs | 23 ------------------- .../oxide_compiler/src/stmt/exception/mod.rs | 1 - .../src/stmt/exception/throw.rs | 5 ---- 11 files changed, 4 insertions(+), 92 deletions(-) diff --git a/crates/oxide_compiler/src/compiler.rs b/crates/oxide_compiler/src/compiler.rs index 3944b7b8..9bf3ccf5 100644 --- a/crates/oxide_compiler/src/compiler.rs +++ b/crates/oxide_compiler/src/compiler.rs @@ -1280,14 +1280,8 @@ impl Compiler { pub(crate) fn count_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) { match stmt { - Statement::ExpressionStatement(_) - | Statement::ReturnStatement(_) - | Statement::BreakStatement(_) - | Statement::ContinueStatement(_) - | Statement::LabeledStatement(_) => self.count_basic(stmt, ctx), - Statement::BlockStatement(_) => self.count_block_domain(stmt, ctx), - Statement::VariableDeclaration(_) | Statement::FunctionDeclaration(_) | Statement::ClassDeclaration(_) => { - self.count_declaration_domain(stmt, ctx) + Statement::BreakStatement(_) | Statement::ContinueStatement(_) | Statement::LabeledStatement(_) => { + self.count_basic(stmt, ctx) } Statement::IfStatement(_) => self.count_control_domain(stmt, ctx), Statement::WhileStatement(_) @@ -1296,7 +1290,7 @@ impl Compiler { | Statement::ForInStatement(_) | Statement::ForOfStatement(_) => self.count_iteration_domain(stmt, ctx), Statement::SwitchStatement(_) => self.count_switch_domain(stmt, ctx), - Statement::ThrowStatement(_) | Statement::TryStatement(_) => self.count_exception_domain(stmt, ctx), + Statement::TryStatement(_) => self.count_exception_domain(stmt, ctx), _ => {} } } diff --git a/crates/oxide_compiler/src/stmt/basic/expression.rs b/crates/oxide_compiler/src/stmt/basic/expression.rs index f1767632..00861c22 100644 --- a/crates/oxide_compiler/src/stmt/basic/expression.rs +++ b/crates/oxide_compiler/src/stmt/basic/expression.rs @@ -2,12 +2,6 @@ use crate::compiler::{CompileCtx, Compiler}; use oxide_parser::Statement; impl Compiler { - pub(super) fn count_expression_statement( - &self, stmt: &oxide_parser::ExpressionStatement<'_>, ctx: &mut CompileCtx, - ) { - self.count_expression(&stmt.expression, ctx); - } - fn emit_expression_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::ExpressionStatement(es) = stmt else { return Ok(None); diff --git a/crates/oxide_compiler/src/stmt/basic/mod.rs b/crates/oxide_compiler/src/stmt/basic/mod.rs index a413c02e..41e33cde 100644 --- a/crates/oxide_compiler/src/stmt/basic/mod.rs +++ b/crates/oxide_compiler/src/stmt/basic/mod.rs @@ -18,8 +18,6 @@ impl Compiler { pub(crate) fn count_basic(&self, stmt: &Statement, ctx: &mut CompileCtx) { match stmt { - Statement::ExpressionStatement(es) => self.count_expression_statement(es, ctx), - Statement::ReturnStatement(ret) => self.count_return_statement(ret, ctx), Statement::BreakStatement(_) => self.count_break_statement(ctx), Statement::ContinueStatement(_) => self.count_continue_statement(ctx), Statement::LabeledStatement(ls) => self.count_labeled_statement(ls, ctx), diff --git a/crates/oxide_compiler/src/stmt/basic/return_stmt.rs b/crates/oxide_compiler/src/stmt/basic/return_stmt.rs index 4dd890d3..718adea8 100644 --- a/crates/oxide_compiler/src/stmt/basic/return_stmt.rs +++ b/crates/oxide_compiler/src/stmt/basic/return_stmt.rs @@ -3,13 +3,6 @@ use oxide_bytecode::opcode::{self, OpCode}; use oxide_parser::Statement; impl Compiler { - pub(super) fn count_return_statement(&self, stmt: &oxide_parser::ReturnStatement<'_>, ctx: &mut CompileCtx) { - if let Some(arg) = &stmt.argument { - self.count_expression(arg, ctx); - } - ctx.projected_pc += 1; - } - fn emit_return_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::ReturnStatement(ret) = stmt else { return Ok(None); diff --git a/crates/oxide_compiler/src/stmt/block.rs b/crates/oxide_compiler/src/stmt/block.rs index ddc4695e..ab5c64ef 100644 --- a/crates/oxide_compiler/src/stmt/block.rs +++ b/crates/oxide_compiler/src/stmt/block.rs @@ -2,12 +2,6 @@ use crate::compiler::{CompileCtx, Compiler}; use oxide_parser::Statement; impl Compiler { - fn count_block_statement(&self, block: &oxide_parser::BlockStatement<'_>, ctx: &mut CompileCtx) { - for s in &block.body { - self.count_statement(s, ctx); - } - } - fn emit_block_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::BlockStatement(block) = stmt else { return Ok(None); @@ -23,12 +17,6 @@ impl Compiler { Ok(r) } - pub(crate) fn count_block_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) { - if let Statement::BlockStatement(block) = stmt { - self.count_block_statement(block, ctx); - } - } - pub(crate) fn emit_block_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { match stmt { Statement::BlockStatement(_) => self.emit_block_statement(stmt, ctx), diff --git a/crates/oxide_compiler/src/stmt/declaration/class_decl.rs b/crates/oxide_compiler/src/stmt/declaration/class_decl.rs index 5a6cfae6..84b11969 100644 --- a/crates/oxide_compiler/src/stmt/declaration/class_decl.rs +++ b/crates/oxide_compiler/src/stmt/declaration/class_decl.rs @@ -3,12 +3,6 @@ use oxide_bytecode::opcode::{self, OpCode}; use oxide_parser::{Statement, VariableDeclarationKind}; impl Compiler { - pub(crate) fn count_class_declaration(&self, class: &oxide_parser::Class<'_>, ctx: &mut CompileCtx) { - ctx.alloc_reg(); - self.count_class(class, ctx); - ctx.projected_pc += 1; - } - pub(crate) fn emit_class_declaration_statement( &self, stmt: &Statement, ctx: &mut CompileCtx, ) -> Result, String> { diff --git a/crates/oxide_compiler/src/stmt/declaration/function_decl.rs b/crates/oxide_compiler/src/stmt/declaration/function_decl.rs index f284373d..dd744812 100644 --- a/crates/oxide_compiler/src/stmt/declaration/function_decl.rs +++ b/crates/oxide_compiler/src/stmt/declaration/function_decl.rs @@ -1,19 +1,8 @@ use crate::compiler::{CompileCtx, Compiler, ParamSpec}; use oxide_bytecode::opcode::{self, OpCode}; -use oxide_parser::{Statement, VariableDeclarationKind}; +use oxide_parser::Statement; impl Compiler { - pub(crate) fn count_function_declaration(&self, decl: &oxide_parser::Function<'_>, ctx: &mut CompileCtx) { - let name = if let Some(id) = &decl.id { - id.name.to_string() - } else { - return; - }; - let func_reg = ctx.alloc_reg(); - let _ = ctx.declare_initialized(&name, func_reg, VariableDeclarationKind::Var, false); - ctx.count_words(2); - } - pub(crate) fn emit_function_declaration_statement( &self, stmt: &Statement, ctx: &mut CompileCtx, ) -> Result, String> { diff --git a/crates/oxide_compiler/src/stmt/declaration/mod.rs b/crates/oxide_compiler/src/stmt/declaration/mod.rs index 5dcdeb6d..269dfead 100644 --- a/crates/oxide_compiler/src/stmt/declaration/mod.rs +++ b/crates/oxide_compiler/src/stmt/declaration/mod.rs @@ -6,15 +6,6 @@ mod function_decl; mod var; impl Compiler { - pub(crate) fn count_declaration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) { - match stmt { - Statement::VariableDeclaration(decl) => self.count_variable_declaration(decl, ctx), - Statement::FunctionDeclaration(fd) => self.count_function_declaration(fd, ctx), - Statement::ClassDeclaration(class) => self.count_class_declaration(class, ctx), - _ => {} - } - } - pub(crate) fn emit_declaration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { match stmt { Statement::VariableDeclaration(_) => self.emit_variable_declaration_statement(stmt, ctx), diff --git a/crates/oxide_compiler/src/stmt/declaration/var.rs b/crates/oxide_compiler/src/stmt/declaration/var.rs index 258d41cb..e0a3cf09 100644 --- a/crates/oxide_compiler/src/stmt/declaration/var.rs +++ b/crates/oxide_compiler/src/stmt/declaration/var.rs @@ -6,29 +6,6 @@ use oxide_bytecode::{ use oxide_parser::{BindingPattern, Expression, Statement, VariableDeclarationKind}; impl Compiler { - pub(crate) fn count_variable_declaration( - &self, decl: &oxide_parser::VariableDeclaration<'_>, ctx: &mut CompileCtx, - ) { - let is_var = matches!(decl.kind, VariableDeclarationKind::Var); - for d in &decl.declarations { - if let Some(init) = &d.init { - self.count_expression(init, ctx); - if is_var { - if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &d.id { - let reg = ctx.alloc_reg(); - let _ = ctx.declare_initialized(bi.name.as_str(), reg, VariableDeclarationKind::Var, false); - ctx.projected_pc += 1; - continue; - } - } - self.count_binding_pattern(&d.id, ctx); - } else { - ctx.alloc_reg(); - ctx.count_words(2); - } - } - } - pub(crate) fn emit_variable_declaration_statement( &self, stmt: &Statement, ctx: &mut CompileCtx, ) -> Result, String> { diff --git a/crates/oxide_compiler/src/stmt/exception/mod.rs b/crates/oxide_compiler/src/stmt/exception/mod.rs index 8186a8ef..4140599a 100644 --- a/crates/oxide_compiler/src/stmt/exception/mod.rs +++ b/crates/oxide_compiler/src/stmt/exception/mod.rs @@ -7,7 +7,6 @@ mod try_catch; impl Compiler { pub(crate) fn count_exception_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) { match stmt { - Statement::ThrowStatement(ts) => self.count_throw_statement(ts, ctx), Statement::TryStatement(ts) => self.count_try_statement(ts, ctx), _ => {} } diff --git a/crates/oxide_compiler/src/stmt/exception/throw.rs b/crates/oxide_compiler/src/stmt/exception/throw.rs index 0d52d36b..3866e859 100644 --- a/crates/oxide_compiler/src/stmt/exception/throw.rs +++ b/crates/oxide_compiler/src/stmt/exception/throw.rs @@ -3,11 +3,6 @@ use oxide_bytecode::opcode::{self, OpCode}; use oxide_parser::Statement; impl Compiler { - pub(crate) fn count_throw_statement(&self, stmt: &oxide_parser::ThrowStatement<'_>, ctx: &mut CompileCtx) { - self.count_expression(&stmt.argument, ctx); - ctx.projected_pc += 1; - } - pub(crate) fn emit_throw_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result, String> { let Statement::ThrowStatement(ts) = stmt else { return Ok(None);