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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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