# komal-ai417/cvm-plus-plus architecture

> Architecture diagram of the GitHub repository komal-ai417/cvm-plus-plus, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/komal-ai417/cvm-plus-plus
- Repository: https://github.com/komal-ai417/cvm-plus-plus

## Overview

#### Purpose and executable entry
CVM++ is a C++17 compiler and stack-based bytecode VM for a small integer language. The executable entry point is `src/main.cpp`; it runs an interactive REPL with persistent bindings, executes a `.cvm` script, or compiles-only with `--dump`. Build definition: `CMakeLists.txt`.

#### Front-end: source to AST
`src/lexer.cpp` / `src/lexer.h` converts source text into tokens, including comments, keywords, and decimal/hex/binary/octal integers. `src/parser.cpp` / `src/parser.h` is a recursive-descent parser that establishes precedence and language control structures, producing the typed AST defined in `src/ast.h` and implemented in `src/ast.cpp`. Errors cross this boundary through `src/error.h`.

#### AST and language representation
The AST uses a compile-time `NodeType` discriminator rather than RTTI. It represents declarations, lexical blocks, expressions, branching, loops, `break`/`continue`, printing, input, and update operators. Parser output is the compiler’s sole semantic input; no runtime parser or AST interpretation layer exists. Paths: `src/ast.h`, `src/token.h`, `src/parser.cpp`.

#### Compilation and optimization
`src/compiler.cpp` / `src/compiler.h` traverses the AST in a single pass and emits executable bytecode. It owns compile-time concerns: lexical scope and shadowing, variable-ID allocation, jump patching for branches/loops, short-circuit control flow, dead-code pruning, and constant folding for literal unary/binary/logical expressions. Its output is a `Chunk`, rather than VM-specific source structures.

#### Bytecode boundary
`src/opcode.h` defines the VM instruction set; `src/chunk.h` owns encoded instruction bytes, constants/operands, and RLE-compressed source-line mapping. This is the key compiler/VM contract: compiler emits compact opcodes and patched jump offsets, while the VM consumes only bytecode and associated metadata. Specialized opcodes and 16-bit variable IDs are part of this format.

#### Execution runtime
`src/vm.cpp` / `src/vm.h` executes a `Chunk` using a bounded stack and preallocated variable storage. It enforces stack overflow/underflow and arithmetic safety, handles stdin for the language `input` primitive, and writes `print` output. GCC/Clang builds use computed-goto/direct-threaded dispatch; other supported C++17 toolchains require compatible interpreter behavior. Portable signed right-shift behavior is implemented at this runtime boundary.

#### Inspection, modes, and validation
`src/disasm.cpp` / `src/disasm.h` decodes bytecode into human-readable instructions and resolved jump targets; `main` invokes it for `--dump` or enabled REPL disassembly. The REPL repeatedly drives lexer → parser → compiler → optional disassembler → VM, preserving valid state across failed later submissions. Regression programs in `tests/` exercise end-to-end language behavior; CI is defined in `.github/workflows/ci.yml`.

Main entry points: [`src/main.cpp`](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/main.cpp) (CLI / REPL), [`src/chunk.h`](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/chunk.h) (Chunk), [`src/lexer.cpp`](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/lexer.cpp) (Lexer), [`src/parser.cpp`](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/parser.cpp) (Parser)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Front end"]
  node_lexer["Lexer<br/>tokenizer<br/>[lexer.cpp]"]
  node_tokens["Tokens<br/>[token.h]"]
  node_parser["Parser<br/>recursive descent<br/>[parser.cpp]"]
  node_ast["Typed AST<br/>NodeType AST<br/>[ast.h]"]
  node_errors["Errors<br/>error boundary<br/>[error.h]"]
end

subgraph group_compiler["Compiler"]
  node_compiler_core["Compiler<br/>AST-to-bytecode<br/>[compiler.cpp]"]
end

subgraph group_bytecode["Bytecode contract"]
  node_opcodes["Opcode set<br/>instruction set<br/>[opcode.h]"]
  node_chunk[("Chunk<br/>bytecode container<br/>[chunk.h]")]
end

subgraph group_runtime["Runtime &amp; inspection"]
  node_vm["Virtual machine<br/>stack bytecode VM<br/>[vm.cpp]"]
  node_disasm["Disassembler<br/>bytecode inspector<br/>[disasm.cpp]"]
end

node_main["CLI / REPL<br/>executable entry<br/>[main.cpp]"]
node_build["CMake build<br/>build definition<br/>[CMakeLists.txt]"]
node_tests["Regression programs<br/>end-to-end tests<br/>[test_script.cvm]"]
node_ci["CI<br/>automation workflow<br/>[ci.yml]"]

node_build -->|"builds"| node_main
node_ci -->|"runs"| node_tests
node_tests -->|"exercise"| node_main
node_main -->|"source text"| node_lexer
node_lexer -->|"emits"| node_tokens
node_tokens -->|"consumed by"| node_parser
node_parser -->|"produces"| node_ast
node_lexer -.->|"reports"| node_errors
node_parser -.->|"reports"| node_errors
node_ast -->|"sole semantic input"| node_compiler_core
node_compiler_core -->|"selects"| node_opcodes
node_compiler_core -->|"emits and patches"| node_chunk
node_opcodes -->|"defines encoding"| node_chunk
node_chunk -->|"executes"| node_vm
node_main -->|"invokes"| node_vm
node_chunk -->|"decodes"| node_disasm
node_main -.->|"--dump / REPL"| node_disasm

click node_main "https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/main.cpp"
click node_lexer "https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/lexer.cpp"
click node_tokens "https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/token.h"
click node_parser "https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/parser.cpp"
click node_ast "https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/ast.h"
click node_errors "https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/error.h"
click node_compiler_core "https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/compiler.cpp"
click node_opcodes "https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/opcode.h"
click node_chunk "https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/chunk.h"
click node_vm "https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/vm.cpp"
click node_disasm "https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/disasm.cpp"
click node_build "https://github.com/komal-ai417/cvm-plus-plus/blob/main/CMakeLists.txt"
click node_tests "https://github.com/komal-ai417/cvm-plus-plus/blob/main/tests/test_script.cvm"
click node_ci "https://github.com/komal-ai417/cvm-plus-plus/blob/main/.github/workflows/ci.yml"

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_lexer,node_tokens,node_parser,node_ast,node_errors toneBlue
class node_compiler_core toneAmber
class node_opcodes,node_chunk toneMint
class node_vm,node_disasm toneRose
class node_main,node_build,node_tests,node_ci toneNeutral
```

## Components

### Front end

- [Lexer](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/lexer.cpp): tokenizer · `src/lexer.cpp`
- [Tokens](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/token.h): language tokens · `src/token.h`
- [Parser](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/parser.cpp): recursive descent · Builds precedence-aware syntax and control-flow constructs. · `src/parser.cpp`
- [Typed AST](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/ast.h): NodeType AST · Compiler-only semantic input; no AST interpreter exists. · `src/ast.h`
- [Errors](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/error.h): error boundary · `src/error.h`

### Compiler

- [Compiler](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/compiler.cpp): AST-to-bytecode · Scopes, IDs, folding, pruning, short-circuit flow, and patched jumps. · `src/compiler.cpp`

### Bytecode contract

- [Opcode set](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/opcode.h): instruction set · `src/opcode.h`
- [Chunk](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/chunk.h): bytecode container · Encoded instructions, operands/constants, and compressed line mapping. · `src/chunk.h`

### Runtime & inspection

- [Virtual machine](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/vm.cpp): stack bytecode VM · Bounded stack, preallocated variables, checked arithmetic, stdin, and print output. · `src/vm.cpp`
- [Disassembler](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/disasm.cpp): bytecode inspector · Decodes instructions and resolved jump targets. · `src/disasm.cpp`

### Other components

- [CLI / REPL](https://github.com/komal-ai417/cvm-plus-plus/blob/main/src/main.cpp): executable entry · Runs scripts, compile-only dumps, and persistent interactive submissions. · `src/main.cpp`
- [CMake build](https://github.com/komal-ai417/cvm-plus-plus/blob/main/CMakeLists.txt): build definition · `CMakeLists.txt`
- [Regression programs](https://github.com/komal-ai417/cvm-plus-plus/blob/main/tests/test_script.cvm): end-to-end tests · `tests/test_script.cvm`
- [CI](https://github.com/komal-ai417/cvm-plus-plus/blob/main/.github/workflows/ci.yml): automation workflow · `.github/workflows/ci.yml`

## Connections

- CMake build → CLI / REPL: builds
- CI → Regression programs: runs
- Regression programs → CLI / REPL: exercise
- CLI / REPL → Lexer: source text
- Lexer → Tokens: emits
- Tokens → Parser: consumed by
- Parser → Typed AST: produces
- Lexer → Errors: reports
- Parser → Errors: reports
- Typed AST → Compiler: sole semantic input
- Compiler → Opcode set: selects
- Compiler → Chunk: emits and patches
- Opcode set → Chunk: defines encoding
- Chunk → Virtual machine: executes
- CLI / REPL → Virtual machine: invokes
- Chunk → Disassembler: decodes
- CLI / REPL → Disassembler: --dump / REPL
