summaryrefslogtreecommitdiff
path: root/src/eval/value.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-06-18 11:59:05 +0200
committerLaurenz <laurmaedje@gmail.com>2021-06-18 11:59:05 +0200
commitbca035172c463e6ac4aaf2591d7d4af2da51c522 (patch)
treed17ba4c0208caab1d30f6f2d19821cbd203e37fa /src/eval/value.rs
parent8b6391040e3fb2ab5f739e26f88621d63ad5d3cc (diff)
Join semantics
Diffstat (limited to 'src/eval/value.rs')
-rw-r--r--src/eval/value.rs29
1 files changed, 21 insertions, 8 deletions
diff --git a/src/eval/value.rs b/src/eval/value.rs
index f2bc4fd6..b29d01f3 100644
--- a/src/eval/value.rs
+++ b/src/eval/value.rs
@@ -5,6 +5,7 @@ use std::fmt::{self, Debug, Display, Formatter};
use std::ops::Deref;
use std::rc::Rc;
+use super::ops;
use super::EvalContext;
use crate::color::{Color, RgbaColor};
use crate::exec::ExecContext;
@@ -61,14 +62,6 @@ impl Value {
Self::Template(vec![TemplateNode::Func(TemplateFunc::new(name, f))])
}
- /// Try to cast the value into a specific type.
- pub fn cast<T>(self) -> CastResult<T, Self>
- where
- T: Cast<Value>,
- {
- T::cast(self)
- }
-
/// The name of the stored value's type.
pub fn type_name(&self) -> &'static str {
match self {
@@ -125,6 +118,26 @@ impl Value {
_ => None,
}
}
+
+ /// Try to cast the value into a specific type.
+ pub fn cast<T>(self) -> CastResult<T, Self>
+ where
+ T: Cast<Value>,
+ {
+ T::cast(self)
+ }
+
+ /// Join with another value.
+ pub fn join(self, ctx: &mut EvalContext, other: Self, span: Span) -> Self {
+ let (lhs, rhs) = (self.type_name(), other.type_name());
+ match ops::join(self, other) {
+ Ok(joined) => joined,
+ Err(prev) => {
+ ctx.diag(error!(span, "cannot join {} with {}", lhs, rhs));
+ prev
+ }
+ }
+ }
}
impl Default for Value {