Skip to content

About

A minimal JavaScript engine written in C++ for learning purposes

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

vitosik-js

A minimal JavaScript engine written in C++ for learning purposes.

Features (Planned)

Tier 1: Calculator with Variables

  • Numbers and basic arithmetic (+, -, *, /)
  • Variables with let
  • Expression evaluation

Tier 2: Control Flow

  • Booleans and comparison operators
  • if/else statements
  • while loops

Tier 3: Functions

  • Function declarations
  • Parameters and return values
  • Simple scope (no closures)

Quick Start

Docker (Recommended)

# 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

Local Build

$ mkdir build && cd build
$ cmake ..
$ make
$ ./vitosik-js

Project Structure

vitosik-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

Architecture

Lexer (Tokenization)

Converts source code into tokens:

"let x = 5;" → [LET, IDENTIFIER(x), ASSIGN, NUMBER(5), SEMICOLON]

Parser (AST Construction)

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

  1. Assignment: =
  2. Equality: ==, !=
  3. Comparison: <, >
  4. Addition/Subtraction: +, -
  5. Multiplication/Division: *, /
  6. 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).

Interpreter (Tree-Walking)

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

Current Status

  • Lexer (tokenization)
  • Parser (AST construction)
  • Interpreter (AST evaluation)
  • Control flow (if, while)
  • Functions

About

A minimal JavaScript engine written in C++ for learning purposes

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages