Skip to main content

CodeGenerator

Trait CodeGenerator 

Source
pub trait CodeGenerator {
    // Required methods
    fn target(&self) -> &TargetProfile;
    fn generate_module(
        &self,
        module: &AIRModule,
    ) -> Result<GeneratedCode, CodegenError>;

    // Provided methods
    fn needs_ai_synthesis(&self, node: &AIRNode) -> bool { ... }
    fn entry_invocation(&self, main_is_async: bool) -> Option<String> { ... }
    fn generate_project(
        &self,
        modules: &[&AIRModule],
    ) -> Result<GeneratedCode, CodegenError> { ... }
}
Expand description

The trait all per-target code generators implement.

Each target (JS, TS, Python, Rust, Go) provides a struct that implements this trait. The generate_module method transforms a fully-lowered AIR module into target-specific source code.

Required Methods§

Source

fn target(&self) -> &TargetProfile

Returns the target profile for this generator.

Source

fn generate_module( &self, module: &AIRModule, ) -> Result<GeneratedCode, CodegenError>

Generates target code from a fully-lowered AIR module.

§Errors

Returns CodegenError if the module contains constructs that cannot be represented in the target language.

Provided Methods§

Source

fn needs_ai_synthesis(&self, node: &AIRNode) -> bool

Returns true when the given AIR node should go through Tier 1 AI synthesis (§17.2, Q3 amended).

The default implementation consults TargetProfile::ai_hints via crate::ai_synthesis::needs_ai_synthesis. Backends that want per-node overrides (e.g., only non-trivial match expressions) can override this method.

Source

fn entry_invocation(&self, main_is_async: bool) -> Option<String>

Returns the source-code snippet that invokes the user’s main function as the entry point for this target, or None if the target has a native entry-point convention (Rust fn main, Go func main) that runs without a synthetic call.

main_is_async is true when the user’s main function is declared async fn; targets with native async runtimes (JS, TS, Python) wrap the call in an event-loop driver in that case.

Targets that need a trailing invocation (JS, TS, Python) override this to return e.g. "main();\n". The default generate_project appends the snippet when any module declares a top-level main function.

Source

fn generate_project( &self, modules: &[&AIRModule], ) -> Result<GeneratedCode, CodegenError>

Generates target code from multiple AIR modules, producing a single concatenated output.

The default implementation generates each module separately and joins the results into one file. Generators that need to deduplicate preambles (e.g., Go’s package declaration) should override this method.

Implementors§