Skip to content

Commit

Permalink
refactor(ast): better ast structure for binding identifier and literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Mar 10, 2024
1 parent da215d4 commit 1b8ab76
Show file tree
Hide file tree
Showing 55 changed files with 308 additions and 228 deletions.
14 changes: 4 additions & 10 deletions crates/fuse-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub enum BindingPatternKind {
#[derive(Debug, PartialEq)]
pub struct BindingIdentifier {
pub span: Span,
pub atom: Atom,
pub identifier: Identifier,
pub mutable: bool,
}

Expand All @@ -100,7 +100,9 @@ pub struct Atom(pub Rc<str>);
#[serializable]
#[derive(Debug, PartialEq)]
pub enum Expression {
Literal(Box<Literal>),
NumberLiteral(Box<NumberLiteral>),
StringLiteral(Box<StringLiteral>),
BooleanLiteral(Box<BooleanLiteral>),
Identifier(Box<Identifier>),
Function(Box<Function>),
If(Box<If>),
Expand All @@ -114,14 +116,6 @@ pub enum Expression {
StructConstructionExpression(Box<StructConstructionExpression>),
}

#[serializable]
#[derive(Debug, PartialEq)]
pub enum Literal {
Number(NumberLiteral),
String(StringLiteral),
Boolean(BooleanLiteral),
}

#[serializable]
#[derive(Debug, PartialEq)]
pub struct BooleanLiteral {
Expand Down
15 changes: 10 additions & 5 deletions crates/fuse-ast/src/ast_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,15 @@ impl AstFactory {
}
}

pub fn binding_identifier(&self, span: Span, atom: Atom, mutable: bool) -> BindingIdentifier {
pub fn binding_identifier(
&self,
span: Span,
identifier: Identifier,
mutable: bool,
) -> BindingIdentifier {
BindingIdentifier {
span,
atom,
identifier,
mutable,
}
}
Expand All @@ -113,15 +118,15 @@ impl AstFactory {
}

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

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

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

pub fn identifier_expression(&self, ident: Identifier) -> Expression {
Expand Down
11 changes: 4 additions & 7 deletions crates/fuse-parser/src/parsers/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a> Parser<'a> {
return Err(Self::unexpected_error(self.cur_token()));
}

let identifier = self.parse_binding_identifier();
let identifier = self.parse_binding_identifier()?;
let type_annotation = if self.consume_if(TokenKind::Colon).is_some() {
Some(self.parse_type_annotation()?)
} else {
Expand All @@ -44,16 +44,13 @@ impl<'a> Parser<'a> {
.binding_identifier_pattern(identifier, type_annotation, false))
}

pub(crate) fn parse_binding_identifier(&mut self) -> BindingIdentifier {
pub(crate) fn parse_binding_identifier(&mut self) -> ParserResult<BindingIdentifier> {
let mut span = self.start_span();
let mutable = self.consume_if(TokenKind::Mut).is_some();
let token = self.consume();
let name = self.view_token(*token);
let identifier = self.parse_identifier()?;

span = self.end_span(span);

let atom = self.ast.atom(name);

self.ast.binding_identifier(span, atom, mutable)
Ok(self.ast.binding_identifier(span, identifier, mutable))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,27 @@ Some(Chunk(
start: 6,
end: 10,
),
atom: Atom("name"),
identifier: Identifier(
span: Span(
start: 6,
end: 10,
),
name: Atom("name"),
),
mutable: false,
)),
type_annotation: None,
optional: false,
),
expression: Some(Literal(Number(NumberLiteral(
expression: Some(NumberLiteral(NumberLiteral(
span: Span(
start: 13,
end: 16,
),
raw: Atom("123"),
value: 123.0,
kind: Decimal,
)))),
))),
)),
],
),
Expand Down
20 changes: 10 additions & 10 deletions crates/fuse-parser/tests/cases/pass/array-initializer-01/ast.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,51 @@ Some(Chunk(
end: 15,
),
elements: [
Expression(Literal(Number(NumberLiteral(
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 1,
end: 2,
),
raw: Atom("1"),
value: 1.0,
kind: Decimal,
)))),
Expression(Literal(Number(NumberLiteral(
))),
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 4,
end: 5,
),
raw: Atom("2"),
value: 2.0,
kind: Decimal,
)))),
Expression(Literal(Number(NumberLiteral(
))),
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 7,
end: 8,
),
raw: Atom("3"),
value: 3.0,
kind: Decimal,
)))),
Expression(Literal(Number(NumberLiteral(
))),
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 10,
end: 11,
),
raw: Atom("4"),
value: 4.0,
kind: Decimal,
)))),
Expression(Literal(Number(NumberLiteral(
))),
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 13,
end: 14,
),
raw: Atom("5"),
value: 5.0,
kind: Decimal,
)))),
))),
],
))),
],
Expand Down
36 changes: 18 additions & 18 deletions crates/fuse-parser/tests/cases/pass/array-initializer-02/ast.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,42 @@ Some(Chunk(
end: 37,
),
elements: [
Expression(Literal(Number(NumberLiteral(
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 1,
end: 2,
),
raw: Atom("1"),
value: 1.0,
kind: Decimal,
)))),
Expression(Literal(Number(NumberLiteral(
))),
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 4,
end: 5,
),
raw: Atom("2"),
value: 2.0,
kind: Decimal,
)))),
Expression(Literal(Number(NumberLiteral(
))),
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 7,
end: 8,
),
raw: Atom("3"),
value: 3.0,
kind: Decimal,
)))),
Expression(Literal(Number(NumberLiteral(
))),
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 10,
end: 11,
),
raw: Atom("4"),
value: 4.0,
kind: Decimal,
)))),
))),
Spread(SpreadArgument(
span: Span(
start: 13,
Expand All @@ -64,33 +64,33 @@ Some(Chunk(
end: 36,
),
elements: [
Expression(Literal(Number(NumberLiteral(
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 17,
end: 18,
),
raw: Atom("5"),
value: 5.0,
kind: Decimal,
)))),
Expression(Literal(Number(NumberLiteral(
))),
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 20,
end: 21,
),
raw: Atom("6"),
value: 6.0,
kind: Decimal,
)))),
Expression(Literal(Number(NumberLiteral(
))),
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 23,
end: 24,
),
raw: Atom("7"),
value: 7.0,
kind: Decimal,
)))),
))),
Spread(SpreadArgument(
span: Span(
start: 26,
Expand All @@ -102,24 +102,24 @@ Some(Chunk(
end: 35,
),
elements: [
Expression(Literal(Number(NumberLiteral(
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 30,
end: 31,
),
raw: Atom("8"),
value: 8.0,
kind: Decimal,
)))),
Expression(Literal(Number(NumberLiteral(
))),
Expression(NumberLiteral(NumberLiteral(
span: Span(
start: 33,
end: 34,
),
raw: Atom("9"),
value: 9.0,
kind: Decimal,
)))),
))),
],
)),
)),
Expand Down
20 changes: 10 additions & 10 deletions crates/fuse-parser/tests/cases/pass/binary-operator-04/ast.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,63 +33,63 @@ Some(Chunk(
start: 6,
end: 7,
)),
lhs: Literal(Number(NumberLiteral(
lhs: NumberLiteral(NumberLiteral(
span: Span(
start: 4,
end: 5,
),
raw: Atom("1"),
value: 1.0,
kind: Decimal,
))),
)),
rhs: BinaryOperator(BinaryOperator(
kind: Multiply(Span(
start: 10,
end: 11,
)),
lhs: Literal(Number(NumberLiteral(
lhs: NumberLiteral(NumberLiteral(
span: Span(
start: 8,
end: 9,
),
raw: Atom("2"),
value: 2.0,
kind: Decimal,
))),
rhs: Literal(Number(NumberLiteral(
)),
rhs: NumberLiteral(NumberLiteral(
span: Span(
start: 12,
end: 13,
),
raw: Atom("3"),
value: 3.0,
kind: Decimal,
))),
)),
)),
)),
rhs: BinaryOperator(BinaryOperator(
kind: Division(Span(
start: 18,
end: 19,
)),
lhs: Literal(Number(NumberLiteral(
lhs: NumberLiteral(NumberLiteral(
span: Span(
start: 16,
end: 17,
),
raw: Atom("4"),
value: 4.0,
kind: Decimal,
))),
rhs: Literal(Number(NumberLiteral(
)),
rhs: NumberLiteral(NumberLiteral(
span: Span(
start: 20,
end: 21,
),
raw: Atom("5"),
value: 5.0,
kind: Decimal,
))),
)),
)),
)),
))),
Expand Down
Loading

0 comments on commit 1b8ab76

Please sign in to comment.