Skip to content

Commit

Permalink
refactor(ast): better literal type wrapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Mar 10, 2024
1 parent 28d5c9a commit da215d4
Show file tree
Hide file tree
Showing 50 changed files with 211 additions and 205 deletions.
12 changes: 9 additions & 3 deletions crates/fuse-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ pub struct Atom(pub Rc<str>);
#[serializable]
#[derive(Debug, PartialEq)]
pub enum Expression {
NumberLiteral(Box<NumberLiteral>),
StringLiteral(Box<StringLiteral>),
BooleanLiteral(Box<BooleanLiteral>),
Literal(Box<Literal>),
Identifier(Box<Identifier>),
Function(Box<Function>),
If(Box<If>),
Expand All @@ -116,6 +114,14 @@ 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
12 changes: 6 additions & 6 deletions crates/fuse-ast/src/ast_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,16 @@ impl AstFactory {
}
}

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

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

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

pub fn identifier_expression(&self, ident: Identifier) -> Expression {
Expand Down
8 changes: 4 additions & 4 deletions crates/fuse-parser/src/parsers/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ impl<'a> Parser<'a> {
let expr = match self.cur_kind() {
True => {
let token = self.consume();
Ok(self.ast.boolean_expression(BooleanLiteral {
Ok(self.ast.boolean_literal_expression(BooleanLiteral {
span: token.span(),
value: true,
}))
}
False => {
let token = self.consume();
Ok(self.ast.boolean_expression(BooleanLiteral {
Ok(self.ast.boolean_literal_expression(BooleanLiteral {
span: token.span(),
value: false,
}))
}
NumberLiteral => self
.parse_number_literal()
.map(|expr| self.ast.number_expression(expr)),
.map(|expr| self.ast.number_literal_expression(expr)),
StringLiteral | InterpolatedStringHead => self
.parse_string_literal()
.map(|expr| self.ast.string_expression(expr)),
.map(|expr| self.ast.string_literal_expression(expr)),
Identifier => self
.parse_identifier()
.map(|id| self.ast.identifier_expression(id)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ Some(Chunk(
type_annotation: None,
optional: false,
),
expression: Some(NumberLiteral(NumberLiteral(
expression: Some(Literal(Number(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(NumberLiteral(NumberLiteral(
Expression(Literal(Number(NumberLiteral(
span: Span(
start: 1,
end: 2,
),
raw: Atom("1"),
value: 1.0,
kind: Decimal,
))),
Expression(NumberLiteral(NumberLiteral(
)))),
Expression(Literal(Number(NumberLiteral(
span: Span(
start: 4,
end: 5,
),
raw: Atom("2"),
value: 2.0,
kind: Decimal,
))),
Expression(NumberLiteral(NumberLiteral(
)))),
Expression(Literal(Number(NumberLiteral(
span: Span(
start: 7,
end: 8,
),
raw: Atom("3"),
value: 3.0,
kind: Decimal,
))),
Expression(NumberLiteral(NumberLiteral(
)))),
Expression(Literal(Number(NumberLiteral(
span: Span(
start: 10,
end: 11,
),
raw: Atom("4"),
value: 4.0,
kind: Decimal,
))),
Expression(NumberLiteral(NumberLiteral(
)))),
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
Expression(Literal(Number(NumberLiteral(
span: Span(
start: 1,
end: 2,
),
raw: Atom("1"),
value: 1.0,
kind: Decimal,
))),
Expression(NumberLiteral(NumberLiteral(
)))),
Expression(Literal(Number(NumberLiteral(
span: Span(
start: 4,
end: 5,
),
raw: Atom("2"),
value: 2.0,
kind: Decimal,
))),
Expression(NumberLiteral(NumberLiteral(
)))),
Expression(Literal(Number(NumberLiteral(
span: Span(
start: 7,
end: 8,
),
raw: Atom("3"),
value: 3.0,
kind: Decimal,
))),
Expression(NumberLiteral(NumberLiteral(
)))),
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
Expression(Literal(Number(NumberLiteral(
span: Span(
start: 17,
end: 18,
),
raw: Atom("5"),
value: 5.0,
kind: Decimal,
))),
Expression(NumberLiteral(NumberLiteral(
)))),
Expression(Literal(Number(NumberLiteral(
span: Span(
start: 20,
end: 21,
),
raw: Atom("6"),
value: 6.0,
kind: Decimal,
))),
Expression(NumberLiteral(NumberLiteral(
)))),
Expression(Literal(Number(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(NumberLiteral(NumberLiteral(
Expression(Literal(Number(NumberLiteral(
span: Span(
start: 30,
end: 31,
),
raw: Atom("8"),
value: 8.0,
kind: Decimal,
))),
Expression(NumberLiteral(NumberLiteral(
)))),
Expression(Literal(Number(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: NumberLiteral(NumberLiteral(
lhs: Literal(Number(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: NumberLiteral(NumberLiteral(
lhs: Literal(Number(NumberLiteral(
span: Span(
start: 8,
end: 9,
),
raw: Atom("2"),
value: 2.0,
kind: Decimal,
)),
rhs: NumberLiteral(NumberLiteral(
))),
rhs: Literal(Number(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: NumberLiteral(NumberLiteral(
lhs: Literal(Number(NumberLiteral(
span: Span(
start: 16,
end: 17,
),
raw: Atom("4"),
value: 4.0,
kind: Decimal,
)),
rhs: NumberLiteral(NumberLiteral(
))),
rhs: Literal(Number(NumberLiteral(
span: Span(
start: 20,
end: 21,
),
raw: Atom("5"),
value: 5.0,
kind: Decimal,
)),
))),
)),
)),
))),
Expand Down
Loading

0 comments on commit da215d4

Please sign in to comment.