From 4f770c1f24daefe3f4eafb3784248650e89463ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Sun, 15 Mar 2026 13:48:51 +0100 Subject: [PATCH] Abstract infix macro MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcel Müller --- src/compiler/mod.rs | 6 ++- src/parser/mod.rs | 37 +++++++++++-------- ..._parser__tests__check_access_operator.snap | 2 +- ...o__parser__tests__check_function_call.snap | 4 +- ...rser__tests__check_logical_expression.snap | 10 ++--- ..._parser__tests__check_math_expression.snap | 6 +-- tests/cases/maths.2-ast.snap | 12 +++--- 7 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index 98e350e..00fe68e 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -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!(), diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 94fba56..d7e1271 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -262,7 +262,12 @@ pub enum TemplateAstExpr<'input> { expression: Option>>, }, Invalid(&'input [TemplateToken]), - Operation { + MathOperation { + op: TokenOperator, + lhs: Box>, + rhs: Box>, + }, + AccessOperation { op: TokenOperator, lhs: Box>, rhs: Box>, @@ -681,10 +686,10 @@ fn parse_expression<'input>( input: &mut Input<'input>, ) -> Result, 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| { diff --git a/src/parser/snapshots/nomo__parser__tests__check_access_operator.snap b/src/parser/snapshots/nomo__parser__tests__check_access_operator.snap index 3fcf602..fd84622 100644 --- a/src/parser/snapshots/nomo__parser__tests__check_access_operator.snap +++ b/src/parser/snapshots/nomo__parser__tests__check_access_operator.snap @@ -6,7 +6,7 @@ TemplateAst { root: [ Interpolation { prev_whitespace_content: None, - expression: Operation { + expression: AccessOperation { op: Dot, lhs: ConditionalAccess( [Ident]"foo" (4..7), diff --git a/src/parser/snapshots/nomo__parser__tests__check_function_call.snap b/src/parser/snapshots/nomo__parser__tests__check_function_call.snap index 40e920c..b85eeee 100644 --- a/src/parser/snapshots/nomo__parser__tests__check_function_call.snap +++ b/src/parser/snapshots/nomo__parser__tests__check_function_call.snap @@ -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), diff --git a/src/parser/snapshots/nomo__parser__tests__check_logical_expression.snap b/src/parser/snapshots/nomo__parser__tests__check_logical_expression.snap index aca365a..19b5f84 100644 --- a/src/parser/snapshots/nomo__parser__tests__check_logical_expression.snap +++ b/src/parser/snapshots/nomo__parser__tests__check_logical_expression.snap @@ -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), diff --git a/src/parser/snapshots/nomo__parser__tests__check_math_expression.snap b/src/parser/snapshots/nomo__parser__tests__check_math_expression.snap index 7ba6415..5b5a0e3 100644 --- a/src/parser/snapshots/nomo__parser__tests__check_math_expression.snap +++ b/src/parser/snapshots/nomo__parser__tests__check_math_expression.snap @@ -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), diff --git a/tests/cases/maths.2-ast.snap b/tests/cases/maths.2-ast.snap index 45be132..4639215 100644 --- a/tests/cases/maths.2-ast.snap +++ b/tests/cases/maths.2-ast.snap @@ -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),