diff options
Diffstat (limited to 'src/eval')
| -rw-r--r-- | src/eval/scope.rs | 21 | ||||
| -rw-r--r-- | src/eval/value.rs | 8 |
2 files changed, 26 insertions, 3 deletions
diff --git a/src/eval/scope.rs b/src/eval/scope.rs index 20c18306..a3c9234b 100644 --- a/src/eval/scope.rs +++ b/src/eval/scope.rs @@ -1,10 +1,10 @@ use std::cell::RefCell; use std::collections::HashMap; -use std::fmt::{self, Debug, Formatter}; +use std::fmt::{self, Debug, Display, Formatter}; use std::iter; use std::rc::Rc; -use super::Value; +use super::{AnyValue, EvalContext, FuncArgs, FuncValue, Type, Value}; /// A slot where a variable is stored. pub type Slot = Rc<RefCell<Value>>; @@ -100,6 +100,23 @@ impl Scope { self.values.insert(var.into(), Rc::new(cell)); } + /// Define a constant function. + pub fn def_func<F>(&mut self, name: impl Into<String>, f: F) + where + F: Fn(&mut EvalContext, &mut FuncArgs) -> Value + 'static, + { + let name = name.into(); + self.def_const(name.clone(), FuncValue::new(Some(name), f)); + } + + /// Define a constant variable with a value of variant `Value::Any`. + pub fn def_any<T>(&mut self, var: impl Into<String>, any: T) + where + T: Type + Debug + Display + Clone + PartialEq + 'static, + { + self.def_const(var, AnyValue::new(any)) + } + /// Define a mutable variable with a value. pub fn def_mut(&mut self, var: impl Into<String>, value: impl Into<Value>) { self.values.insert(var.into(), Rc::new(RefCell::new(value.into()))); diff --git a/src/eval/value.rs b/src/eval/value.rs index a69398f0..0d87c28f 100644 --- a/src/eval/value.rs +++ b/src/eval/value.rs @@ -6,7 +6,7 @@ use std::ops::Deref; use std::rc::Rc; use super::{EvalContext, NodeMap}; -use crate::color::Color; +use crate::color::{Color, RgbaColor}; use crate::exec::ExecContext; use crate::geom::{Angle, Length, Linear, Relative}; use crate::syntax::{Span, Spanned, Tree}; @@ -622,6 +622,12 @@ impl From<&str> for Value { } } +impl From<RgbaColor> for Value { + fn from(v: RgbaColor) -> Self { + Self::Color(Color::Rgba(v)) + } +} + impl From<AnyValue> for Value { fn from(v: AnyValue) -> Self { Self::Any(v) |
