split binop into separate struct

This commit is contained in:
connorskees 2023-01-07 07:03:35 +00:00
parent 190201bcce
commit 02d36f872a
3 changed files with 47 additions and 40 deletions

View File

@ -40,15 +40,18 @@ pub(crate) struct InterpolatedFunction {
#[derive(Debug, Clone, Default)]
pub(crate) struct AstSassMap(pub Vec<(Spanned<AstExpr>, 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<Self>,
op: BinaryOp,
rhs: Arc<Self>,
allows_slash: bool,
span: Span,
},
BinaryOp(Arc<BinaryOpExpr>),
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<Self> {

View File

@ -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 { .. }

View File

@ -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),