From 6471ce8f299c74bfde8a6dd430ee923853a2e2d3 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sat, 8 Feb 2020 16:07:37 -0500 Subject: [PATCH] Refactor value --- src/common.rs | 33 ++++++++ src/{value.rs => value/mod.rs} | 147 +-------------------------------- src/value/ops.rs | 116 ++++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 145 deletions(-) rename src/{value.rs => value/mod.rs} (75%) create mode 100644 src/value/ops.rs diff --git a/src/common.rs b/src/common.rs index 0a087f5..a9a86cb 100644 --- a/src/common.rs +++ b/src/common.rs @@ -391,3 +391,36 @@ impl QuoteKind { } } } + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum ListSeparator { + Space, + Comma, +} + +impl ListSeparator { + pub fn as_str(self) -> &'static str { + match self { + Self::Space => " ", + Self::Comma => ", ", + } + } + + #[allow(dead_code)] + pub fn name(self) -> &'static str { + match self { + Self::Space => "space", + Self::Comma => "comma", + } + } +} + +impl Display for ListSeparator { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Space => write!(f, " "), + Self::Comma => write!(f, ", "), + } + } +} diff --git a/src/value.rs b/src/value/mod.rs similarity index 75% rename from src/value.rs rename to src/value/mod.rs index f65a645..bc1db0c 100644 --- a/src/value.rs +++ b/src/value/mod.rs @@ -2,7 +2,6 @@ use std::convert::{TryFrom, TryInto}; use std::fmt::{self, Display}; use std::iter::{Iterator, Peekable}; -use std::ops::{Add, Sub}; use num_rational::BigRational; use num_bigint::BigInt; @@ -10,42 +9,12 @@ use num_bigint::BigInt; use crate::args::eat_call_args; use crate::builtin::GLOBAL_FUNCTIONS; use crate::color::Color; -use crate::common::{Keyword, Op, QuoteKind, Scope, Symbol}; +use crate::common::{Keyword, ListSeparator, Op, QuoteKind, Scope, Symbol}; use crate::units::Unit; use crate::utils::{devour_whitespace_or_comment, parse_interpolation}; use crate::{Token, TokenKind}; -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum ListSeparator { - Space, - Comma, -} - -impl ListSeparator { - pub fn as_str(self) -> &'static str { - match self { - Self::Space => " ", - Self::Comma => ", ", - } - } - - pub fn name(self) -> &'static str { - match self { - Self::Space => "space", - Self::Comma => "comma", - } - } -} - -impl Display for ListSeparator { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Space => write!(f, " "), - Self::Comma => write!(f, ", "), - } - } -} +mod ops; #[derive(Debug, Clone, Eq, PartialEq)] pub(crate) enum Value { @@ -61,118 +30,6 @@ pub(crate) enum Value { Ident(String, QuoteKind), } -impl Add for Value { - type Output = Self; - - fn add(self, other: Self) -> Self { - match self { - Self::Important | Self::True | Self::False => match other { - Self::Ident(s, QuoteKind::Double) | Self::Ident(s, QuoteKind::Single) => { - Value::Ident(format!("{}{}", self, s), QuoteKind::Double) - } - Self::Null => Value::Ident(self.to_string(), QuoteKind::None), - _ => Value::Ident(format!("{}{}", self, other), QuoteKind::None), - }, - Self::Null => match other { - Self::Null => Self::Null, - _ => Value::Ident(format!("{}", other), QuoteKind::None), - }, - Self::Dimension(num, unit) => match other { - Self::Dimension(num2, unit2) => Value::Dimension(num + num2, unit), - _ => todo!(), - }, - // Self::List(..) => todo!(), - // Self::Color(..) => todo!(), - // Self::BinaryOp(..) => todo!(), - // Self::Paren(..) => todo!(), - Self::Ident(s1, quotes1) => match other { - Self::Ident(s2, quotes2) => { - let quotes = match (quotes1, quotes2) { - (QuoteKind::Double, _) - | (QuoteKind::Single, _) - | (_, QuoteKind::Double) - | (_, QuoteKind::Single) => QuoteKind::Double, - _ => QuoteKind::None, - }; - Value::Ident(format!("{}{}", s1, s2), quotes) - } - Self::Important | Self::True | Self::False | Self::Dimension(..) => { - let quotes = match quotes1 { - QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, - QuoteKind::None => QuoteKind::None, - }; - Value::Ident(format!("{}{}", s1, other), quotes) - } - Self::Null => { - let quotes = match quotes1 { - QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, - QuoteKind::None => QuoteKind::None, - }; - Value::Ident(s1, quotes) - } - _ => todo!(), - }, - _ => todo!(), - } - } -} - -impl Sub for Value { - type Output = Self; - - fn sub(self, other: Self) -> Self { - match self { - // Self::Important => todo!(), - // Self::True => todo!(), - // Self::False => todo!(), - // Self::Null => todo!(), - Self::Dimension(num, unit) => match other { - // Self::Dimension(num2, unit2) => Value::Dimension(num - num2, unit), - _ => todo!(), - }, - // Self::List(..) => todo!(), - // Self::Color(..) => todo!(), - // Self::BinaryOp(..) => todo!(), - // Self::Paren(..) => todo!(), - Self::Ident(s1, quotes1) => match other { - Self::Ident(s2, quotes2) => { - let quotes1 = match quotes1 { - QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, - QuoteKind::None => QuoteKind::None, - }; - let quotes2 = match quotes2 { - QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, - QuoteKind::None => QuoteKind::None, - }; - Value::Ident( - format!("{}{}{}-{}{}{}", quotes1, s1, quotes1, quotes2, s2, quotes2), - QuoteKind::None, - ) - } - Self::Important | Self::True | Self::False | Self::Dimension(..) => { - let quotes = match quotes1 { - QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, - QuoteKind::None => QuoteKind::None, - }; - Value::Ident( - format!("{}{}{}-{}", quotes, s1, quotes, other), - QuoteKind::None, - ) - } - Self::Null => { - let quotes = match quotes1 { - QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, - QuoteKind::None => QuoteKind::None, - }; - Value::Ident(format!("{}{}{}-", quotes, s1, quotes), QuoteKind::None) - } - _ => todo!(), - }, - _ => todo!(), - } - } -} - impl Display for Value { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { diff --git a/src/value/ops.rs b/src/value/ops.rs new file mode 100644 index 0000000..9b0fd06 --- /dev/null +++ b/src/value/ops.rs @@ -0,0 +1,116 @@ +use std::ops::{Add, Sub}; + +use crate::common::QuoteKind; +use crate::value::Value; + +impl Add for Value { + type Output = Self; + + fn add(self, other: Self) -> Self { + match self { + Self::Important | Self::True | Self::False => match other { + Self::Ident(s, QuoteKind::Double) | Self::Ident(s, QuoteKind::Single) => { + Value::Ident(format!("{}{}", self, s), QuoteKind::Double) + } + Self::Null => Value::Ident(self.to_string(), QuoteKind::None), + _ => Value::Ident(format!("{}{}", self, other), QuoteKind::None), + }, + Self::Null => match other { + Self::Null => Self::Null, + _ => Value::Ident(format!("{}", other), QuoteKind::None), + }, + Self::Dimension(num, unit) => match other { + Self::Dimension(num2, unit2) => Value::Dimension(num + num2, unit), + _ => todo!(), + }, + // Self::List(..) => todo!(), + // Self::Color(..) => todo!(), + // Self::BinaryOp(..) => todo!(), + // Self::Paren(..) => todo!(), + Self::Ident(s1, quotes1) => match other { + Self::Ident(s2, quotes2) => { + let quotes = match (quotes1, quotes2) { + (QuoteKind::Double, _) + | (QuoteKind::Single, _) + | (_, QuoteKind::Double) + | (_, QuoteKind::Single) => QuoteKind::Double, + _ => QuoteKind::None, + }; + Value::Ident(format!("{}{}", s1, s2), quotes) + } + Self::Important | Self::True | Self::False | Self::Dimension(..) => { + let quotes = match quotes1 { + QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, + QuoteKind::None => QuoteKind::None, + }; + Value::Ident(format!("{}{}", s1, other), quotes) + } + Self::Null => { + let quotes = match quotes1 { + QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, + QuoteKind::None => QuoteKind::None, + }; + Value::Ident(s1, quotes) + } + _ => todo!(), + }, + _ => todo!(), + } + } +} + +impl Sub for Value { + type Output = Self; + + fn sub(self, other: Self) -> Self { + match self { + // Self::Important => todo!(), + // Self::True => todo!(), + // Self::False => todo!(), + // Self::Null => todo!(), + Self::Dimension(num, unit) => match other { + // Self::Dimension(num2, unit2) => Value::Dimension(num - num2, unit), + _ => todo!(), + }, + // Self::List(..) => todo!(), + // Self::Color(..) => todo!(), + // Self::BinaryOp(..) => todo!(), + // Self::Paren(..) => todo!(), + Self::Ident(s1, quotes1) => match other { + Self::Ident(s2, quotes2) => { + let quotes1 = match quotes1 { + QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, + QuoteKind::None => QuoteKind::None, + }; + let quotes2 = match quotes2 { + QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, + QuoteKind::None => QuoteKind::None, + }; + Value::Ident( + format!("{}{}{}-{}{}{}", quotes1, s1, quotes1, quotes2, s2, quotes2), + QuoteKind::None, + ) + } + Self::Important | Self::True | Self::False | Self::Dimension(..) => { + let quotes = match quotes1 { + QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, + QuoteKind::None => QuoteKind::None, + }; + Value::Ident( + format!("{}{}{}-{}", quotes, s1, quotes, other), + QuoteKind::None, + ) + } + Self::Null => { + let quotes = match quotes1 { + QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, + QuoteKind::None => QuoteKind::None, + }; + Value::Ident(format!("{}{}{}-", quotes, s1, quotes), QuoteKind::None) + } + _ => todo!(), + }, + _ => todo!(), + } + } +}