An experimental Go parser and WebAssembly compiler written in Rust. Goiaba translates Go source code into WebAssembly bytecode, enabling Go programs to run in web browsers and other WebAssembly environments.
- Parse Go source code into an Abstract Syntax Tree (AST)
- Compile Go functions to WebAssembly modules
- Support for fundamental Go language features (functions, control flow, arithmetic)
- Export Go functions for use in JavaScript/WebAssembly environments
- Export Go functions for use in Rust through C ABI
- Export Go functions for use in Zig through C ABI
- Command-line interface for compilation
- Programmatic API for integration into Rust projects
Add to your Cargo.toml:
Basic compilation:
Compile with verbose output:
Generate a complete web project with HTML and JavaScript:
Advanced usage with multiple options:
- Function definitions with parameters and return types
- Integer arithmetic operations (+, -, *, /, %)
- Comparison operations (<, >, <=, >=, ==, !=)
- Bitwise operations (&, |, ^, <<, >>)
- Logical operations (&&, ||, !)
- Variable declarations and assignments
- If-else statements and nested conditionals
- For loops with initialization, condition, and post statements
- Recursive function calls
- Function calls with multiple arguments
- Increment and decrement operators (++, --)
- Unary operators (-, !)
- Struct types with field access and assignment
- Composite literals for struct initialization
- Arrays and slices
- String literals and operations
- Switch statements
- Pointer operations
- Methods on types
- Interfaces
- Multiple return values
- Defer statements
- Panic and recover
- Goroutines and channels
- Package imports
- Standard library support
To make Go functions callable from WebAssembly, use the //export directive:
The exported name will be used in the WebAssembly module exports.
- Go source code parsing to Abstract Syntax Tree (AST)
- Translation of Go constructs to WebAssembly representations
- WebAssembly bytecode generation
- Function definitions with parameter and return types
- Variable declarations and assignments
- Control flow statements (if/else, for loops)
- Exportable WASM functions
- Arithmetic operations (+, -, *, /, %)
- Comparison operations (<, >, <=, >=, ==, !=)
- Bitwise operations (&, |, ^, <<, >>)
- Logical operations (&&, ||, !)
- Increment/decrement operators (++, --)
- Recursive function calls
- Struct types with field access and assignment
- Command-line interface
- Unary operators (negation, logical NOT)
- Arrays and slices
- String literals and operations
- Switch statements
- Pointer dereferencing and operations
- Methods on types
- Interfaces
- Multiple return values
- Defer statements
- Panic and recover
- Package imports
- Standard library functions
- Floating-point operations
- Memory management optimizations
- Goroutines and channels
- Complete standard library support
- Source maps for debugging
- Optimization passes for generated WASM
- JavaScript bindings generation (wasm-bindgen)
- Rust code generation
- Zig code generation
- LLVM-IR target compilation
Goiaba consists of several key components:
- Parser: Lexical analysis and syntax parsing of Go source code
- AST: Internal representation of Go program structure
- Translator: Conversion from Go AST to WebAssembly IR
- Compiler: Generation of WebAssembly bytecode
- CLI: Command-line interface for user interaction
The generated WebAssembly code prioritizes correctness over optimization. Future versions will include:
- Dead code elimination
- Constant folding
- Register allocation improvements
- Memory access optimization
- Function inlining for small functions
Contributions are welcome. Please ensure all tests pass before submitting pull requests:
Run the test suite:
Current limitations of the compiler, yet to be added:
- No garbage collection (manual memory management)
- Limited standard library support
- No concurrency primitives (goroutines, channels)
- Single file compilation only
- No optimizer passes
BSD-3-Clause
Copyright (c) 2024 Raphael Amorim
This project builds upon concepts from the Go language specification and WebAssembly standards. Parser implementation is adapted from the Goscript project.
.png)


