A modular code translator (transpiler) from a simple Java-like language (“Mini-Java”) to JavaScript, implemented in JavaScript.
This project demonstrates how to implement a basic language transpiler: from tokenization and parsing, to AST generation and code output.
- Translates Mini-Java code (variable declarations, assignments, functions, arithmetic) to JavaScript
- Modular, readable code following SOLID principles
- Clear separation between lexer, parser, AST, code generator, and IO
- Easy to extend and experiment for learning, demos, or further development
java-to-js-transpiler/
├── src/
│ ├── lexer.js
│ ├── token.js
│ ├── parser.js
│ ├── abstract-syntax-tree.js
│ ├── javascript-generator.js
│ ├── file-reader.js
│ └── main.js
├── examples/
│ └── sample.mjava
├── README.md
├── package.json
src/lexer.js
: Lexical analyzer—converts Mini-Java source code into tokens.src/token.js
: Token class definition.src/parser.js
: Parses the token stream into an Abstract Syntax Tree (AST).src/abstract-syntax-tree.js
: AST node class definitions.src/javascript-generator.js
: Converts the AST into equivalent JavaScript code.src/file-reader.js
: Utility class for reading files.src/main.js
: Entry point: glues all components together.examples/sample.mjava
: Example input file written in Mini-Java.
- Node.js (version 16+ recommended)
Clone the repository or download the source code:
git clone https://github.com/robinsonur/java-to-js-transpiler.git
cd java-to-js-transpiler
You can run the translator using the included start script:
npm start
Or directly with Node (default input is examples/sample.mjava
):
node src/main.js
To translate another file:
node src/index.js path/to/your-file.mjava
Input (examples/sample.mjava
):
int suma(int a, int b) {
int resultado = a + b;
return resultado;
}
int x = 5;
int y = 2;
int z = suma(x, y);
Output:
function suma(a, b) {
let resultado = a + b;
return resultado;
}
let x = 5;
let y = 2;
let z = suma(x, y);
-
Lexical Analysis: The lexer (
src/lexer.js
) reads the Mini-Java code and converts it into a sequence of tokens. -
Parsing: The parser (
src/parser.js
) processes the token stream and constructs an Abstract Syntax Tree (AST), using class definitions fromsrc/abstract-syntax-tree.js
. -
Code Generation: The JavaScript generator (
src/javascript-generator.js
) traverses the AST and produces JavaScript code. -
Execution: The entry point (
src/main.js
) manages file input, executes the translation pipeline, and displays both the original Mini-Java code and its JavaScript output.
- Modify or add AST nodes and update the code generator to support new features.
- Easily add support for more advanced statements, new types, or output formats.
Robinson U. Rodríguez GitHub