A minimal JavaScript engine written in C++ for learning purposes.
- Numbers and basic arithmetic (
+,-,*,/) - Variables with
let - Expression evaluation
- Booleans and comparison operators
if/elsestatementswhileloops
- Function declarations
- Parameters and return values
- Simple scope (no closures)
# Build and run
$ make all
# Or step by step:
$ make build # Build Docker image
$ make run # Run the program
$ make shell # Open interactive shell in container$ mkdir build && cd build
$ cmake ..
$ make
$ ./vitosik-jsvitosik-js/
├── include/ # Header files
│ ├── Token.h # Token types and Token class
│ ├── Lexer.h # Lexer (tokenizer)
│ ├── AST.h # AST node definitions
│ ├── Parser.h # Parser (recursive descent)
│ ├── ASTPrinter.h # Debug utility for printing AST
│ ├── Value.h # Runtime value representation
│ ├── Environment.h # Variable storage with scoping
│ └── Interpreter.h # Tree-walking interpreter
├── src/ # Implementation files
│ ├── main.cpp # Entry point
│ ├── Token.cpp
│ ├── Lexer.cpp
│ ├── AST.cpp
│ ├── Parser.cpp
│ ├── ASTPrinter.cpp
│ ├── Value.cpp
│ ├── Environment.cpp
│ └── Interpreter.cpp
├── CMakeLists.txt # Build configuration
├── Dockerfile # Docker container setup
├── Makefile # Convenience commands
└── README.md
Converts source code into tokens:
"let x = 5;" → [LET, IDENTIFIER(x), ASSIGN, NUMBER(5), SEMICOLON]
Recursive descent parser with precedence climbing. Converts tokens into an Abstract Syntax Tree (AST).
AST Nodes:
- Expressions:
NumberLiteral,Identifier,BinaryExpr,AssignmentExpr - Statements:
LetDeclaration,ExpressionStmt,BlockStmt - Program: Root node containing all statements
Operator Precedence (lowest to highest):
- Assignment:
= - Equality:
==,!= - Comparison:
<,> - Addition/Subtraction:
+,- - Multiplication/Division:
*,/ - Primary: literals, identifiers, parentheses
Example:
let z = x + y * 2;Parses to:
LetDeclaration {
name: "z"
initializer: BinaryExpr {
left: Identifier("x")
op: ADD
right: BinaryExpr {
left: Identifier("y")
op: MUL
right: NumberLiteral(2)
}
}
}
The parser uses the Visitor Pattern for AST traversal, making it easy to add new operations (evaluation, optimization, code generation).
Tree-walking interpreter that executes the AST.
Components:
- Value: Runtime value representation (currently supports numbers and undefined)
- Environment: Variable storage with lexical scoping support
- Interpreter: Visitor that walks the AST and evaluates expressions/executes statements
Features:
- Variable declaration with
let - Variable assignment
- Arithmetic operations (
+,-,*,/) - Comparison operations (
==,!=,<,>) - Nested scopes (via
BlockStmt)
Example:
let x = 5;
let y = 10;
let z = x + y * 2; // z = 25
z = z + 1; // z = 26
x + y + z; // Result: 41- Lexer (tokenization)
- Parser (AST construction)
- Interpreter (AST evaluation)
- Control flow (
if,while) - Functions