Files
RustAst/src/ast/rtl/mod.rs
T
Brummel b436e09840 Add documentation infrastructure for RTL symbols
Introduces `RtlDocEntry` and `RtlRegistration` structs to store and
manage documentation for native functions and constants. The
`Environment` struct is updated to include a registry for these
documentation entries.

The `register_native_fn` and `register_constant` methods now return an
`RtlRegistration` builder, allowing for optional chaining of `.doc()`,
`.description()`, and `.examples()` methods before the registration is
finalized when the builder is dropped.

Macros `rtl_fn!` and `rtl_const!` are introduced to simplify the
registration process with documentation.

The MCP server is extended with `get_rtl_docs` and `get_symbol_doc`
tools to expose this documentation externally.

Specific RTL functions and constants (e.g., `+`, `-`, `*`, `/`, `NaN`,
`true`, `false`, `date`, `now`, math functions, `series`, `push`, `len`,
`create-random-ohlc`, `create-ticker`, `pipe`) have been updated to
include their respective documentation using the new infrastructure.

The `Signature` and `StaticType` types have gained `to_doc_string`
methods for generating human-readable documentation strings.
2026-03-25 15:02:19 +01:00

111 lines
3.1 KiB
Rust

pub mod core;
pub mod datetime;
pub mod intrinsics;
pub mod math;
pub mod series;
pub mod streams;
pub mod type_registry;
use crate::ast::environment::Environment;
pub fn register(env: &Environment) {
core::register(env);
datetime::register(env);
math::register(env);
series::register(env);
streams::register(env);
}
/// Registers a native RTL function and optionally attaches documentation.
///
/// Required: `doc:` — a short one-line description.
/// Optional: `description:` — a longer explanation.
/// Optional: `examples:` — a `&'static [&'static str]` of Myc code snippets.
///
/// # Example
/// ```ignore
/// rtl_fn!(env, "+",
/// doc: "Adds two values.",
/// add_ty, Pure, |args| { ... }
/// );
///
/// rtl_fn!(env, "push",
/// doc: "Pushes a new record into a series.",
/// description: "The record must match the series layout.",
/// examples: &["(push my_ticks {:price 10.5})"],
/// push_ty, Impure, |args| { ... }
/// );
/// ```
#[macro_export]
macro_rules! rtl_fn {
// With description and examples
($env:expr, $name:expr,
doc: $doc:expr,
description: $desc:expr,
examples: $ex:expr,
$ty:expr, $purity:expr, $impl:expr $(,)?) => {
$env.register_native_fn($name, $ty, $purity, $impl)
.doc($doc)
.description($desc)
.examples($ex);
};
// With description only
($env:expr, $name:expr,
doc: $doc:expr,
description: $desc:expr,
$ty:expr, $purity:expr, $impl:expr $(,)?) => {
$env.register_native_fn($name, $ty, $purity, $impl)
.doc($doc)
.description($desc);
};
// With examples only
($env:expr, $name:expr,
doc: $doc:expr,
examples: $ex:expr,
$ty:expr, $purity:expr, $impl:expr $(,)?) => {
$env.register_native_fn($name, $ty, $purity, $impl)
.doc($doc)
.examples($ex);
};
// doc only
($env:expr, $name:expr,
doc: $doc:expr,
$ty:expr, $purity:expr, $impl:expr $(,)?) => {
$env.register_native_fn($name, $ty, $purity, $impl)
.doc($doc);
};
}
/// Registers a native RTL constant and optionally attaches documentation.
/// Same optional fields as `rtl_fn!`.
#[macro_export]
macro_rules! rtl_const {
// With description and examples
($env:expr, $name:expr,
doc: $doc:expr,
description: $desc:expr,
examples: $ex:expr,
$ty:expr, $val:expr $(,)?) => {
$env.register_constant($name, $ty, $val)
.doc($doc)
.description($desc)
.examples($ex);
};
// With description only
($env:expr, $name:expr,
doc: $doc:expr,
description: $desc:expr,
$ty:expr, $val:expr $(,)?) => {
$env.register_constant($name, $ty, $val)
.doc($doc)
.description($desc);
};
// doc only
($env:expr, $name:expr,
doc: $doc:expr,
$ty:expr, $val:expr $(,)?) => {
$env.register_constant($name, $ty, $val)
.doc($doc);
};
}