2020-02-09 18:28:24 -05:00
|
|
|
use std::ops::{Add, Div, Mul, Sub};
|
2020-02-08 16:07:37 -05:00
|
|
|
|
|
|
|
use crate::common::QuoteKind;
|
2020-02-09 16:05:07 -05:00
|
|
|
use crate::units::Unit;
|
2020-02-08 16:07:37 -05:00
|
|
|
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),
|
2020-02-09 20:26:14 -05:00
|
|
|
Self::Ident(s, q) => {
|
|
|
|
let quotes = match q {
|
|
|
|
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
|
|
|
|
QuoteKind::None => QuoteKind::None,
|
|
|
|
};
|
|
|
|
Value::Ident(format!("{}{}{}", num, unit, s), quotes)
|
|
|
|
}
|
2020-02-08 16:07:37 -05:00
|
|
|
_ => todo!(),
|
|
|
|
},
|
|
|
|
// Self::List(..) => todo!(),
|
2020-02-09 10:49:37 -05:00
|
|
|
Self::Color(c) => match other {
|
|
|
|
Self::Ident(s, QuoteKind::Double) | Self::Ident(s, QuoteKind::Single) => {
|
|
|
|
Value::Ident(format!("{}{}", c, s), QuoteKind::Double)
|
|
|
|
}
|
|
|
|
Self::Null => Value::Ident(c.to_string(), QuoteKind::None),
|
|
|
|
Self::Color(..) => todo!("figure out if it's possible to add colors"),
|
|
|
|
_ => Value::Ident(format!("{}{}", c, other), QuoteKind::None),
|
2020-02-09 12:18:41 -05:00
|
|
|
},
|
2020-02-08 16:07:37 -05:00
|
|
|
// 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)
|
|
|
|
}
|
2020-02-09 10:49:37 -05:00
|
|
|
Self::Color(c) => {
|
|
|
|
let quotes = match quotes1 {
|
|
|
|
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
|
|
|
|
QuoteKind::None => QuoteKind::None,
|
|
|
|
};
|
|
|
|
Value::Ident(format!("{}{}", s1, c), quotes)
|
|
|
|
}
|
2020-02-08 16:07:37 -05:00
|
|
|
_ => todo!(),
|
|
|
|
},
|
|
|
|
_ => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub for Value {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn sub(self, other: Self) -> Self {
|
|
|
|
match self {
|
2020-02-09 16:05:07 -05:00
|
|
|
Self::Null => todo!(),
|
2020-02-08 16:07:37 -05:00
|
|
|
Self::Dimension(num, unit) => match other {
|
2020-02-09 16:05:07 -05:00
|
|
|
Self::Dimension(num2, unit2) => Value::Dimension(num - num2, unit),
|
2020-02-08 16:07:37 -05:00
|
|
|
_ => todo!(),
|
|
|
|
},
|
|
|
|
// Self::List(..) => todo!(),
|
2020-02-09 11:07:13 -05:00
|
|
|
Self::Color(c) => match other {
|
|
|
|
Self::Ident(s, quotes) => {
|
|
|
|
let quotes = match quotes {
|
|
|
|
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
|
|
|
|
QuoteKind::None => QuoteKind::None,
|
|
|
|
};
|
|
|
|
Value::Ident(format!("{}-{}{}{}", c, quotes, s, quotes), QuoteKind::None)
|
|
|
|
}
|
|
|
|
Self::Null => Value::Ident(format!("{}-", c), QuoteKind::None),
|
|
|
|
Self::Dimension(..) => todo!("investigate adding numbers and colors"),
|
2020-02-09 12:18:41 -05:00
|
|
|
_ => Value::Ident(format!("{}-{}", c, other), QuoteKind::None),
|
2020-02-09 11:07:13 -05:00
|
|
|
},
|
2020-02-09 18:28:24 -05:00
|
|
|
Self::BinaryOp(..) | Self::Paren(..) => self.eval(),
|
2020-02-08 16:07:37 -05:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|
2020-02-09 12:18:41 -05:00
|
|
|
Self::Important
|
|
|
|
| Self::True
|
|
|
|
| Self::False
|
|
|
|
| Self::Dimension(..)
|
|
|
|
| Self::Color(..) => {
|
2020-02-08 16:07:37 -05:00
|
|
|
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!(),
|
|
|
|
},
|
2020-02-09 16:05:07 -05:00
|
|
|
_ => match other {
|
|
|
|
Self::Ident(s, quotes) => {
|
|
|
|
let quotes = match quotes {
|
|
|
|
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
|
|
|
|
QuoteKind::None => QuoteKind::None,
|
|
|
|
};
|
2020-02-09 18:28:24 -05:00
|
|
|
Value::Ident(
|
|
|
|
format!("{}-{}{}{}", self, quotes, s, quotes),
|
|
|
|
QuoteKind::None,
|
|
|
|
)
|
2020-02-09 16:05:07 -05:00
|
|
|
}
|
|
|
|
Self::Null => Value::Ident(format!("{}-", self), QuoteKind::None),
|
|
|
|
_ => Value::Ident(format!("{}-{}", self, other), QuoteKind::None),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mul for Value {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn mul(self, other: Self) -> Self {
|
|
|
|
match self {
|
|
|
|
Self::Null => todo!(),
|
|
|
|
Self::Dimension(num, unit) => match other {
|
2020-02-09 19:07:44 -05:00
|
|
|
Self::Dimension(num2, unit2) => Value::Dimension(num * num2, unit),
|
2020-02-09 16:05:07 -05:00
|
|
|
_ => todo!(),
|
|
|
|
},
|
2020-02-09 18:28:24 -05:00
|
|
|
Self::BinaryOp(..) | Self::Paren(..) => self.eval(),
|
|
|
|
_ => todo!("incompatible mul types"),
|
2020-02-09 16:05:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Div for Value {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn div(self, other: Self) -> Self {
|
|
|
|
match self {
|
|
|
|
Self::Null => todo!(),
|
|
|
|
Self::Dimension(num, unit) => match other {
|
2020-02-09 18:28:24 -05:00
|
|
|
Self::Dimension(num2, unit2) => {
|
|
|
|
if unit == unit2 {
|
|
|
|
Value::Dimension(num / num2, Unit::None)
|
|
|
|
} else {
|
|
|
|
todo!("unit conversions")
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 08:13:09 -05:00
|
|
|
Self::Ident(s, q) => {
|
|
|
|
let quotes = match q {
|
|
|
|
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
|
|
|
|
QuoteKind::None => QuoteKind::None,
|
|
|
|
};
|
|
|
|
Value::Ident(
|
|
|
|
format!("{}{}/{}{}{}", num, unit, quotes, s, quotes),
|
|
|
|
QuoteKind::None,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::BinaryOp(..) | Self::Paren(..) => Self::Dimension(num, unit) / other.eval(),
|
2020-02-09 16:05:07 -05:00
|
|
|
_ => todo!(),
|
|
|
|
},
|
|
|
|
// Self::List(..) => todo!(),
|
|
|
|
Self::Color(c) => match other {
|
|
|
|
Self::Ident(s, quotes) => {
|
|
|
|
let quotes = match quotes {
|
|
|
|
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
|
|
|
|
QuoteKind::None => QuoteKind::None,
|
|
|
|
};
|
|
|
|
Value::Ident(format!("{}/{}{}{}", c, quotes, s, quotes), QuoteKind::None)
|
|
|
|
}
|
|
|
|
Self::Null => Value::Ident(format!("{}/", c), QuoteKind::None),
|
|
|
|
Self::Dimension(..) => todo!("investigate adding numbers and colors"),
|
|
|
|
_ => Value::Ident(format!("{}/{}", c, other), QuoteKind::None),
|
|
|
|
},
|
2020-02-09 18:28:24 -05:00
|
|
|
Self::BinaryOp(..) | Self::Paren(..) => self.eval(),
|
2020-02-09 16:05:07 -05:00
|
|
|
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(..)
|
|
|
|
| Self::Color(..) => {
|
|
|
|
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!(),
|
|
|
|
},
|
|
|
|
_ => match other {
|
|
|
|
Self::Ident(s, quotes) => {
|
|
|
|
let quotes = match quotes {
|
|
|
|
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
|
|
|
|
QuoteKind::None => QuoteKind::None,
|
|
|
|
};
|
2020-02-09 18:28:24 -05:00
|
|
|
Value::Ident(
|
|
|
|
format!("{}/{}{}{}", self, quotes, s, quotes),
|
|
|
|
QuoteKind::None,
|
|
|
|
)
|
2020-02-09 16:05:07 -05:00
|
|
|
}
|
|
|
|
Self::Null => Value::Ident(format!("{}/", self), QuoteKind::None),
|
|
|
|
_ => Value::Ident(format!("{}/{}", self, other), QuoteKind::None),
|
|
|
|
},
|
2020-02-08 16:07:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|