| Crates.io | waspy |
| lib.rs | waspy |
| version | 0.5.2 |
| created_at | 2025-05-26 14:05:42.818893+00 |
| updated_at | 2025-05-26 14:05:42.818893+00 |
| description | A Python interpreter written in Rust, designed for WebAssembly. |
| homepage | |
| repository | https://github.com/anistark/waspy |
| max_upload_size | |
| id | 1689756 |
| size | 323,842 |
A Python to WebAssembly compiler written in Rust.
Waspy translates Python functions into WebAssembly. The implementation supports basic arithmetic operations, control flow, and multiple functions with enhanced type support.
[Python Source Code]
↓ (rustpython_parser)
[Python AST]
↓ (ir module)
[Custom IR (functions, ops)]
↓ (wasm-encoder)
[Raw WASM binary]
↓ (binaryen optimizer)
[Optimized .wasm]
↓
[Run/test in browser or server]
+, -, *, /, %, //, **)==, !=, <, <=, >, >=)and, or)cargo add waspy
[dependencies] waspy = "0.5.0"
use waspy::compile_python_to_wasm;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let python_code = r#"
def add(a: int, b: int) -> int:
return a + b
def fibonacci(n: int) -> int:
if n <= 1:
return n
a = 0
b = 1
i = 2
while i <= n:
temp = a + b
a = b
b = temp
i = i + 1
return b
"#;
let wasm = compile_python_to_wasm(python_code)?;
// Write to file or use the WebAssembly binary
std::fs::write("output.wasm", &wasm)?;
Ok(())
}
With Compiler Options:
use waspy::{compile_python_to_wasm_with_options, CompilerOptions};
let options = CompilerOptions {
optimize: true,
debug_info: true,
generate_html: true,
..CompilerOptions::default()
};
let wasm = compile_python_to_wasm_with_options(python_code, &options)?;
For multiple files compilation:
use waspy::compile_multiple_python_files;
let sources = vec![
("math.py", "def add(a: int, b: int) -> int:\n return a + b"),
("main.py", "def compute(x: int) -> int:\n return add(x, 10)")
];
let wasm = compile_multiple_python_files(&sources, true)?;
For unoptimized WebAssembly (useful for debugging or further processing):
use waspy::compile_python_to_wasm_with_options;
let wasm = compile_python_to_wasm_with_options(python_code, false)?;
Compiling Projects:
use waspy::compile_python_project;
let wasm = compile_python_project("./my_python_project", true)?;
def factorial(n: int) -> int:
result = 1
i = 1
while i <= n:
result = result * i
i = i + 1
return result
def max_num(a: float, b: float) -> float:
if a > b:
return a
else:
return b
The compiled WebAssembly can be used in various environments:
// Browser or Node.js
WebAssembly.instantiate(wasmBuffer).then(result => {
const instance = result.instance;
console.log(instance.exports.factorial(5)); // 120
console.log(instance.exports.max_num(42, 17)); // 42
});
Waspy supports multiple function definitions:
The type system now includes:
i32 typef64 with conversion to i32 when necessaryi32 (0 for false, 1 for true)The compiler supports basic control flow constructs:
and and or with short-circuit evaluationWaspy handles variables through WebAssembly locals:
Enhanced error reporting system:
Waspy includes several examples to demonstrate its functionality:
# Basic compiler example
cargo run --example simple_compiler
# Advanced compiler with options
cargo run --example advanced_compiler examples/typed_demo.py --metadata --html
# Multi-file compilation
cargo run --example multi_file_compiler examples/output/combined.wasm examples/basic_operations.py examples/calculator.py
# Project compilation
cargo run --example project_compiler examples/calculator_project examples/output/project.wasm
# Type system demonstration
cargo run --example typed_demo
Contributions are welcome! See CONTRIBUTING.md for details on how to get started.