use ref instead of box for ast

This commit is contained in:
nora 2021-12-30 16:55:50 +01:00
parent 62e4ffac6c
commit 5e66841577
2 changed files with 31 additions and 48 deletions

View file

@ -3,7 +3,6 @@
use crate::errors::Span; use crate::errors::Span;
use crate::value::Symbol; use crate::value::Symbol;
use bumpalo::boxed::Box;
use bumpalo::collections::Vec; use bumpalo::collections::Vec;
#[derive(Debug, PartialEq, Eq, Hash)] #[derive(Debug, PartialEq, Eq, Hash)]
@ -63,7 +62,7 @@ pub struct IfStmt<'ast> {
pub span: Span, pub span: Span,
pub cond: Expr<'ast>, pub cond: Expr<'ast>,
pub body: Block<'ast>, pub body: Block<'ast>,
pub else_part: Option<Box<'ast, ElsePart<'ast>>>, pub else_part: Option<&'ast ElsePart<'ast>>,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -92,9 +91,9 @@ pub struct WhileStmt<'ast> {
pub enum Expr<'ast> { pub enum Expr<'ast> {
Ident(Ident), Ident(Ident),
Literal(Literal<'ast>), Literal(Literal<'ast>),
UnaryOp(Box<'ast, UnaryOp<'ast>>), UnaryOp(&'ast UnaryOp<'ast>),
BinaryOp(Box<'ast, BinaryOp<'ast>>), BinaryOp(&'ast BinaryOp<'ast>),
Call(Box<'ast, Call<'ast>>), Call(&'ast Call<'ast>),
} }
impl Expr<'_> { impl Expr<'_> {

View file

@ -4,7 +4,6 @@ mod test;
use crate::ast::*; use crate::ast::*;
use crate::errors::{CompilerError, Span}; use crate::errors::{CompilerError, Span};
use crate::lex::{Token, TokenKind}; use crate::lex::{Token, TokenKind};
use bumpalo::boxed::Box;
use bumpalo::collections::Vec; use bumpalo::collections::Vec;
use bumpalo::Bump; use bumpalo::Bump;
use std::iter::Peekable; use std::iter::Peekable;
@ -43,15 +42,12 @@ macro_rules! parse_bin_op {
($self: ident, $lhs: ident, $kind: expr, $function: ident) => {{ ($self: ident, $lhs: ident, $kind: expr, $function: ident) => {{
let _ = $self.next(); let _ = $self.next();
let rhs = $self.$function()?; let rhs = $self.$function()?;
Ok(Expr::BinaryOp(Box::new_in( Ok(Expr::BinaryOp($self.bump.alloc(BinaryOp {
BinaryOp {
span: $lhs.span().extend(rhs.span()), span: $lhs.span().extend(rhs.span()),
lhs: $lhs, lhs: $lhs,
rhs, rhs,
kind: $kind, kind: $kind,
}, })))
$self.bump,
)))
}}; }};
} }
@ -221,7 +217,7 @@ where
.option_extend(else_part.as_ref().map(|part| part.span())), .option_extend(else_part.as_ref().map(|part| part.span())),
cond, cond,
body, body,
else_part: else_part.map(|part| Box::new_in(part, self.bump)), else_part: else_part.map(|part| &*self.bump.alloc(part)),
}) })
} }
@ -463,26 +459,20 @@ where
Some(TokenKind::Not) => { Some(TokenKind::Not) => {
let unary_op_span = self.next().unwrap().span; let unary_op_span = self.next().unwrap().span;
let expr = self.call()?; let expr = self.call()?;
Ok(Expr::UnaryOp(Box::new_in( Ok(Expr::UnaryOp(self.bump.alloc(UnaryOp {
UnaryOp {
span: unary_op_span.extend(expr.span()), span: unary_op_span.extend(expr.span()),
expr, expr,
kind: UnaryOpKind::Not, kind: UnaryOpKind::Not,
}, })))
self.bump,
)))
} }
Some(TokenKind::Minus) => { Some(TokenKind::Minus) => {
let unary_op_span = self.next().unwrap().span; let unary_op_span = self.next().unwrap().span;
let expr = self.call()?; let expr = self.call()?;
Ok(Expr::UnaryOp(Box::new_in( Ok(Expr::UnaryOp(self.bump.alloc(UnaryOp {
UnaryOp {
span: unary_op_span.extend(expr.span()), span: unary_op_span.extend(expr.span()),
expr, expr,
kind: UnaryOpKind::Neg, kind: UnaryOpKind::Neg,
}, })))
self.bump,
)))
} }
_ => self.call(), _ => self.call(),
}; };
@ -502,27 +492,21 @@ where
let args = self.parse_list(TokenKind::ParenC, Self::expression)?; let args = self.parse_list(TokenKind::ParenC, Self::expression)?;
let close_span = self.expect(TokenKind::ParenC)?.span; let close_span = self.expect(TokenKind::ParenC)?.span;
Expr::Call(Box::new_in( Expr::Call(self.bump.alloc(Call {
Call {
callee: expr, callee: expr,
span: open_span.extend(close_span), span: open_span.extend(close_span),
kind: CallKind::Fn(args), kind: CallKind::Fn(args),
}, }))
self.bump,
))
} }
Some(TokenKind::Dot) => { Some(TokenKind::Dot) => {
let dot_span = self.expect(TokenKind::Dot)?.span; let dot_span = self.expect(TokenKind::Dot)?.span;
let field = self.ident()?; let field = self.ident()?;
Expr::Call(Box::new_in( Expr::Call(self.bump.alloc(Call {
Call {
callee: expr, callee: expr,
span: dot_span.extend(field.span), span: dot_span.extend(field.span),
kind: CallKind::Field(field), kind: CallKind::Field(field),
}, }))
self.bump,
))
} }
_ => break, _ => break,
} }