Skip to content

Commit

Permalink
refactor(ast): add reference cell to identifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Mar 11, 2024
1 parent 31f569d commit a3f05a1
Show file tree
Hide file tree
Showing 40 changed files with 104 additions and 1 deletion.
7 changes: 6 additions & 1 deletion crates/fuse-ast/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fuse_common::Span;
use fuse_common_proc::serializable;
use std::rc::Rc;
use std::{cell::Cell, rc::Rc};

#[serializable]
#[derive(Debug)]
Expand Down Expand Up @@ -185,8 +185,13 @@ pub enum InterpolationFormat {
pub struct Identifier {
pub span: Span,
pub name: Atom,
pub reference: Cell<Option<ReferenceId>>,
}

#[serializable]
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ReferenceId(u32);

#[serializable]
#[derive(Debug, PartialEq)]
pub struct Function {
Expand Down
3 changes: 3 additions & 0 deletions crates/fuse-parser/src/parsers/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cell::Cell;

use crate::{lexer::TokenKind, Parser, ParserResult};
use fuse_ast::{
ArrayExpressionElement, BinaryOperator, BooleanLiteral, ConstructionExpression,
Expand Down Expand Up @@ -71,6 +73,7 @@ impl<'a> Parser<'a> {
Ok(Identifier {
span: token.span(),
name: self.ast.atom(view),
reference: Cell::default(),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Some(Chunk(
end: 10,
),
name: Atom("name"),
reference: None,
),
mutable: false,
)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Some(Chunk(
end: 1,
),
name: Atom("x"),
reference: None,
)),
rhs: BinaryOperator(BinaryOperator(
kind: LogicalAnd(Span(
Expand All @@ -34,13 +35,15 @@ Some(Chunk(
end: 5,
),
name: Atom("a"),
reference: None,
)),
rhs: Identifier(Identifier(
span: Span(
start: 10,
end: 11,
),
name: Atom("b"),
reference: None,
)),
)),
))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Some(Chunk(
end: 1,
),
name: Atom("x"),
reference: None,
)),
rhs: BinaryOperator(BinaryOperator(
kind: LogicalAnd(Span(
Expand All @@ -39,13 +40,15 @@ Some(Chunk(
end: 5,
),
name: Atom("a"),
reference: None,
)),
rhs: Identifier(Identifier(
span: Span(
start: 10,
end: 11,
),
name: Atom("b"),
reference: None,
)),
)),
rhs: Identifier(Identifier(
Expand All @@ -54,6 +57,7 @@ Some(Chunk(
end: 17,
),
name: Atom("c"),
reference: None,
)),
)),
))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Some(Chunk(
end: 1,
),
name: Atom("x"),
reference: None,
)),
rhs: BinaryOperator(BinaryOperator(
kind: LogicalOr(Span(
Expand All @@ -39,13 +40,15 @@ Some(Chunk(
end: 5,
),
name: Atom("a"),
reference: None,
)),
rhs: Identifier(Identifier(
span: Span(
start: 10,
end: 11,
),
name: Atom("b"),
reference: None,
)),
)),
rhs: Identifier(Identifier(
Expand All @@ -54,6 +57,7 @@ Some(Chunk(
end: 16,
),
name: Atom("c"),
reference: None,
)),
)),
))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Some(Chunk(
end: 1,
),
name: Atom("x"),
reference: None,
)),
rhs: BinaryOperator(BinaryOperator(
kind: Minus(Span(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Some(Chunk(
end: 1,
),
name: Atom("x"),
reference: None,
)),
rhs: BinaryOperator(BinaryOperator(
kind: LessThan(Span(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Some(Chunk(
end: 1,
),
name: Atom("x"),
reference: None,
)),
rhs: BinaryOperator(BinaryOperator(
kind: LogicalAnd(Span(
Expand All @@ -39,13 +40,15 @@ Some(Chunk(
end: 5,
),
name: Atom("x"),
reference: None,
)),
rhs: Identifier(Identifier(
span: Span(
start: 8,
end: 11,
),
name: Atom("max"),
reference: None,
)),
)),
rhs: BinaryOperator(BinaryOperator(
Expand All @@ -59,13 +62,15 @@ Some(Chunk(
end: 17,
),
name: Atom("x"),
reference: None,
)),
rhs: Identifier(Identifier(
span: Span(
start: 21,
end: 24,
),
name: Atom("min"),
reference: None,
)),
)),
)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Some(Chunk(
end: 1,
),
name: Atom("x"),
reference: None,
)),
rhs: BinaryOperator(BinaryOperator(
kind: Multiply(Span(
Expand All @@ -39,6 +40,7 @@ Some(Chunk(
end: 6,
),
name: Atom("x"),
reference: None,
)),
)),
rhs: NumberLiteral(NumberLiteral(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Some(Chunk(
end: 4,
),
name: Atom("test"),
reference: None,
)),
arguments: [],
))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Some(Chunk(
end: 4,
),
name: Atom("test"),
reference: None,
)),
arguments: [
Identifier(Identifier(
Expand All @@ -30,20 +31,23 @@ Some(Chunk(
end: 6,
),
name: Atom("a"),
reference: None,
)),
Identifier(Identifier(
span: Span(
start: 8,
end: 9,
),
name: Atom("b"),
reference: None,
)),
Identifier(Identifier(
span: Span(
start: 11,
end: 12,
),
name: Atom("c"),
reference: None,
)),
],
))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Some(Chunk(
end: 3,
),
name: Atom("foo"),
reference: None,
)),
rhs: BinaryOperator(BinaryOperator(
kind: Member(Span(
Expand All @@ -39,6 +40,7 @@ Some(Chunk(
end: 7,
),
name: Atom("bar"),
reference: None,
)),
arguments: [],
)),
Expand All @@ -53,6 +55,7 @@ Some(Chunk(
end: 13,
),
name: Atom("baz"),
reference: None,
)),
arguments: [],
)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Some(Chunk(
end: 13,
),
name: Atom("EnumName"),
reference: None,
),
variants: [
EnumVariant(
Expand All @@ -31,6 +32,7 @@ Some(Chunk(
end: 23,
),
name: Atom("VariantA"),
reference: None,
),
value: None,
),
Expand All @@ -41,6 +43,7 @@ Some(Chunk(
end: 33,
),
name: Atom("VariantB"),
reference: None,
),
value: None,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Some(Chunk(
end: 10,
),
name: Atom("Ident"),
reference: None,
),
variants: [
EnumVariant(
Expand All @@ -31,6 +32,7 @@ Some(Chunk(
end: 16,
),
name: Atom("Var1"),
reference: None,
),
value: Some(StringLiteral(StringLiteral(
span: Span(
Expand All @@ -52,6 +54,7 @@ Some(Chunk(
end: 32,
),
name: Atom("Var2"),
reference: None,
),
value: Some(StringLiteral(StringLiteral(
span: Span(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Some(Chunk(
end: 13,
),
name: Atom("test"),
reference: None,
)),
params: FunctionParameters(
span: Span(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Some(Chunk(
end: 13,
),
name: Atom("test"),
reference: None,
)),
params: FunctionParameters(
span: Span(
Expand All @@ -51,6 +52,7 @@ Some(Chunk(
end: 15,
),
name: Atom("a"),
reference: None,
),
mutable: false,
)),
Expand All @@ -75,6 +77,7 @@ Some(Chunk(
end: 18,
),
name: Atom("b"),
reference: None,
),
mutable: false,
)),
Expand All @@ -99,6 +102,7 @@ Some(Chunk(
end: 21,
),
name: Atom("c"),
reference: None,
),
mutable: false,
)),
Expand All @@ -119,6 +123,7 @@ Some(Chunk(
end: 26,
),
name: Atom("b"),
reference: None,
))),
],
)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Some(Chunk(
end: 7,
),
name: Atom("test"),
reference: None,
)),
params: FunctionParameters(
span: Span(
Expand All @@ -51,6 +52,7 @@ Some(Chunk(
end: 9,
),
name: Atom("a"),
reference: None,
),
mutable: false,
)),
Expand All @@ -69,6 +71,7 @@ Some(Chunk(
end: 15,
),
name: Atom("a"),
reference: None,
))),
)),
],
Expand Down
Loading

0 comments on commit a3f05a1

Please sign in to comment.