diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-03-17 11:32:15 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-03-17 11:45:57 +0100 |
| commit | 312197b276748e1a17258ad21837850f582a467c (patch) | |
| tree | 3fd0c078a2673a98b74bc12b4d654a4c143b4e1f /src/eval | |
| parent | e8435df5ec718e8ecc8a2ad48e4eb3ddd1f92a72 (diff) | |
Counters
Diffstat (limited to 'src/eval')
| -rw-r--r-- | src/eval/library.rs | 11 | ||||
| -rw-r--r-- | src/eval/methods.rs | 15 |
2 files changed, 25 insertions, 1 deletions
diff --git a/src/eval/library.rs b/src/eval/library.rs index 1240d9bb..45c23d17 100644 --- a/src/eval/library.rs +++ b/src/eval/library.rs @@ -6,11 +6,12 @@ use comemo::Tracked; use ecow::EcoString; use once_cell::sync::OnceCell; -use super::Module; +use super::{Args, Dynamic, Module, Value}; use crate::diag::SourceResult; use crate::doc::Document; use crate::geom::{Abs, Dir}; use crate::model::{Content, Introspector, Label, NodeId, StyleChain, StyleMap, Vt}; +use crate::syntax::Span; use crate::util::hash128; use crate::World; @@ -89,6 +90,14 @@ pub struct LangItems { pub math_accent: fn(base: Content, accent: char) -> Content, /// A fraction in a formula: `x/2`. pub math_frac: fn(num: Content, denom: Content) -> Content, + /// Dispatch a method on a counter. This is hacky and should be superseded + /// by more dynamic method dispatch. + pub counter_method: fn( + dynamic: &Dynamic, + method: &str, + args: Args, + span: Span, + ) -> SourceResult<Value>, } impl Debug for LangItems { diff --git a/src/eval/methods.rs b/src/eval/methods.rs index 197a2f65..a449ac16 100644 --- a/src/eval/methods.rs +++ b/src/eval/methods.rs @@ -134,6 +134,14 @@ pub fn call( _ => return missing(), }, + Value::Dyn(dynamic) => { + if dynamic.type_name() == "counter" { + return (vm.items.counter_method)(&dynamic, method, args, span); + } + + return missing(); + } + _ => return missing(), }; @@ -281,6 +289,13 @@ pub fn methods_on(type_name: &str) -> &[(&'static str, bool)] { ], "function" => &[("where", true), ("with", true)], "arguments" => &[("named", false), ("pos", false)], + "counter" => &[ + ("get", true), + ("final", true), + ("both", true), + ("step", true), + ("update", true), + ], _ => &[], } } |
