Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ divan = "0.1.21"
mira-core = { path = ".", features = ["mimalloc"] }

[dependencies]
bumpalo = { version = "3", features = ["boxed"] }
unicode-ident = "1"
winnow = "1"
strum = { version = "0.28", features = ["derive"] }
Expand Down
28 changes: 28 additions & 0 deletions crates/core/src/arena.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use bumpalo::Bump;

/// Arena allocator for AST nodes.
///
/// Allocations made through this arena live until the arena is dropped or [`AstArena::reset`] is called.
pub struct AstArena {
bump: Bump,
}

impl AstArena {
pub fn new() -> Self {
Self { bump: Bump::new() }
}

pub fn alloc<'a, T>(&'a self, value: T) -> bumpalo::boxed::Box<'a, T> {
bumpalo::boxed::Box::new_in(value, &self.bump)
}

pub fn reset(&mut self) {
self.bump.reset();
}
}

impl Default for AstArena {
fn default() -> Self {
Self::new()
}
}
13 changes: 9 additions & 4 deletions crates/core/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ impl<'s, 'c: 's> Compiler<'s, 'c> {
)
}

pub fn parse<'t>(&mut self, tokens: &'t [Token<'t>]) -> Option<Script<'t>> {
pub fn parse<'t, 'a>(
&mut self,
arena: &'a crate::arena::AstArena,
tokens: &'t [Token<'t>],
) -> Option<Script<'t, 'a>> {
assert!(!tokens.is_empty(), "Cannot parse an empty token list");

// Parsing
let mut stream = parser::to_input(tokens);
let Ok(mut script) = parser::parse(&mut stream) else {
let Ok(mut script) = parser::parse(arena, &mut stream) else {
let remaining = stream.peek_finish();
let range = if remaining.is_empty() {
tokens.last().unwrap().range.clone()
Expand All @@ -93,7 +97,7 @@ impl<'s, 'c: 's> Compiler<'s, 'c> {
Some(script)
}

pub fn emit(&mut self, script: &Script<'s>) -> Option<Vec<u8>> {
pub fn emit<'a>(&mut self, script: &Script<'s, 'a>) -> Option<Vec<u8>> {
// Emitting
let bytecode = emitter::emit(script, &mut self.diagnostics_collector);

Expand All @@ -118,7 +122,8 @@ impl<'s, 'c: 's> Compiler<'s, 'c> {
let Some(tokens) = compiler.lex() else {
return (None, compiler.encode_diagnostics());
};
let Some(script) = compiler.parse(&tokens) else {
let arena = crate::arena::AstArena::new();
let Some(script) = compiler.parse(&arena, &tokens) else {
return (None, compiler.encode_diagnostics());
};
let Some(chunk) = compiler.emit(&script) else {
Expand Down
26 changes: 13 additions & 13 deletions crates/core/src/emitter/emitter_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ impl DerefMut for Closures {
}

impl<'s, 'c> Emitter<'s, 'c> {
pub fn declare_block(
pub fn declare_block<'a>(
&mut self,
stmts: &'s Vec<Statement<'s>>,
expr: &'s Option<Box<Expression<'s>>>,
stmts: &'s Vec<Statement<'s, 'a>>,
expr: &'s Option<bumpalo::boxed::Box<'a, Expression<'s, 'a>>>,
exports: &mut ModuleExports<'s, 'c>,
) {
for stmt in stmts {
Expand All @@ -72,10 +72,10 @@ impl<'s, 'c> Emitter<'s, 'c> {
self.declare_expression(expr);
}
}
pub fn emit_block(
pub fn emit_block<'a>(
&mut self,
stmts: &'s Vec<Statement<'s>>,
expr: &'s Option<Box<Expression<'s>>>,
stmts: &'s Vec<Statement<'s, 'a>>,
expr: &'s Option<bumpalo::boxed::Box<'a, Expression<'s, 'a>>>,
scope_range: SourceRange,
ret: Register,
brk: Option<Register>,
Expand All @@ -100,13 +100,13 @@ impl<'s, 'c> Emitter<'s, 'c> {
};
has_never
}
pub fn emit_fn(
pub fn emit_fn<'a>(
&mut self,
ret: Register,
name_range: SourceRange,
scope_start: usize,
args: &'s Option<ParameterList<'s>>,
body: &'s Expression<'s>,
args: &'s Option<ParameterList<'s, 'a>>,
body: &'s Expression<'s, 'a>,
) {
let Expression::Block(_, stmts, expr, cp) = body else {
// unreachable!("Expected block expression");
Expand All @@ -121,14 +121,14 @@ impl<'s, 'c> Emitter<'s, 'c> {
expr,
);
}
pub fn emit_fn_like(
pub fn emit_fn_like<'a>(
&mut self,
ret: Register,
name_range: SourceRange,
scope_range: SourceRange,
args: &'s Option<ParameterList<'s>>,
stmts: &'s Vec<Statement<'s>>,
expr: &'s Option<Box<Expression<'s>>>,
args: &'s Option<ParameterList<'s, 'a>>,
stmts: &'s Vec<Statement<'s, 'a>>,
expr: &'s Option<bumpalo::boxed::Box<'a, Expression<'s, 'a>>>,
) {
let arg_len = args.as_ref().map_or(1, |args| args.len());
let mut has_var_args = false;
Expand Down
40 changes: 20 additions & 20 deletions crates/core/src/emitter/emitter_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::{
variable::BindType,
};

fn number_constant(exp: &Expression<'_>) -> Option<f64> {
fn number_constant(exp: &Expression<'_, '_>) -> Option<f64> {
match exp {
Expression::Literal(token) => match token.kind {
TokenKind::Number(n, _) => Some(n),
Expand All @@ -32,7 +32,7 @@ fn number_constant(exp: &Expression<'_>) -> Option<f64> {
}

impl<'s, 'c> Emitter<'s, 'c> {
fn declare_callable_expr(&mut self, callable: &'s Expression<'s>) {
fn declare_callable_expr<'a>(&mut self, callable: &'s Expression<'s, 'a>) {
// 此时的 Grouping 用于标记 callable 为复杂表达式以启用空安全,跳过 declare_expression 的 Grouping 处理
if let Expression::Grouping(_, inner, _) = callable
&& (inner.is_variable() || inner.is_grouping())
Expand All @@ -46,7 +46,7 @@ impl<'s, 'c> Emitter<'s, 'c> {
self.declare_expression(callable);
}
}
fn declare_cond_expr(&mut self, cond: &'s Expression<'s>) {
fn declare_cond_expr<'a>(&mut self, cond: &'s Expression<'s, 'a>) {
if let Literal(lit) = cond
&& !lit.is_boolean_literal()
{
Expand All @@ -55,20 +55,20 @@ impl<'s, 'c> Emitter<'s, 'c> {
}
self.declare_expression(cond);
}
fn declare_indexing_expr(&mut self, record_like: &'s Expression<'s>) {
fn declare_indexing_expr<'a>(&mut self, record_like: &'s Expression<'s, 'a>) {
if record_like.is_literal() || record_like.is_interpolated_string() {
self.diagnostics
.push(DiagnosticCode::LiteralNotIndexable, record_like.range());
}
self.declare_expression(record_like);
}
fn declare_call(
fn declare_call<'a>(
&mut self,
this: Option<&'s Expression<'s>>,
this: Option<&'s Expression<'s, 'a>>,
l: &'s TokenRef<'s>,
r: &'s TokenRef<'s>,
callable: &'s Callable<'s>,
args: &'s [ArgElement<'s>],
callable: &'s Callable<'s, 'a>,
args: &'s [ArgElement<'s, 'a>],
) {
if self.config.diagnostic_tag {
let start = this.map_or_else(|| callable.range().start, |c| c.range().start);
Expand Down Expand Up @@ -117,7 +117,7 @@ impl<'s, 'c> Emitter<'s, 'c> {
};
});
}
pub fn declare_expression(&mut self, outer: &'s Expression<'s>) {
pub fn declare_expression<'a>(&mut self, outer: &'s Expression<'s, 'a>) {
match outer {
Literal(_) => (),
InterpolatedString(_, exprs) => {
Expand Down Expand Up @@ -325,9 +325,9 @@ impl<'s, 'c> Emitter<'s, 'c> {
Unknown { .. } => (),
}
}
pub fn emit_expression_reg(
pub fn emit_expression_reg<'a>(
&mut self,
expr: &'s Expression<'s>,
expr: &'s Expression<'s, 'a>,
brk: Option<Register>,
) -> Register {
if let Variable(id_token) = expr {
Expand Down Expand Up @@ -379,7 +379,7 @@ impl<'s, 'c> Emitter<'s, 'c> {
Some(id)
}

fn emit_global_access(&mut self, expr: &'s Expression<'s>) -> Option<Cow<'s, str>> {
fn emit_global_access<'a>(&mut self, expr: &'s Expression<'s, 'a>) -> Option<Cow<'s, str>> {
let id = if let Variable(id_token) = expr {
let id = id_token.to_id_name()?;
if self.scopes.find_variable(id).is_some() {
Expand Down Expand Up @@ -408,9 +408,9 @@ impl<'s, 'c> Emitter<'s, 'c> {
Some(id)
}

fn emit_expr_callable(
fn emit_expr_callable<'a>(
&mut self,
callable: &'s Expression<'s>,
callable: &'s Expression<'s, 'a>,
args_reg: impl FnOnce(&mut Self) -> (Vec<Register>, Vec<OpParam>),
ret: Register,
brk: Option<Register>,
Expand All @@ -437,11 +437,11 @@ impl<'s, 'c> Emitter<'s, 'c> {
}
}

fn emit_call(
fn emit_call<'a>(
&mut self,
callable: &'s Callable<'s>,
arg0: Option<&'s Expression<'s>>,
args: &'s [ArgElement<'s>],
callable: &'s Callable<'s, 'a>,
arg0: Option<&'s Expression<'s, 'a>>,
args: &'s [ArgElement<'s, 'a>],
ret: Register,
brk: Option<Register>,
) {
Expand Down Expand Up @@ -506,9 +506,9 @@ impl<'s, 'c> Emitter<'s, 'c> {
}
}

pub fn emit_expression(
pub fn emit_expression<'a>(
&mut self,
expr: &'s Expression<'s>,
expr: &'s Expression<'s, 'a>,
ret: Register,
brk: Option<Register>,
) {
Expand Down
30 changes: 17 additions & 13 deletions crates/core/src/emitter/emitter_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use super::{
};

impl<'s, 'c> Emitter<'s, 'c> {
pub fn declare_pattern(
pub fn declare_pattern<'a>(
&mut self,
pattern: &'s Pattern<'s>,
pattern: &'s Pattern<'s, 'a>,
bind_type: Option<BindType>,
kw_pub: &Option<TokenRef<'s>>,
exports: &mut ModuleExports<'s, 'c>,
Expand Down Expand Up @@ -94,7 +94,11 @@ impl<'s, 'c> Emitter<'s, 'c> {
}
}

fn emit_failed_pattern(&mut self, pattern: &'s Pattern<'s>, bind_type: Option<BindType>) {
fn emit_failed_pattern<'a>(
&mut self,
pattern: &'s Pattern<'s, 'a>,
bind_type: Option<BindType>,
) {
// This function is called from `emit_pattern`,
// Do not emit diagnostics, initialization, or set markers
match pattern {
Expand Down Expand Up @@ -153,9 +157,9 @@ impl<'s, 'c> Emitter<'s, 'c> {
}
}

fn emit_literal_constant(
fn emit_literal_constant<'a>(
&mut self,
pattern_constant: &'s Pattern<'s>,
pattern_constant: &'s Pattern<'s, 'a>,
value: Register,
) -> Option<Constant<'s>> {
match pattern_constant {
Expand Down Expand Up @@ -206,10 +210,10 @@ impl<'s, 'c> Emitter<'s, 'c> {
}
}

fn emit_constant_pattern<const SAME: bool>(
fn emit_constant_pattern<'a, const SAME: bool>(
&mut self,
success: Register,
pattern: &'s Pattern<'s>,
pattern: &'s Pattern<'s, 'a>,
value: Register,
) -> bool {
let op: OpCode = if SAME { OpCode::Same } else { OpCode::Nsame };
Expand Down Expand Up @@ -249,10 +253,10 @@ impl<'s, 'c> Emitter<'s, 'c> {
}
}

fn emit_literal_guard(
fn emit_literal_guard<'a>(
&mut self,
success: Register,
pattern: &'s Pattern<'s>,
pattern: &'s Pattern<'s, 'a>,
value: Register,
literal: Constant<'s>,
) {
Expand All @@ -273,10 +277,10 @@ impl<'s, 'c> Emitter<'s, 'c> {
Constant::String(_) => self.op_unary(pattern.range(), success, OpCode::IsString, value),
}
}
fn emit_constant_guard(
fn emit_constant_guard<'a>(
&mut self,
success: Register,
pattern: &'s Pattern<'s>,
pattern: &'s Pattern<'s, 'a>,
value: Register,
constant: Register,
) {
Expand All @@ -293,10 +297,10 @@ impl<'s, 'c> Emitter<'s, 'c> {
);
}

pub fn emit_pattern(
pub fn emit_pattern<'a>(
&mut self,
success: Register,
pattern: &'s Pattern<'s>,
pattern: &'s Pattern<'s, 'a>,
value: Register,
bind_type: Option<BindType>,
) {
Expand Down
10 changes: 7 additions & 3 deletions crates/core/src/emitter/emitter_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use super::{
};

impl<'s, 'c> Emitter<'s, 'c> {
pub fn declare_statement(
pub fn declare_statement<'a>(
&mut self,
stmt: &'s Statement<'s>,
stmt: &'s Statement<'s, 'a>,
exports: &mut ModuleExports<'s, 'c>,
) {
match stmt {
Expand Down Expand Up @@ -74,7 +74,11 @@ impl<'s, 'c> Emitter<'s, 'c> {
}
}

pub fn emit_statement(&mut self, stmt: &'s Statement<'s>, brk: Option<Register>) -> bool {
pub fn emit_statement<'a>(
&mut self,
stmt: &'s Statement<'s, 'a>,
brk: Option<Register>,
) -> bool {
match stmt {
Expression(expression, _) | BlockExpression(expression) => {
self.emit_expression(expression, Register::EMPTY, brk);
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/emitter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use emitter_struct::Emitter;
pub use opcode::OpCode;
use opcode::Register;

pub fn emit<'s, 'c>(
script: &Script<'s>,
pub fn emit<'s, 'c, 'a>(
script: &Script<'s, 'a>,
diagnostics_collector: &mut DiagnosticsCollector<'s, 'c>,
) -> Vec<u8> {
let mut emitter = Emitter::new(diagnostics_collector);
Expand Down
Loading
Loading