summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-04 19:21:35 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-04 19:21:35 +0200
commitf4460f8abd7dee1806cf59b4d3777581a6ed154a (patch)
tree8e591d253a537a76f8c309e23b09bf7a9a762fd5 /src/syntax
parent0f7c70fd93db23ec866ae13aa2f146b7787afabf (diff)
Style nits 🎈
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/ast/expr.rs91
-rw-r--r--src/syntax/ast/lit.rs30
-rw-r--r--src/syntax/ast/mod.rs2
-rw-r--r--src/syntax/ast/tree.rs2
4 files changed, 64 insertions, 61 deletions
diff --git a/src/syntax/ast/expr.rs b/src/syntax/ast/expr.rs
index 5cd5187b..718b0568 100644
--- a/src/syntax/ast/expr.rs
+++ b/src/syntax/ast/expr.rs
@@ -1,8 +1,8 @@
//! Expressions.
+use super::*;
use crate::eval::Value;
use crate::layout::LayoutContext;
-use crate::syntax::{Decoration, Ident, Lit, LitDict, SpanWith, Spanned};
use crate::DynFuture;
/// An expression.
@@ -10,12 +10,12 @@ use crate::DynFuture;
pub enum Expr {
/// A literal: `true`, `1cm`, `"hi"`, `{_Hey!_}`.
Lit(Lit),
+ /// An invocation of a function: `[foo: ...]`, `foo(...)`.
+ Call(ExprCall),
/// A unary operation: `-x`.
Unary(ExprUnary),
/// A binary operation: `a + b`, `a / b`.
Binary(ExprBinary),
- /// An invocation of a function: `[foo: ...]`, `foo(...)`.
- Call(ExprCall),
}
impl Expr {
@@ -24,14 +24,43 @@ impl Expr {
Box::pin(async move {
match self {
Self::Lit(lit) => lit.eval(ctx).await,
+ Self::Call(call) => call.eval(ctx).await,
Self::Unary(unary) => unary.eval(ctx).await,
Self::Binary(binary) => binary.eval(ctx).await,
- Self::Call(call) => call.eval(ctx).await,
}
})
}
}
+/// An invocation of a function: `[foo: ...]`, `foo(...)`.
+#[derive(Debug, Clone, PartialEq)]
+pub struct ExprCall {
+ /// The name of the function.
+ pub name: Spanned<Ident>,
+ /// The arguments to the function.
+ pub args: LitDict,
+}
+
+impl ExprCall {
+ /// Evaluate the call expression to a value.
+ pub async fn eval(&self, ctx: &mut LayoutContext) -> Value {
+ let name = &self.name.v;
+ let span = self.name.span;
+ let args = self.args.eval(ctx).await;
+
+ if let Some(func) = ctx.state.scope.func(name) {
+ ctx.f.decorations.push(Decoration::Resolved.span_with(span));
+ (func.clone())(args, ctx).await
+ } else {
+ if !name.is_empty() {
+ error!(@ctx.f, span, "unknown function");
+ ctx.f.decorations.push(Decoration::Unresolved.span_with(span));
+ }
+ Value::Dict(args)
+ }
+ }
+}
+
/// A unary operation: `-x`.
#[derive(Debug, Clone, PartialEq)]
pub struct ExprUnary {
@@ -54,13 +83,13 @@ impl ExprUnary {
let span = self.op.span.join(self.expr.span);
match self.op.v {
UnOp::Neg => match value {
- Int(x) => Int(-x),
- Float(x) => Float(-x),
- Length(x) => Length(-x),
- Relative(x) => Relative(-x),
- Linear(x) => Linear(-x),
+ Int(v) => Int(-v),
+ Float(v) => Float(-v),
+ Length(v) => Length(-v),
+ Relative(v) => Relative(-v),
+ Linear(v) => Linear(-v),
v => {
- error!(@ctx.f, span, "cannot negate {}", v.name());
+ error!(@ctx.f, span, "cannot negate {}", v.ty());
Value::Error
}
},
@@ -104,7 +133,8 @@ impl ExprBinary {
BinOp::Add => match (lhs, rhs) {
// Numbers to themselves.
(Int(a), Int(b)) => Int(a + b),
- (Int(i), Float(f)) | (Float(f), Int(i)) => Float(i as f64 + f),
+ (Int(a), Float(b)) => Float(a as f64 + b),
+ (Float(a), Int(b)) => Float(a + b as f64),
(Float(a), Float(b)) => Float(a + b),
// Lengths, relatives and linears to themselves.
@@ -123,11 +153,11 @@ impl ExprBinary {
// Complex data types to themselves.
(Str(a), Str(b)) => Str(a + &b),
(Dict(a), Dict(b)) => Dict(concat(a, b)),
- (Tree(a), Tree(b)) => Tree(concat(a, b)),
+ (Content(a), Content(b)) => Content(concat(a, b)),
(Commands(a), Commands(b)) => Commands(concat(a, b)),
(a, b) => {
- error!(@ctx.f, span, "cannot add {} and {}", a.name(), b.name());
+ error!(@ctx.f, span, "cannot add {} and {}", a.ty(), b.ty());
Value::Error
}
},
@@ -151,7 +181,7 @@ impl ExprBinary {
(Linear(a), Linear(b)) => Linear(a - b),
(a, b) => {
- error!(@ctx.f, span, "cannot subtract {1} from {0}", a.name(), b.name());
+ error!(@ctx.f, span, "cannot subtract {1} from {0}", a.ty(), b.ty());
Value::Error
}
},
@@ -182,7 +212,7 @@ impl ExprBinary {
(Str(a), Int(b)) => Str(a.repeat(b.max(0) as usize)),
(a, b) => {
- error!(@ctx.f, span, "cannot multiply {} with {}", a.name(), b.name());
+ error!(@ctx.f, span, "cannot multiply {} with {}", a.ty(), b.ty());
Value::Error
}
},
@@ -203,7 +233,7 @@ impl ExprBinary {
(Linear(a), Float(b)) => Linear(a / b),
(a, b) => {
- error!(@ctx.f, span, "cannot divide {} by {}", a.name(), b.name());
+ error!(@ctx.f, span, "cannot divide {} by {}", a.ty(), b.ty());
Value::Error
}
},
@@ -232,32 +262,3 @@ pub enum BinOp {
/// The division operator: `/`.
Div,
}
-
-/// An invocation of a function: `[foo: ...]`, `foo(...)`.
-#[derive(Debug, Clone, PartialEq)]
-pub struct ExprCall {
- /// The name of the function.
- pub name: Spanned<Ident>,
- /// The arguments to the function.
- pub args: LitDict,
-}
-
-impl ExprCall {
- /// Evaluate the call expression to a value.
- pub async fn eval(&self, ctx: &mut LayoutContext) -> Value {
- let name = &self.name.v;
- let span = self.name.span;
- let args = self.args.eval(ctx).await;
-
- if let Some(func) = ctx.state.scope.func(name) {
- ctx.f.decorations.push(Decoration::Resolved.span_with(span));
- (func.clone())(args, ctx).await
- } else {
- if !name.is_empty() {
- error!(@ctx.f, span, "unknown function");
- ctx.f.decorations.push(Decoration::Unresolved.span_with(span));
- }
- Value::Dict(args)
- }
- }
-}
diff --git a/src/syntax/ast/lit.rs b/src/syntax/ast/lit.rs
index acc3aa0b..414d5490 100644
--- a/src/syntax/ast/lit.rs
+++ b/src/syntax/ast/lit.rs
@@ -1,10 +1,10 @@
//! Literals.
+use super::*;
use crate::color::RgbaColor;
-use crate::eval::{DictKey, DictValue, SpannedEntry, Value};
+use crate::eval::{DictKey, SpannedEntry, Value, ValueDict};
use crate::layout::LayoutContext;
use crate::length::Length;
-use crate::syntax::{Expr, Ident, SpanWith, Spanned, SynTree};
use crate::DynFuture;
/// A literal.
@@ -41,16 +41,16 @@ impl Lit {
/// Evaluate the dictionary literal to a dictionary value.
pub async fn eval(&self, ctx: &mut LayoutContext) -> Value {
match *self {
- Lit::Ident(ref i) => Value::Ident(i.clone()),
- Lit::Bool(b) => Value::Bool(b),
- Lit::Int(i) => Value::Int(i),
- Lit::Float(f) => Value::Float(f),
- Lit::Length(l) => Value::Length(l.as_raw()),
- Lit::Percent(p) => Value::Relative(p / 100.0),
- Lit::Color(c) => Value::Color(c),
- Lit::Str(ref s) => Value::Str(s.clone()),
- Lit::Dict(ref d) => Value::Dict(d.eval(ctx).await),
- Lit::Content(ref c) => Value::Tree(c.clone()),
+ 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()),
}
}
}
@@ -66,9 +66,9 @@ impl LitDict {
}
/// Evaluate the dictionary literal to a dictionary value.
- pub fn eval<'a>(&'a self, ctx: &'a mut LayoutContext) -> DynFuture<'a, DictValue> {
+ pub fn eval<'a>(&'a self, ctx: &'a mut LayoutContext) -> DynFuture<'a, ValueDict> {
Box::pin(async move {
- let mut dict = DictValue::new();
+ let mut dict = ValueDict::new();
for entry in &self.0 {
let val = entry.expr.v.eval(ctx).await;
@@ -76,7 +76,7 @@ impl LitDict {
if let Some(key) = &entry.key {
dict.insert(&key.v, SpannedEntry::new(key.span, spanned));
} else {
- dict.push(SpannedEntry::val(spanned));
+ dict.push(SpannedEntry::value(spanned));
}
}
diff --git a/src/syntax/ast/mod.rs b/src/syntax/ast/mod.rs
index 56ae4134..df0fbc23 100644
--- a/src/syntax/ast/mod.rs
+++ b/src/syntax/ast/mod.rs
@@ -7,3 +7,5 @@ mod tree;
pub use expr::*;
pub use lit::*;
pub use tree::*;
+
+use super::*;
diff --git a/src/syntax/ast/tree.rs b/src/syntax/ast/tree.rs
index c5dc4c65..5723710d 100644
--- a/src/syntax/ast/tree.rs
+++ b/src/syntax/ast/tree.rs
@@ -1,6 +1,6 @@
//! The syntax tree.
-use crate::syntax::{Expr, Ident, SpanVec, Spanned};
+use super::*;
/// A collection of nodes which form a tree together with the nodes' children.
pub type SynTree = SpanVec<SynNode>;