# avr34/ggvm architecture

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

- Interactive diagram: https://gitdiagram.com/avr34/ggvm
- Repository: https://github.com/avr34/ggvm

## Overview

GGVM is a stack-based virtual machine for executing a custom assembly language, including graphics instructions backed by the gg drawing library. The principal workflow is to provide an assembly file, tokenize it, expand imports and register labels, then execute instructions that manipulate stack and variables, branch, and draw or save images. The README documents local and system-wide imports and the planned instruction set; source excerpts show the CLI, tokenizer, VM lifecycle, dispatch, and memory structures. Implementation details for several graphics operations and the standard-library routines were not sampled.

Main entry points: [`internal/vm/core.go`](https://github.com/avr34/ggvm/blob/main/internal/vm/core.go) (Graphics operations), [`main.go`](https://github.com/avr34/ggvm/blob/main/main.go) (CLI entry), [`internal/vm/tokenizer/tokenizer.go`](https://github.com/avr34/ggvm/blob/main/internal/vm/tokenizer/tokenizer.go) (Tokenizer), [`internal/vm/vm.go`](https://github.com/avr34/ggvm/blob/main/internal/vm/vm.go) (VM lifecycle)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_input["Input and parsing"]
  node_cli["CLI entry<br/>[main.go]"]
  node_tokenizer["Tokenizer<br/>[tokenizer.go]"]
end

subgraph group_runtime["VM runtime"]
  node_vmstate["VM lifecycle<br/>[vm.go]"]
  node_imports["Import expansion<br/>[core.go]"]
  node_dispatch["Instruction dispatch<br/>[vm.go]"]
  node_coreops["Core operations<br/>[core.go]"]
  node_memory[("Stack and variables<br/>[mem.go]")]
  node_corecatalog["Core instruction set<br/>[Instructions.go]"]
end

subgraph group_graphics["Graphics output"]
  node_ggops["Graphics operations<br/>[core.go]"]
  node_ggcatalog["Graphics instruction set<br/>[Instructions.go]"]
  node_gglibrary["gg drawing library"]
  node_image["Saved image"]
end

subgraph group_libraries["Assembly libraries"]
  node_assembly["Assembly program"]
  node_stdlib["Math routines"]
  node_examples["Example programs"]
end

node_user(("Assembly user"))

node_user -->|"runs file"| node_cli
node_cli -->|"tokenizes source"| node_tokenizer
node_cli -->|"starts VM"| node_vmstate
node_vmstate -->|"expands imports"| node_imports
node_vmstate -->|"steps tokens"| node_dispatch
node_dispatch -->|"dispatches core"| node_coreops
node_dispatch -->|"dispatches graphics"| node_ggops
node_coreops -->|"reads and writes"| node_memory
node_ggops -->|"uses stack"| node_memory
node_tokenizer -->|"loads commands"| node_corecatalog
node_tokenizer -->|"loads commands"| node_ggcatalog
node_coreops -->|"executes opcodes"| node_corecatalog
node_ggops -.->|"executes opcodes"| node_ggcatalog
node_ggops -->|"draws with"| node_gglibrary
node_ggops -->|"saves output"| node_image
node_assembly -->|"provides source"| node_cli
node_imports -->|"imports routines"| node_stdlib
node_imports -->|"expands imports"| node_assembly
node_examples -.->|"contains programs"| node_assembly

click node_cli "https://github.com/avr34/ggvm/blob/main/main.go"
click node_tokenizer "https://github.com/avr34/ggvm/blob/main/internal/vm/tokenizer/tokenizer.go"
click node_vmstate "https://github.com/avr34/ggvm/blob/main/internal/vm/vm.go"
click node_imports "https://github.com/avr34/ggvm/blob/main/internal/vm/core.go"
click node_dispatch "https://github.com/avr34/ggvm/blob/main/internal/vm/vm.go"
click node_coreops "https://github.com/avr34/ggvm/blob/main/internal/vm/core.go"
click node_memory "https://github.com/avr34/ggvm/blob/main/internal/vm/mem/mem.go"
click node_corecatalog "https://github.com/avr34/ggvm/blob/main/internal/vm/core/Instructions.go"
click node_ggops "https://github.com/avr34/ggvm/blob/main/internal/vm/core.go"
click node_ggcatalog "https://github.com/avr34/ggvm/blob/main/internal/vm/ggwrapper/Instructions.go"
click node_stdlib "https://github.com/avr34/ggvm/tree/main/std/math"
click node_examples "https://github.com/avr34/ggvm/tree/main/examples/core"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_tokenizer,node_user toneBlue
class node_vmstate,node_imports,node_dispatch,node_coreops,node_memory,node_corecatalog toneAmber
class node_ggops,node_ggcatalog,node_gglibrary,node_image toneMint
class node_assembly,node_stdlib,node_examples toneRose
```

## Components

### Input and parsing

- [CLI entry](https://github.com/avr34/ggvm/blob/main/main.go): `main.go`
- [Tokenizer](https://github.com/avr34/ggvm/blob/main/internal/vm/tokenizer/tokenizer.go): `internal/vm/tokenizer/tokenizer.go`

### VM runtime

- [VM lifecycle](https://github.com/avr34/ggvm/blob/main/internal/vm/vm.go): `internal/vm/vm.go`
- [Import expansion](https://github.com/avr34/ggvm/blob/main/internal/vm/core.go): `internal/vm/core.go`
- [Instruction dispatch](https://github.com/avr34/ggvm/blob/main/internal/vm/vm.go): `internal/vm/vm.go`
- [Core operations](https://github.com/avr34/ggvm/blob/main/internal/vm/core.go): `internal/vm/core.go`
- [Stack and variables](https://github.com/avr34/ggvm/blob/main/internal/vm/mem/mem.go): `internal/vm/mem/mem.go`
- [Core instruction set](https://github.com/avr34/ggvm/blob/main/internal/vm/core/Instructions.go): `internal/vm/core/Instructions.go`

### Graphics output

- [Graphics operations](https://github.com/avr34/ggvm/blob/main/internal/vm/core.go): `internal/vm/core.go`
- [Graphics instruction set](https://github.com/avr34/ggvm/blob/main/internal/vm/ggwrapper/Instructions.go): `internal/vm/ggwrapper/Instructions.go`
- **gg drawing library**
- **Saved image**

### Assembly libraries

- **Assembly program**
- [Math routines](https://github.com/avr34/ggvm/tree/main/std/math): `std/math`
- [Example programs](https://github.com/avr34/ggvm/tree/main/examples/core): `examples/core`

### Other components

- **Assembly user**

## Connections

- Assembly user → CLI entry: runs file (evidence: `main.go`)
- CLI entry → Tokenizer: tokenizes source (evidence: `main.go`)
- CLI entry → VM lifecycle: starts VM (evidence: `main.go`)
- VM lifecycle → Import expansion: expands imports (evidence: `internal/vm/vm.go`)
- VM lifecycle → Instruction dispatch: steps tokens (evidence: `internal/vm/vm.go`)
- Instruction dispatch → Core operations: dispatches core (evidence: `internal/vm/vm.go`)
- Instruction dispatch → Graphics operations: dispatches graphics (evidence: `internal/vm/vm.go`)
- Core operations → Stack and variables: reads and writes (evidence: `internal/vm/core.go`)
- Graphics operations → Stack and variables: uses stack (evidence: `internal/vm/core.go`)
- Tokenizer → Core instruction set: loads commands (evidence: `internal/vm/tokenizer/tokenizer.go`)
- Tokenizer → Graphics instruction set: loads commands (evidence: `internal/vm/tokenizer/tokenizer.go`)
- Core operations → Core instruction set: executes opcodes (evidence: `internal/vm/core.go`)
- Graphics operations → Graphics instruction set: executes opcodes
- Graphics operations → gg drawing library: draws with (evidence: `README.md`)
- Graphics operations → Saved image: saves output (evidence: `README.md`)
- Assembly program → CLI entry: provides source (evidence: `main.go`)
- Import expansion → Math routines: imports routines (evidence: `README.md`)
- Import expansion → Assembly program: expands imports
- Example programs → Assembly program: contains programs
