# lfe/lfe architecture

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

- Interactive diagram: https://gitdiagram.com/lfe/lfe
- Repository: https://github.com/lfe/lfe

## Overview

##### Project Type & Purpose
- LFE (Lisp Flavoured Erlang) is both a compiler front-end and an interactive shell that translates Lisp syntax to Erlang. It allows developers to write Lisp-style code that is fully compatible with Erlang.
- The project supports several use-cases: compiling LFE code (via binaries such as lfec, lfedoc, and lfescript), running an interactive REPL, and providing documentation and examples for usage and development.

##### Analyzing the File Structure
- Top-Level Directories & Files:
- The “bin” directory contains the executable binaries (lfe, lfec, lfescript, etc.) that are used to run various aspects of the system.
- The “src” directory contains the core implementation of the LFE system in Erlang (.erl and .hrl files). Within src, there are files for the parser (lfe_parse.erl), evaluator (lfe_eval.erl and variants), macro system (lfe_macro.erl and associated headers), and other compiler components.
- “c_src” includes the C source code (e.g., lfeexec.c) that likely provides lower-level executable support or integration with the Erlang runtime.
- The “doc” directory holds extensive documentation files which describe various subsystems and usage details, including guides, man pages, and notes on language specifics.
- “dev” and “test” directories provide test cases and development-related code for ensuring quality and correct functionality.
- Other directories, such as “examples”, “emacs”, and configuration files (Makefile, rebar.config, Dockerfile, etc.) indicate support for development, build automation, CI/CD pipelines, and integrations.

- Architectural Patterns:
- The separation of compiler components (parser, evaluator, macro handling) in the “src” directory suggests a modular design.
- The existence of both higher-level shells (REPL, lfescript) and lower-level compilation (lfec, lfedoc) tools indicates a layered architecture that abstracts user interactions from backend compilation logic.
- The use of Makefile, Docker, and continuous integration (via .github/workflows) shows that build and deployment concerns are well encapsulated.

##### Detailed Breakdown
- At the top, represent the user interface components:
- REPL (bin/lfe)
- Command-line utilities (lfec, lfedoc, lfescript)
- Parsing: lfe_parse.erl and associated modules.
- Macro System: lfe_macro.erl, lfe_macro_include.erl, etc.
- Code Generation/Evaluation: lfe_codegen.erl, lfe_eval.erl and variants.
- C Integration block (c_src) that interacts with the Erlang runtime for low-level operations.
- Support/documentation section:
- Documentation (doc/ directory)
- Developer tools (dev, examples, testing)
- On the side or as a foundational layer, include the Build & Deployment components:
- Makefile, rebar.config, Dockerfile, and CI/CD (.github/workflows).
- Finally, at the base or as an external dependency, represent the Erlang VM which acts as the runtime.

## Diagram (Mermaid)

```mermaid
graph TD
    %% User Interface Layer
    subgraph "User Interface"
        REPL["REPL (lfe)"]
        Lfec["lfec"]
        Lfedoc["lfedoc"]
        Lfescript["lfescript"]
    end

    %% Compiler/Interpreter Layer
    subgraph "Compiler/Interpreter"
        Parser["Parser Module"]
        subgraph "Macro System"
            M1["lfe_macro.erl"]
            M2["lfe_macro_include.erl"]
            M3["lfe_macro_export.erl"]
            M4["lfe_macro_record.erl"]
            M5["lfe_macro_struct.erl"]
            M6["lfe_macro.hrl"]
        end
        subgraph "Evaluator"
            E1["lfe_eval.erl"]
            E2["lfe_eval_bits.erl"]
        end
        CodeGen["lfe_codegen.erl"]
    end

    %% C Integration Layer
    CI["C Integration (lfeexec.c)"]

    %% Erlang VM (Runtime)
    VM["Erlang VM"]

    %% Build & Deployment Support
    subgraph "Build & Deployment"
        B1["Makefile"]
        B2["Dockerfile"]
        B3["rebar.config"]
    end

    %% Testing & CI/CD Support
    subgraph "Testing & CI/CD"
        T1["test/"]
        T2[".github/workflows"]
    end

    %% Documentation & Developer Tools
    subgraph "Documentation & Developer Tools"
        D1["doc/"]
        D2["dev/"]
        D3["examples/"]
    end

    %% Main Data Flow
    REPL -->|"sends code"| Parser
    Lfec -->|"sends code"| Parser
    Lfedoc -->|"sends code"| Parser
    Lfescript -->|"sends code"| Parser

    Parser -->|"parses to"| M1
    M1 -->|"expands macros"| E1
    E1 -->|"evaluates to"| CodeGen
    E2 -->|"alternate eval"| CodeGen
    CodeGen -->|"generates executable"| VM
    CI -->|"bridges low-level execution"| VM

    %% Support Dependencies
    T1 -->|"triggers"| B1
    T2 -->|"triggers"| B1
    B1 -->|"builds binaries for"| REPL
    D1 -->|"documents"| Parser
    D2 -->|"aids development of"| Parser
    D3 -->|"provides examples for"| Parser

    %% Styles
    classDef user fill:#f9f,stroke:#333,stroke-width:2px;
    classDef compiler fill:#ccf,stroke:#333,stroke-width:2px;
    classDef c_integration fill:#fc9,stroke:#333,stroke-width:2px;
    classDef runtime fill:#f99,stroke:#333,stroke-width:2px;
    classDef build fill:#9f9,stroke:#333,stroke-width:2px;
    classDef support fill:#cff,stroke:#333,stroke-width:2px;

    class REPL,Lfec,Lfedoc,Lfescript user;
    class Parser,M1,M2,M3,M4,M5,M6,E1,E2,CodeGen compiler;
    class CI c_integration;
    class VM runtime;
    class B1,B2,B3 build;
    class D1,D2,D3 support;
    class T1,T2 support;

    %% Click Events for Components
    click REPL "https://github.com/lfe/lfe/tree/develop/bin/lfe"
    click Lfec "https://github.com/lfe/lfe/tree/develop/bin/lfec"
    click Lfedoc "https://github.com/lfe/lfe/tree/develop/bin/lfedoc"
    click Lfescript "https://github.com/lfe/lfe/tree/develop/bin/lfescript"
    click Parser "https://github.com/lfe/lfe/blob/develop/src/lfe_parse.erl"
    click E1 "https://github.com/lfe/lfe/blob/develop/src/lfe_eval.erl"
    click E2 "https://github.com/lfe/lfe/blob/develop/src/lfe_eval_bits.erl"
    click M1 "https://github.com/lfe/lfe/blob/develop/src/lfe_macro.erl"
    click M2 "https://github.com/lfe/lfe/blob/develop/src/lfe_macro_include.erl"
    click M3 "https://github.com/lfe/lfe/blob/develop/src/lfe_macro_export.erl"
    click M4 "https://github.com/lfe/lfe/blob/develop/src/lfe_macro_record.erl"
    click M5 "https://github.com/lfe/lfe/blob/develop/src/lfe_macro_struct.erl"
    click M6 "https://github.com/lfe/lfe/blob/develop/src/lfe_macro.hrl"
    click CodeGen "https://github.com/lfe/lfe/blob/develop/src/lfe_codegen.erl"
    click CI "https://github.com/lfe/lfe/blob/develop/c_src/lfeexec.c"
    click D1 "https://github.com/lfe/lfe/tree/develop/doc/"
    click D2 "https://github.com/lfe/lfe/tree/develop/dev/"
    click D3 "https://github.com/lfe/lfe/tree/develop/examples/"
    click T1 "https://github.com/lfe/lfe/tree/develop/test/"
    click T2 "https://github.com/lfe/lfe/tree/develop/.github/workflows"
    click B1 "https://github.com/lfe/lfe/tree/develop/Makefile"
    click B2 "https://github.com/lfe/lfe/tree/develop/Dockerfile"
    click B3 "https://github.com/lfe/lfe/blob/develop/rebar.config"
```
