@gram-lang/modules
Resolves and composes multi-file @use import directives into a single unified AST before compilation. This package handles dependency graph traversal, cycle detection, hygienic intermediate variable renaming, yield-based scaling, and stocked (--stock) leaf integration.
Like all packages in the core pipeline, @gram-lang/modules is pure and environment-agnostic: it defines the ModuleHost interface for reading files and resolving paths, but never touches the filesystem directly.
loadModuleGraph
Section titled “loadModuleGraph”Loads the full transitive import graph starting at entryUri. Performs a single depth-first search (DFS) pass:
- Every URI is read and parsed at most once (diamond dependencies are loaded once).
- Import cycles (
A → B → A) are detected via the current DFS traversal stack and recorded as non-fatalMODULE_CYCLEdiagnostics. - Traversal depth exceeding
maxDepth(default32) emits aMODULE_DEPTH_EXCEEDEDdiagnostic. - Returns a topological
order(leaves first), which ensures dependencies are measured and composed before their importers.
ModuleHost
Section titled “ModuleHost”The seam between @gram-lang/modules and the host environment (filesystem, virtual memory, language server buffer, or web playground).
createMemoryHost
Section titled “createMemoryHost”Creates an in-memory ModuleHost backed by a dictionary or Map of URI paths to .gram source strings. Supports standard POSIX relative paths (./, ../) and project-root @/... paths. Used by the browser Playground and unit tests.
composeRecipe
Section titled “composeRecipe”Composes a loaded ModuleGraph into a single, self-contained RecipeAST:
- Topological Traversal: Visits modules in leaves-first
graph.order. - Export Discovery: Identifies public section-level exports via
computeExports. - Yield Measurement: Pre-analyzes each module once using the provided
options.dbto measure physical yield (resolveYield). - Proportional Scaling: Computes the scale factor (
computeScaleFactor) based on the importer’s requested quantities relative to the module’s yield. - Hygienic Renaming: Clones and prefixes internal intermediate variables (
&dough→&crust$dough) to avoid naming collisions across files. - Stock Mode (
--stock): For URIs listed inoptions.stock, omits preparation steps from the timeline and registers synthetic ingredient records (syntheticIngredients) carrying the module’s true mass and nutritional values.
ComposeOptions
Section titled “ComposeOptions”ComposeResult
Section titled “ComposeResult”finalizeComposed
Section titled “finalizeComposed”Decorates @gram-lang/kitchen’s CompilationResult with module metadata:
- Attaches
modules: ModuleInfo[]to the root result. - Tags each section with its originating module descriptor (
section.module: { binding, uri, title, mode }). - Deduplicates warning diagnostics between graph loading, composition, and compilation.
Secondary utilities
Section titled “Secondary utilities”Export analysis & scaling
Section titled “Export analysis & scaling”computeExports: Extracts section-level->&declarations and deterministically identifies the default export.resolveYield: Recursively measures the total physical mass (in grams) of an exported section and any intermediate sections it references.computeScaleFactor: Evaluates the ratio between requested reference quantities in the host recipe and the module’s yield.
Reverse dependency indexing
Section titled “Reverse dependency indexing”Used by gram watch and the Language Server (@gram-lang/language-server) to perform incremental diagnostics and cache invalidation when a shared sub-recipe is edited.
Module diagnostics
Section titled “Module diagnostics”warningSeverityOf(code): Unified lookup resolving severity ("error" | "warning" | "info") across both@gram-lang/kitchenand@gram-lang/moduleswarning codes.allWarningInfo: Full list of diagnostic codes, severities, and message templates.