A blazingly fast, bidirectional source-to-source compiler written in Rust. Seamlessly translates Python to JavaScript and vice-versa.
- Bidirectional Translation: Convert Python code to JavaScript, or JavaScript to Python.
- Core Language Support: Handles variables, control flow (if/for/while), functions, classes, and I/O operations.
- Type-Safe AST: Utilizes a universal Abstract Syntax Tree to ensure reliable and accurate code generation between languages.
- CLI Interface: Simple and easy to use command-line tool.
-
Compile the project:
cargo build --release
-
Run the compiler:
The language is automatically detected based on the file extension (
.pyfor Python,.jsfor JavaScript).-
Translate Python to JavaScript:
./target/release/cobweb -- input.py
-
Translate JavaScript to Python:
./target/release/cobweb -- input.js
The translated code will be printed to the standard output.
-
Given the following Python code in input.py:
def greet(name):
if name:
print("Hello, " + name + "!")
else:
print("Hello, world!")
greet("Alice")Running Cobweb will produce the equivalent JavaScript:
$ ./target/release/cobweb input.py
function greet(name) {
if (name) {
console.log("Hello, " + name + "!");
} else {
console.log("Hello, world!");
}
}
greet("Alice");Cobweb is built with a modern compiler architecture:
- Parsing: The source code is parsed using
pest, which generates a parse tree from a formal grammar (.pestfiles). A pre-processor handles Python's indentation. - AST Generation: The parse tree is transformed into a language-agnostic Abstract Syntax Tree (AST). This universal representation captures the code's logic without being tied to a specific syntax.
- Code Generation: The AST is then walked by a code generator, which emits the final source code in the target language (Python or JavaScript).
This project is not explicitly licensed.