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
42 changes: 1 addition & 41 deletions Cargo.lock

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

9 changes: 2 additions & 7 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@ formatter = []
wasm = ["dep:wasm-bindgen"]
wasm-constants = ["dep:wasm-bindgen"]
serde = ["dep:serde"]
mimalloc = ["dep:mimalloc"]

[dev-dependencies]
divan = "0.1.21"
mira-core = { path = ".", features = ["mimalloc"] }
mira-core = { path = "." }

[dependencies]
unicode-ident = "1"
winnow = "1"
strum = { version = "0.28", features = ["derive"] }
wasm-bindgen = { version = "0.2", optional = true }
serde = { version = "1", optional = true, features = ["derive"] }

[target.'cfg(target_os = "windows")'.dependencies]
mimalloc = { version = "0.1", optional = true, features = [] }
[target.'cfg(not(target_os = "windows"))'.dependencies]
mimalloc = { version = "0.1", optional = true, features = ["v2"] }
bumpalo = { version = "3", features = ["boxed"] }
8 changes: 5 additions & 3 deletions crates/core/src/compile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bumpalo::Bump;
use winnow::stream::{Location, Stream};

use crate::parser::{self, AstWalker};
Expand Down Expand Up @@ -68,11 +69,11 @@ impl<'s, 'c: 's> Compiler<'s, 'c> {
)
}

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

// Parsing
let mut stream = parser::to_input(tokens);
let mut stream = parser::to_input(tokens, arena);
let Ok(mut script) = parser::parse(&mut stream) else {
let remaining = stream.peek_finish();
let range = if remaining.is_empty() {
Expand Down Expand Up @@ -115,10 +116,11 @@ impl<'s, 'c: 's> Compiler<'s, 'c> {

pub fn compile(input: &str, config: &Config) -> CompileResult {
let mut compiler = Compiler::new(input, config);
let arena = Bump::new();
let Some(tokens) = compiler.lex() else {
return (None, compiler.encode_diagnostics());
};
let Some(script) = compiler.parse(&tokens) else {
let Some(script) = compiler.parse(&tokens, &arena) else {
return (None, compiler.encode_diagnostics());
};
let Some(chunk) = compiler.emit(&script) else {
Expand Down
10 changes: 6 additions & 4 deletions crates/core/src/emitter/emitter_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::ops::{Deref, DerefMut};
use crate::{
diagnostic::SourceRange,
emitter::variable::Variable,
parser::{ArrayElementBase, AstWalker as _, Expression, ParameterList, Pattern, Statement},
parser::{
ArrayElementBase, AstBox, AstWalker as _, Expression, ParameterList, Pattern, Statement,
},
};

use super::{
Expand Down Expand Up @@ -62,7 +64,7 @@ impl<'s, 'c> Emitter<'s, 'c> {
pub fn declare_block(
&mut self,
stmts: &'s Vec<Statement<'s>>,
expr: &'s Option<Box<Expression<'s>>>,
expr: &'s Option<AstBox<'s, Expression<'s>>>,
exports: &mut ModuleExports<'s, 'c>,
) {
for stmt in stmts {
Expand All @@ -75,7 +77,7 @@ impl<'s, 'c> Emitter<'s, 'c> {
pub fn emit_block(
&mut self,
stmts: &'s Vec<Statement<'s>>,
expr: &'s Option<Box<Expression<'s>>>,
expr: &'s Option<AstBox<'s, Expression<'s>>>,
scope_range: SourceRange,
ret: Register,
brk: Option<Register>,
Expand Down Expand Up @@ -128,7 +130,7 @@ impl<'s, 'c> Emitter<'s, 'c> {
scope_range: SourceRange,
args: &'s Option<ParameterList<'s>>,
stmts: &'s Vec<Statement<'s>>,
expr: &'s Option<Box<Expression<'s>>>,
expr: &'s Option<AstBox<'s, Expression<'s>>>,
) {
let arg_len = args.as_ref().map_or(1, |args| args.len());
let mut has_var_args = false;
Expand Down
10 changes: 10 additions & 0 deletions crates/core/src/formatter/manager/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ impl<T: Formattable> Formattable for Box<T> {
}
}

impl<T: Formattable> Formattable for bumpalo::boxed::Box<'_, T> {
fn measure(&self, formatter: &FormatManager, indent: usize) -> usize {
self.as_ref().measure(formatter, indent)
}

fn format(&self, formatter: &mut FormatManager, complexity: usize) {
self.as_ref().format(formatter, complexity)
}
}

impl<T: Formattable> Formattable for Option<T> {
fn measure(&self, formatter: &FormatManager, indent: usize) -> usize {
if let Some(value) = self {
Expand Down
5 changes: 1 addition & 4 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod emitter;
pub mod lexer;
pub mod parser;

pub use bumpalo::Bump;
pub use compile::Compiler;
pub use config::Config;
pub use diagnostic::{DiagnosticCode, SerializedDiagnostics, SourceDiagnostic, SourceRange};
Expand All @@ -22,7 +23,3 @@ pub mod prelude {
pub use std::string::ToString as _;
pub use strum::VariantArray as _;
}

#[cfg(all(feature = "mimalloc", not(target_family = "wasm")))]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
6 changes: 3 additions & 3 deletions crates/core/src/parser/array_element.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::prelude::*;

#[derive(Debug, Clone, PartialEq, strum::EnumIs)]
#[derive(Debug, PartialEq, strum::EnumIs)]
pub enum ArrayElementBase<'s, E, S> {
/// Element
Element(Box<E>),
Element(AstBox<'s, E>),
/// `..` Spread
Spread(TokenRef<'s>, Box<S>),
Spread(TokenRef<'s>, AstBox<'s, S>),
}

use ArrayElementBase::*;
Expand Down
16 changes: 9 additions & 7 deletions crates/core/src/parser/array_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
};

type _ArrayElement<'s, E, S> = ListItem<'s, ArrayElementBase<'s, E, S>>;
fn array_element<'s, E: Clone + PartialEq + 's, S: Clone + PartialEq + 's>(
fn array_element<'s, E: PartialEq + 's, S: PartialEq + 's>(
element: impl Parser<'s, E>,
spread: impl Parser<'s, S>,
mut missing: impl FnMut(usize) -> E + Copy,
Expand All @@ -21,8 +21,9 @@ fn array_element<'s, E: Clone + PartialEq + 's, S: Clone + PartialEq + 's>(
let pos = comma.range.start;
let missing = missing(pos);
return Ok(ListItem::new_with_comma(
ArrayElementBase::Element(Box::new(missing)),
ArrayElementBase::Element(AstBox::new_in(missing, i.state)),
comma,
i.state,
));
}
if *first == Operator::CloseBracket
Expand All @@ -32,13 +33,14 @@ fn array_element<'s, E: Clone + PartialEq + 's, S: Clone + PartialEq + 's>(
{
return fail.parse_next(i);
}
let arena = i.state;
let result = if *first == Operator::SpreadRange {
(token(Operator::SpreadRange), spread)
.map(|(s, e)| ArrayElementBase::Spread(s, e.into()))
.map(move |(s, e)| ArrayElementBase::Spread(s, AstBox::new_in(e, arena)))
.parse_next(i)?
} else {
element
.map(|e| ArrayElementBase::Element(Box::new(e)))
.map(move |e| ArrayElementBase::Element(AstBox::new_in(e, arena)))
.parse_next(i)?
};
let last = peek(any).parse_next(i)?;
Expand All @@ -57,15 +59,15 @@ fn array_element<'s, E: Clone + PartialEq + 's, S: Clone + PartialEq + 's>(
|| *last == Keyword::Let
|| *last == Keyword::Const
{
return Ok(ListItem::new(result));
return Ok(ListItem::new(result, i.state));
}
let comma = token_or_insert(Operator::Comma, DiagnosticCode::MissingComma).parse_next(i)?;
Ok(ListItem::new_with_comma(result, comma))
Ok(ListItem::new_with_comma(result, comma, i.state))
}
}

type _ArrayLike<'s, E, S> = (TokenRef<'s>, Vec<_ArrayElement<'s, E, S>>, TokenRef<'s>);
pub(super) fn array_base<'t, 's: 't, E: Clone + PartialEq + 's, S: Clone + PartialEq + 's>(
pub(super) fn array_base<'t, 's: 't, E: PartialEq + 's, S: PartialEq + 's>(
mut open: impl Parser<'s, TokenRef<'s>>,
mut close: impl Parser<'s, TokenRef<'s>>,
element: impl Parser<'s, E>,
Expand Down
9 changes: 9 additions & 0 deletions crates/core/src/parser/ast_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ impl<'s, E: AstWalker<'s>> AstWalker<'s> for Option<E> {
}
}

impl<'s, E: AstWalker<'s>> AstWalker<'s> for AstBox<'s, E> {
fn collect_diagnostics(&mut self, collector: &mut DiagnosticsCollector<'_, '_>) {
self.deref_mut().collect_diagnostics(collector);
}
fn range(&self) -> SourceRange {
self.deref().range()
}
}

impl<'s, E: AstWalker<'s>> AstWalker<'s> for Box<E> {
fn collect_diagnostics(&mut self, collector: &mut DiagnosticsCollector<'_, '_>) {
self.deref_mut().collect_diagnostics(collector);
Expand Down
Loading
Loading