tamer: Lower sum expressions

This was a fairly simple addition, since rate blocks already lower into sum
expressions; these are just non-identified.

This does emphasize that the nir::parse `ele_parse!` abstraction I spent so
much time on ended up not being a perfect fit, as it now has some
boilerplate after it was stripped of much of its capabilities some time ago.

Don't worry, `nir::air` and `asg::graph::xmli` will get cleaned up.

DEV-13708
main
Mike Gerwitz 2023-02-22 23:45:23 -05:00
parent ee9128fbe0
commit 9990be58a7
7 changed files with 64 additions and 9 deletions

View File

@ -127,10 +127,9 @@ impl<'a> ParseState for AsgTreeToXirf<'a> {
toks.push(yields(ident.name(), expr.span()));
Transition(Self::Ready(Default::default()))
.ok(stmt(expr, depth))
Transition(self).ok(stmt(expr, depth))
}
_ => todo!("non-ident expr"),
_ => Transition(self).ok(expr_ele(expr, depth)),
},
Object::Root(_) => diagnostic_unreachable!(
@ -187,6 +186,15 @@ fn yields(name: SPair, span: Span) -> Xirf {
Xirf::attr(QN_YIELDS, name, (span, name))
}
fn expr_ele(expr: &Expr, depth: Depth) -> Xirf {
let qname = match expr.op() {
ExprOp::Sum => QN_C_SUM,
op => todo!("expr_ele qname: {op:?}"),
};
Xirf::open(qname, OpenSpan::without_name_span(expr.span()), depth)
}
pub struct TreeContext<'a>(TokenStack, &'a Asg);
// Custom `Debug` impl to omit ASG rendering,

View File

@ -184,6 +184,9 @@ pub enum NirEntity {
/// representing a calculation with a scalar result.
Rate,
/// Summation (Σ) expression.
Sum,
/// Template parameter (metavariable).
TplParam,
}
@ -205,6 +208,7 @@ impl Display for NirEntity {
match self {
Package => write!(f, "package"),
Rate => write!(f, "rate block"),
Sum => write!(f, "sum expression"),
TplParam => write!(f, "template param (metavariable)"),
}
}

View File

@ -74,11 +74,11 @@ impl ParseState for NirToAir {
Transition(Ready).ok(Air::PkgClose(span))
}
(Ready, Nir::Open(NirEntity::Rate, span)) => {
(Ready, Nir::Open(NirEntity::Rate | NirEntity::Sum, span)) => {
Transition(Ready).ok(Air::ExprOpen(ExprOp::Sum, span))
}
(Ready, Nir::Close(NirEntity::Rate, span)) => {
(Ready, Nir::Close(NirEntity::Rate | NirEntity::Sum, span)) => {
Transition(Ready).ok(Air::ExprClose(span))
}

View File

@ -56,3 +56,19 @@ fn rate_to_sum_expr() {
Sut::parse(toks.into_iter()).collect(),
);
}
#[test]
fn calc_expr() {
let toks = vec![
Nir::Open(NirEntity::Sum, S1),
Nir::Close(NirEntity::Sum, S2),
];
assert_eq!(
Ok(vec![
O(Air::ExprOpen(ExprOp::Sum, S1)),
O(Air::ExprClose(S2)),
]),
Sut::parse(toks.into_iter()).collect(),
);
}

View File

@ -664,7 +664,7 @@ ele_parse! {
/// identified by [`@generates`](QN_GENERATES).
///
/// Summation is generated automatically by [`RateEachStmt`].
SumExpr := QN_C_SUM {
SumExpr := QN_C_SUM(_, ospan) {
@ {
QN_OF => TodoAttr,
QN_GENERATES => TodoAttr,
@ -673,7 +673,8 @@ ele_parse! {
QN_LABEL => TodoAttr,
QN_SYM => TodoAttr,
QN_DIM => TodoAttr,
} => Todo,
} => NirEntity::Sum.open(ospan),
/(cspan) => NirEntity::Sum.close(cspan),
WhenExpr,
CalcExpr,

View File

@ -3,6 +3,19 @@
xmlns:t="http://www.lovullo.com/rater/apply-template">
<rate yields="rateFoo" />
<rate yields="rateBar" />
<rate yields="rateBar">
<c:sum />
</rate>
<rate yields="rateBaz">
<c:sum>
<c:sum />
</c:sum>
<c:sum>
<c:sum />
<c:sum />
</c:sum>
</rate>
</package>

View File

@ -7,6 +7,19 @@
The output `out.xmli` is asserted against `expected.xml`.
<rate yields="rateFoo" />
<rate yields="rateBar" />
<rate yields="rateBar">
<c:sum />
</rate>
<rate yields="rateBaz">
<c:sum>
<c:sum />
</c:sum>
<c:sum>
<c:sum />
<c:sum />
</c:sum>
</rate>
</package>