Improve binary op error messages
This commit is contained in:
parent
7949699229
commit
4e7cf277e1
@ -40,7 +40,17 @@ impl Display for Value {
|
|||||||
.join(sep.as_str())
|
.join(sep.as_str())
|
||||||
),
|
),
|
||||||
Self::Color(c) => write!(f, "{}", c),
|
Self::Color(c) => write!(f, "{}", c),
|
||||||
Self::BinaryOp(..) => write!(f, "{}", self.clone().eval().unwrap()),
|
Self::BinaryOp(..) => write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
match self.clone().eval() {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("{}", e);
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
),
|
||||||
Self::Paren(val) => write!(f, "{}", val),
|
Self::Paren(val) => write!(f, "{}", val),
|
||||||
Self::Ident(val, kind) => {
|
Self::Ident(val, kind) => {
|
||||||
if kind == &QuoteKind::None {
|
if kind == &QuoteKind::None {
|
||||||
|
@ -31,7 +31,11 @@ impl Add for Value {
|
|||||||
Value::Ident(format!("{}{}{}", num, unit, s), quotes)
|
Value::Ident(format!("{}{}{}", num, unit, s), quotes)
|
||||||
}
|
}
|
||||||
Self::Null => Value::Ident(format!("{}{}", num, unit), QuoteKind::None),
|
Self::Null => Value::Ident(format!("{}{}", num, unit), QuoteKind::None),
|
||||||
_ => todo!(),
|
_ => {
|
||||||
|
return Err(
|
||||||
|
format!("Undefined operation \"{}{} + {}\".", num, unit, other).into(),
|
||||||
|
)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
// Self::List(..) => todo!(),
|
// Self::List(..) => todo!(),
|
||||||
Self::Color(c) => match dbg!(&other) {
|
Self::Color(c) => match dbg!(&other) {
|
||||||
@ -105,7 +109,9 @@ impl Sub for Value {
|
|||||||
Value::Ident(format!("{}-{}{}{}", c, quotes, s, quotes), QuoteKind::None)
|
Value::Ident(format!("{}-{}{}{}", c, quotes, s, quotes), QuoteKind::None)
|
||||||
}
|
}
|
||||||
Self::Null => Value::Ident(format!("{}-", c), QuoteKind::None),
|
Self::Null => Value::Ident(format!("{}-", c), QuoteKind::None),
|
||||||
Self::Dimension(..) => todo!("investigate adding numbers and colors"),
|
v @ Self::Dimension(..) => {
|
||||||
|
return Err(format!("Undefined operation \"{} - {}\".", c, v).into())
|
||||||
|
}
|
||||||
_ => Value::Ident(format!("{}-{}", c, other), QuoteKind::None),
|
_ => Value::Ident(format!("{}-{}", c, other), QuoteKind::None),
|
||||||
},
|
},
|
||||||
Self::BinaryOp(..) | Self::Paren(..) => self.eval()?,
|
Self::BinaryOp(..) | Self::Paren(..) => self.eval()?,
|
||||||
@ -173,7 +179,11 @@ impl Mul for Value {
|
|||||||
Self::Null => todo!(),
|
Self::Null => todo!(),
|
||||||
Self::Dimension(num, unit) => match other {
|
Self::Dimension(num, unit) => match other {
|
||||||
Self::Dimension(num2, unit2) => Value::Dimension(num * num2, unit),
|
Self::Dimension(num2, unit2) => Value::Dimension(num * num2, unit),
|
||||||
_ => todo!(),
|
_ => {
|
||||||
|
return Err(
|
||||||
|
format!("Undefined operation \"{}{} * {}\".", num, unit, other).into(),
|
||||||
|
)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Self::BinaryOp(..) | Self::Paren(..) => self.eval()?,
|
Self::BinaryOp(..) | Self::Paren(..) => self.eval()?,
|
||||||
_ => todo!("incompatible mul types"),
|
_ => todo!("incompatible mul types"),
|
||||||
@ -219,7 +229,9 @@ impl Div for Value {
|
|||||||
Value::Ident(format!("{}/{}{}{}", c, quotes, s, quotes), QuoteKind::None)
|
Value::Ident(format!("{}/{}{}{}", c, quotes, s, quotes), QuoteKind::None)
|
||||||
}
|
}
|
||||||
Self::Null => Value::Ident(format!("{}/", c), QuoteKind::None),
|
Self::Null => Value::Ident(format!("{}/", c), QuoteKind::None),
|
||||||
Self::Dimension(..) => todo!("investigate adding numbers and colors"),
|
Self::Dimension(..) => {
|
||||||
|
return Err(format!("Undefined operation \"{} / {}\".", c, other).into())
|
||||||
|
}
|
||||||
_ => Value::Ident(format!("{}/{}", c, other), QuoteKind::None),
|
_ => Value::Ident(format!("{}/{}", c, other), QuoteKind::None),
|
||||||
},
|
},
|
||||||
Self::BinaryOp(..) | Self::Paren(..) => self.eval()?,
|
Self::BinaryOp(..) | Self::Paren(..) => self.eval()?,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user