Skip to content

genan2003/Cobweb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cobweb

A blazingly fast, bidirectional source-to-source compiler written in Rust. Seamlessly translates Python to JavaScript and vice-versa.

Features

  • 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.

Usage

  1. Compile the project:

    cargo build --release
  2. Run the compiler:

    The language is automatically detected based on the file extension (.py for Python, .js for 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.

Example

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");

How It Works

Cobweb is built with a modern compiler architecture:

  1. Parsing: The source code is parsed using pest, which generates a parse tree from a formal grammar (.pest files). A pre-processor handles Python's indentation.
  2. 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.
  3. Code Generation: The AST is then walked by a code generator, which emits the final source code in the target language (Python or JavaScript).

License

This project is not explicitly licensed.

About

A blazingly fast, bidirectional source-to-source compiler written in Rust. Seamlessly translates Python to JavaScript and vice-versa, covering variables, control flow (if/for/while), functions, classes, and I/O. Built with a type-safe universal AST for reliable code generation.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages