Skip to content

Commit

Permalink
Add head lowering for ExpressionOperatorChain
Browse files Browse the repository at this point in the history
  • Loading branch information
purefunctor committed Feb 1, 2025
1 parent aa1fe11 commit 404a062
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 30 deletions.
41 changes: 19 additions & 22 deletions crates/lowering/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,28 +157,25 @@ fn lower_expression(state: &mut State, cst: &cst::Expression) -> ExpressionId {
ExpressionKind::Typed { expression, signature }
}
cst::Expression::ExpressionOperatorChain(o) => {
let pairs: Vec<_> = o
.children()
.map(|p| {
let qualified = p.qualified();

let qualifier = qualified.as_ref().and_then(|q| {
let q = q.qualifier()?;
let t = q.text()?;
Some(SmolStr::from(t.text()))
});

let operator = qualified.as_ref().and_then(|q| {
let o = q.operator()?;
Some(SmolStr::from(o.text()))
});

let element = p.expression().map(|e| lower_expression(state, &e));

OperatorPair { qualifier, operator, element }
})
.collect();
ExpressionKind::OperatorChain { pairs }
let lower_pair = |state: &mut State, p: &cst::ExpressionOperatorPair| {
let qualified = p.qualified();
let qualifier = qualified.as_ref().and_then(|q| {
let q = q.qualifier()?;
let t = q.text()?;
Some(SmolStr::from(t.text()))
});
let operator = qualified.as_ref().and_then(|q| {
let o = q.operator()?;
Some(SmolStr::from(o.text()))
});
let element = p.expression().map(|e| lower_expression(state, &e));
OperatorPair { qualifier, operator, element }
};

let head = o.expression().map(|e| lower_expression(state, &e));
let tail: Vec<_> = o.children().map(|p| lower_pair(state, &p)).collect();

ExpressionKind::OperatorChain { head, tail }
}
cst::Expression::ExpressionInfixChain(i) => {
let lower_pair = |state: &mut State, p: &cst::ExpressionInfixPair| {
Expand Down
2 changes: 1 addition & 1 deletion crates/lowering/src/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct TickPair<T> {
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum ExpressionKind {
Typed { expression: Option<ExpressionId>, signature: Option<TypeId> },
OperatorChain { pairs: Vec<OperatorPair<ExpressionId>> },
OperatorChain { head: Option<ExpressionId>, tail: Vec<OperatorPair<ExpressionId>> },
InfixChain { head: Option<ExpressionId>, tail: Vec<TickPair<ExpressionId>> },
Negate,
ApplicationChain,
Expand Down
2 changes: 0 additions & 2 deletions crates/lowering/tests/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ fn lower_declaration<'s>(source: impl AsRef<[&'s str]>) -> (IndexingResult, Lowe
let (node, _) = parsing::parse(&lexed, &tokens);
let module = cst::Module::cast(node).unwrap();

dbg!(&module);

let (index, _) = indexing::index(&module);
let lower = lowering::lower(&module, &index);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ SourceMap {
types: {},
binders: {},
expressions: {
AstPtr {
raw: SyntaxNodePtr {
kind: ExpressionInteger,
range: 25..27,
},
}: Integer,
AstPtr {
raw: SyntaxNodePtr {
kind: ExpressionInteger,
Expand Down Expand Up @@ -37,14 +43,17 @@ SourceMap {
range: 25..43,
},
}: OperatorChain {
pairs: [
head: Some(
Id<syntax::cst::Expression>(0),
),
tail: [
OperatorPair {
qualifier: None,
operator: Some(
"+",
),
element: Some(
Id<syntax::cst::Expression>(0),
Id<syntax::cst::Expression>(1),
),
},
OperatorPair {
Expand All @@ -53,7 +62,7 @@ SourceMap {
"+",
),
element: Some(
Id<syntax::cst::Expression>(1),
Id<syntax::cst::Expression>(2),
),
},
OperatorPair {
Expand All @@ -62,7 +71,7 @@ SourceMap {
"+",
),
element: Some(
Id<syntax::cst::Expression>(2),
Id<syntax::cst::Expression>(3),
),
},
OperatorPair {
Expand All @@ -71,7 +80,7 @@ SourceMap {
"+",
),
element: Some(
Id<syntax::cst::Expression>(3),
Id<syntax::cst::Expression>(4),
),
},
],
Expand Down
5 changes: 5 additions & 0 deletions crates/syntax/src/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@ has_child!(
| signature() -> Type
);

has_child!(
ExpressionOperatorChain
| expression() -> Expression
);

has_children!(
ExpressionOperatorChain
| children() -> ExpressionOperatorPair
Expand Down

0 comments on commit 404a062

Please sign in to comment.