Pure Vanilla JS Zero Dependencies Stack-Based VM

See how code becomes instructions.

CalcLang compiles an arithmetic language through a complete five-stage pipeline down to custom stack bytecode, executing in an isolated virtual machine without shortcuts.

Ready
Source Code CalcLang Script
Lines: 2 • Chars: 38
Status: Ready
Press Run Program to execute and see printed values here.
Virtual Machine State

CalcLang Language Guide

CalcLang is an intentionally focused, deterministic arithmetic language engineered to teach core compiler concepts.

1. Statements & Scope

Every statement must terminate with a semicolon (;). CalcLang operates with a single global scope:

  • let <id> = <expr>;
    Declares a variable. Duplicate declarations are rejected by the semantic analyzer. A variable cannot reference itself in its own initializer.
  • print <expr>;
    Evaluates the expression and outputs the integer value followed by a newline.
  • // comment
    Single-line comments are ignored by the tokenizer.

2. Operator Precedence & Associativity

Calculations adhere to standard arithmetic rules. Division truncates toward zero:

Level Operator Associativity Semantics
1 (Highest) ( ) N/A Parentheses grouping
2 - (unary) Right-to-left Arithmetic negation
3 * , / Left-to-right Multiplication, Truncating Division
4 (Lowest) + , - Left-to-right Addition, Subtraction

3. Annotated Working Example

// CalcLang Demonstration Program
let radius = 7;
let piApprox = 22 / 7;       // Truncates toward zero: 3
let area = piApprox * radius * radius;
print area;                  // Outputs: 147

Pipeline Walkthrough:

  1. Tokenizer: Converts characters into 28 tokens, identifying keywords, identifiers, integers, and operators.
  2. Parser: Builds AST with 3 LetStatement nodes and 1 PrintStatement node using recursive descent.
  3. Semantic Analyzer: Confirms radius, piApprox, and area are defined before use and have unique names.
  4. Bytecode Generator: Emits 17 stack instructions: PUSH, LOAD, STORE, DIV, MUL, PRINT, and HALT.
  5. Virtual Machine: Executes each instruction against an operand stack, pushing intermediate results and outputting 147.

The Five-Stage Pipeline

Designed cleanly with separation of concerns. Every component can be explained by team members independently.

01

Tokenizer

Scans raw characters sequentially. Tracks 1-indexed line and column numbers. Skips whitespace and line comments. Rejects invalid characters immediately with precise coordinates.

02

Parser

Implements top-down recursive descent. Enforces grammar rules for statements, additive expressions, multiplicative expressions, unary negation, and grouped primaries.

03

Semantic Analyzer

Maintains a symbol table for the global scope. Verifies declaration order, prevents use of undeclared variables, prohibits duplicate declarations, and guards against self-referencing initializers.

04

Bytecode Generator

Translates the validated AST into a linear stream of stack instructions (PUSH, LOAD, STORE, ADD, SUB, MUL, DIV, NEG, PRINT) while preserving source locations for runtime error tracking.

05

Stack Virtual Machine

Simulates hardware execution with an operand stack and variable storage. Operates without eval(). Validates safe integer boundaries and catches division by zero while preserving any output generated prior to error.