RV is a small Python-like programming language implemented in C++. This repository contains the full RV execution pipeline: lexical analysis, parsing, AST construction, legacy tree-based evaluation, bytecode-style IR generation, and a virtual machine that executes the generated IR. It also includes a supported Flask backend for interpreter requests and is compatible with a live frontend visualizer deployment.
Live frontend:
https://rv-interpreter-frontend.vercel.app/
The current default runtime is the IR generator plus bytecode interpreter. The older tree evaluator is still supported through a command-line flag and remains useful as a simpler semantic reference while the VM evolves.
flowchart TD
Source["RV source file (.rv)"] --> Lexer["Lexer"]
Lexer --> Tokens["Token stream"]
Tokens --> Parser["Top-level parser"]
Parser --> ArithmeticParser["Arithmetic parser"]
ArithmeticParser --> AST["AST expressions"]
AST --> TreeEvaluator["Tree evaluator\nlegacy runtime"]
AST --> IRGenerator["IR generator"]
IRGenerator --> Tables["Const table\nIdentifier table\nFunction table"]
IRGenerator --> IR["IR instruction stream"]
Tables --> VM["Bytecode interpreter"]
IR --> VM
VM --> Output["Program output"]
At a high level:
- The lexer converts RV source text into typed tokens.
- The parser builds top-level AST nodes for statements such as variable assignments, loops, branches, and functions.
- The arithmetic parser handles expression parsing, including precedence, function calls, list literals, and indexing.
- The tree evaluator executes the AST directly.
- The IR generator lowers AST nodes into a compact instruction stream and supporting tables.
- The bytecode interpreter executes that IR with stack frames, register files, variable environments, and function return state.
- The Flask backend exposes the interpreter to the external visualizer.
RV currently supports:
- Integer, boolean, string, and list values
- Variable declaration with
let - Reassignment and compound assignment
- Arithmetic, comparison, equality, boolean, and unary operators
if/elsecontrol flowwhileloops- Functions and
return - Recursive function calls
- List and string indexing
- List and string element mutation
- Builtins including
append,remove,type,string, andsize
Example:
function fact(n) {
if (n == 1) {
return 1;
}
return n * fact(n - 1);
}
let values = [1, 2, 3];
values[1] = fact(4);
print(values);
src/
lexer.cpp Tokenizes RV source
parser.cpp Parses top-level language constructs
arithmetic_parser.cpp Parses expressions and operator precedence
tree_evaluator.cpp Legacy AST evaluator
ir_generator.cpp Lowers AST nodes into RV IR
interpreter.cpp Executes generated IR
value.cpp Runtime value model and operators
builtins.cpp Builtin RV functions
main.cpp CLI entry point
includes/ C++ headers
test_code/ RV programs used by tests
test_outputs/ Expected lexer/parser/runtime outputs
docs/legacy/ Preserved older documentation
app.py Flask API backend
tester.py Python test harness
Makefile Primary local build commands
CMakeLists.txt Alternate CMake build definition
The project uses C++20.
Build the main CLI executable:
make mainThis produces:
bin/main
Build the test executable:
make testThis produces:
bin/test
The default make target also builds bin/test.
Run a program with the default bytecode runtime:
./bin/main test_code/simple_add.rvRun with the legacy tree evaluator:
./bin/main test_code/simple_add.rv --tree-evaluatePrint lexer output:
./bin/main test_code/simple_add.rv --output-lexerPrint parser output:
./bin/main test_code/simple_add.rv --output-parserPrint generated IR:
./bin/main test_code/simple_add.rv --output-irFlags can be combined:
./bin/main test_code/complex_function.rv --output-lexer --output-parser --output-ir| Flag | Description |
|---|---|
--output-lexer |
Prints tokens grouped by source line. |
--output-parser |
Prints the parsed AST expression sequence. |
--output-ir |
Prints the generated IR instruction stream. |
--tree-evaluate |
Executes the AST directly with the legacy tree evaluator instead of the bytecode interpreter. |
Without --tree-evaluate, execution uses the IR generator and bytecode interpreter.
Run the full test harness:
python3 tester.pyThe test runner compiles bin/test and executes the RV programs in test_code/, comparing output against files in test_outputs/.
The Flask backend expects the C++ executable to already exist.
Build the CLI first:
make mainInstall Python dependencies:
pip3 install -r requirements.txtRun the server:
flask runThe main API endpoint is:
POST /interpret
The backend writes incoming source lines to backend_code/user_script.rv, runs bin/main with debug output flags, and returns lexer, parser, IR, and program-output data to the caller.
Detailed docs:
- RV language reference
- RV grammar
- IR instruction reference
- Lexer internals
- Parser internals
- Arithmetic parser internals
- Tree evaluator internals
- IR generator internals
- Bytecode interpreter internals
Current preserved legacy docs:
The interpreter can be used with a separate frontend visualizer:
https://github.com/rohanvan123/rv-interpreter-frontend