Skip to main content

A lightweight JavaScript interpreter implemented in Python.

Project description

JavaScript Interpreter

A lightweight JavaScript interpreter implemented in Python.

This project reads JavaScript-style source code, turns it into tokens, builds an Abstract Syntax Tree (AST), and evaluates that AST step by step inside a runtime environment.

What This Interpreter Does

  • Parses JavaScript-like source code
  • Evaluates expressions, variables, and function calls
  • Supports control flow: if, while, do...while, for, break, continue, return
  • Supports let, const, and var declarations
  • Handles arrays, objects, string values, numbers, booleans, null, and undefined
  • Provides built-in APIs: console.log, Math, and Date
  • Offers an interactive REPL with debug helpers

How It Works: Input to Output

  1. Source code is loaded from a file or typed into the REPL.
  2. lexer.py reads the characters and converts them into a stream of tokens.
    • Example token types: IDENTIFIER, NUMBER, STRING, PLUS, LPAREN, SEMICOLON
  3. parser.py uses a Pratt parser to convert tokens into an AST.
    • The AST is a tree structure representing the program.
    • Example nodes: Program, VariableStatement, InfixExpression, FunctionDeclaration, CallExpression
  4. evaluator.py walks the AST and executes each node.
    • Expressions are evaluated, statements are executed, and values are returned.
    • A runtime environment keeps track of variables, scopes, and functions.
  5. environment.py manages scope chains and variable lookup.
    • Global scope is created first, then nested scopes are built for blocks and functions.
  6. Output is printed to the console, either from console.log or the REPL response.

Example: let x = 10; console.log(x + 5);

  • Lexer converts source into tokens: LET, IDENTIFIER(x), ASSIGN, NUMBER(10), SEMICOLON, ...
  • Parser builds an AST representing a variable declaration and a call to console.log
  • Evaluator declares x and computes x + 5
  • console.log prints 15 to stdout

Requirements

  • Python 3.8 or newer

Run the Interpreter

Start the REPL

python main.py

Once running, type JavaScript expressions and statements directly.

Run a JavaScript File

python main.py test.js

REPL Example

$ python main.py
   ___       _   _                    _ _
  / _ \_ __ | |_(_) __ _ _ __ __ ___ (_) |_ _   _
 / /_\/ '_ \| __| |/ _` | '__/ _` \ \ / / __| | | |
/ /_\\| |_) | |_| | (_| | | | (_| |\ V /| |_| |_| |
\____/| .__/ \__|_|\__, |_|  \__,_| \_/  \__|\__, |
      |_|          |___/                     |___/
  JavaScript Interpreter (Python implementation)
  Type code to evaluate. Commands:
    .exit / .quit : Exit the shell
    .tokens     : Toggle token stream visualization
    .ast        : Toggle AST tree visualization
    .env        : Print active global scope bindings

js> let x = 5;
js> let y = 7;
js> function add(a, b) { return a + b; }
js> console.log(add(x, y));
12
js> .env
--- Active Scope Bindings ---
  this : [object Object]
  console : [object Object]
  Math : [object Object]
  Date : [object Function]
  x : 5
  y : 7
---
js> .exit
Goodbye!

Example JavaScript File

Create test.js with:

let greeting = "Hello";
let name = "World";

function joinWords(a, b) {
  return a + ", " + b + "!";
}

console.log(joinWords(greeting, name));

Run it:

python main.py test.js

Expected output:

Hello, World!

REPL Debug Helpers

  • .tokens — show the current input as a token stream
  • .ast — show the current input parsed into an AST tree
  • .env — show global scope bindings and values

Project Files

  • main.py — entry point for REPL or file execution
  • repl.py — interactive shell and helper commands
  • lexer.py — tokenizes the source text
  • parser.py — builds the AST using Pratt parsing
  • ast_nodes.py — AST node definitions and syntax structures
  • evaluator.py — executes the AST and manages runtime semantics
  • environment.py — scope chain and variable lookup logic
  • values.py — JavaScript value wrappers, coercion, and object models
  • test_interpreter.py — interpreter test cases
  • test.js — sample JavaScript script

Notes & Limitations

  • This interpreter is a learning implementation, not a full JavaScript engine.
  • It does not implement the full ECMAScript standard.
  • Some JavaScript edge cases and built-in APIs are simplified or missing.
  • Use it to explore parsing, AST evaluation, and interpreter design.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

javascript_interpreter-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

javascript_interpreter-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file javascript_interpreter-0.1.0.tar.gz.

File metadata

  • Download URL: javascript_interpreter-0.1.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for javascript_interpreter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2749f3bdd1e28205ff697cdd9e8c616246375263260a9964081105eebdda5f29
MD5 48a4fdee7579d785105492020074ba7e
BLAKE2b-256 2a2b3acb07938283a4c6f34e208627493ddeb5cf4a660c787dff38abe464ba05

See more details on using hashes here.

File details

Details for the file javascript_interpreter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for javascript_interpreter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 81e993eab3a9ce65f3a4971b3be1b94b0744e993c1b40204f297fd8f3b40ac1d
MD5 6de73ecb07a655732996c4a8b68b3366
BLAKE2b-256 cab658710581484ac3f24c758c266c4ea1281e3a9e60f707ea9e06a5c0da7f6e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page