First-class templates keep structure in AST-local TIR, then hand folded strings or neutral runtime data downstream.

Why templates need their own temporary IR

Look at the page fragment again:

[$html:
    <p>[greet(visitor)]</p>
]

A template retains wrappers, slots, child values, conditionals, loops and output order. Its structure lets the checker validate composition and the runtime render selected content lazily.

Moth gives templates a first-class home and a temporary Template IR (TIR) that lives only while AST work runs.

Template structure

The fragment wants a paragraph wrapper, an interpolation of a string-producing call and a stable place in the page's fragment order.

If the compiler treated the body as blind concatenation, it could still emit characters on a happy path. It would struggle to:

Structure earns its keep.

TIR phases

One module AST build owns one TemplateIrStore. Parser emission writes text, expressions, child templates, slots, inserts, wrappers and control-flow roots directly into that store.

The phase sequence is:

Parsed -> Composed -> Formatted -> Finalized

Teaching names for the parts:

Exact view identity includes root, phase and value context. Structural walks follow each conditional's one body and each loop's header and body through that view, preserving effective overlay reads with structural fallback when no override exists. Consumers read through this authority rather than inventing parallel overlay stacks.

Folding requires Composed or later. AST-to-HIR handoff requires Finalized.

Template structure gets a real home for exactly as long as the checker needs it, then leaves nothing behind for later stages to misread.

Folded and runtime paths

Classification happens in one place. prepare_tir_view validates structurally reachable TIR and classifies the whole value as foldable, runtime or helper. In ConstRequired mode, validation includes retained conditional and loop bodies even when a selector is statically false. AST finalisation then consumes exactly that preparation rather than deciding again:

Compile-time fragments never live in HIR. HIR receives ordinary string and control-flow work for runtime pieces only. The TIR store drops before the completed AST leaves the stage. It is temporary scaffolding, not permanent structure. No TIR id tours a public interface or backend.

For the greeting project, a fully static visitor and prefix can collapse toward the familiar fragment. If visitor were dynamic input, the structure would still check at compile time while runtime handoff carried the moving parts.

Source-kind adapters

Moth template .mtf and Markdown .md files do not invent forever-parallel compilers. They adapt into ordinary synthetic content #String declarations.

Later ordering and folding treat both as ordinary compile-time constants. Return to the greeting project after that side path: @entry.moth remains the star, and document kinds prove the "one semantic pipeline" bet.

An .mtf body can read application data with an explicit qualified reference:

# Welcome to [@settings site_name]

That is an ordinary dependency reference. It creates a provider edge and a local ordering edge but installs no name. .mtf has no implicit same-directory facade constants. The fixed constant prelude from the Core @core/html and @core/css packages is the only bare-name scope, and root-local @mod, @package or @entry declarations stay invisible. Shared data belongs in an ordinary helper file. Both cycle directions are diagnosed by the normal Stage 0 and Stage 3 owners, never by a template-specific sorter.

Tooling that wants folded .mtf content asks the compiler's direct Moth template service. That service is a shorter path over the same owners, stopping before HIR. It is not a second parser, and project tooling never prepares, binds, orders or folds the template itself.

What comes back is folded content, not a finished artefact. It can still hold opaque resource and site-root anchors, because only a builder's link plan can resolve those. Output writing stays outside the service.

What can fail here

TIR ownership must stay distinct from AST finalisation and HIR validation. Post-TIR parser optimisation plans may discuss performance. They do not redefine the semantic handoff.

Why Moth does it this way

Alternative: general macros, JSX-style external transforms or a separate markup compiler with its own type system.

Choice: first-class constrained templates with AST-local TIR.

Benefit: template structure gains semantic checks without a second backend language.

Cost: TIR needs strict lifetime and handoff rules. It is not a public interface for users or backends.

Roadmap and current status

Exit state

You leave with finalised TIR handoff, folded fragments and neutral runtime template data. Next: how a failure should travel through all these owners.

Compiler design / Previous / Next