# xnacly/purple-garden architecture

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

- Interactive diagram: https://gitdiagram.com/xnacly/purple-garden
- Repository: https://github.com/xnacly/purple-garden

## Overview

##### Project type and purpose
- purple_garden is a small Lisp‐inspired language toolchain implemented in C. It functions as both a compiler (front end + code generator) and an interpreter (virtual machine), optimized for performance.

##### File structure
- Top‐level modules (each backed by a pair of `.c/.h` files):
- io.{c,h}        – file reading (uses mmap)
- lexer.{c,h}     – tokenizes source into a token stream
- parser.{c,h}    – parses tokens into an AST
- cc.{c,h}        – “compiler” that lowers AST to bytecode + global constant pool
- vm.{c,h}        – register‐based virtual machine that executes bytecode
- builtins.{c,h}  – built‐in functions (@println, @len, etc.) mapped at compile time
- mem.{c,h}       – memory allocators (bump allocator for front end, GC/block for VM)
- common.{c,h}, strings.{c,h} – shared data structures (String windows, hashing)
- Entry point: main.c calls into each module in a linear pipeline.
- Utilities & configuration: Makefile, flake.nix, docs/, examples/, tests/.

##### README insights
- Describes a 4‐stage pipeline: IO → lex → parse → compile → VM run (with optional disassemble or AOT flags).
- Highlights optimizations: zero‐copy strings, bump allocators, computed goto in lexer, register‐based VM, hash interned identifiers at lex time.
- Provides performance benchmarks, disassembler output, release/test workflows.

## Diagram (Mermaid)

```mermaid
flowchart TB
  %% Front End Layer
  subgraph "Front End"
    io["IO Subsystem\n(mmap reader)"]:::frontend
    lexer["Lexer\n(computed goto)"]:::frontend
    parser["Parser\n(AST builder)"]:::frontend
    common["Shared Data Structures\n(common.c/h,\nstrings.c/h)"]:::frontend
  end

  %% Middle End Layer
  subgraph "Middle End"
    cc["Compiler\n(AST → bytecode + pool)"]:::middle
    constant_pool["Global Constant Pool\n(symbol table)"]:::storage
    builtins["Built-in Functions Map"]:::middle
    mem["Memory Allocators\n(front-end bump; VM GC)"]:::middle
    aot["AOT Generator\n(flag -a)"]:::middle
  end

  %% Back End Layer
  subgraph "Back End"
    vm["Virtual Machine\n(register engine)"]:::backend
  end

  %% Utilities & Tests
  subgraph "Utilities & Tests"
    main["CLI / Main Module"]:::util
    disasm["Disassembler\n(flag -d)"]:::util
    tests["Testing Harness"]:::util
    build["Build & Configuration"]:::util
    docs["Documentation"]:::util
    examples["Example Programs"]:::util
  end

  %% Data Flow
  main -->|"read file"| io
  io -->|"input string"| lexer
  lexer -->|"tokens"| parser
  parser -->|"AST"| cc
  cc -->|"bytecode + pool info"| vm
  cc -->|"generate pool entries"| constant_pool
  cc -->|"resolve @builtin"| builtins
  cc -->|"allocate bytecode & pool"| mem
  vm -->|"allocate state & values"| mem
  vm -->|"dispatch BUILTIN"| builtins
  vm -->|"program output"| main

  %% Control Flags (dashed)
  main -.->|"-d"| disasm
  main -.->|"-a"| aot
  aot -->|"AOT functions"| vm

  %% Auxiliary
  tests -.-> main
  build -.-> main
  docs -.-> main
  examples -.-> io

  %% Click Events
  click main "https://github.com/xnacly/purple-garden/blob/master/main.c"
  click io "https://github.com/xnacly/purple-garden/blob/master/io.c"
  click lexer "https://github.com/xnacly/purple-garden/blob/master/lexer.c"
  click parser "https://github.com/xnacly/purple-garden/blob/master/parser.c"
  click common "https://github.com/xnacly/purple-garden/blob/master/common.c"
  click cc "https://github.com/xnacly/purple-garden/blob/master/cc.c"
  click constant_pool "https://github.com/xnacly/purple-garden/blob/master/cc.c"
  click builtins "https://github.com/xnacly/purple-garden/blob/master/builtins.c"
  click mem "https://github.com/xnacly/purple-garden/blob/master/mem.c"
  click vm "https://github.com/xnacly/purple-garden/blob/master/vm.c"
  click tests "https://github.com/xnacly/purple-garden/blob/master/tests/test.c"
  click build "https://github.com/xnacly/purple-garden/tree/master/Makefile"
  click docs "https://github.com/xnacly/purple-garden/blob/master/docs/design.md"
  click examples "https://github.com/xnacly/purple-garden/tree/master/examples/"

  %% Styles
  classDef frontend fill:#cce5ff,stroke:#333,stroke-width:1px
  classDef middle fill:#d4edda,stroke:#333,stroke-width:1px
  classDef backend fill:#ffe5b4,stroke:#333,stroke-width:1px
  classDef util fill:#e2e3e5,stroke:#333,stroke-width:1px
  classDef storage fill:#f8d7da,stroke:#333,stroke-width:1px
```
