Refactor value
This commit is contained in:
parent
789c55ac3d
commit
6471ce8f29
@ -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, ", "),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
use std::convert::{TryFrom, TryInto};
|
use std::convert::{TryFrom, TryInto};
|
||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
use std::iter::{Iterator, Peekable};
|
use std::iter::{Iterator, Peekable};
|
||||||
use std::ops::{Add, Sub};
|
|
||||||
|
|
||||||
use num_rational::BigRational;
|
use num_rational::BigRational;
|
||||||
use num_bigint::BigInt;
|
use num_bigint::BigInt;
|
||||||
@ -10,42 +9,12 @@ use num_bigint::BigInt;
|
|||||||
use crate::args::eat_call_args;
|
use crate::args::eat_call_args;
|
||||||
use crate::builtin::GLOBAL_FUNCTIONS;
|
use crate::builtin::GLOBAL_FUNCTIONS;
|
||||||
use crate::color::Color;
|
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::units::Unit;
|
||||||
use crate::utils::{devour_whitespace_or_comment, parse_interpolation};
|
use crate::utils::{devour_whitespace_or_comment, parse_interpolation};
|
||||||
use crate::{Token, TokenKind};
|
use crate::{Token, TokenKind};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
mod ops;
|
||||||
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, ", "),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub(crate) enum Value {
|
pub(crate) enum Value {
|
||||||
@ -61,118 +30,6 @@ pub(crate) enum Value {
|
|||||||
Ident(String, QuoteKind),
|
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 {
|
impl Display for Value {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
116
src/value/ops.rs
Normal file
116
src/value/ops.rs
Normal file
@ -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!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user