Skip to content

About

C++ interpreter for my own programming language RV

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

RV Interpreter

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.

Architecture

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"]
Loading

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.

Language Snapshot

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 / else control flow
  • while loops
  • Functions and return
  • Recursive function calls
  • List and string indexing
  • List and string element mutation
  • Builtins including append, remove, type, string, and size

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);

Repository Layout

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

Build

The project uses C++20.

Build the main CLI executable:

make main

This produces:

bin/main

Build the test executable:

make test

This produces:

bin/test

The default make target also builds bin/test.

Run RV Programs

Run a program with the default bytecode runtime:

./bin/main test_code/simple_add.rv

Run with the legacy tree evaluator:

./bin/main test_code/simple_add.rv --tree-evaluate

Print lexer output:

./bin/main test_code/simple_add.rv --output-lexer

Print parser output:

./bin/main test_code/simple_add.rv --output-parser

Print generated IR:

./bin/main test_code/simple_add.rv --output-ir

Flags can be combined:

./bin/main test_code/complex_function.rv --output-lexer --output-parser --output-ir

CLI Flags

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.

Test

Run the full test harness:

python3 tester.py

The test runner compiles bin/test and executes the RV programs in test_code/, comparing output against files in test_outputs/.

Run The Backend

The Flask backend expects the C++ executable to already exist.

Build the CLI first:

make main

Install Python dependencies:

pip3 install -r requirements.txt

Run the server:

flask run

The 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.

Documentation

Detailed docs:

Current preserved legacy docs:

Related Project

The interpreter can be used with a separate frontend visualizer:

https://github.com/rohanvan123/rv-interpreter-frontend

About

C++ interpreter for my own programming language RV

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages