1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
|
use std::any::Any;
use std::cmp::Ordering;
use std::collections::{BTreeMap, HashMap};
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;
use crate::geom::{Angle, Fractional, Length, Linear, Relative};
use crate::syntax::{Expr, Span, Spanned, Tree};
/// A computational value.
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
/// The value that indicates the absence of a meaningful value.
None,
/// A value that indicates some smart default behaviour.
Auto,
/// A boolean: `true, false`.
Bool(bool),
/// An integer: `120`.
Int(i64),
/// A floating-point number: `1.2`, `10e-4`.
Float(f64),
/// A length: `12pt`, `3cm`.
Length(Length),
/// An angle: `1.5rad`, `90deg`.
Angle(Angle),
/// A relative value: `50%`.
Relative(Relative),
/// A combination of an absolute length and a relative value: `20% + 5cm`.
Linear(Linear),
/// A fractional value: `1fr`.
Fractional(Fractional),
/// A color value: `#f79143ff`.
Color(Color),
/// A string: `"string"`.
Str(String),
/// An array value: `(1, "hi", 12cm)`.
Array(ArrayValue),
/// A dictionary value: `(color: #f79143, pattern: dashed)`.
Dict(DictValue),
/// A template value: `[*Hi* there]`.
Template(TemplateValue),
/// An executable function.
Func(FuncValue),
/// Any object.
Any(AnyValue),
/// The result of invalid operations.
Error,
}
impl Value {
/// Create a new template value consisting of a single dynamic node.
pub fn template<F>(f: F) -> Self
where
F: Fn(&mut ExecContext) + 'static,
{
Self::Template(vec![TemplateNode::Func(TemplateFunc::new(f))])
}
/// The name of the stored value's type.
pub fn type_name(&self) -> &'static str {
match self {
Self::None => "none",
Self::Auto => "auto",
Self::Bool(_) => bool::TYPE_NAME,
Self::Int(_) => i64::TYPE_NAME,
Self::Float(_) => f64::TYPE_NAME,
Self::Length(_) => Length::TYPE_NAME,
Self::Angle(_) => Angle::TYPE_NAME,
Self::Relative(_) => Relative::TYPE_NAME,
Self::Linear(_) => Linear::TYPE_NAME,
Self::Fractional(_) => Fractional::TYPE_NAME,
Self::Color(_) => Color::TYPE_NAME,
Self::Str(_) => String::TYPE_NAME,
Self::Array(_) => ArrayValue::TYPE_NAME,
Self::Dict(_) => DictValue::TYPE_NAME,
Self::Template(_) => TemplateValue::TYPE_NAME,
Self::Func(_) => FuncValue::TYPE_NAME,
Self::Any(v) => v.type_name(),
Self::Error => "error",
}
}
/// Recursively compute whether two values are equal.
pub fn eq(&self, rhs: &Self) -> bool {
match (self, rhs) {
(&Self::Int(a), &Self::Float(b)) => a as f64 == b,
(&Self::Float(a), &Self::Int(b)) => a == b as f64,
(&Self::Length(a), &Self::Linear(b)) => a == b.abs && b.rel.is_zero(),
(&Self::Relative(a), &Self::Linear(b)) => a == b.rel && b.abs.is_zero(),
(&Self::Linear(a), &Self::Length(b)) => a.abs == b && a.rel.is_zero(),
(&Self::Linear(a), &Self::Relative(b)) => a.rel == b && a.abs.is_zero(),
(Self::Array(a), Self::Array(b)) => {
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| x.eq(y))
}
(Self::Dict(a), Self::Dict(b)) => {
a.len() == b.len()
&& a.iter().all(|(k, x)| b.get(k).map_or(false, |y| x.eq(y)))
}
(a, b) => a == b,
}
}
/// Compare a value with another value.
pub fn cmp(&self, rhs: &Self) -> Option<Ordering> {
match (self, rhs) {
(Self::Int(a), Self::Int(b)) => a.partial_cmp(b),
(Self::Int(a), Self::Float(b)) => (*a as f64).partial_cmp(b),
(Self::Float(a), Self::Int(b)) => a.partial_cmp(&(*b as f64)),
(Self::Float(a), Self::Float(b)) => a.partial_cmp(b),
(Self::Angle(a), Self::Angle(b)) => a.partial_cmp(b),
(Self::Length(a), Self::Length(b)) => a.partial_cmp(b),
_ => 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 {
fn default() -> Self {
Value::None
}
}
/// An array value: `(1, "hi", 12cm)`.
pub type ArrayValue = Vec<Value>;
/// A dictionary value: `(color: #f79143, pattern: dashed)`.
pub type DictValue = BTreeMap<String, Value>;
/// A template value: `[*Hi* there]`.
pub type TemplateValue = Vec<TemplateNode>;
/// One chunk of a template.
///
/// Evaluating a template expression creates only a single node. Adding multiple
/// templates can yield multi-node templates.
#[derive(Debug, Clone)]
pub enum TemplateNode {
/// A template that consists of a syntax tree plus already evaluated
/// expression.
Tree {
/// The syntax tree of the corresponding template expression.
tree: Rc<Tree>,
/// The evaluated expressions for the `tree`.
map: ExprMap,
},
/// A template that was converted from a string.
Str(String),
/// A function template that can implement custom behaviour.
Func(TemplateFunc),
}
impl PartialEq for TemplateNode {
fn eq(&self, _: &Self) -> bool {
// TODO: Figure out what we want here.
false
}
}
/// A map from expressions to the values they evaluated to.
///
/// The raw pointers point into the expressions contained in some [`Tree`].
/// Since the lifetime is erased, the tree could go out of scope while the hash
/// map still lives. Although this could lead to lookup panics, it is not unsafe
/// since the pointers are never dereferenced.
pub type ExprMap = HashMap<*const Expr, Value>;
/// A reference-counted dynamic template node that can implement custom
/// behaviour.
#[derive(Clone)]
pub struct TemplateFunc(Rc<dyn Fn(&mut ExecContext)>);
impl TemplateFunc {
/// Create a new function template from a rust function or closure.
pub fn new<F>(f: F) -> Self
where
F: Fn(&mut ExecContext) + 'static,
{
Self(Rc::new(f))
}
}
impl PartialEq for TemplateFunc {
fn eq(&self, _: &Self) -> bool {
// TODO: Figure out what we want here.
false
}
}
impl Deref for TemplateFunc {
type Target = dyn Fn(&mut ExecContext);
fn deref(&self) -> &Self::Target {
self.0.as_ref()
}
}
impl Debug for TemplateFunc {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("TemplateAny").finish()
}
}
/// A wrapper around a reference-counted executable function.
#[derive(Clone)]
pub struct FuncValue {
/// The string is boxed to make the whole struct fit into 24 bytes, so that
/// a [`Value`] fits into 32 bytes.
name: Option<Box<String>>,
f: Rc<dyn Fn(&mut EvalContext, &mut FuncArgs) -> Value>,
}
impl FuncValue {
/// Create a new function value from a rust function or closure.
pub fn new<F>(name: Option<String>, f: F) -> Self
where
F: Fn(&mut EvalContext, &mut FuncArgs) -> Value + 'static,
{
Self { name: name.map(Box::new), f: Rc::new(f) }
}
/// The name of the function.
pub fn name(&self) -> Option<&str> {
self.name.as_ref().map(|s| s.as_str())
}
}
impl PartialEq for FuncValue {
fn eq(&self, _: &Self) -> bool {
// TODO: Figure out what we want here.
false
}
}
impl Deref for FuncValue {
type Target = dyn Fn(&mut EvalContext, &mut FuncArgs) -> Value;
fn deref(&self) -> &Self::Target {
self.f.as_ref()
}
}
impl Debug for FuncValue {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("ValueFunc").field("name", &self.name).finish()
}
}
/// Evaluated arguments to a function.
#[derive(Debug, Clone, PartialEq)]
pub struct FuncArgs {
/// The span of the whole argument list.
pub span: Span,
/// The positional arguments.
pub items: Vec<FuncArg>,
}
impl FuncArgs {
/// Find and consume the first castable positional argument.
pub fn eat<T>(&mut self, ctx: &mut EvalContext) -> Option<T>
where
T: Cast<Spanned<Value>>,
{
(0 .. self.items.len()).find_map(|index| {
let slot = &mut self.items[index];
if slot.name.is_some() {
return None;
}
let value = std::mem::replace(&mut slot.value, Spanned::zero(Value::None));
let span = value.span;
match T::cast(value) {
CastResult::Ok(t) => {
self.items.remove(index);
Some(t)
}
CastResult::Warn(t, m) => {
self.items.remove(index);
ctx.diag(warning!(span, "{}", m));
Some(t)
}
CastResult::Err(value) => {
slot.value = value;
None
}
}
})
}
/// Find and consume the first castable positional argument, producing a
/// `missing argument: {what}` error if no match was found.
pub fn expect<T>(&mut self, ctx: &mut EvalContext, what: &str) -> Option<T>
where
T: Cast<Spanned<Value>>,
{
let found = self.eat(ctx);
if found.is_none() {
ctx.diag(error!(self.span, "missing argument: {}", what));
}
found
}
/// Find, consume and collect all castable positional arguments.
///
/// This function returns a vector instead of an iterator because the
/// iterator would require unique access to the context, rendering it rather
/// unusable. If you need to process arguments one-by-one, you probably want
/// to use a while-let loop together with [`eat()`](Self::eat).
pub fn all<T>(&mut self, ctx: &mut EvalContext) -> Vec<T>
where
T: Cast<Spanned<Value>>,
{
std::iter::from_fn(|| self.eat(ctx)).collect()
}
/// Cast and remove the value for the given named argument, producing an
/// error if the conversion fails.
pub fn named<T>(&mut self, ctx: &mut EvalContext, name: &str) -> Option<T>
where
T: Cast<Spanned<Value>>,
{
let index = self
.items
.iter()
.position(|arg| arg.name.as_ref().map_or(false, |other| name == other))?;
let value = self.items.remove(index).value;
let span = value.span;
match T::cast(value) {
CastResult::Ok(t) => Some(t),
CastResult::Warn(t, m) => {
ctx.diag(warning!(span, "{}", m));
Some(t)
}
CastResult::Err(value) => {
ctx.diag(error!(
span,
"expected {}, found {}",
T::TYPE_NAME,
value.v.type_name(),
));
None
}
}
}
/// Produce "unexpected argument" errors for all remaining arguments.
pub fn finish(self, ctx: &mut EvalContext) {
for arg in &self.items {
if arg.value.v != Value::Error {
ctx.diag(error!(arg.span, "unexpected argument"));
}
}
}
}
/// An argument to a function call: `12` or `draw: false`.
#[derive(Debug, Clone, PartialEq)]
pub struct FuncArg {
/// The span of the whole argument.
pub span: Span,
/// The name of the argument (`None` for positional arguments).
pub name: Option<String>,
/// The value of the argument.
pub value: Spanned<Value>,
}
/// A wrapper around a dynamic value.
pub struct AnyValue(Box<dyn Bounds>);
impl AnyValue {
/// Create a new instance from any value that satisifies the required bounds.
pub fn new<T>(any: T) -> Self
where
T: Type + Debug + Display + Clone + PartialEq + 'static,
{
Self(Box::new(any))
}
/// Whether the wrapped type is `T`.
pub fn is<T: 'static>(&self) -> bool {
self.0.as_any().is::<T>()
}
/// Try to downcast to a specific type.
pub fn downcast<T: 'static>(self) -> Result<T, Self> {
if self.is::<T>() {
Ok(*self.0.into_any().downcast().unwrap())
} else {
Err(self)
}
}
/// Try to downcast to a reference to a specific type.
pub fn downcast_ref<T: 'static>(&self) -> Option<&T> {
self.0.as_any().downcast_ref()
}
/// The name of the stored value's type.
pub fn type_name(&self) -> &'static str {
self.0.dyn_type_name()
}
}
impl Clone for AnyValue {
fn clone(&self) -> Self {
Self(self.0.dyn_clone())
}
}
impl PartialEq for AnyValue {
fn eq(&self, other: &Self) -> bool {
self.0.dyn_eq(other)
}
}
impl Debug for AnyValue {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_tuple("ValueAny").field(&self.0).finish()
}
}
impl Display for AnyValue {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
trait Bounds: Debug + Display + 'static {
fn as_any(&self) -> &dyn Any;
fn into_any(self: Box<Self>) -> Box<dyn Any>;
fn dyn_eq(&self, other: &AnyValue) -> bool;
fn dyn_clone(&self) -> Box<dyn Bounds>;
fn dyn_type_name(&self) -> &'static str;
}
impl<T> Bounds for T
where
T: Type + Debug + Display + Clone + PartialEq + 'static,
{
fn as_any(&self) -> &dyn Any {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
fn dyn_eq(&self, other: &AnyValue) -> bool {
if let Some(other) = other.downcast_ref::<Self>() {
self == other
} else {
false
}
}
fn dyn_clone(&self) -> Box<dyn Bounds> {
Box::new(self.clone())
}
fn dyn_type_name(&self) -> &'static str {
T::TYPE_NAME
}
}
/// Types that can be stored in values.
pub trait Type {
/// The name of the type.
const TYPE_NAME: &'static str;
}
impl<T> Type for Spanned<T>
where
T: Type,
{
const TYPE_NAME: &'static str = T::TYPE_NAME;
}
/// Cast from a value to a specific type.
pub trait Cast<V>: Type + Sized {
/// Try to cast the value into an instance of `Self`.
fn cast(value: V) -> CastResult<Self, V>;
}
/// The result of casting a value to a specific type.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum CastResult<T, V> {
/// The value was cast successfully.
Ok(T),
/// The value was cast successfully, but with a warning message.
Warn(T, String),
/// The value could not be cast into the specified type.
Err(V),
}
impl<T, V> CastResult<T, V> {
/// Access the conversion result, discarding a possibly existing warning.
pub fn ok(self) -> Option<T> {
match self {
CastResult::Ok(t) | CastResult::Warn(t, _) => Some(t),
CastResult::Err(_) => None,
}
}
}
impl Type for Value {
const TYPE_NAME: &'static str = "value";
}
impl Cast<Value> for Value {
fn cast(value: Value) -> CastResult<Self, Value> {
CastResult::Ok(value)
}
}
impl<T> Cast<Spanned<Value>> for T
where
T: Cast<Value>,
{
fn cast(value: Spanned<Value>) -> CastResult<Self, Spanned<Value>> {
let span = value.span;
match T::cast(value.v) {
CastResult::Ok(t) => CastResult::Ok(t),
CastResult::Warn(t, m) => CastResult::Warn(t, m),
CastResult::Err(v) => CastResult::Err(Spanned::new(v, span)),
}
}
}
impl<T> Cast<Spanned<Value>> for Spanned<T>
where
T: Cast<Value>,
{
fn cast(value: Spanned<Value>) -> CastResult<Self, Spanned<Value>> {
let span = value.span;
match T::cast(value.v) {
CastResult::Ok(t) => CastResult::Ok(Spanned::new(t, span)),
CastResult::Warn(t, m) => CastResult::Warn(Spanned::new(t, span), m),
CastResult::Err(v) => CastResult::Err(Spanned::new(v, span)),
}
}
}
macro_rules! primitive {
($type:ty:
$type_name:literal,
$variant:path
$(, $pattern:pat => $out:expr)* $(,)?
) => {
impl Type for $type {
const TYPE_NAME: &'static str = $type_name;
}
impl From<$type> for Value {
fn from(v: $type) -> Self {
$variant(v)
}
}
impl Cast<Value> for $type {
fn cast(value: Value) -> CastResult<Self, Value> {
match value {
$variant(v) => CastResult::Ok(v),
$($pattern => CastResult::Ok($out),)*
v => CastResult::Err(v),
}
}
}
};
}
primitive! { bool: "boolean", Value::Bool }
primitive! { i64: "integer", Value::Int }
primitive! {
f64: "float",
Value::Float,
Value::Int(v) => v as f64,
}
primitive! { Length: "length", Value::Length }
primitive! { Angle: "angle", Value::Angle }
primitive! { Relative: "relative", Value::Relative }
primitive! {
Linear: "linear",
Value::Linear,
Value::Length(v) => v.into(),
Value::Relative(v) => v.into(),
}
primitive! { Fractional: "fractional", Value::Fractional }
primitive! { Color: "color", Value::Color }
primitive! { String: "string", Value::Str }
primitive! { ArrayValue: "array", Value::Array }
primitive! { DictValue: "dictionary", Value::Dict }
primitive! {
TemplateValue: "template",
Value::Template,
Value::Str(v) => vec![TemplateNode::Str(v)],
}
primitive! { FuncValue: "function", Value::Func }
impl From<usize> for Value {
fn from(v: usize) -> Self {
Self::Int(v as i64)
}
}
impl From<&str> for Value {
fn from(v: &str) -> Self {
Self::Str(v.to_string())
}
}
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)
}
}
/// Make a type castable from a value.
///
/// Given a type `T`, this implements the following traits:
/// - [`Type`] for `T`,
/// - [`Cast<Value>`](Cast) for `T`.
///
/// # Example
/// ```
/// # use typst::value;
/// enum FontFamily {
/// Serif,
/// Named(String),
/// }
///
/// value! {
/// FontFamily: "font family",
/// Value::Str(string) => Self::Named(string),
/// }
/// ```
/// This would allow the type `FontFamily` to be cast from:
/// - a [`Value::Any`] variant already containing a `FontFamily`,
/// - a string, producing a named font family.
macro_rules! castable {
($type:ty:
$type_name:literal
$(, $pattern:pat => $out:expr)*
$(, #($anyvar:ident: $anytype:ty) => $anyout:expr)*
$(,)?
) => {
impl $crate::eval::Type for $type {
const TYPE_NAME: &'static str = $type_name;
}
impl $crate::eval::Cast<$crate::eval::Value> for $type {
fn cast(
value: $crate::eval::Value,
) -> $crate::eval::CastResult<Self, $crate::eval::Value> {
use $crate::eval::*;
#[allow(unreachable_code)]
match value {
$($pattern => CastResult::Ok($out),)*
Value::Any(mut any) => {
any = match any.downcast::<Self>() {
Ok(t) => return CastResult::Ok(t),
Err(any) => any,
};
$(any = match any.downcast::<$anytype>() {
Ok($anyvar) => return CastResult::Ok($anyout),
Err(any) => any,
};)*
CastResult::Err(Value::Any(any))
},
v => CastResult::Err(v),
}
}
}
};
}
|