summaryrefslogtreecommitdiff
path: root/src/syntax/ast/lit.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-04 20:07:01 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-04 20:07:01 +0200
commita41d7ab47dda1e30465bdf91fd02bca0e634a38d (patch)
tree981f115f6cd87a4906bee058f7c80e1a200c2770 /src/syntax/ast/lit.rs
parentc1dd872b34507a9f45b39a8a6ac70606b642a19d (diff)
Expression evaluation with Eval trait 🧮
Diffstat (limited to 'src/syntax/ast/lit.rs')
-rw-r--r--src/syntax/ast/lit.rs55
1 files changed, 8 insertions, 47 deletions
diff --git a/src/syntax/ast/lit.rs b/src/syntax/ast/lit.rs
index 414d5490..31566e89 100644
--- a/src/syntax/ast/lit.rs
+++ b/src/syntax/ast/lit.rs
@@ -2,10 +2,8 @@
use super::*;
use crate::color::RgbaColor;
-use crate::eval::{DictKey, SpannedEntry, Value, ValueDict};
-use crate::layout::LayoutContext;
+use crate::eval::DictKey;
use crate::length::Length;
-use crate::DynFuture;
/// A literal.
#[derive(Debug, Clone, PartialEq)]
@@ -37,54 +35,10 @@ pub enum Lit {
Content(SynTree),
}
-impl Lit {
- /// Evaluate the dictionary literal to a dictionary value.
- pub async fn eval(&self, ctx: &mut LayoutContext) -> Value {
- match *self {
- Lit::Ident(ref v) => Value::Ident(v.clone()),
- Lit::Bool(v) => Value::Bool(v),
- Lit::Int(v) => Value::Int(v),
- Lit::Float(v) => Value::Float(v),
- Lit::Length(v) => Value::Length(v.as_raw()),
- Lit::Percent(v) => Value::Relative(v / 100.0),
- Lit::Color(v) => Value::Color(v),
- Lit::Str(ref v) => Value::Str(v.clone()),
- Lit::Dict(ref v) => Value::Dict(v.eval(ctx).await),
- Lit::Content(ref v) => Value::Content(v.clone()),
- }
- }
-}
-
/// A dictionary literal: `(false, 12cm, greeting = "hi")`.
#[derive(Debug, Clone, PartialEq)]
pub struct LitDict(pub Vec<LitDictEntry>);
-impl LitDict {
- /// Create an empty dict literal.
- pub fn new() -> Self {
- Self(vec![])
- }
-
- /// Evaluate the dictionary literal to a dictionary value.
- pub fn eval<'a>(&'a self, ctx: &'a mut LayoutContext) -> DynFuture<'a, ValueDict> {
- Box::pin(async move {
- let mut dict = ValueDict::new();
-
- for entry in &self.0 {
- let val = entry.expr.v.eval(ctx).await;
- let spanned = val.span_with(entry.expr.span);
- if let Some(key) = &entry.key {
- dict.insert(&key.v, SpannedEntry::new(key.span, spanned));
- } else {
- dict.push(SpannedEntry::value(spanned));
- }
- }
-
- dict
- })
- }
-}
-
/// An entry in a dictionary literal: `false` or `greeting = "hi"`.
#[derive(Debug, Clone, PartialEq)]
pub struct LitDictEntry {
@@ -93,3 +47,10 @@ pub struct LitDictEntry {
/// The value of the entry: `"hi"`.
pub expr: Spanned<Expr>,
}
+
+impl LitDict {
+ /// Create an empty dict literal.
+ pub fn new() -> Self {
+ Self(vec![])
+ }
+}