Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/mod.rs

2.4 KiB, 2 runs

created by r1870400018:63246, 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//! Contract (U0, 2026-09-23): the standard library. Every area file exposes
2//! `define(&mut Scope)` for its globals, its inner native enum and `call(f, engine, args)`; an area
3//! whose type has methods also exposes `method(name) -> Option<XFn>`. An element family file
4//! (`text`, `layout`, `grid`, `visual`, `math`, `intro`, `model`) additionally exposes `fields(kind)`,
5//! `construct(engine, kind, args) -> Outcome<Option<Content>>` (`None` to use the generic schema walk)
6//! and `show(engine, elem, styles) -> Outcome<Option<Content>>` (`None` for a primitive flow lays out).
7//! Global element functions are registered here generically; `math.*` elements are the math area's.
8//! The file is `string.rs`, not `str.rs`, because a module named `str` shadows the primitive type.
9
10pub mod array;
11pub mod calc;
12pub mod color;
13pub mod data;
14pub mod datetime;
15pub mod decimal;
16pub mod dict;
17pub mod foundations;
18pub mod geom;
19pub mod grid;
20pub mod intro;
21pub mod layout;
22pub mod math;
23pub mod model;
24pub mod numbering;
25pub mod pdf;
26pub mod string;
27pub mod sym;
28pub mod text;
29pub mod visual;
30
31use crate::eval::content::ElemKind;
32use crate::eval::func::Func;
33use crate::eval::scope::Scope;
34use crate::eval::value::Value;
35
36/// The root scope every module's scope chains to.
37pub fn library() -> Scope {
38 let mut s = Scope::new();
39 for k in ElemKind::ALL {
40 if k.is_global() {
41 s.define(k.path(), Value::Func(Func::Element(*k)));
42 }
43 }
44 foundations::define(&mut s);
45 calc::define(&mut s);
46 string::define(&mut s);
47 array::define(&mut s);
48 dict::define(&mut s);
49 numbering::define(&mut s);
50 color::define(&mut s);
51 datetime::define(&mut s);
52 data::define(&mut s);
53 sym::define(&mut s);
54 geom::define(&mut s);
55 text::define(&mut s);
56 layout::define(&mut s);
57 grid::define(&mut s);
58 model::define(&mut s);
59 math::define(&mut s);
60 intro::define(&mut s);
61 pdf::define(&mut s);
62 // `std` is the library as a module, so a name a document redefines stays reachable as `std.text`.
63 let std = s.clone();
64 s.define("std", Value::Module(std::sync::Arc::new(crate::eval::value::Module::new("std", std))));
65 s
66}
67
68/// Defines every element whose path starts with `prefix.` under its last segment, for a module scope
69/// such as `math`.
70pub fn define_elements(scope: &mut Scope, prefix: &str) {
71 for k in ElemKind::ALL {
72 let p = k.path();
73 if p.len() > prefix.len() + 1 && p.starts_with(prefix) && p.as_bytes().get(prefix.len()) == Some(&b'.') {
74 scope.define(k.name(), Value::Func(Func::Element(*k)));
75 }
76 }
77}