typst/src/library/mod.rs
Laurenz 30f16bbf64 Add Value type and replace dyn-nodes with call-exprs 🏗
- In addition to syntax trees there are now `Value`s, which syntax trees can be evaluated into (e.g. the tree is `5+5` and the value is `10`)
- Parsing is completely pure, function calls are not parsed into nodes, but into simple call expressions, which are resolved later
- Functions aren't dynamic nodes anymore, but simply functions which receive their arguments as a table and the layouting context
- Functions may return any `Value`
- Layouting is powered by functions which return the new `Commands` value, which informs the layouting engine what to do
- When a function returns a non-`Commands` value, the layouter simply dumps the value into the document in monospace
2020-08-16 22:39:21 +02:00

65 lines
1.4 KiB
Rust

//! The standard library.
mod align;
mod boxed;
mod font;
mod page;
mod spacing;
pub use align::*;
pub use boxed::*;
pub use font::*;
pub use page::*;
pub use spacing::*;
use std::rc::Rc;
use crate::compute::scope::Scope;
use crate::prelude::*;
macro_rules! std {
(fallback: $fallback:expr $(, $name:literal => $func:expr)* $(,)?) => {
/// Create a scope with all standard library functions.
pub fn _std() -> Scope {
let mut std = Scope::new(wrap!(val));
$(std.insert($name, wrap!($func));)*
std
}
};
}
macro_rules! wrap {
($func:expr) => {
Rc::new(|args, ctx| Box::pin($func(args, ctx)))
};
}
std! {
fallback: val,
"align" => align,
"box" => boxed,
"dump" => dump,
"font" => font,
"h" => h,
"page" => page,
"pagebreak" => pagebreak,
"v" => v,
"val" => val,
}
/// `val`: Layouts its body flatly, ignoring other arguments.
///
/// This is also the fallback function, which is used when a function name
/// cannot be resolved.
pub async fn val(mut args: TableValue, _: LayoutContext<'_>) -> Pass<Value> {
Pass::commands(match args.take::<SyntaxTree>() {
Some(tree) => vec![LayoutSyntaxTree(tree)],
None => vec![],
}, Feedback::new())
}
/// `dump`: Dumps its arguments.
pub async fn dump(args: TableValue, _: LayoutContext<'_>) -> Pass<Value> {
Pass::okay(Value::Table(args))
}