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.
This commit is contained in:
@@ -15,3 +15,96 @@ pub fn register(env: &Environment) {
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user