Skip to content

Commit

Permalink
refactor: box statements and expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Mar 4, 2024
1 parent 65f2f19 commit 57f0d07
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 27 deletions.
26 changes: 17 additions & 9 deletions crates/fuse-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Chunk {
}

#[serializable]
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct Block {
pub statements: Vec<Statement>,
}
Expand All @@ -25,11 +25,11 @@ impl Block {
#[derive(Debug, PartialEq)]
pub enum Statement {
/// Empty statement for example `;;`
Empty(EmptyStatement),
Empty(Box<EmptyStatement>),
/// An expression statement.
Expression(Expression),
Expression(Box<Expression>),
/// A variable declaration using const, let or global keywords.
VariableDeclaration(VariableDeclaration),
VariableDeclaration(Box<VariableDeclaration>),
}

#[serializable]
Expand Down Expand Up @@ -88,11 +88,11 @@ pub struct Atom(pub Rc<str>);
#[serializable]
#[derive(Debug, PartialEq)]
pub enum Expression {
NumberLiteral(NumberLiteral),
StringLiteral(StringLiteral),
BooleanLiteral(BooleanLiteral),
Identifier(Identifier),
Function(Function),
NumberLiteral(Box<NumberLiteral>),
StringLiteral(Box<StringLiteral>),
BooleanLiteral(Box<BooleanLiteral>),
Identifier(Box<Identifier>),
Function(Box<Function>),
}

#[serializable]
Expand Down Expand Up @@ -172,6 +172,7 @@ pub struct Function {
pub span: Span,
pub params: FunctionParameters,
pub return_type: Option<TypeAnnotation>,
pub body: Option<FunctionBody>,
}

#[serializable]
Expand All @@ -196,3 +197,10 @@ pub struct BindingRest {
pub binding: BindingIdentifier,
pub type_annotation: Option<TypeAnnotation>,
}

#[serializable]
#[derive(Debug, PartialEq)]
pub enum FunctionBody {
Block(Block),
Expression(Expression),
}
28 changes: 26 additions & 2 deletions crates/fuse-ast/src/ast_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ impl AstFactory {
}

pub fn empty_statement(&self, span: Span) -> Statement {
Statement::Empty(EmptyStatement { span })
Statement::Empty(Box::from(EmptyStatement { span }))
}

pub fn declaration_statement(&self, decl: VariableDeclaration) -> Statement {
Statement::VariableDeclaration(decl)
Statement::VariableDeclaration(Box::from(decl))
}

pub fn expression_statement(&self, expr: Expression) -> Statement {
Statement::Expression(Box::from(expr))
}

pub fn variable_declaration(
Expand Down Expand Up @@ -85,4 +89,24 @@ impl AstFactory {
kind,
}
}

pub fn boolean_expression(&self, literal: BooleanLiteral) -> Expression {
Expression::BooleanLiteral(Box::from(literal))
}

pub fn number_expression(&self, literal: NumberLiteral) -> Expression {
Expression::NumberLiteral(Box::from(literal))
}

pub fn string_expression(&self, literal: StringLiteral) -> Expression {
Expression::StringLiteral(Box::from(literal))
}

pub fn identifier_expression(&self, ident: Identifier) -> Expression {
Expression::Identifier(Box::from(ident))
}

pub fn function_expression(&self, func: Function) -> Expression {
Expression::Function(Box::from(func))
}
}
1 change: 1 addition & 0 deletions crates/fuse-parser/src/lexer/token_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub enum TokenKind {
Minus,
Plus,
ThinArrow,
Arrow,
}

impl TokenKind {
Expand Down
50 changes: 36 additions & 14 deletions crates/fuse-parser/src/parsers/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
use crate::{lexer::TokenKind, Parser, ParserResult};
use fuse_ast::{
BindingPattern, BindingPatternKind, BooleanLiteral, Expression, Function, FunctionParameter,
FunctionParameters, Identifier, TypeAnnotation,
BindingPattern, BindingPatternKind, Block, BooleanLiteral, Expression, Function,
FunctionParameter, FunctionParameters, Identifier, TypeAnnotation,
};
use fuse_common::Span;

impl<'a> Parser<'a> {
pub(crate) fn parse_expression(&mut self) -> ParserResult<Expression> {
match self.cur_kind() {
TokenKind::True => Ok(Expression::BooleanLiteral(BooleanLiteral {
span: self.consume().span(),
value: true,
})),
TokenKind::False => Ok(Expression::BooleanLiteral(BooleanLiteral {
span: self.consume().span(),
value: false,
})),
TokenKind::NumberLiteral => Ok(Expression::NumberLiteral(self.parse_number_literal()?)),
TokenKind::True => {
let token = self.consume();
Ok(self.ast.boolean_expression(BooleanLiteral {
span: token.span(),
value: true,
}))
}
TokenKind::False => {
let token = self.consume();
Ok(self.ast.boolean_expression(BooleanLiteral {
span: token.span(),
value: false,
}))
}
TokenKind::NumberLiteral => {
let expr = self.parse_number_literal()?;
Ok(self.ast.number_expression(expr))
}
TokenKind::StringLiteral | TokenKind::InterpolatedStringHead => {
Ok(Expression::StringLiteral(self.parse_string_literal()?))
let expr = self.parse_string_literal()?;
Ok(self.ast.string_expression(expr))
}
TokenKind::Identifier => self.parse_identifier().map(|id| Expression::Identifier(id)),
TokenKind::Identifier => self
.parse_identifier()
.map(|id| self.ast.identifier_expression(id)),
TokenKind::Function | TokenKind::Fn => self
.parse_function_expression()
.map(|func| Expression::Function(func)),
.map(|func| self.ast.function_expression(func)),
_ => Err(Self::unexpected_error(self.cur_token())),
}
}
Expand All @@ -48,6 +60,7 @@ impl<'a> Parser<'a> {
span: self.end_span(start),
params,
return_type,
body: None,
})
}

Expand Down Expand Up @@ -103,4 +116,13 @@ impl<'a> Parser<'a> {
}
self.parse_type_annotation().map(|t| Some(t))
}

fn parse_function_body(&mut self) -> ParserResult<Block> {
if let Some(_) = self.consume_if(TokenKind::Arrow) {
self.parse_expression();
todo!()
} else {
self.parse_block()
}
}
}
4 changes: 2 additions & 2 deletions crates/fuse-parser/src/parsers/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<'a> Parser<'a> {

TokenKind::Const | TokenKind::Let | TokenKind::Global => self
.parse_variable_declaration()
.map(|decl| Statement::VariableDeclaration(decl)),
.map(|decl| self.ast.declaration_statement(decl)),

TokenKind::True
| TokenKind::False
Expand All @@ -44,7 +44,7 @@ impl<'a> Parser<'a> {
| TokenKind::Function
| TokenKind::Fn => self
.parse_expression()
.map(|expr| Statement::Expression(expr)),
.map(|expr| self.ast.expression_statement(expr)),

kind if kind.is_trivial() => {
unreachable!("All trivial tokens should be eaten by a `TokenReference`.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Some(Chunk(
rest: None,
),
return_type: None,
body: None,
))),
],
),
Expand Down

0 comments on commit 57f0d07

Please sign in to comment.