# openttd/nml architecture

> Architecture diagram of the GitHub repository openttd/nml, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openttd/nml
- Repository: https://github.com/openttd/nml

## Overview

##### Main Components

a) Input Processing Layer:
- NML Source Files (.nml)
- Language Files (.lng)
- Graphics Files (.pcx)
- Sound Files (.wav)
- Custom Tags Files

b) Compiler Core:
- Parser Module
- Abstract Syntax Tree (AST)
- Expression Evaluator
- Action Generator

c) Output Generation Layer:
- GRF Encoder
- NFO Generator
- Optimization Engine
- Cache Manager

##### Component Relationships

**[Input Files] → [Parser/Lexer] → [AST] → [Action Generator] → [Output Encoders] → [Final Output]**

##### Specific Implementation Details

- nml/parser.py (Input parsing)
- nml/ast/* (Abstract Syntax Tree components)
- nml/actions/* (Game-specific action generation)
- nml/expression/* (Expression handling)
- nml/output_* (Different output formats)

##### Key Features to Highlight

a) Input Processing:
- Multiple file type support (nml, lng, pcx, wav)
- Custom tag processing
- Language file handling

b) Compilation Pipeline:
- Lexical analysis and parsing
- AST generation and manipulation
- Expression evaluation
- Action generation

c) Output Generation:
- GRF encoding with compression (lz77)
- NFO file generation
- Caching mechanism
- Multiple palette support

##### Additional Elements

Supporting components:
- Sprite cache system
- Language processing
- Error handling
- Optimization pipeline

Use the following symbols:
- Rectangles: Main processing modules
- Double lines: Primary data flow

Remember to show:
- Clear separation between compilation phases
- Data transformation flow
- Cache interaction points
- Optional processing paths (like optimization)
- Error handling paths

This will create a comprehensive visualization of NML's architecture that accurately represents its compiler nature while maintaining readability and technical accuracy.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Input
        NML[NML Source Files]:::input
        LNG[Language Files]:::input
        PCX[Graphics Files]:::input
        WAV[Sound Files]:::input
        TAG[Custom Tags]:::input
    end

    subgraph "Parser Layer"
        PARSER[Parser Module]:::parser
        STRINGS[String Processing]:::parser
    end

    subgraph "Core Compilation"
        AST[Abstract Syntax Tree]:::core
        EXPR[Expression Evaluator]:::core
        PALETTE[Palette Handler]:::core
    end

    subgraph "Action Generation"
        ACTION[Action Generator]:::action
        SPRITE[Sprite Encoder]:::action
    end

    subgraph "Output Processing"
        BASE[Base Output Handler]:::output
        GRF[GRF Encoder]:::output
        NFO[NFO Generator]:::output
        COMPRESS[Compression Engine]:::output
    end

    subgraph "Cache & Storage"
        CACHE[Cache Manager]:::storage
    end

    %% Input connections
    NML --> PARSER
    LNG --> STRINGS
    PCX --> SPRITE
    WAV --> ACTION
    TAG --> PARSER

    %% Parser layer connections
    PARSER --> AST
    STRINGS --> AST

    %% Core compilation connections
    AST --> EXPR
    EXPR --> ACTION
    PALETTE --> SPRITE

    %% Action generation connections
    ACTION --> BASE
    SPRITE --> CACHE
    CACHE --> BASE

    %% Output connections
    BASE --> GRF
    BASE --> NFO
    COMPRESS --> GRF

    %% Click events for component mapping
    click PARSER "https://github.com/OpenTTD/nml/blob/master/nml/parser.py"
    click AST "https://github.com/OpenTTD/nml/tree/master/nml/ast/"
    click EXPR "https://github.com/OpenTTD/nml/tree/master/nml/expression/"
    click ACTION "https://github.com/OpenTTD/nml/tree/master/nml/actions/"
    click GRF "https://github.com/OpenTTD/nml/blob/master/nml/output_grf.py"
    click NFO "https://github.com/OpenTTD/nml/blob/master/nml/output_nfo.py"
    click CACHE "https://github.com/OpenTTD/nml/blob/master/nml/spritecache.py"
    click COMPRESS "https://github.com/OpenTTD/nml/blob/master/nml/lz77.py"
    click SPRITE "https://github.com/OpenTTD/nml/blob/master/nml/spriteencoder.py"
    click STRINGS "https://github.com/OpenTTD/nml/blob/master/nml/grfstrings.py"
    click BASE "https://github.com/OpenTTD/nml/blob/master/nml/output_base.py"
    click PALETTE "https://github.com/OpenTTD/nml/blob/master/nml/palette.py"
    click NML "https://github.com/OpenTTD/nml/blob/master/examples/*/example_*.nml"
    click LNG "https://github.com/OpenTTD/nml/blob/master/examples/*/lang/*.lng"

    %% Styling
    classDef input fill:#6495ED,stroke:#333,stroke-width:2px
    classDef parser fill:#90EE90,stroke:#333,stroke-width:2px
    classDef core fill:#98FB98,stroke:#333,stroke-width:2px
    classDef action fill:#FFD700,stroke:#333,stroke-width:2px
    classDef output fill:#FFA500,stroke:#333,stroke-width:2px
    classDef storage fill:#D3D3D3,stroke:#333,stroke-width:2px

    %% Legend
    subgraph Legend
        L1[Input Components]:::input
        L2[Parser Components]:::parser
        L3[Core Components]:::core
        L4[Action Components]:::action
        L5[Output Components]:::output
        L6[Storage Components]:::storage
    end
```
