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

This commit is contained in:
爱特每羊队
2026-06-25 21:53:26 +08:00
parent db0444dabd
commit 3596fc3430
33 changed files with 403 additions and 222 deletions
@@ -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);
}
}
+10 -2
View File
@@ -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),
_ => {}
}
}
}
+19 -20
View File
@@ -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),
_ => {}
}
}
}
+16 -14
View File
@@ -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),
_ => {}
}
}
}
+17 -18
View File
@@ -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);
}
}
}
@@ -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),
}
}
}
+35 -36
View File
@@ -955,25 +955,24 @@ impl Compiler {
pub(crate) fn emit_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
match stmt {
Statement::ExpressionStatement(_) => self.emit_expression_statement(stmt, ctx),
Statement::VariableDeclaration(_) => self.emit_variable_declaration_statement(stmt, ctx),
Statement::ReturnStatement(_) => self.emit_return_statement(stmt, ctx),
Statement::BlockStatement(_) => self.emit_block_statement(stmt, ctx),
Statement::IfStatement(_) => self.emit_if_statement(stmt, ctx),
Statement::WhileStatement(_) => self.emit_while_statement(stmt, ctx),
Statement::DoWhileStatement(_) => self.emit_do_while_statement(stmt, ctx),
Statement::ForStatement(_) => self.emit_for_statement(stmt, ctx),
Statement::ForInStatement(_) => self.emit_for_in_statement(stmt, ctx),
Statement::ForOfStatement(_) => self.emit_for_of_statement(stmt, ctx),
Statement::SwitchStatement(_) => self.emit_switch_statement(stmt, ctx),
Statement::ExpressionStatement(_) | Statement::ReturnStatement(_) | Statement::EmptyStatement(_) => {
self.emit_basic_domain(stmt, ctx)
}
Statement::BlockStatement(_) => self.emit_block_domain(stmt, ctx),
Statement::VariableDeclaration(_) | Statement::FunctionDeclaration(_) | Statement::ClassDeclaration(_) => {
self.emit_declaration_domain(stmt, ctx)
}
Statement::IfStatement(_) => self.emit_control_domain(stmt, ctx),
Statement::WhileStatement(_)
| Statement::DoWhileStatement(_)
| Statement::ForStatement(_)
| Statement::ForInStatement(_)
| Statement::ForOfStatement(_) => self.emit_iteration_domain(stmt, ctx),
Statement::SwitchStatement(_) => self.emit_switch_domain(stmt, ctx),
Statement::ThrowStatement(_) | Statement::TryStatement(_) => self.emit_exception_domain(stmt, ctx),
Statement::BreakStatement(b) => self.emit_break_statement(b, ctx),
Statement::ContinueStatement(c) => self.emit_continue_statement(c, ctx),
Statement::FunctionDeclaration(_) => self.emit_function_declaration_statement(stmt, ctx),
Statement::ClassDeclaration(_) => self.emit_class_declaration_statement(stmt, ctx),
Statement::ThrowStatement(_) => self.emit_throw_statement(stmt, ctx),
Statement::TryStatement(_) => self.emit_try_statement(stmt, ctx),
Statement::LabeledStatement(ls) => self.emit_labeled_statement(ls, ctx),
Statement::EmptyStatement(_) => Ok(None),
_ => Ok(None),
}
}
@@ -984,30 +983,30 @@ impl Compiler {
| Expression::BooleanLiteral(_)
| Expression::NullLiteral(_)
| Expression::RegExpLiteral(_) => self.emit_literal(expr, ctx),
Expression::PrivateInExpression(pin) => self.emit_private_in_expression(pin, ctx),
Expression::BinaryExpression(bin) => self.emit_binary_expression(bin, 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::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),
Expression::ObjectExpression(obj) => self.emit_object_expression(obj, ctx),
Expression::ArrayExpression(arr) => self.emit_array_expression(arr, ctx),
Expression::BinaryExpression(_)
| Expression::PrivateInExpression(_)
| Expression::UnaryExpression(_)
| Expression::ConditionalExpression(_)
| Expression::LogicalExpression(_)
| Expression::UpdateExpression(_) => self.emit_operator(expr, ctx),
Expression::StaticMemberExpression(_)
| Expression::ComputedMemberExpression(_)
| Expression::PrivateFieldExpression(_)
| Expression::ChainExpression(_) => self.emit_member_domain(expr, ctx),
Expression::ObjectExpression(_) | Expression::ArrayExpression(_) => self.emit_object_domain(expr, ctx),
Expression::AssignmentExpression(assign) => self.emit_assignment_expression(assign, ctx),
Expression::UpdateExpression(update) => self.emit_update_expression(update, ctx),
Expression::TemplateLiteral(_) | Expression::TaggedTemplateExpression(_) => {
self.emit_template_domain(expr, ctx)
}
Expression::ArrowFunctionExpression(_)
| Expression::FunctionExpression(_)
| Expression::ClassExpression(_)
| Expression::NewExpression(_) => self.emit_function_domain(expr, ctx),
Expression::Identifier(ident) => self.emit_identifier_expression(ident, ctx),
Expression::TemplateLiteral(tl) => self.emit_template_literal_expression(tl, ctx),
Expression::TaggedTemplateExpression(tt) => self.emit_tagged_template_expression(tt, ctx),
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),
Expression::ParenthesizedExpression(p) => self.emit_parenthesized_expression(p, ctx),
Expression::CallExpression(call) => self.emit_call_expression(call, ctx),
Expression::CallExpression(_) => self.emit_call_domain(expr, ctx),
Expression::ThisExpression(_) => self.emit_this_expression(ctx),
Expression::SequenceExpression(seq) => self.emit_sequence_expression(seq, ctx),
Expression::ParenthesizedExpression(p) => self.emit_parenthesized_expression(p, ctx),
_ => self.emit_unsupported_expression(expr, ctx),
}
}
@@ -1,18 +1,14 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_expression_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_expression_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ExpressionStatement(es) = stmt else {
return Ok(None);
};
Ok(Some(self.emit_expression(&es.expression, ctx)?))
}
pub(in crate::emitter) fn emit_return_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_return_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ReturnStatement(ret) = stmt else {
return Ok(None);
};
@@ -92,7 +88,7 @@ impl Compiler {
Ok(None)
}
pub(in crate::emitter) fn is_iteration_statement(stmt: &Statement) -> bool {
fn is_iteration_statement(stmt: &Statement) -> bool {
matches!(
stmt,
Statement::WhileStatement(_)
@@ -102,4 +98,15 @@ impl Compiler {
| Statement::ForOfStatement(_)
)
}
pub(in crate::emitter) fn emit_basic_domain(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
match stmt {
Statement::ExpressionStatement(_) => self.emit_expression_statement(stmt, ctx),
Statement::ReturnStatement(_) => self.emit_return_statement(stmt, ctx),
Statement::EmptyStatement(_) => Ok(None),
_ => Ok(None),
}
}
}
@@ -1,9 +1,7 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_block_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_block_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::BlockStatement(block) = stmt else {
return Ok(None);
};
@@ -17,4 +15,13 @@ impl Compiler {
ctx.pop_scope();
Ok(r)
}
pub(in crate::emitter) fn emit_block_domain(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
match stmt {
Statement::BlockStatement(_) => self.emit_block_statement(stmt, ctx),
_ => Ok(None),
}
}
}
@@ -1,9 +1,7 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_if_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_if_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::IfStatement(ifs) = stmt else {
return Ok(None);
};
@@ -46,4 +44,13 @@ impl Compiler {
Ok(Some(result_reg))
}
pub(in crate::emitter) fn emit_control_domain(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
match stmt {
Statement::IfStatement(_) => self.emit_if_statement(stmt, ctx),
_ => Ok(None),
}
}
}
@@ -1,7 +1,7 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_variable_declaration_statement(
fn emit_variable_declaration_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
let Statement::VariableDeclaration(decl) = stmt else {
@@ -41,7 +41,7 @@ impl Compiler {
Ok(r)
}
pub(in crate::emitter) fn emit_function_declaration_statement(
fn emit_function_declaration_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
let Statement::FunctionDeclaration(fd) = stmt else {
@@ -83,9 +83,7 @@ impl Compiler {
Ok(None)
}
pub(in crate::emitter) fn emit_class_declaration_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_class_declaration_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ClassDeclaration(class) = stmt else {
return Ok(None);
};
@@ -101,4 +99,15 @@ impl Compiler {
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, ctor_reg, 0));
Ok(None)
}
pub(in crate::emitter) 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),
Statement::FunctionDeclaration(_) => self.emit_function_declaration_statement(stmt, ctx),
Statement::ClassDeclaration(_) => self.emit_class_declaration_statement(stmt, ctx),
_ => Ok(None),
}
}
}
@@ -1,9 +1,7 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_throw_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_throw_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ThrowStatement(ts) = stmt else {
return Ok(None);
};
@@ -12,9 +10,7 @@ impl Compiler {
Ok(None)
}
pub(in crate::emitter) fn emit_try_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_try_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::TryStatement(ts) = stmt else {
return Ok(None);
};
@@ -122,4 +118,14 @@ impl Compiler {
Ok(Some(result_reg))
}
pub(in crate::emitter) fn emit_exception_domain(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
match stmt {
Statement::ThrowStatement(_) => self.emit_throw_statement(stmt, ctx),
Statement::TryStatement(_) => self.emit_try_statement(stmt, ctx),
_ => Ok(None),
}
}
}
@@ -1,9 +1,7 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_while_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_while_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::WhileStatement(wh) = stmt else {
return Ok(None);
};
@@ -32,9 +30,7 @@ impl Compiler {
Ok(None)
}
pub(in crate::emitter) fn emit_do_while_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_do_while_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::DoWhileStatement(dw) = stmt else {
return Ok(None);
};
@@ -57,9 +53,7 @@ impl Compiler {
Ok(None)
}
pub(in crate::emitter) fn emit_for_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_for_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ForStatement(fr) = stmt else {
return Ok(None);
};
@@ -113,9 +107,7 @@ impl Compiler {
Ok(None)
}
pub(in crate::emitter) fn emit_for_in_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_for_in_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ForInStatement(fi) = stmt else {
return Ok(None);
};
@@ -176,9 +168,7 @@ impl Compiler {
Ok(None)
}
pub(in crate::emitter) fn emit_for_of_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_for_of_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::ForOfStatement(fo) = stmt else {
return Ok(None);
};
@@ -230,4 +220,17 @@ impl Compiler {
ctx.pop_loop();
Ok(None)
}
pub(in crate::emitter) fn emit_iteration_domain(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
match stmt {
Statement::WhileStatement(_) => self.emit_while_statement(stmt, ctx),
Statement::DoWhileStatement(_) => self.emit_do_while_statement(stmt, ctx),
Statement::ForStatement(_) => self.emit_for_statement(stmt, ctx),
Statement::ForInStatement(_) => self.emit_for_in_statement(stmt, ctx),
Statement::ForOfStatement(_) => self.emit_for_of_statement(stmt, ctx),
_ => Ok(None),
}
}
}
@@ -1,9 +1,7 @@
use super::*;
impl Compiler {
pub(in crate::emitter) fn emit_switch_statement(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
fn emit_switch_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) -> Result<Option<u8>, String> {
let Statement::SwitchStatement(sw) = stmt else {
return Ok(None);
};
@@ -47,4 +45,13 @@ impl Compiler {
ctx.pop_switch();
Ok(None)
}
pub(in crate::emitter) fn emit_switch_domain(
&self, stmt: &Statement, ctx: &mut CompileCtx,
) -> Result<Option<u8>, String> {
match stmt {
Statement::SwitchStatement(_) => self.emit_switch_statement(stmt, ctx),
_ => Ok(None),
}
}
}