Skip to main content

Module loader

Module loader 

Source
Expand description

Module loader for multi-file LOGOS projects.

Handles resolution and loading of module sources from various URI schemes, with caching to prevent duplicate loading.

§Supported URI Schemes

SchemeExampleDescription
file:file:./geometry.mdLocal filesystem (relative)
logos:logos:stdBuilt-in standard library
(none)geometry.mdDefaults to file: scheme

§Security

The loader prevents path traversal attacks by checking that resolved paths remain within the project root directory.

§Caching

Modules are cached by their normalized URI. The same module loaded from different base paths will be cached separately.

§Example

let mut loader = Loader::new(project_root);
let source = loader.resolve(Path::new("main.md"), "file:./lib/math.md")?;
println!("Loaded: {}", source.path.display());

Structs§

DepRoot
A declared dependency’s resolved root: the alias an Import <Name>. binds to (matched case-insensitively against the bare import name) and the canonicalized directory its modules live under. Populated from a project’s Largo.toml [dependencies]; empty for a single-project build.
LibraryProvenance
Establishes, for the compilation or execution that runs while it lives, which functions of the program are the standard library’s definitions. Restores the enclosing provenance on drop, so nested and sequential compiles on one thread never see each other’s program.
Loader
Module loader that handles multiple URI schemes.
ModuleSource
A loaded module’s source content and metadata.
PreludeSegment
One run of apply_prelude’s output tied back to where it came from: user_start is the byte offset of an untouched run of the caller’s own text, None marks spliced stdlib text. Downstream span translation (deep check) inverts the prelude edit exactly through these runs.

Functions§

apply_prelude
Prepend the stdlib modules a program actually uses. Returns the source unchanged when the program references no stdlib vocabulary or opts out with ## NoPrelude (in which case the opt-out marker is stripped so it never reaches the parser). This is the auto-import seam for both the interpreter and the compiler.
apply_prelude_mapped
apply_prelude plus the exact edit it performed, as ordered output runs. A consumer that parses the expanded text and reports spans against the USER’s buffer must translate through these segments rather than assume the modules were prepended: they are spliced AFTER any leading title/comment prose, and ## NoPrelude REMOVES its marker lines.
content_type_for
★ THE MIME TABLE LIVES IN ONE PLACE, AND THAT PLACE IS static.lg. ★
defined_names_with_visibility
Like defined_names, but also reports whether each definition is private (declared ## To private … / ## A private …). Public-by-default: a name with no private marker is false. Powers module encapsulation — a private name is usable by its owning module but not importable by any other file. Public so the language server can filter private names out of its cross-module surfaces (completion, auto-import, workspace symbols) from the one source of truth.
is_library_function
Is name the standard library’s own definition in the program being compiled?
keep_reachable_functions
Drop top-level ## To … function definitions that are UNREACHABLE from the program’s entry, keeping every non-function section (## A structs, ## Main, prose) and every function whose name is transitively called. This is the C backends’ tree-shake: a flat C namespace can’t tolerate emitting an unused stdlib function that either collides with a libc name or exercises a C-codegen gap the function is never actually reached through (the Rust backend tolerates both, so this is C-only).
library_owned_functions
The functions of an assembled program that ARE the standard library’s definitions: those whose ## To section is byte-identical to the section the library itself ships under that name.
prelude
The full embedded prelude — every module’s CODE concatenated (what apply_prelude prepends), documentation notes stripped. Identical bytes on every target (include_str! is compile-time).
prelude_module_sources
The RAW embedded stdlib module sources, ## Note documentation included — the seam the LSP reads literate docs from (teach::extract_literate_docs).
prelude_vocabulary
Every identifier the prelude defines (across all modules) — derived from the modules.
prune_unreachable_prelude
Drop ONLY the auto-imported stdlib PRELUDE functions that are never reachable, keeping EVERY user-defined function untouched. This is the chip-encoder analog of keep_reachable_functions, but safe for LOGOS’s non-name( call forms: apply_prelude splices a WHOLE module when the program mentions any of its names, so an UNCALLED prelude helper that reaches a host builtin (parseFloat, format, …) would make the flat chip image reject even though it never runs. This prunes those dead prelude helpers so the encoder lowers only what the program executes — with two safety differences from the C tree-shake: • it NEVER drops a user function (the prune set is exactly the prelude vocabulary), so a user procedure invoked by a LOGOS call form the call-only scan misses (Launch a task to worker, higher-order apply(f, …)) is always kept; • reachability uses the PERMISSIVE references (any use position — the same predicate apply_prelude splices on), so a prelude function reached via to <fn> / of <fn> / a value position is retained. The output is byte-identical to the input when nothing is pruned (no prelude, or all reachable), so the AOT/other hot paths are untouched. Over-pruning can only ever surface as a LOUD undefined reference.
prune_unreachable_prelude_from
prune_unreachable_prelude told which names the USER’s own (pre-prelude) source declares, so a name the stdlib also has is recognized as the user’s and kept. Callers that have the original source should prefer this; the bare form behaves as before.
prune_unreachable_prelude_mapped
prune_unreachable_prelude plus the KEPT byte ranges of the INPUT (merged and in order), so a consumer that parses the pruned text can remap an offset back to the original source. Deep check uses this to drop EXACTLY what largo build drops (it prunes the same way before it parses) while keeping every rustc finding mapped to the user’s own line. The ranges partition the kept output: concatenating source[s..e] for each (s, e) reproduces the pruned string.
prune_unreachable_prelude_mapped_from
prune_unreachable_prelude_from with the byte ranges of spliced it kept, as prune_unreachable_prelude_mapped reports them — for a caller that maps offsets in the pruned text back, and must still prune exactly as a build does.
prune_unreachable_prelude_mapped_with
prune_unreachable_prelude_mapped with the names the user’s own source declares, which are never eligible for pruning (see there).