How cococopi/corros works
Purpose and Entry Points
Corros is a dependency-free Rust implementation of a bytecode-compiled scripting language, plus a self-hosted implementation written in Corros. The native CLI runs scripts, starts an interactive REPL, dumps bytecode, and executes serialized bytecode. Primary entry points are src/main.rs (CLI), src/lib.rs (crate API), and src/repl.rs (interactive mode).
Source Loading and Module Boundary
Programs are read and expanded before compilation. adopt imports are resolved relative to source files, spliced into the token stream, and guarded against cycles by src/loader.rs. The language prelude is injected ahead of user code from lib/prelude.cor, making much of the standard library ordinary Corros source rather than Rust runtime code.
Front End: Lexing and Single-Pass Compilation
src/lexer.rs scans source into Corros tokens, including language-specific keywords, literals, operators, and comments. src/compiler.rs combines recursive-descent parsing with immediate bytecode emission: it manages scopes, globals/locals, assignment expressions, control-flow jumps, functions, closures, and upvalue capture. Compilation errors are represented and source-located through src/error.rs.
Bytecode Representation
src/chunk.rs defines executable chunks: instructions, constant pools, and bytecode disassembly used by --dump. src/bc.rs supports bytecode-oriented execution and is the boundary used by the self-hosting bootstrap flow (--run-bc). Compiler output is therefore independent of the source parser at runtime.
Runtime Model and VM
src/vm.rs is a stack-machine interpreter that executes chunks, manages call frames, globals, closures/upvalues, collection operations, and runtime traceback generation. Runtime objects and their semantics—numbers, strings, lists, maps, ranges, functions, closures, and native functions—are centralized in src/value.rs. This is the main host-language boundary: Rust owns memory/runtime representation while Corros code supplies higher-level behavior.
Native and Corros Standard Libraries
src/stdlib.rs registers host builtins for I/O, conversion, math, timing, assertions/errors, and primitives that require native access. lib/prelude.cor layers Corros-defined list, string, map, and range behavior above those primitives, including method dispatch. Thus method-heavy user programs generally flow through prelude code, with Rust as fallback for irreducible host operations.
Self-Hosting Bootstrap
The self-hosted compiler and VM are Corros programs at selfhost/compiler.cor and selfhost/vm.cor. selfhost/demo.sh uses the Rust implementation to compile both, then runs the compiled VM/compiler chain and compares produced bytecode for byte-identical results. This validates compatibility between the native compiler/VM and the language-level implementation.
Runtime and Infrastructure
The project is one Rust crate (Cargo.toml) requiring Rust 1.70+ for source builds and has no external dependencies. Distribution is via release binaries with source-build fallback in install.sh; there are no databases, network services, or web infrastructure in the runtime architecture.
Main entry points: main.rs Native CLI · loader.rs Source loader · chunk.rs Bytecode chunks · vm.rs Native VM
Components 17
Native Rust runtime
- Native CLI · Runs source, REPL, dumps, and serialized bytecode.
src/main.rs - Crate API
src/lib.rs - Interactive REPL
src/repl.rs - Source loader · Resolves adopt imports, injects the prelude, and rejects cycles.
src/loader.rs - Lexer
src/lexer.rs - Native compiler · Single-pass recursive-descent compiler with scope and closure handling.
src/compiler.rs - Source errors
src/error.rs - Bytecode chunks · Instructions, constants, and disassembly representation.
src/chunk.rs - Bytecode execution boundary · Supports --run-bc and bootstrap bytecode execution.
src/bc.rs - Native VM · Executes chunks with frames, globals, closures, and tracebacks.
src/vm.rs - Runtime values · Rust representations and semantics for language objects.
src/value.rs - Native standard library · Registers I/O, conversion, math, timing, and primitive operations.
src/stdlib.rs - Corros crate · Dependency-free Rust crate requiring Rust 1.70+.
Cargo.toml
Corros language library
- Language prelude · Defines collection behavior and method dispatch over native primitives.
lib/prelude.cor
Self-hosting bootstrap
- Self-hosted compiler
selfhost/compiler.cor - Self-hosted VM
selfhost/vm.cor - Bootstrap verifier · Compiles and runs the self-hosted chain, comparing bytecode exactly.
selfhost/demo.sh
Connections 21
- Corros crate → Native CLI: builds
- Native CLI → Interactive REPL: interactive mode
- Native CLI → Source loader: loads source
- Native CLI → Bytecode execution boundary: --run-bc
- Interactive REPL → Source loader: submits source
- Source loader → Language prelude: injects
- Source loader → Lexer: expanded source
- Lexer → Native compiler: tokens
- Native compiler → Bytecode chunks: emits
- Native compiler → Source errors: reports
- Bytecode chunks → Bytecode execution boundary: serialized bytecode
- Bytecode chunks → Native VM: executable chunk
- Bytecode execution boundary → Native VM: dispatches
- Native VM → Runtime values: operates on
- Native VM → Native standard library: calls builtins
- Language prelude → Native standard library: uses primitives
- Bootstrap verifier → Self-hosted compiler: compiles
- Bootstrap verifier → Self-hosted VM: compiles and runs
- Bootstrap verifier → Native CLI: uses native implementation
- Self-hosted compiler → Self-hosted VM: produces bytecode for
- Self-hosted VM → Bytecode chunks: compatibility target