diff --git a/src/ast/environment.rs b/src/ast/environment.rs index 0ee3e89..188c9e5 100644 --- a/src/ast/environment.rs +++ b/src/ast/environment.rs @@ -21,13 +21,13 @@ use crate::ast::compiler::macros::{MacroEvaluator, MacroExpander, MacroRegistry} use crate::ast::compiler::optimizer::Optimizer; use crate::ast::compiler::specializer::{FunctionRegistry, MonoCache, RtlLookupFunc, Specializer}; use crate::ast::rtl::{self, intrinsics}; +use crate::ast::rtl::docs::{PipelineGenerator, RtlDocEntry, RtlRegistration}; use crate::ast::types::{ CommentLine, NativeFunction, NodeIdentity, Purity, SourceLocation, StaticType, Value, }; use crate::ast::diagnostics::Diagnostics; pub use crate::ast::compiler::CompilationResult; -pub type PipelineGenerator = Box bool>; const SYSTEM_LIB_SOURCE: &str = include_str!("rtl/prelude.myc"); const SYSTEM_LIB_PATH: &str = "/prelude.myc"; @@ -36,62 +36,6 @@ fn make_rtl_lookup() -> RtlLookupFunc { Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args)) } -/// Documentation entry for a single RTL symbol (function or constant). -/// Populated via the builder returned by `register_native_fn` / `register_constant`. -pub struct RtlDocEntry { - pub name: String, - /// Short one-line description (required). - pub one_liner: &'static str, - /// Optional longer explanation. - pub description: Option<&'static str>, - /// Optional Myc code examples. - pub examples: Option<&'static [&'static str]>, -} - -/// Builder returned by `register_native_fn` and `register_constant`. -/// Call `.doc(...)` to attach documentation; optional `.description(...)` and `.examples(...)` -/// can be chained. The entry is written to the registry when the builder is dropped. -pub struct RtlRegistration { - name: String, - rtl_docs: Rc>>, - one_liner: Option<&'static str>, - description: Option<&'static str>, - examples: Option<&'static [&'static str]>, -} - -impl RtlRegistration { - /// Attach a required one-line description. - pub fn doc(mut self, one_liner: &'static str) -> Self { - self.one_liner = Some(one_liner); - self - } - - /// Attach an optional longer description (must call `.doc()` first). - pub fn description(mut self, desc: &'static str) -> Self { - self.description = Some(desc); - self - } - - /// Attach optional usage examples as Myc code strings. - pub fn examples(mut self, ex: &'static [&'static str]) -> Self { - self.examples = Some(ex); - self - } -} - -impl Drop for RtlRegistration { - fn drop(&mut self) { - if let Some(one_liner) = self.one_liner { - self.rtl_docs.borrow_mut().push(RtlDocEntry { - name: self.name.clone(), - one_liner, - description: self.description, - examples: self.examples, - }); - } - } -} - pub struct Environment { pub root_types: Rc>>, pub root_purity: Rc>>, @@ -603,24 +547,12 @@ impl Environment { purity: purity_level, }), ); - RtlRegistration { - name: name.to_string(), - rtl_docs: Rc::clone(&self.rtl_docs), - one_liner: None, - description: None, - examples: None, - } + RtlRegistration::new(name.to_string(), Rc::clone(&self.rtl_docs)) } pub fn register_constant(&self, name: &str, ty: StaticType, val: Value) -> RtlRegistration { self.allocate_slot(name, ty, Purity::Pure, val); - RtlRegistration { - name: name.to_string(), - rtl_docs: Rc::clone(&self.rtl_docs), - one_liner: None, - description: None, - examples: None, - } + RtlRegistration::new(name.to_string(), Rc::clone(&self.rtl_docs)) } diff --git a/src/ast/rtl/mod.rs b/src/ast/rtl/mod.rs index a31f4a0..9802a42 100644 --- a/src/ast/rtl/mod.rs +++ b/src/ast/rtl/mod.rs @@ -1,11 +1,14 @@ pub mod core; pub mod datetime; +pub mod docs; pub mod intrinsics; pub mod math; pub mod series; pub mod streams; pub mod type_registry; +pub use docs::{PipelineGenerator, RtlDocEntry, RtlRegistration}; + use crate::ast::environment::Environment; pub fn register(env: &Environment) {