# pannagaperumal/moxy architecture

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

- Interactive diagram: https://gitdiagram.com/pannagaperumal/moxy
- Repository: https://github.com/pannagaperumal/moxy

## Overview

1. **What this repo is**
   - Moxy is a **pure-Go scripting language runtime** for embedding in Go applications.
   - The core architecture is a classic language toolchain: **source text → lexer → parser → AST → compiler → bytecode VM**, with a separate **tree-walk evaluator** also present.

2. **Primary entry points**
   - `cmd/moxy/main.go` is the **CLI** entry point for running `.pb` scripts.
   - `internal/repl/repl.go` provides an **interactive shell** for manual execution.
   - The root-level `moxy.go` and `moxy/` artifacts suggest a **public embedding surface** for Go host applications.

3. **Frontend: turning text into a program**
   - `internal/token` defines the language’s token types.
   - `internal/lexer` converts raw source into tokens.
   - `internal/parser` builds the program structure, with separate files for **expressions, statements, literals, and helpers**.
   - The `ast/` package holds the parsed syntax tree nodes, including special forms like **for statements, hash literals, and index expressions**.

4. **Language model and syntax coverage**
   - The AST and parser structure indicate support for **variables, functions, conditionals, loops, arrays/maps, indexing, and literals**.
   - The examples and docs show the language is designed around **Go-like syntax** and 0-based indexing.

5. **Compiler path**
   - `internal/compiler` translates AST into bytecode.
   - `internal/code` defines the bytecode instruction set and encoding.
   - `internal/compiler/symbol_table.go` handles name resolution for locals, globals, and likely built-in symbols during compilation.

6. **Virtual machine runtime**
   - `internal/vm` executes compiled bytecode.
   - Key runtime pieces are separated into **frames**, **stack**, **opcode definitions**, and **operations**, which is a typical stack-based VM design.
   - This layer is the main high-performance execution path highlighted in the README.

7. **Runtime object system**
   - `types/` defines the runtime values the VM and evaluator work with: **objects, hashes, closures, compiled functions, and builtins**.
   - This package is the shared boundary between the language runtime and host-facing values.

8. **Alternative interpreter path**
   - `internal/evaluator` implements direct AST execution.
   - It has separate files for **expressions, statements, builtins, and helpers**, implying a tree-walking mode used for simpler execution, testing, or debugging alongside the VM pipeline.

9. **Embedding and sandbox boundary**
   - Moxy is intended to be embedded into a Go host, so the key boundary is between **host application code** and **script-exposed runtime values/functions**.
   - The `plugin_host` area and `examples/plugin_example/host.go` show a plugin-style integration model where scripts are treated as controlled extensions rather than full application code.

10. **Examples as architecture guides**
   - `examples/standard_examples/` demonstrates core language features and expected runtime behavior.
   - `examples/plugin_example/` demonstrates the host/plugin interaction path.
   - These examples are useful for understanding which subsystems are meant to work together in real deployments.

11. **Documentation and design intent**
   - `docs/DESIGN_SPEC.md`, `docs/VM_ARCHITECTURE.md`, and `docs/PLUGINS.md` indicate the repo is structured around a deliberately specified VM architecture and plugin model.
   - `docs/Moxy_ROADMAP.md` suggests the current focus is the VM/bytecode foundation, with additional standard library and concurrency features planned later.

12. **How to draw the architecture**
   - A good diagram should show two main execution paths:
     - **Compilation path:** source → lexer/parser → AST → compiler → bytecode → VM → runtime objects/results
     - **Interpretation path:** source → lexer/parser → AST → evaluator → runtime objects/results
   - Surround these with the **CLI**, **REPL**, and **host embedding/plugin boundary** as the main entry/use cases.

Main entry points: [`internal/vm/vm.go`](https://github.com/pannagaperumal/moxy/blob/master/internal/vm/vm.go) (VM), [`internal/compiler/compiler.go`](https://github.com/pannagaperumal/moxy/blob/master/internal/compiler/compiler.go) (Compiler), [`internal/lexer/lexer.go`](https://github.com/pannagaperumal/moxy/blob/master/internal/lexer/lexer.go) (Lexer), [`internal/evaluator/evaluator.go`](https://github.com/pannagaperumal/moxy/blob/master/internal/evaluator/evaluator.go) (Evaluator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entrypoints["Entry points"]
  node_cli["CLI<br/>command<br/>[main.go]"]
  node_repl(("REPL<br/>interactive shell<br/>[repl.go]"))
  node_host_api["Host API<br/>embedding surface<br/>[moxy.go]"]
  node_public_pkg["moxy pkg<br/>public package"]
end

subgraph group_frontend["Language frontend"]
  node_token["Tokens<br/>[token.go]"]
  node_lexer["Lexer<br/>scanner<br/>[lexer.go]"]
  node_parser["Parser<br/>[parser.go]"]
  node_ast["AST<br/>syntax tree<br/>[ast.go]"]
end

subgraph group_runtime["Runtime core"]
  node_compiler["Compiler<br/>[compiler.go]"]
  node_symbols["Symbols<br/>name resolution<br/>[symbol_table.go]"]
  node_code["Bytecode<br/>instruction set<br/>[code.go]"]
  node_vm["VM<br/>[vm.go]"]
  node_frame["Frames<br/>[frame.go]"]
  node_stack["Stack<br/>[stack.go]"]
  node_evaluator["Evaluator<br/>tree-walk interpreter<br/>[evaluator.go]"]
  node_types[("Runtime Types<br/>value system<br/>[object.go]")]
end

subgraph group_embedding["Embedding boundary"]
  node_plugin_host["Plugin Host<br/>integration boundary"]
end

subgraph group_examples["Examples"]
  node_std_examples["Std Examples<br/>scripts"]
  node_plugin_example["Plugin Example<br/>host demo<br/>[host.go]"]
end

subgraph group_docs["Docs"]
  node_docs_arch["Design Docs<br/>architecture docs<br/>[VM_ARCHITECTURE.md]"]
end

node_cli -->|"runs"| node_lexer
node_repl -->|"feeds"| node_lexer
node_host_api -->|"embeds"| node_compiler
node_host_api -->|"embeds"| node_evaluator
node_lexer -->|"produces"| node_token
node_lexer -->|"streams"| node_parser
node_token -->|"consumed by"| node_parser
node_parser -->|"builds"| node_ast
node_ast -->|"compiled by"| node_compiler
node_ast -->|"executed by"| node_evaluator
node_compiler -->|"resolves"| node_symbols
node_compiler -->|"emits"| node_code
node_code -->|"loads"| node_vm
node_symbols -->|"supports"| node_vm
node_vm -->|"uses"| node_frame
node_vm -->|"uses"| node_stack
node_vm -->|"creates"| node_types
node_evaluator -->|"creates"| node_types
node_plugin_host -->|"exposes"| node_types
node_plugin_example -->|"demonstrates"| node_plugin_host
node_std_examples -->|"exercises"| node_compiler
node_std_examples -->|"exercises"| node_evaluator
node_docs_arch -->|"describes"| node_vm

click node_cli "https://github.com/pannagaperumal/moxy/blob/master/cmd/moxy/main.go"
click node_repl "https://github.com/pannagaperumal/moxy/blob/master/internal/repl/repl.go"
click node_host_api "https://github.com/pannagaperumal/moxy/blob/master/moxy.go"
click node_public_pkg "https://github.com/pannagaperumal/moxy/tree/master/moxy"
click node_token "https://github.com/pannagaperumal/moxy/blob/master/internal/token/token.go"
click node_lexer "https://github.com/pannagaperumal/moxy/blob/master/internal/lexer/lexer.go"
click node_parser "https://github.com/pannagaperumal/moxy/blob/master/internal/parser/parser.go"
click node_ast "https://github.com/pannagaperumal/moxy/blob/master/ast/ast.go"
click node_compiler "https://github.com/pannagaperumal/moxy/blob/master/internal/compiler/compiler.go"
click node_symbols "https://github.com/pannagaperumal/moxy/blob/master/internal/compiler/symbol_table.go"
click node_code "https://github.com/pannagaperumal/moxy/blob/master/internal/code/code.go"
click node_vm "https://github.com/pannagaperumal/moxy/blob/master/internal/vm/vm.go"
click node_frame "https://github.com/pannagaperumal/moxy/blob/master/internal/vm/frame.go"
click node_stack "https://github.com/pannagaperumal/moxy/blob/master/internal/vm/stack.go"
click node_evaluator "https://github.com/pannagaperumal/moxy/blob/master/internal/evaluator/evaluator.go"
click node_types "https://github.com/pannagaperumal/moxy/blob/master/types/object.go"
click node_plugin_host "https://github.com/pannagaperumal/moxy/tree/master/plugin_host"
click node_std_examples "https://github.com/pannagaperumal/moxy/tree/master/examples/standard_examples"
click node_plugin_example "https://github.com/pannagaperumal/moxy/blob/master/examples/plugin_example/host.go"
click node_docs_arch "https://github.com/pannagaperumal/moxy/blob/master/docs/VM_ARCHITECTURE.md"

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_repl,node_host_api,node_public_pkg toneBlue
class node_token,node_lexer,node_parser,node_ast toneAmber
class node_compiler,node_symbols,node_code,node_vm,node_frame,node_stack,node_evaluator,node_types toneMint
class node_plugin_host toneRose
class node_std_examples,node_plugin_example toneIndigo
class node_docs_arch toneTeal
```

## Components

### Entry points

Ways to run or embed Moxy

- [CLI](https://github.com/pannagaperumal/moxy/blob/master/cmd/moxy/main.go): command · Runs .pb scripts from the command line · `cmd/moxy/main.go`
- [REPL](https://github.com/pannagaperumal/moxy/blob/master/internal/repl/repl.go): interactive shell · Interactive execution loop · `internal/repl/repl.go`
- [Host API](https://github.com/pannagaperumal/moxy/blob/master/moxy.go): embedding surface · Public Go-facing entry point for embedding · `moxy.go`
- [moxy pkg](https://github.com/pannagaperumal/moxy/tree/master/moxy): public package · Additional exported embedding surface or package artifacts · `moxy`

### Language frontend

Source parsing and AST construction

- [Tokens](https://github.com/pannagaperumal/moxy/blob/master/internal/token/token.go): syntax tokens · Language token definitions · `internal/token/token.go`
- [Lexer](https://github.com/pannagaperumal/moxy/blob/master/internal/lexer/lexer.go): scanner · Converts source text into tokens · `internal/lexer/lexer.go`
- [Parser](https://github.com/pannagaperumal/moxy/blob/master/internal/parser/parser.go): parser · Builds statements and expressions from tokens · `internal/parser/parser.go`
- [AST](https://github.com/pannagaperumal/moxy/blob/master/ast/ast.go): syntax tree · Parsed program node model · `ast/ast.go`

### Runtime core

Compilation, bytecode, and execution

- [Compiler](https://github.com/pannagaperumal/moxy/blob/master/internal/compiler/compiler.go): bytecode compiler · Translates AST into bytecode · `internal/compiler/compiler.go`
- [Symbols](https://github.com/pannagaperumal/moxy/blob/master/internal/compiler/symbol_table.go): name resolution · Resolves locals, globals, and builtins during compilation · `internal/compiler/symbol_table.go`
- [Bytecode](https://github.com/pannagaperumal/moxy/blob/master/internal/code/code.go): instruction set · Opcode encoding and instruction definitions · `internal/code/code.go`
- [VM](https://github.com/pannagaperumal/moxy/blob/master/internal/vm/vm.go): stack vm · Executes compiled bytecode · `internal/vm/vm.go`
- [Frames](https://github.com/pannagaperumal/moxy/blob/master/internal/vm/frame.go): call frames · Runtime call frame management · `internal/vm/frame.go`
- [Stack](https://github.com/pannagaperumal/moxy/blob/master/internal/vm/stack.go): operand stack · VM stack storage and manipulation · `internal/vm/stack.go`
- [Evaluator](https://github.com/pannagaperumal/moxy/blob/master/internal/evaluator/evaluator.go): tree-walk interpreter · Direct AST execution path · `internal/evaluator/evaluator.go`
- [Runtime Types](https://github.com/pannagaperumal/moxy/blob/master/types/object.go): value system · Shared objects, functions, closures, hashes, and builtins · `types/object.go`

### Embedding boundary

Host integration and plugin-oriented control

- [Plugin Host](https://github.com/pannagaperumal/moxy/tree/master/plugin_host): integration boundary · Controlled host/plugin execution model · `plugin_host`

### Examples

Scripts and host demos that exercise the architecture

- [Std Examples](https://github.com/pannagaperumal/moxy/tree/master/examples/standard_examples): scripts · Core language and runtime examples · `examples/standard_examples`
- [Plugin Example](https://github.com/pannagaperumal/moxy/blob/master/examples/plugin_example/host.go): host demo · Host application plus script plugin example · `examples/plugin_example/host.go`

### Docs

Architecture and design references

- [Design Docs](https://github.com/pannagaperumal/moxy/blob/master/docs/VM_ARCHITECTURE.md): architecture docs · VM and plugin architecture specifications · `docs/VM_ARCHITECTURE.md`

## Connections

- CLI → Lexer: runs · Command-line execution starts the language pipeline
- REPL → Lexer: feeds · Interactive input enters the same frontend pipeline
- Host API → Compiler: embeds · Host applications can compile scripts through the public API
- Host API → Evaluator: embeds · Host applications can also drive tree-walk execution
- Lexer → Tokens: produces · Source text is tokenized
- Lexer → Parser: streams · Tokens flow into parsing
- Tokens → Parser: consumed by · Parser relies on token kinds and literals
- Parser → AST: builds · Parser constructs the syntax tree
- AST → Compiler: compiled by · AST is the compiler input
- AST → Evaluator: executed by · AST is also the interpreter input
- Compiler → Symbols: resolves · Compilation uses symbol resolution
- Compiler → Bytecode: emits · Compiler outputs bytecode instructions
- Bytecode → VM: loads · VM consumes encoded instructions
- Symbols → VM: supports · Resolved names are manifested as runtime operands and globals
- VM → Frames: uses · Execution is organized by frames
- VM → Stack: uses · Stack-based evaluation and operand storage
- VM → Runtime Types: creates · VM produces and manipulates runtime values
- Evaluator → Runtime Types: creates · Tree-walk execution produces the same runtime values
- Plugin Host → Runtime Types: exposes · Plugins interact with host-controlled runtime values
- Plugin Example → Plugin Host: demonstrates · Example host shows the plugin integration path
- Std Examples → Compiler: exercises · Standard examples validate the compiled execution path
- Std Examples → Evaluator: exercises · Examples also reflect interpreter-supported language features
- Design Docs → VM: describes · Architecture docs explain the bytecode runtime design
