A Python-to-JavaScript transpiler written in Node.js. It converts a subset of Python syntax into runnable JavaScript, executing it directly in the terminal.
- Overview
- Features
- Project Structure
- How It Works
- Getting Started
- Supported Syntax
- Examples
- Running Tests
- Limitations
FakePy Transpiler takes Python source code (written in fakepy.txt), tokenizes it, parses it into an AST, and generates equivalent JavaScript code that is then executed at runtime. It supports a meaningful subset of Python including variables, control flow, functions, lists, and more.
- ✅ Variables (assignment and reassignment)
- ✅ Arithmetic and comparison operators
- ✅
print()with optionalend=argument - ✅
input()with async support - ✅
if / elseconditionals - ✅
forloops withrange()(start, stop, step) - ✅
whileloops - ✅
breakandcontinue - ✅ Function definitions (
def) and calls - ✅
returnstatements - ✅ Lists with index access
- ✅ List methods:
append,pop,reverse,sort,index,copy,extend,insert,remove,count - ✅ Built-ins:
len(),int(),float(),str() - ✅ Boolean literals:
True/False - ✅ Logical operators:
and,or - ✅ String concatenation via
+ - ✅ Comments with
#
transpiler/
│
├── index.js # Entry point — CLI interface, runs txt file or test suite
├── lexer.js # Tokenizer — converts raw source code into a token list
├── parser.js # Parser — builds an AST from the token list
├── ast.js # AST node class definitions
├── codegen.js # Code generator — converts AST into JavaScript source
├── runetime.js # Runtime helper — executes generated JS via eval
├── tests.js # Test cases and expected outputs
├── fakepy.txt # Sample Python-like source file to transpile and run
└── package.json # Project metadata (ES modules)
The transpiler follows the classic compiler pipeline:
Source Code (fakepy.txt)
│
▼
[ Lexer ] → Tokenizes the raw text into a stream of tokens
│
▼
[ Parser ] → Builds an Abstract Syntax Tree (AST)
│
▼
[ Code Generator ] → Traverses the AST and emits JavaScript code
│
▼
[ Runtime ] → Executes the generated JS using eval()
- Node.js v16 or higher (ES modules support required)
git clone https://github.com/your-username/fakepy-transpiler.git
cd fakepy-transpilerNo dependencies to install — the project uses only Node.js built-ins.
node index.jsYou'll be prompted to choose between:
- Run
fakepy.txt— transpiles and executes the sample Python file - Run tests — runs the built-in test suite and shows pass/fail results
x = 10
name = "Filippo"
x += 5if x > 5:
print("grande")
else:
print("piccolo")for i in range(10):
print(i)
for i in range(0, 10, 2):
print(i)
while x > 0:
x -= 1def somma(a, b):
return a + b
risultato = somma(3, 7)
print(risultato)a = [1, 2, 3]
a.append(4)
a.reverse()
print(a[0])
print(len(a))nome = input("Come ti chiami? ")
print(nome)When you launch the program and select option 2, it runs 16 automated tests covering:
- Function definitions and calls
- Nested functions
- List manipulation
- Loops and conditionals
- Return values
- List methods (
append,pop,reverse,sort,index,copy)
Results are printed as ✅ (pass) or ❌ (fail) with expected vs actual output.
- Only
for i in range(...)loops are supported — nofor x in listiteration - No classes or object-oriented features
- No
elifchains (onlyif / else) - No multi-line strings or f-strings
- No exception handling (
try / except) - Indentation must use spaces (tabs not supported)
- No module imports within the fake Python source
Filippo Chiarolla