How komal-ai417/cvm-plus-plus works
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: main.cpp CLI / REPL · chunk.h Chunk · lexer.cpp Lexer · parser.cpp Parser
Components 14
Front end
Compiler
- Compiler · Scopes, IDs, folding, pruning, short-circuit flow, and patched jumps.
src/compiler.cpp
Bytecode contract
- Opcode set
src/opcode.h - Chunk · Encoded instructions, operands/constants, and compressed line mapping.
src/chunk.h
Runtime & inspection
- Virtual machine · Bounded stack, preallocated variables, checked arithmetic, stdin, and print output.
src/vm.cpp - Disassembler · Decodes instructions and resolved jump targets.
src/disasm.cpp
Other components
- CLI / REPL · Runs scripts, compile-only dumps, and persistent interactive submissions.
src/main.cpp - CMake build
CMakeLists.txt - Regression programs
tests/test_script.cvm - CI
.github/workflows/ci.yml
Connections 17
- 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