oxedyne/fe2o3/fe2o3_austenite/src/eval/func.rs
7.1 KiB, 15 runs
created by r1870400018:63210, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // U0 owns this file. `NativeFunc`'s variant list is fixed; each inner enum, and the `call` it dispatches |
| 2 | // to, belongs to the file named beside it. Calling any `Func` is `Engine::call_func` in `eval.rs` (U2). |
| 3 | |
| 4 | use crate::eval::args::Args; |
| 5 | use crate::eval::content::ElemKind; |
| 6 | use crate::eval::lib::{ |
| 7 | array::ArrayFn, |
| 8 | calc::CalcFn, |
| 9 | color::ColorFn, |
| 10 | data::DataFn, |
| 11 | datetime::DatetimeFn, |
| 12 | dict::DictFn, |
| 13 | foundations::FoundFn, |
| 14 | geom::GeomFn, |
| 15 | intro::IntroFn, |
| 16 | layout::LayoutFn, |
| 17 | math::MathFn, |
| 18 | model::ModelFn, |
| 19 | numbering::NumberingFn, |
| 20 | pdf::PdfFn, |
| 21 | string::StrFn, |
| 22 | sym::SymFn, |
| 23 | text::TextFn, |
| 24 | visual::VisualFn, |
| 25 | }; |
| 26 | use crate::eval::lib; |
| 27 | use crate::eval::methods::CoreFn; |
| 28 | use crate::eval::fp; |
| 29 | #[cfg(feature = "plugin")] |
| 30 | use crate::eval::plugin::PluginFunc; |
| 31 | use crate::eval::scope::Scope; |
| 32 | use crate::eval::select::StyleFn; |
| 33 | use crate::eval::value::Value; |
| 34 | use crate::eval::Engine; |
| 35 | use crate::syntax::{ |
| 36 | Span, |
| 37 | SyntaxNode, |
| 38 | }; |
| 39 | |
| 40 | use oxedyne_fe2o3_core::prelude::*; |
| 41 | use oxedyne_fe2o3_hash::fingerprint::Fingerprint; |
| 42 | use oxedyne_fe2o3_hash::fingerprint::LazyFingerprint; |
| 43 | |
| 44 | use std::sync::Arc; |
| 45 | |
| 46 | #[derive(Clone, Debug)] |
| 47 | pub enum Func { |
| 48 | Native(NativeFunc), |
| 49 | Element(ElemKind), |
| 50 | Closure(Arc<Closure>), |
| 51 | With(Arc<(Func, Args)>), // `f.with(..)`: arguments applied before the call's own |
| 52 | #[cfg(feature = "plugin")] |
| 53 | Plugin(Arc<PluginFunc>), // an exported function of a WebAssembly plugin |
| 54 | } |
| 55 | |
| 56 | impl Func { |
| 57 | /// The name `repr` prints, `None` for an anonymous closure. |
| 58 | pub fn name(&self) -> Option<&str> { |
| 59 | match self { |
| 60 | Func::Native(n) => Some(n.name()), |
| 61 | Func::Element(k) => Some(k.name()), |
| 62 | Func::Closure(c) => c.name(), |
| 63 | Func::With(w) => w.0.name(), |
| 64 | #[cfg(feature = "plugin")] |
| 65 | Func::Plugin(p) => Some(p.name()), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn with(self, args: Args) -> Func { Func::With(Arc::new((self, args))) } |
| 70 | |
| 71 | /// The element this function constructs, seeing through `.with`, for `show heading: ..` and |
| 72 | /// `heading.where(..)`. |
| 73 | pub fn element(&self) -> Option<ElemKind> { |
| 74 | match self { |
| 75 | Func::Element(k) => Some(*k), |
| 76 | Func::With(w) => w.0.element(), |
| 77 | _ => None, |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// A closure parameter. A named default is evaluated when the closure is defined, as in Typst. |
| 83 | #[derive(Clone, Debug)] |
| 84 | pub enum Param { |
| 85 | Pos(SyntaxNode), // an identifier or a destructuring pattern |
| 86 | Named { name: String, default: Value }, |
| 87 | Sink(Option<String>), // `..rest`, or bare `..` |
| 88 | } |
| 89 | |
| 90 | /// A user function. `captured` is a snapshot of the defining scope: Typst closures capture by value. |
| 91 | /// |
| 92 | /// A closure is built once and held behind an `Arc`, so every closure that captures it shares it, and its |
| 93 | /// fingerprint is worked out once however many paths reach it. The fields are private so that nothing can |
| 94 | /// change a closure under a fingerprint already read. |
| 95 | #[derive(Clone, Debug)] |
| 96 | pub struct Closure { |
| 97 | name: Option<String>, // set by `let f(x) = ..`, for recursion and `repr` |
| 98 | params: Vec<Param>, |
| 99 | body: SyntaxNode, |
| 100 | captured: Scope, |
| 101 | span: Span, |
| 102 | fp: LazyFingerprint, |
| 103 | } |
| 104 | |
| 105 | impl Closure { |
| 106 | pub fn new(name: Option<String>, params: Vec<Param>, body: SyntaxNode, captured: Scope, span: Span) -> Self { |
| 107 | Self { name, params, body, captured, span, fp: LazyFingerprint::new() } |
| 108 | } |
| 109 | |
| 110 | pub fn name(&self) -> Option<&str> { self.name.as_deref() } |
| 111 | pub fn params(&self) -> &[Param] { &self.params } |
| 112 | pub fn body(&self) -> &SyntaxNode { &self.body } |
| 113 | pub fn captured(&self) -> &Scope { &self.captured } |
| 114 | pub fn span(&self) -> Span { self.span } |
| 115 | |
| 116 | /// What the closure is, never where it was written (see `eval::fp`). It covers everything the closure |
| 117 | /// captured, so it is kept: a capture reached by many paths would otherwise be walked once per path. |
| 118 | pub fn fingerprint(&self) -> Fingerprint { self.fp.get_or_init(|| fp::closure_fp(self)) } |
| 119 | } |
| 120 | |
| 121 | /// Every native function, grouped by the library area that implements it. |
| 122 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 123 | pub enum NativeFunc { |
| 124 | Core(CoreFn), // methods.rs, U2: `with`, content/args/func methods |
| 125 | Found(FoundFn), // lib/foundations.rs, U3 |
| 126 | Calc(CalcFn), // lib/calc.rs, U3 |
| 127 | Str(StrFn), // lib/string.rs, U3 |
| 128 | Array(ArrayFn), // lib/array.rs, U3 |
| 129 | Dict(DictFn), // lib/dict.rs, U3 |
| 130 | Numbering(NumberingFn), // lib/numbering.rs, U3 |
| 131 | Color(ColorFn), // lib/color.rs, U3 |
| 132 | Datetime(DatetimeFn), // lib/datetime.rs, U3 |
| 133 | Data(DataFn), // lib/data.rs, U3 |
| 134 | Sym(SymFn), // lib/sym.rs, U3 |
| 135 | Geom(GeomFn), // lib/geom.rs, U3: length, angle, ratio, alignment, direction methods |
| 136 | Style(StyleFn), // select.rs, U4 |
| 137 | Text(TextFn), // lib/text.rs, U6a |
| 138 | Layout(LayoutFn), // lib/layout.rs, U6b |
| 139 | Visual(VisualFn), // lib/visual.rs, U6d |
| 140 | Model(ModelFn), // lib/model/mod.rs, U5 |
| 141 | Math(MathFn), // lib/math.rs, U7 |
| 142 | Intro(IntroFn), // lib/intro.rs, U8 |
| 143 | Pdf(PdfFn), // lib/pdf.rs |
| 144 | } |
| 145 | |
| 146 | impl NativeFunc { |
| 147 | pub fn name(&self) -> &'static str { |
| 148 | match self { |
| 149 | NativeFunc::Core(f) => f.name(), |
| 150 | NativeFunc::Found(f) => f.name(), |
| 151 | NativeFunc::Calc(f) => f.name(), |
| 152 | NativeFunc::Str(f) => f.name(), |
| 153 | NativeFunc::Array(f) => f.name(), |
| 154 | NativeFunc::Dict(f) => f.name(), |
| 155 | NativeFunc::Numbering(f) => f.name(), |
| 156 | NativeFunc::Color(f) => f.name(), |
| 157 | NativeFunc::Datetime(f) => f.name(), |
| 158 | NativeFunc::Data(f) => f.name(), |
| 159 | NativeFunc::Sym(f) => f.name(), |
| 160 | NativeFunc::Geom(f) => f.name(), |
| 161 | NativeFunc::Style(f) => f.name(), |
| 162 | NativeFunc::Text(f) => f.name(), |
| 163 | NativeFunc::Layout(f) => f.name(), |
| 164 | NativeFunc::Visual(f) => f.name(), |
| 165 | NativeFunc::Model(f) => f.name(), |
| 166 | NativeFunc::Math(f) => f.name(), |
| 167 | NativeFunc::Intro(f) => f.name(), |
| 168 | NativeFunc::Pdf(f) => f.name(), |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /// Runs the function. A method call arrives with its receiver as the first positional argument. |
| 173 | pub fn call(self, engine: &mut Engine, args: Args) -> Outcome<Value> { |
| 174 | match self { |
| 175 | NativeFunc::Core(f) => crate::eval::methods::call(f, engine, args), |
| 176 | NativeFunc::Found(f) => lib::foundations::call(f, engine, args), |
| 177 | NativeFunc::Calc(f) => lib::calc::call(f, engine, args), |
| 178 | NativeFunc::Str(f) => lib::string::call(f, engine, args), |
| 179 | NativeFunc::Array(f) => lib::array::call(f, engine, args), |
| 180 | NativeFunc::Dict(f) => lib::dict::call(f, engine, args), |
| 181 | NativeFunc::Numbering(f) => lib::numbering::call(f, engine, args), |
| 182 | NativeFunc::Color(f) => lib::color::call(f, engine, args), |
| 183 | NativeFunc::Datetime(f) => lib::datetime::call(f, engine, args), |
| 184 | NativeFunc::Data(f) => lib::data::call(f, engine, args), |
| 185 | NativeFunc::Sym(f) => lib::sym::call(f, engine, args), |
| 186 | NativeFunc::Geom(f) => lib::geom::call(f, engine, args), |
| 187 | NativeFunc::Style(f) => crate::eval::select::call(f, engine, args), |
| 188 | NativeFunc::Text(f) => lib::text::call(f, engine, args), |
| 189 | NativeFunc::Layout(f) => lib::layout::call(f, engine, args), |
| 190 | NativeFunc::Visual(f) => lib::visual::call(f, engine, args), |
| 191 | NativeFunc::Model(f) => lib::model::call(f, engine, args), |
| 192 | NativeFunc::Math(f) => lib::math::call(f, engine, args), |
| 193 | NativeFunc::Intro(f) => lib::intro::call(f, engine, args), |
| 194 | NativeFunc::Pdf(f) => lib::pdf::call(f, engine, args), |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /// The error every stub `call` returns until its unit lands. |
| 200 | pub fn unimplemented(area: &str, name: &str) -> Error<ErrTag> { |
| 201 | err!("{}.{} is not implemented yet", area, name; Unimplemented) |
| 202 | } |