summaryrefslogtreecommitdiff
path: root/src/eval
diff options
context:
space:
mode:
Diffstat (limited to 'src/eval')
-rw-r--r--src/eval/mod.rs3
-rw-r--r--src/eval/ops.rs12
-rw-r--r--src/eval/value.rs6
3 files changed, 19 insertions, 2 deletions
diff --git a/src/eval/mod.rs b/src/eval/mod.rs
index d1307b6d..5bc2f101 100644
--- a/src/eval/mod.rs
+++ b/src/eval/mod.rs
@@ -18,7 +18,7 @@ use std::rc::Rc;
use crate::cache::Cache;
use crate::color::Color;
use crate::diag::{Diag, DiagSet, Pass};
-use crate::geom::{Angle, Length, Relative};
+use crate::geom::{Angle, Fractional, Length, Relative};
use crate::loading::{FileHash, Loader};
use crate::parse::parse;
use crate::syntax::visit::Visit;
@@ -250,6 +250,7 @@ impl Eval for Expr {
Self::Length(_, v, unit) => Value::Length(Length::with_unit(v, unit)),
Self::Angle(_, v, unit) => Value::Angle(Angle::with_unit(v, unit)),
Self::Percent(_, v) => Value::Relative(Relative::new(v / 100.0)),
+ Self::Fractional(_, v) => Value::Fractional(Fractional::new(v)),
Self::Color(_, v) => Value::Color(Color::Rgba(v)),
Self::Str(_, ref v) => Value::Str(v.clone()),
Self::Ident(ref v) => match ctx.scopes.get(&v) {
diff --git a/src/eval/ops.rs b/src/eval/ops.rs
index 69a0b02b..15c09e03 100644
--- a/src/eval/ops.rs
+++ b/src/eval/ops.rs
@@ -11,6 +11,7 @@ pub fn pos(value: Value) -> Value {
Length(v) => Length(v),
Angle(v) => Angle(v),
Relative(v) => Relative(v),
+ Fractional(v) => Fractional(v),
Linear(v) => Linear(v),
_ => Error,
}
@@ -24,6 +25,7 @@ pub fn neg(value: Value) -> Value {
Length(v) => Length(-v),
Angle(v) => Angle(-v),
Relative(v) => Relative(-v),
+ Fractional(v) => Fractional(-v),
Linear(v) => Linear(-v),
_ => Error,
}
@@ -44,6 +46,7 @@ pub fn add(lhs: Value, rhs: Value) -> Value {
(Relative(a), Length(b)) => Linear(a + b),
(Relative(a), Relative(b)) => Relative(a + b),
(Relative(a), Linear(b)) => Linear(a + b),
+ (Fractional(a), Fractional(b)) => Fractional(a + b),
(Linear(a), Length(b)) => Linear(a + b),
(Linear(a), Relative(b)) => Linear(a + b),
(Linear(a), Linear(b)) => Linear(a + b),
@@ -84,6 +87,7 @@ pub fn sub(lhs: Value, rhs: Value) -> Value {
(Relative(a), Length(b)) => Linear(a - b),
(Relative(a), Relative(b)) => Relative(a - b),
(Relative(a), Linear(b)) => Linear(a - b),
+ (Fractional(a), Fractional(b)) => Fractional(a - b),
(Linear(a), Length(b)) => Linear(a - b),
(Linear(a), Relative(b)) => Linear(a - b),
(Linear(a), Linear(b)) => Linear(a - b),
@@ -108,8 +112,13 @@ pub fn mul(lhs: Value, rhs: Value) -> Value {
(Float(a), Angle(b)) => Angle(a * b),
(Relative(a), Int(b)) => Relative(a * b as f64),
(Relative(a), Float(b)) => Relative(a * b),
+ (Fractional(a), Fractional(b)) => Fractional(a * b.get()),
+ (Fractional(a), Int(b)) => Fractional(a * b as f64),
+ (Fractional(a), Float(b)) => Fractional(a * b),
(Int(a), Relative(b)) => Relative(a as f64 * b),
+ (Int(a), Fractional(b)) => Fractional(a as f64 * b),
(Float(a), Relative(b)) => Relative(a * b),
+ (Float(a), Fractional(b)) => Fractional(a * b),
(Linear(a), Int(b)) => Linear(a * b as f64),
(Linear(a), Float(b)) => Linear(a * b),
(Int(a), Linear(b)) => Linear(a as f64 * b),
@@ -134,6 +143,9 @@ pub fn div(lhs: Value, rhs: Value) -> Value {
(Relative(a), Int(b)) => Relative(a / b as f64),
(Relative(a), Float(b)) => Relative(a / b),
(Relative(a), Relative(b)) => Float(a / b),
+ (Fractional(a), Fractional(b)) => Float(a.get() / b.get()),
+ (Fractional(a), Int(b)) => Fractional(a / b as f64),
+ (Fractional(a), Float(b)) => Fractional(a / b),
(Linear(a), Int(b)) => Linear(a / b as f64),
(Linear(a), Float(b)) => Linear(a / b),
_ => Error,
diff --git a/src/eval/value.rs b/src/eval/value.rs
index 94f7f569..498403e6 100644
--- a/src/eval/value.rs
+++ b/src/eval/value.rs
@@ -8,7 +8,7 @@ use std::rc::Rc;
use super::EvalContext;
use crate::color::{Color, RgbaColor};
use crate::exec::ExecContext;
-use crate::geom::{Angle, Length, Linear, Relative};
+use crate::geom::{Angle, Fractional, Length, Linear, Relative};
use crate::syntax::{Expr, Span, Spanned, Tree};
/// A computational value.
@@ -28,6 +28,8 @@ pub enum Value {
Angle(Angle),
/// A relative value: `50%`.
Relative(Relative),
+ /// A fractional value: `1fr`.
+ Fractional(Fractional),
/// A combination of an absolute length and a relative value: `20% + 5cm`.
Linear(Linear),
/// A color value: `#f79143ff`.
@@ -75,6 +77,7 @@ impl Value {
Self::Length(_) => Length::TYPE_NAME,
Self::Angle(_) => Angle::TYPE_NAME,
Self::Relative(_) => Relative::TYPE_NAME,
+ Self::Fractional(_) => Fractional::TYPE_NAME,
Self::Linear(_) => Linear::TYPE_NAME,
Self::Color(_) => Color::TYPE_NAME,
Self::Str(_) => String::TYPE_NAME,
@@ -601,6 +604,7 @@ primitive! {
primitive! { Length: "length", Value::Length }
primitive! { Angle: "angle", Value::Angle }
primitive! { Relative: "relative", Value::Relative }
+primitive! { Fractional: "fractional", Value::Fractional }
primitive! {
Linear: "linear",
Value::Linear,