refactor: 完成语法域入口点迁移,收紧内部方法可见性
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_assignment_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_assignment_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::AssignmentExpression(assign) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -103,4 +103,8 @@ impl Compiler {
|
||||
ctx.projected_pc += 1; // LOAD_VAR result <- rhs
|
||||
ctx.labels.label_map.insert(Label::TernaryEnd(id), ctx.projected_pc);
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_assignment(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
self.count_assignment_expression(expr, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_call_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_call_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::CallExpression(call) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -50,7 +50,7 @@ impl Compiler {
|
||||
ctx.count_load_var(); // result <- regs[0]
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_new_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_new_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::NewExpression(ne) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -63,4 +63,12 @@ impl Compiler {
|
||||
ctx.alloc_reg(); // result register
|
||||
ctx.count_instr_with_ext(1); // NEW_EXPRESSION + arg_count ext word
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_call_domain(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
match expr {
|
||||
Expression::CallExpression(_) => self.count_call_expression(expr, ctx),
|
||||
Expression::NewExpression(_) => self.count_new_expression(expr, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_conditional_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_conditional_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::ConditionalExpression(cond) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -21,7 +21,7 @@ impl Compiler {
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_sequence_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_sequence_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::SequenceExpression(seq) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -30,7 +30,7 @@ impl Compiler {
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_logical_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_logical_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::LogicalExpression(log) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -61,4 +61,13 @@ impl Compiler {
|
||||
ctx.labels.label_map.insert(skip_label, ctx.projected_pc);
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_conditional_chain(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
match expr {
|
||||
Expression::ConditionalExpression(_) => self.count_conditional_expression(expr, ctx),
|
||||
Expression::SequenceExpression(_) => self.count_sequence_expression(expr, ctx),
|
||||
Expression::LogicalExpression(_) => self.count_logical_expression(expr, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,29 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_arrow_function_expression(&self, ctx: &mut CompileCtx) {
|
||||
fn count_arrow_function_expression(&self, ctx: &mut CompileCtx) {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_CONST
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_function_expression(&self, ctx: &mut CompileCtx) {
|
||||
fn count_function_expression(&self, ctx: &mut CompileCtx) {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_CONST
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_class_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_class_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::ClassExpression(class) = expr else {
|
||||
return;
|
||||
};
|
||||
self.count_class(class, ctx);
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_function_domain(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
match expr {
|
||||
Expression::ArrowFunctionExpression(_) => self.count_arrow_function_expression(ctx),
|
||||
Expression::FunctionExpression(_) => self.count_function_expression(ctx),
|
||||
Expression::ClassExpression(_) => self.count_class_expression(expr, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ impl Compiler {
|
||||
ctx.projected_pc += 1; // LOAD_VAR
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_regexp_literal(&self, ctx: &mut CompileCtx) {
|
||||
fn count_regexp_literal(&self, ctx: &mut CompileCtx) {
|
||||
ctx.alloc_reg(); // pattern reg (LOAD_CONST)
|
||||
ctx.projected_pc += 1;
|
||||
ctx.alloc_reg(); // flags reg (LOAD_CONST)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_static_member_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_static_member_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::StaticMemberExpression(member) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -17,7 +17,7 @@ impl Compiler {
|
||||
ctx.count_ic_instr_with_ext(); // IC_GET_PROP + 3 ext words
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_computed_member_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_computed_member_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::ComputedMemberExpression(member) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -27,7 +27,7 @@ impl Compiler {
|
||||
ctx.projected_pc += 1; // GET_PROP_DYNAMIC
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_private_field_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_private_field_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::PrivateFieldExpression(member) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -41,4 +41,13 @@ impl Compiler {
|
||||
};
|
||||
self.count_expression(&p.expression, ctx);
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_member_domain(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
match expr {
|
||||
Expression::StaticMemberExpression(_) => self.count_static_member_expression(expr, ctx),
|
||||
Expression::ComputedMemberExpression(_) => self.count_computed_member_expression(expr, ctx),
|
||||
Expression::PrivateFieldExpression(_) => self.count_private_field_expression(expr, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,30 +14,29 @@ mod template;
|
||||
impl Compiler {
|
||||
pub(crate) fn count_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
match expr {
|
||||
Expression::BinaryExpression(_) => self.count_binary_expression(expr, ctx),
|
||||
Expression::PrivateInExpression(_) => self.count_private_in_expression(expr, ctx),
|
||||
Expression::UnaryExpression(_) => self.count_unary_expression(expr, ctx),
|
||||
Expression::CallExpression(_) => self.count_call_expression(expr, ctx),
|
||||
Expression::AssignmentExpression(_) => self.count_assignment_expression(expr, ctx),
|
||||
Expression::ConditionalExpression(_) => self.count_conditional_expression(expr, ctx),
|
||||
Expression::SequenceExpression(_) => self.count_sequence_expression(expr, ctx),
|
||||
Expression::LogicalExpression(_) => self.count_logical_expression(expr, ctx),
|
||||
Expression::BinaryExpression(_)
|
||||
| Expression::PrivateInExpression(_)
|
||||
| Expression::UnaryExpression(_)
|
||||
| Expression::UpdateExpression(_) => self.count_operator(expr, ctx),
|
||||
Expression::CallExpression(_) | Expression::NewExpression(_) => self.count_call_domain(expr, ctx),
|
||||
Expression::AssignmentExpression(_) => self.count_assignment(expr, ctx),
|
||||
Expression::ConditionalExpression(_)
|
||||
| Expression::SequenceExpression(_)
|
||||
| Expression::LogicalExpression(_) => self.count_conditional_chain(expr, ctx),
|
||||
Expression::ChainExpression(_) => self.count_chain_expression(expr, ctx),
|
||||
Expression::ObjectExpression(_) => self.count_object_expression(expr, ctx),
|
||||
Expression::TemplateLiteral(_) => self.count_template_literal(expr, ctx),
|
||||
Expression::TaggedTemplateExpression(_) => self.count_tagged_template_expression(expr, ctx),
|
||||
Expression::ArrowFunctionExpression(_) => self.count_arrow_function_expression(ctx),
|
||||
Expression::FunctionExpression(_) => self.count_function_expression(ctx),
|
||||
Expression::ClassExpression(_) => self.count_class_expression(expr, ctx),
|
||||
Expression::NewExpression(_) => self.count_new_expression(expr, ctx),
|
||||
Expression::ArrayExpression(_) => self.count_array_expression(expr, ctx),
|
||||
Expression::StaticMemberExpression(_) => self.count_static_member_expression(expr, ctx),
|
||||
Expression::ComputedMemberExpression(_) => self.count_computed_member_expression(expr, ctx),
|
||||
Expression::PrivateFieldExpression(_) => self.count_private_field_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::UpdateExpression(_) => self.count_update_expression(expr, ctx),
|
||||
Expression::NumericLiteral(_)
|
||||
| Expression::StringLiteral(_)
|
||||
| Expression::BooleanLiteral(_)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_object_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_object_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::ObjectExpression(obj) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -25,7 +25,7 @@ impl Compiler {
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_array_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_array_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::ArrayExpression(arr) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -42,4 +42,12 @@ impl Compiler {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) 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),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_binary_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_binary_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::BinaryExpression(bin) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -14,7 +14,7 @@ impl Compiler {
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_private_in_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_private_in_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::PrivateInExpression(pin) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -22,7 +22,7 @@ impl Compiler {
|
||||
ctx.count_private_access();
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_unary_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_unary_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::UnaryExpression(un) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -79,7 +79,7 @@ impl Compiler {
|
||||
ctx.projected_pc += 1; // LOAD_CONST true
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_update_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_update_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::UpdateExpression(update) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -106,4 +106,14 @@ impl Compiler {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_operator(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
match expr {
|
||||
Expression::BinaryExpression(_) => self.count_binary_expression(expr, ctx),
|
||||
Expression::PrivateInExpression(_) => self.count_private_in_expression(expr, ctx),
|
||||
Expression::UnaryExpression(_) => self.count_unary_expression(expr, ctx),
|
||||
Expression::UpdateExpression(_) => self.count_update_expression(expr, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_template_literal(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_template_literal(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::TemplateLiteral(tl) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -12,7 +12,7 @@ impl Compiler {
|
||||
ctx.count_template_str(segment_count);
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_tagged_template_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
fn count_tagged_template_expression(&self, expr: &Expression, ctx: &mut CompileCtx) {
|
||||
let Expression::TaggedTemplateExpression(tt) = expr else {
|
||||
return;
|
||||
};
|
||||
@@ -50,4 +50,12 @@ impl Compiler {
|
||||
ctx.count_call_instr_with_arg_ext();
|
||||
ctx.count_load_var(); // result <- regs[0]
|
||||
}
|
||||
|
||||
pub(in crate::counter) 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),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,26 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_expression_statement(
|
||||
&self, stmt: &oxide_parser::ExpressionStatement<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
fn count_expression_statement(&self, stmt: &oxide_parser::ExpressionStatement<'_>, ctx: &mut CompileCtx) {
|
||||
self.count_expression(&stmt.expression, ctx);
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_return_statement(
|
||||
&self, stmt: &oxide_parser::ReturnStatement<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
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; // RETURN
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_break_statement(&self, ctx: &mut CompileCtx) {
|
||||
fn count_break_statement(&self, ctx: &mut CompileCtx) {
|
||||
ctx.projected_pc += 1; // JMP
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_continue_statement(&self, ctx: &mut CompileCtx) {
|
||||
fn count_continue_statement(&self, ctx: &mut CompileCtx) {
|
||||
ctx.projected_pc += 1; // JMP
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_labeled_statement(
|
||||
&self, stmt: &oxide_parser::LabeledStatement<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
fn count_labeled_statement(&self, stmt: &oxide_parser::LabeledStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let body_is_loop = matches!(
|
||||
stmt.body,
|
||||
Statement::WhileStatement(_)
|
||||
@@ -36,14 +30,22 @@ impl Compiler {
|
||||
| Statement::ForOfStatement(_)
|
||||
);
|
||||
if body_is_loop {
|
||||
// Loop body owns its labels; labeled break/continue reuse them.
|
||||
self.count_statement(&stmt.body, ctx);
|
||||
} else {
|
||||
// Non-loop label: consume one id (parity with emit) and place the
|
||||
// LabeledEnd target right after the body for `break label`.
|
||||
let id = ctx.next_label_id();
|
||||
self.count_statement(&stmt.body, ctx);
|
||||
ctx.labels.label_map.insert(Label::LabeledEnd(id), ctx.projected_pc);
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) 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),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_block_statement(
|
||||
&self, block: &oxide_parser::BlockStatement<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
fn count_block_statement(&self, block: &oxide_parser::BlockStatement<'_>, ctx: &mut CompileCtx) {
|
||||
for s in &block.body {
|
||||
self.count_statement(s, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_block_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
if let Statement::BlockStatement(block) = stmt {
|
||||
self.count_block_statement(block, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_if_statement(&self, stmt: &oxide_parser::IfStatement<'_>, ctx: &mut CompileCtx) {
|
||||
fn count_if_statement(&self, stmt: &oxide_parser::IfStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let else_label = Label::IfElse(id);
|
||||
let end_label = Label::IfEnd(id);
|
||||
@@ -21,4 +21,10 @@ impl Compiler {
|
||||
}
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_control_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
if let Statement::IfStatement(ifs) = stmt {
|
||||
self.count_if_statement(ifs, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_variable_declaration(
|
||||
&self, decl: &oxide_parser::VariableDeclaration<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
fn count_variable_declaration(&self, decl: &oxide_parser::VariableDeclaration<'_>, ctx: &mut CompileCtx) {
|
||||
for d in &decl.declarations {
|
||||
if let Some(init) = &d.init {
|
||||
self.count_expression(init, ctx);
|
||||
@@ -15,9 +13,7 @@ impl Compiler {
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_function_declaration(
|
||||
&self, decl: &oxide_parser::Function<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
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 {
|
||||
@@ -31,9 +27,18 @@ impl Compiler {
|
||||
ctx.count_words(2); // LOAD_CONST(BytecodeFunc) + STORE_VAR
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_class_declaration(&self, class: &oxide_parser::Class<'_>, ctx: &mut CompileCtx) {
|
||||
fn count_class_declaration(&self, class: &oxide_parser::Class<'_>, ctx: &mut CompileCtx) {
|
||||
ctx.alloc_reg(); // class binding reg
|
||||
self.count_class(class, ctx);
|
||||
ctx.projected_pc += 1; // STORE_VAR binding <- ctor
|
||||
}
|
||||
|
||||
pub(in crate::counter) 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),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_throw_statement(
|
||||
&self, stmt: &oxide_parser::ThrowStatement<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
fn count_throw_statement(&self, stmt: &oxide_parser::ThrowStatement<'_>, ctx: &mut CompileCtx) {
|
||||
self.count_expression(&stmt.argument, ctx);
|
||||
ctx.projected_pc += 1; // THROW
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_try_statement(&self, stmt: &oxide_parser::TryStatement<'_>, ctx: &mut CompileCtx) {
|
||||
fn count_try_statement(&self, stmt: &oxide_parser::TryStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let catch_label = Label::CatchBody(id);
|
||||
let try_end_label = Label::TryEnd(id);
|
||||
@@ -65,4 +63,12 @@ impl Compiler {
|
||||
|
||||
ctx.labels.label_map.insert(try_end_label, ctx.projected_pc);
|
||||
}
|
||||
|
||||
pub(in crate::counter) 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),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_while_statement(
|
||||
&self, stmt: &oxide_parser::WhileStatement<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
fn count_while_statement(&self, stmt: &oxide_parser::WhileStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::WhileStart(id);
|
||||
let end_label = Label::WhileEnd(id);
|
||||
@@ -16,9 +14,7 @@ impl Compiler {
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_do_while_statement(
|
||||
&self, stmt: &oxide_parser::DoWhileStatement<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
fn count_do_while_statement(&self, stmt: &oxide_parser::DoWhileStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::DoWhileStart(id);
|
||||
let end_label = Label::DoWhileEnd(id);
|
||||
@@ -30,7 +26,7 @@ impl Compiler {
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_for_statement(&self, stmt: &oxide_parser::ForStatement<'_>, ctx: &mut CompileCtx) {
|
||||
fn count_for_statement(&self, stmt: &oxide_parser::ForStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::ForStart(id);
|
||||
let update_label = Label::ForUpdate(id);
|
||||
@@ -65,9 +61,7 @@ impl Compiler {
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_for_in_statement(
|
||||
&self, stmt: &oxide_parser::ForInStatement<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
fn count_for_in_statement(&self, stmt: &oxide_parser::ForInStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::ForInStart(id);
|
||||
let end_label = Label::ForInEnd(id);
|
||||
@@ -102,9 +96,7 @@ impl Compiler {
|
||||
ctx.count_instr(); // FOR_IN_CLEANUP
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_for_of_statement(
|
||||
&self, stmt: &oxide_parser::ForOfStatement<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
fn count_for_of_statement(&self, stmt: &oxide_parser::ForOfStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let start_label = Label::ForOfStart(id);
|
||||
let end_label = Label::ForOfEnd(id);
|
||||
@@ -140,4 +132,15 @@ impl Compiler {
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
ctx.count_instr(); // FOR_OF_CLOSE
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_iteration_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
match stmt {
|
||||
Statement::WhileStatement(wh) => self.count_while_statement(wh, ctx),
|
||||
Statement::DoWhileStatement(dw) => self.count_do_while_statement(dw, ctx),
|
||||
Statement::ForStatement(fr) => self.count_for_statement(fr, ctx),
|
||||
Statement::ForInStatement(fi) => self.count_for_in_statement(fi, ctx),
|
||||
Statement::ForOfStatement(fo) => self.count_for_of_statement(fo, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,24 +11,23 @@ mod switch;
|
||||
impl Compiler {
|
||||
pub(crate) fn count_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
match stmt {
|
||||
Statement::ExpressionStatement(es) => self.count_expression_statement(es, ctx),
|
||||
Statement::VariableDeclaration(decl) => self.count_variable_declaration(decl, ctx),
|
||||
Statement::ReturnStatement(ret) => self.count_return_statement(ret, ctx),
|
||||
Statement::IfStatement(ifs) => self.count_if_statement(ifs, ctx),
|
||||
Statement::WhileStatement(wh) => self.count_while_statement(wh, ctx),
|
||||
Statement::DoWhileStatement(dw) => self.count_do_while_statement(dw, ctx),
|
||||
Statement::ForStatement(fr) => self.count_for_statement(fr, ctx),
|
||||
Statement::ForInStatement(fi) => self.count_for_in_statement(fi, ctx),
|
||||
Statement::ForOfStatement(fo) => self.count_for_of_statement(fo, ctx),
|
||||
Statement::SwitchStatement(sw) => self.count_switch_statement(sw, ctx),
|
||||
Statement::BreakStatement(_) => self.count_break_statement(ctx),
|
||||
Statement::ContinueStatement(_) => self.count_continue_statement(ctx),
|
||||
Statement::BlockStatement(block) => self.count_block_statement(block, ctx),
|
||||
Statement::FunctionDeclaration(fd) => self.count_function_declaration(fd, ctx),
|
||||
Statement::ClassDeclaration(class) => self.count_class_declaration(class, ctx),
|
||||
Statement::ThrowStatement(ts) => self.count_throw_statement(ts, ctx),
|
||||
Statement::TryStatement(ts) => self.count_try_statement(ts, ctx),
|
||||
Statement::LabeledStatement(ls) => self.count_labeled_statement(ls, ctx),
|
||||
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::IfStatement(_) => self.count_control_domain(stmt, ctx),
|
||||
Statement::WhileStatement(_)
|
||||
| Statement::DoWhileStatement(_)
|
||||
| Statement::ForStatement(_)
|
||||
| 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),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
impl Compiler {
|
||||
pub(in crate::counter) fn count_switch_statement(
|
||||
&self, stmt: &oxide_parser::SwitchStatement<'_>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
fn count_switch_statement(&self, stmt: &oxide_parser::SwitchStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let end_label = Label::SwitchEnd(id);
|
||||
ctx.push_switch(end_label);
|
||||
@@ -38,4 +36,10 @@ impl Compiler {
|
||||
ctx.labels.label_map.insert(end_label, ctx.projected_pc);
|
||||
ctx.pop_switch();
|
||||
}
|
||||
|
||||
pub(in crate::counter) fn count_switch_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
if let Statement::SwitchStatement(sw) = stmt {
|
||||
self.count_switch_statement(sw, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user