From 81d5dbbb7e9050742a8aa35ecfaea61ad9a624be Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Sun, 4 Jul 2021 16:19:38 -0400 Subject: [PATCH] implement math.div --- src/builtin/functions/math.rs | 17 +++++++++++++++++ src/builtin/modules/math.rs | 3 ++- tests/math-module.rs | 10 ++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/builtin/functions/math.rs b/src/builtin/functions/math.rs index 2c19c08..126d87a 100644 --- a/src/builtin/functions/math.rs +++ b/src/builtin/functions/math.rs @@ -7,6 +7,7 @@ use rand::Rng; use crate::{ args::CallArgs, + common::Op, error::SassResult, parse::{HigherIntermediateValue, Parser, ValueVisitor}, unit::Unit, @@ -270,6 +271,22 @@ pub(crate) fn max(args: CallArgs, parser: &mut Parser<'_>) -> SassResult Ok(Value::Dimension(Some(max.0), max.1, true)) } +pub(crate) fn divide(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { + args.max_args(2)?; + + let number1 = args.get_err(0, "number1")?; + let number2 = args.get_err(1, "number2")?; + + ValueVisitor::new(parser, args.span()).eval( + HigherIntermediateValue::BinaryOp( + Box::new(HigherIntermediateValue::Literal(number1)), + Op::Div, + Box::new(HigherIntermediateValue::Literal(number2)), + ), + true, + ) +} + pub(crate) fn declare(f: &mut GlobalFunctionMap) { f.insert("percentage", Builtin::new(percentage)); f.insert("round", Builtin::new(round)); diff --git a/src/builtin/modules/math.rs b/src/builtin/modules/math.rs index ecc553b..aa886e3 100644 --- a/src/builtin/modules/math.rs +++ b/src/builtin/modules/math.rs @@ -5,7 +5,7 @@ use num_traits::{One, Signed, Zero}; use crate::{ args::CallArgs, builtin::{ - math::{abs, ceil, comparable, floor, max, min, percentage, round}, + math::{abs, ceil, comparable, divide, floor, max, min, percentage, round}, meta::{unit, unitless}, modules::Module, }, @@ -607,6 +607,7 @@ pub(crate) fn declare(f: &mut Module) { f.insert_builtin("log", log); f.insert_builtin("pow", pow); f.insert_builtin("hypot", hypot); + f.insert_builtin("div", divide); f.insert_builtin("atan2", atan2); #[cfg(feature = "random")] f.insert_builtin("random", random); diff --git a/tests/math-module.rs b/tests/math-module.rs index 4076df6..6f3f27a 100644 --- a/tests/math-module.rs +++ b/tests/math-module.rs @@ -588,3 +588,13 @@ test!( "@use 'sass:math';\na {\n color: math.atan2(math.acos(2), 3deg);\n}\n", "a {\n color: NaNdeg;\n}\n" ); +test!( + div_two_integers, + "@use 'sass:math';\na {\n color: math.div(1, 2);\n}\n", + "a {\n color: 0.5;\n}\n" +); +test!( + div_two_strings, + "@use 'sass:math';\na {\n color: math.div(\"1\",\"2\");\n}\n", + "a {\n color: \"1\"/\"2\";\n}\n" +);