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, andvardeclarations - Handles arrays, objects, string values, numbers, booleans,
null, andundefined - Provides built-in APIs:
console.log,Math, andDate - Offers an interactive REPL with debug helpers
How It Works: Input to Output
- Source code is loaded from a file or typed into the REPL.
lexer.pyreads the characters and converts them into a stream of tokens.- Example token types:
IDENTIFIER,NUMBER,STRING,PLUS,LPAREN,SEMICOLON
- Example token types:
parser.pyuses 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
evaluator.pywalks 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.
environment.pymanages scope chains and variable lookup.- Global scope is created first, then nested scopes are built for blocks and functions.
- Output is printed to the console, either from
console.logor 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
xand computesx + 5 console.logprints15to 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 executionrepl.py— interactive shell and helper commandslexer.py— tokenizes the source textparser.py— builds the AST using Pratt parsingast_nodes.py— AST node definitions and syntax structuresevaluator.py— executes the AST and manages runtime semanticsenvironment.py— scope chain and variable lookup logicvalues.py— JavaScript value wrappers, coercion, and object modelstest_interpreter.py— interpreter test casestest.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2749f3bdd1e28205ff697cdd9e8c616246375263260a9964081105eebdda5f29
|
|
| MD5 |
48a4fdee7579d785105492020074ba7e
|
|
| BLAKE2b-256 |
2a2b3acb07938283a4c6f34e208627493ddeb5cf4a660c787dff38abe464ba05
|
File details
Details for the file javascript_interpreter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: javascript_interpreter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81e993eab3a9ce65f3a4971b3be1b94b0744e993c1b40204f297fd8f3b40ac1d
|
|
| MD5 |
6de73ecb07a655732996c4a8b68b3366
|
|
| BLAKE2b-256 |
cab658710581484ac3f24c758c266c4ea1281e3a9e60f707ea9e06a5c0da7f6e
|