From 869708b45485f8945786da1c832002ab32fb5f64 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 20 May 2021 11:03:53 +0200 Subject: [PATCH] Removed code duplication in kalk_num --- kalk/src/kalk_num/mod.rs | 125 +++++++++++++++++++++++++++++++-- kalk/src/kalk_num/regular.rs | 126 ++++------------------------------ kalk/src/kalk_num/with_rug.rs | 119 +------------------------------- kalk/src/test_helpers.rs | 0 kalk_mobile/android/gradlew | 0 kalk_web/package.json | 2 +- 6 files changed, 137 insertions(+), 235 deletions(-) mode change 100644 => 100755 kalk/src/test_helpers.rs mode change 100644 => 100755 kalk_mobile/android/gradlew diff --git a/kalk/src/kalk_num/mod.rs b/kalk/src/kalk_num/mod.rs index 4521c64..f134cf5 100644 --- a/kalk/src/kalk_num/mod.rs +++ b/kalk/src/kalk_num/mod.rs @@ -1,6 +1,10 @@ #[cfg(feature = "rug")] pub mod with_rug; #[cfg(feature = "rug")] +use rug::ops::Pow; +#[cfg(feature = "rug")] +use rug::Float; +#[cfg(feature = "rug")] pub use with_rug::*; #[cfg(not(feature = "rug"))] @@ -8,6 +12,7 @@ pub mod regular; #[cfg(not(feature = "rug"))] pub use regular::*; +use crate::ast::Expr; use lazy_static::lazy_static; use std::collections::HashMap; use wasm_bindgen::prelude::*; @@ -44,11 +49,29 @@ lazy_static! { } #[wasm_bindgen] -impl KalkNum { - #[cfg(not(feature = "rug"))] - #[wasm_bindgen(js_name = estimate)] - pub fn estimate_js(&self) -> Option { - self.estimate() +#[derive(Clone)] +pub struct ScientificNotation { + pub negative: bool, + pub(crate) digits: String, + pub exponent: i32, +} + +#[wasm_bindgen] +impl ScientificNotation { + #[wasm_bindgen(js_name = toString)] + pub fn to_string(&self) -> String { + let sign = if self.negative { "-" } else { "" }; + let mut digits_and_mul = if self.digits == "1" { + String::new() + } else { + format!("{}*", &self.digits) + }; + + if self.digits.len() > 1 { + digits_and_mul.insert(1usize, '.'); + } + + format!("{}{}10^{}", sign, digits_and_mul, self.exponent - 1) } } @@ -73,7 +96,67 @@ impl KalkNum { } } - // Get an estimate of what the number is, eg. 3.141592 => π + pub fn to_string_big(&self) -> String { + self.value.to_string() + } + + pub fn is_too_big(&self) -> bool { + self.value.is_infinite() + } + + pub fn to_string_with_unit(&self) -> String { + format!("{} {}", self.to_string(), self.unit) + } + + pub fn has_unit(&self) -> bool { + self.unit.len() > 0 + } + + pub(crate) fn convert_to_unit( + &self, + context: &mut crate::interpreter::Context, + to_unit: &str, + ) -> Option { + let result = crate::interpreter::convert_unit( + context, + &Expr::Literal(self.to_f64()), + &self.unit, + to_unit, + ); + + if let Ok(num) = result { + Some(num) + } else { + None + } + } + + pub(crate) fn add(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { + let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); + KalkNum::new(self.value + right.value, &right.unit) + } + + pub(crate) fn sub(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { + let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); + KalkNum::new(self.value - right.value, &right.unit) + } + + pub(crate) fn mul(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { + let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); + KalkNum::new(self.value * right.value, &right.unit) + } + + pub(crate) fn div(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { + let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); + KalkNum::new(self.value / right.value, &right.unit) + } + + pub(crate) fn rem(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { + let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); + KalkNum::new(self.value % right.value, &right.unit) + } + + /// Get an estimate of what the number is, eg. 3.141592 => π pub fn estimate(&self) -> Option { let fract = self.value.clone().fract().abs(); let integer = self.value.clone().trunc(); @@ -167,6 +250,18 @@ impl KalkNum { } } +fn calculate_unit( + context: &mut crate::interpreter::Context, + left: &KalkNum, + right: KalkNum, +) -> Option { + if left.has_unit() && right.has_unit() { + right.convert_to_unit(context, &left.unit) + } else { + Some(KalkNum::new(right.value, &left.unit)) + } +} + fn trim_zeroes(input: &str) -> String { if input.contains(".") { input @@ -178,6 +273,24 @@ fn trim_zeroes(input: &str) -> String { } } +impl Into for ScientificNotation { + fn into(self) -> String { + self.to_string() + } +} + +impl Into for KalkNum { + fn into(self) -> String { + self.to_string() + } +} + +impl Into for KalkNum { + fn into(self) -> f64 { + self.to_f64() + } +} + #[cfg(test)] mod tests { use crate::kalk_num::KalkNum; diff --git a/kalk/src/kalk_num/regular.rs b/kalk/src/kalk_num/regular.rs index 46ce394..8660f3c 100644 --- a/kalk/src/kalk_num/regular.rs +++ b/kalk/src/kalk_num/regular.rs @@ -1,4 +1,4 @@ -use crate::ast::Expr; +use crate::kalk_num::*; use wasm_bindgen::prelude::*; #[wasm_bindgen] @@ -8,33 +8,6 @@ pub struct KalkNum { pub(crate) unit: String, } -#[wasm_bindgen] -#[derive(Clone)] -pub struct ScientificNotation { - pub negative: bool, - pub(crate) digits: String, - pub exponent: i32, -} - -#[wasm_bindgen] -impl ScientificNotation { - #[wasm_bindgen(js_name = toString)] - pub fn to_string(&self) -> String { - let sign = if self.negative { "-" } else { "" }; - let mut digits_and_mul = if self.digits == "1" { - String::new() - } else { - format!("{}*", &self.digits) - }; - - if self.digits.len() > 1 { - digits_and_mul.insert(1usize, '.'); - } - - format!("{}{}10^{}", sign, digits_and_mul, self.exponent - 1) - } -} - #[wasm_bindgen] impl KalkNum { pub fn new(value: f64, unit: &str) -> Self { @@ -67,18 +40,23 @@ impl KalkNum { } #[wasm_bindgen(js_name = toStringBig)] - pub fn to_string_big(&self) -> String { - self.value.to_string() + pub fn to_string_big_js(&self) -> String { + self.to_string_big() } #[wasm_bindgen(js_name = isTooBig)] - pub fn is_too_big(&self) -> bool { - self.value.is_infinite() + pub fn is_too_big_js(&self) -> bool { + self.is_too_big() } #[wasm_bindgen(js_name = toStringWithUnit)] - pub fn to_string_with_unit(&self) -> String { - format!("{} {}", self.to_string(), self.unit) + pub fn to_string_with_unit_js(&self) -> String { + self.to_string_with_unit() + } + + #[wasm_bindgen(js_name = hasUnit)] + pub fn has_unit_js(&self) -> bool { + self.has_unit() } #[wasm_bindgen(js_name = getUnit)] @@ -86,58 +64,14 @@ impl KalkNum { self.unit.clone() } - #[wasm_bindgen(js_name = hasUnit)] - pub fn has_unit(&self) -> bool { - self.unit.len() > 0 - } - #[wasm_bindgen(js_name = toScientificNotation)] pub fn to_scientific_notation_js(&self) -> ScientificNotation { self.to_scientific_notation() } - pub(crate) fn convert_to_unit( - &self, - context: &mut crate::interpreter::Context, - to_unit: &str, - ) -> Option { - let result = crate::interpreter::convert_unit( - context, - &Expr::Literal(self.value), - &self.unit, - to_unit, - ); - - if let Ok(num) = result { - Some(num) - } else { - None - } - } - - pub(crate) fn add(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { - let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); - KalkNum::new(self.value + right.value, &right.unit) - } - - pub(crate) fn sub(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { - let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); - KalkNum::new(self.value - right.value, &right.unit) - } - - pub(crate) fn mul(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { - let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); - KalkNum::new(self.value * right.value, &right.unit) - } - - pub(crate) fn div(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { - let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); - KalkNum::new(self.value / right.value, &right.unit) - } - - pub(crate) fn rem(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { - let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); - KalkNum::new(self.value % right.value, &right.unit) + #[wasm_bindgen(js_name = estimate)] + pub fn estimate_js(&self) -> Option { + self.estimate() } pub(crate) fn pow(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { @@ -146,36 +80,6 @@ impl KalkNum { } } -fn calculate_unit( - context: &mut crate::interpreter::Context, - left: &KalkNum, - right: KalkNum, -) -> Option { - if left.has_unit() && right.has_unit() { - right.convert_to_unit(context, &left.unit) - } else { - Some(KalkNum::new(right.value, &left.unit)) - } -} - -impl Into for ScientificNotation { - fn into(self) -> String { - self.to_string() - } -} - -impl Into for KalkNum { - fn into(self) -> String { - self.to_string() - } -} - -impl Into for KalkNum { - fn into(self) -> f64 { - self.value - } -} - impl From for KalkNum { fn from(x: f64) -> Self { KalkNum::new(x, "") diff --git a/kalk/src/kalk_num/with_rug.rs b/kalk/src/kalk_num/with_rug.rs index c9a6233..0ccf659 100644 --- a/kalk/src/kalk_num/with_rug.rs +++ b/kalk/src/kalk_num/with_rug.rs @@ -1,6 +1,4 @@ -use crate::ast::Expr; -use rug::ops::Pow; -use rug::Float; +use crate::kalk_num::*; impl Default for KalkNum { fn default() -> Self { @@ -14,29 +12,6 @@ pub struct KalkNum { pub(crate) unit: String, } -pub struct ScientificNotation { - pub negative: bool, - pub digits: String, - pub exponent: i32, -} - -impl ScientificNotation { - pub fn to_string(&self) -> String { - let sign = if self.negative { "-" } else { "" }; - let mut digits_and_mul = if self.digits == "1" { - String::new() - } else { - format!("{}*", &self.digits) - }; - - if self.digits.len() > 1 { - digits_and_mul.insert(1usize, '.'); - } - - format!("{}{}10^{}", sign, digits_and_mul, self.exponent - 1) - } -} - impl KalkNum { pub fn new(value: Float, unit: &str) -> Self { Self { @@ -66,106 +41,16 @@ impl KalkNum { } } - pub fn to_string_big(&self) -> String { - self.value.to_string() - } - - pub fn is_too_big(&self) -> bool { - self.value.is_infinite() - } - - pub fn to_string_with_unit(&self) -> String { - format!("{} {}", self.to_string(), self.unit) - } - pub fn get_unit(&self) -> &str { &self.unit } - pub fn has_unit(&self) -> bool { - self.unit.len() > 0 - } - - pub fn convert_to_unit( - &self, - context: &mut crate::interpreter::Context, - to_unit: &str, - ) -> Option { - let result = crate::interpreter::convert_unit( - context, - &Expr::Literal(self.value.to_f64()), - &self.unit, - to_unit, - ); - - if let Ok(num) = result { - Some(num) - } else { - None - } - } - - pub fn add(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { - let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); - KalkNum::new(self.value + right.value, &right.unit) - } - - pub fn sub(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { - let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); - KalkNum::new(self.value - right.value, &right.unit) - } - - pub fn mul(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { - let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); - KalkNum::new(self.value * right.value, &right.unit) - } - - pub fn div(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { - let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); - KalkNum::new(self.value / right.value, &right.unit) - } - - pub fn rem(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { - let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); - KalkNum::new(self.value % right.value, &right.unit) - } - - pub fn pow(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { + pub(crate) fn pow(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); KalkNum::new(self.value.pow(right.value), &right.unit) } } -fn calculate_unit( - context: &mut crate::interpreter::Context, - left: &KalkNum, - right: KalkNum, -) -> Option { - if left.has_unit() && right.has_unit() { - right.convert_to_unit(context, &left.unit) - } else { - Some(KalkNum::new(right.value, &left.unit)) - } -} - -impl Into for ScientificNotation { - fn into(self) -> String { - self.to_string() - } -} - -impl Into for KalkNum { - fn into(self) -> f64 { - self.to_f64() - } -} - -impl Into for KalkNum { - fn into(self) -> String { - self.to_string() - } -} - impl From for KalkNum { fn from(x: f64) -> Self { KalkNum::new(Float::with_val(63, x), "") diff --git a/kalk/src/test_helpers.rs b/kalk/src/test_helpers.rs old mode 100644 new mode 100755 diff --git a/kalk_mobile/android/gradlew b/kalk_mobile/android/gradlew old mode 100644 new mode 100755 diff --git a/kalk_web/package.json b/kalk_web/package.json index dfb7f35..b32ffdb 100644 --- a/kalk_web/package.json +++ b/kalk_web/package.json @@ -1,6 +1,6 @@ { "name": "@paddim8/kalk-component", - "version": "1.0.16", + "version": "1.0.17", "description": "A Svelte component for kalk, a calculator that supports user-defined functions and variables.", "svelte": "src/main.ts", "main": "public/build/bundle.js",