From 02d36f872aeb637057ca4a8f62905a68f652c356 Mon Sep 17 00:00:00 2001 From: connorskees Date: Sat, 7 Jan 2023 07:03:35 +0000 Subject: [PATCH] split binop into separate struct --- grass_internal/src/ast/expr.rs | 27 ++++++++++--------- grass_internal/src/evaluate/visitor.rs | 36 ++++++++++++++------------ grass_internal/src/parse/value.rs | 24 ++++++++--------- 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/grass_internal/src/ast/expr.rs b/grass_internal/src/ast/expr.rs index 725fc96..df95ad2 100644 --- a/grass_internal/src/ast/expr.rs +++ b/grass_internal/src/ast/expr.rs @@ -40,15 +40,18 @@ pub(crate) struct InterpolatedFunction { #[derive(Debug, Clone, Default)] pub(crate) struct AstSassMap(pub Vec<(Spanned, AstExpr)>); +#[derive(Debug, Clone)] +pub(crate) struct BinaryOpExpr { + pub lhs: AstExpr, + pub op: BinaryOp, + pub rhs: AstExpr, + pub allows_slash: bool, + pub span: Span, +} + #[derive(Debug, Clone)] pub(crate) enum AstExpr { - BinaryOp { - lhs: Arc, - op: BinaryOp, - rhs: Arc, - allows_slash: bool, - span: Span, - }, + BinaryOp(Arc), True, False, Calculation { @@ -167,19 +170,19 @@ impl AstExpr { pub fn is_slash_operand(&self) -> bool { match self { Self::Number { .. } | Self::Calculation { .. } => true, - Self::BinaryOp { allows_slash, .. } => *allows_slash, + Self::BinaryOp(binop) => binop.allows_slash, _ => false, } } pub fn slash(left: Self, right: Self, span: Span) -> Self { - Self::BinaryOp { - lhs: Arc::new(left), + Self::BinaryOp(Arc::new(BinaryOpExpr { + lhs: left, op: BinaryOp::Div, - rhs: Arc::new(right), + rhs: right, allows_slash: true, span, - } + })) } pub const fn span(self, span: Span) -> Spanned { diff --git a/grass_internal/src/evaluate/visitor.rs b/grass_internal/src/evaluate/visitor.rs index 0b37b0c..34e8cd0 100644 --- a/grass_internal/src/evaluate/visitor.rs +++ b/grass_internal/src/evaluate/visitor.rs @@ -2421,13 +2421,15 @@ impl<'a> Visitor<'a> { }), AstExpr::List(list) => self.visit_list_expr(list)?, AstExpr::String(StringExpr(text, quote), ..) => self.visit_string(text, quote)?, - AstExpr::BinaryOp { - lhs, - op, - rhs, - allows_slash, - span, - } => self.visit_bin_op((*lhs).clone(), op, (*rhs).clone(), allows_slash, span)?, + AstExpr::BinaryOp(binop) => { + self.visit_bin_op( + binop.lhs.clone(), + binop.op, + binop.rhs.clone(), + binop.allows_slash, + binop.span, + )? + } AstExpr::True => Value::True, AstExpr::False => Value::False, AstExpr::Calculation { name, args } => { @@ -2475,15 +2477,17 @@ impl<'a> Visitor<'a> { debug_assert!(string_expr.1 == QuoteKind::None); CalculationArg::Interpolation(self.perform_interpolation(string_expr.0, false)?) } - AstExpr::BinaryOp { lhs, op, rhs, .. } => SassCalculation::operate_internal( - op, - self.visit_calculation_value((*lhs).clone(), in_min_or_max, span)?, - self.visit_calculation_value((*rhs).clone(), in_min_or_max, span)?, - in_min_or_max, - !self.flags.in_supports_declaration(), - self.options, - span, - )?, + AstExpr::BinaryOp(binop) => { + SassCalculation::operate_internal( + binop.op, + self.visit_calculation_value(binop.lhs.clone(), in_min_or_max, span)?, + self.visit_calculation_value(binop.rhs.clone(), in_min_or_max, span)?, + in_min_or_max, + !self.flags.in_supports_declaration(), + self.options, + span, + )? + } AstExpr::Number { .. } | AstExpr::Calculation { .. } | AstExpr::Variable { .. } diff --git a/grass_internal/src/parse/value.rs b/grass_internal/src/parse/value.rs index 0a230f0..866369a 100644 --- a/grass_internal/src/parse/value.rs +++ b/grass_internal/src/parse/value.rs @@ -538,13 +538,13 @@ impl<'a, 'c, P: StylesheetParser<'a>> ValueParser<'a, 'c, P> { self.single_expression = Some(AstExpr::slash(left.node, right.node, span).span(span)); } else { self.single_expression = Some( - AstExpr::BinaryOp { - lhs: Arc::new(left.node), + AstExpr::BinaryOp(Arc::new(BinaryOpExpr { + lhs: left.node, op: operator, - rhs: Arc::new(right.node), + rhs: right.node, allows_slash: false, span, - } + })) .span(span), ); self.allow_slash = false; @@ -1600,17 +1600,17 @@ impl<'a, 'c, P: StylesheetParser<'a>> ValueParser<'a, 'c, P> { let span = product.span.merge(rhs.span); - product.node = AstExpr::BinaryOp { - lhs: Arc::new(product.node), + product.node = AstExpr::BinaryOp(Arc::new(BinaryOpExpr { + lhs: product.node, op: if op == '*' { BinaryOp::Mul } else { BinaryOp::Div }, - rhs: Arc::new(rhs.node), + rhs: rhs.node, allows_slash: false, span, - }; + })); product.span = span; } @@ -1654,17 +1654,17 @@ impl<'a, 'c, P: StylesheetParser<'a>> ValueParser<'a, 'c, P> { let span = sum.span.merge(rhs.span); - sum = AstExpr::BinaryOp { - lhs: Arc::new(sum.node), + sum = AstExpr::BinaryOp(Arc::new(BinaryOpExpr { + lhs: sum.node, op: if next == '+' { BinaryOp::Plus } else { BinaryOp::Minus }, - rhs: Arc::new(rhs.node), + rhs: rhs.node, allows_slash: false, span, - } + })) .span(span); } _ => return Ok(sum),