Abstract infix macro

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-15 13:48:51 +01:00
parent a590839b21
commit 4f770c1f24
7 changed files with 42 additions and 35 deletions

View file

@ -424,8 +424,9 @@ fn emit_ast_expr(
| TemplateAstExpr::Invalid { .. }
| TemplateAstExpr::Literal { .. }
| TemplateAstExpr::FunctionCall { .. }
| TemplateAstExpr::Operation { .. }
| TemplateAstExpr::MathOperation { .. }
| TemplateAstExpr::ConditionalAccess { .. }
| TemplateAstExpr::AccessOperation { .. }
| TemplateAstExpr::VariableAccess { .. } => eval.push(Instruction::Abort),
}
}
@ -458,7 +459,7 @@ fn emit_expr_load(
slot: emit_slot,
});
}
TemplateAstExpr::Operation { op, lhs, rhs } => {
TemplateAstExpr::MathOperation { op, lhs, rhs } => {
let left_slot = machine.reserve_slot();
emit_expr_load(machine, eval, left_slot, lhs);
let right_slot = machine.reserve_slot();
@ -491,6 +492,7 @@ fn emit_expr_load(
}
TemplateAstExpr::ConditionalChain { .. } => todo!(),
TemplateAstExpr::ElseConditional { .. } => todo!(),
TemplateAstExpr::AccessOperation { .. } => todo!(),
TemplateAstExpr::EndBlock => todo!(),
TemplateAstExpr::Block { .. } => todo!(),
TemplateAstExpr::ForChain { .. } => todo!(),

View file

@ -262,7 +262,12 @@ pub enum TemplateAstExpr<'input> {
expression: Option<Box<TemplateAstExpr<'input>>>,
},
Invalid(&'input [TemplateToken]),
Operation {
MathOperation {
op: TokenOperator,
lhs: Box<TemplateAstExpr<'input>>,
rhs: Box<TemplateAstExpr<'input>>,
},
AccessOperation {
op: TokenOperator,
lhs: Box<TemplateAstExpr<'input>>,
rhs: Box<TemplateAstExpr<'input>>,
@ -681,10 +686,10 @@ fn parse_expression<'input>(
input: &mut Input<'input>,
) -> Result<TemplateAstExpr<'input>, AstError> {
macro_rules! infix {
($parser:expr => [ $($side:tt $val:tt => $prec:expr),* $(,)? ]) => {
($parser:expr => [ $($side:tt $op:tt $val:tt => $prec:expr),* $(,)? ]) => {
dispatch! { surrounded(ws, parse_operator);
$(
TokenOperator::$val => $side($prec, |_, lhs, rhs| Ok(TemplateAstExpr::Operation {
TokenOperator::$val => $side($prec, |_, lhs, rhs| Ok(TemplateAstExpr::$op {
op: TokenOperator::$val,
lhs: Box::new(lhs),
rhs: Box::new(rhs)
@ -698,19 +703,19 @@ fn parse_expression<'input>(
"expression",
expression(surrounded(ws, parse_operand)).infix(infix! {
surrounded(ws, parse_operator) => [
Left Plus => 18,
Left Minus => 18,
Left Times => 20,
Left Divide => 20,
Left And => 10,
Left Or => 7,
Left Equal => 12,
Left NotEqual => 12,
Left Greater => 15,
Left GreaterOrEqual => 15,
Left Lesser => 15,
Left LesserOrEqual => 15,
Left Dot => 23,
Left MathOperation Plus => 18,
Left MathOperation Minus => 18,
Left MathOperation Times => 20,
Left MathOperation Divide => 20,
Left MathOperation And => 10,
Left MathOperation Or => 7,
Left MathOperation Equal => 12,
Left MathOperation NotEqual => 12,
Left MathOperation Greater => 15,
Left MathOperation GreaterOrEqual => 15,
Left MathOperation Lesser => 15,
Left MathOperation LesserOrEqual => 15,
Left AccessOperation Dot => 23,
]
}).postfix(dispatch! { surrounded(ws, parse_operator);
TokenOperator::QuestionMark => Postfix(22, |input, rhs| {

View file

@ -6,7 +6,7 @@ TemplateAst {
root: [
Interpolation {
prev_whitespace_content: None,
expression: Operation {
expression: AccessOperation {
op: Dot,
lhs: ConditionalAccess(
[Ident]"foo" (4..7),

View file

@ -9,7 +9,7 @@ TemplateAst {
expression: FunctionCall {
name: [Ident]"foo" (4..7),
args: [
Operation {
MathOperation {
op: Times,
lhs: Literal {
source: [Literal(Integer(2))]"2" (8..9),
@ -27,7 +27,7 @@ TemplateAst {
FunctionCall {
name: [Ident]"bar" (15..18),
args: [
Operation {
MathOperation {
op: Plus,
lhs: Literal {
source: [Literal(Integer(2))]"2" (19..20),

View file

@ -6,9 +6,9 @@ TemplateAst {
root: [
Interpolation {
prev_whitespace_content: None,
expression: Operation {
expression: MathOperation {
op: Or,
lhs: Operation {
lhs: MathOperation {
op: And,
lhs: Literal {
source: [Literal(Bool(true))]"true" (4..8),
@ -23,9 +23,9 @@ TemplateAst {
},
},
},
rhs: Operation {
rhs: MathOperation {
op: And,
lhs: Operation {
lhs: MathOperation {
op: GreaterOrEqual,
lhs: Literal {
source: [Literal(Integer(3))]"3" (21..22),
@ -40,7 +40,7 @@ TemplateAst {
},
},
},
rhs: Operation {
rhs: MathOperation {
op: Equal,
lhs: Literal {
source: [Literal(Integer(5))]"5" (31..32),

View file

@ -6,9 +6,9 @@ TemplateAst {
root: [
Interpolation {
prev_whitespace_content: None,
expression: Operation {
expression: MathOperation {
op: Plus,
lhs: Operation {
lhs: MathOperation {
op: Times,
lhs: Literal {
source: [Literal(Integer(5))]"5" (4..5),
@ -23,7 +23,7 @@ TemplateAst {
},
},
},
rhs: Operation {
rhs: MathOperation {
op: Divide,
lhs: Literal {
source: [Literal(Integer(2))]"2" (12..13),

View file

@ -9,7 +9,7 @@ TemplateAst {
root: [
Interpolation {
prev_whitespace_content: None,
expression: Operation {
expression: MathOperation {
op: Times,
lhs: Literal {
source: [Literal(Integer(5))]"5" (4..5),
@ -30,9 +30,9 @@ TemplateAst {
},
Interpolation {
prev_whitespace_content: None,
expression: Operation {
expression: MathOperation {
op: Plus,
lhs: Operation {
lhs: MathOperation {
op: Times,
lhs: Literal {
source: [Literal(Integer(2))]"2" (17..18),
@ -47,7 +47,7 @@ TemplateAst {
},
},
},
rhs: Operation {
rhs: MathOperation {
op: Times,
lhs: Literal {
source: [Literal(Integer(4))]"4" (25..26),
@ -69,9 +69,9 @@ TemplateAst {
},
Interpolation {
prev_whitespace_content: None,
expression: Operation {
expression: MathOperation {
op: Plus,
lhs: Operation {
lhs: MathOperation {
op: Divide,
lhs: Literal {
source: [Literal(Integer(3))]"3" (38..39),