refactor(compiler): remove statement count helpers

This commit is contained in:
爱特每羊队
2026-08-05 01:03:42 +08:00
parent 638da1f481
commit 3ce7fd587d
11 changed files with 4 additions and 92 deletions
+3 -9
View File
@@ -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),
_ => {}
}
}
@@ -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<Option<u8>, String> {
let Statement::ExpressionStatement(es) = stmt else {
return Ok(None);
@@ -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),
@@ -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<Option<u8>, String> {
let Statement::ReturnStatement(ret) = stmt else {
return Ok(None);
-12
View File
@@ -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<Option<u8>, 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<Option<u8>, String> {
match stmt {
Statement::BlockStatement(_) => self.emit_block_statement(stmt, ctx),
@@ -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<Option<u8>, String> {
@@ -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<Option<u8>, String> {
@@ -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<Option<u8>, String> {
match stmt {
Statement::VariableDeclaration(_) => self.emit_variable_declaration_statement(stmt, ctx),
@@ -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<Option<u8>, String> {
@@ -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),
_ => {}
}
@@ -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<Option<u8>, String> {
let Statement::ThrowStatement(ts) = stmt else {
return Ok(None);