goiaba

Crates.iogoiaba
lib.rsgoiaba
version0.0.3
created_at2025-09-20 18:29:17.612259+00
updated_at2025-09-23 19:37:33.703922+00
descriptionExperimental Go parser and compiler
homepage
repositoryhttps://github.com/raphamorim/goiaba
max_upload_size
id1848107
size429,315
Raphael Amorim (raphamorim)

documentation

README

Goiaba

Experimental Go parser and compiler.

WebAssembly Compilation Usage

use goiaba::wasm::compiler::compile_str;
use wasmtime::{Engine, Instance, Module, Store};

fn main() {
    let go_source = r#"
        package main
        
        //export add
        func add(x int, y int) int {
            return x + y
        }
    "#;

    let wasm_bytes = compile_str(go_source).expect("Failed to compile Go to WASM");

    // Create a WASM runtime
    let engine = Engine::default();
    let module = Module::from_binary(&engine, &wasm_bytes).expect("Failed to load WASM module");
    let mut store = Store::new(&engine, ());

    // Instantiate the module
    let instance =
        Instance::new(&mut store, &module, &[]).expect("Failed to instantiate module");

    // Get the exported function
    let add_func = instance
        .get_typed_func::<(i32, i32), i32>(&mut store, "add")
        .expect("Failed to get 'add' function");

    // Call the function
    let result = add_func
        .call(&mut store, (5, 3))
        .expect("Failed to call 'add' function");

    // Verify the result
    assert_eq!(result, 8);
}

CLI Usage

goiaba main.go -o main.wasm
goiaba input.go --output output.wasm --verbose

# Generate a complete web project
goiaba main.go -w ./web-project

# Compile with custom output and web generation  
goiaba calculator.go -o calc.wasm -w ./demo --verbose

Todo

  • Parses Go source code into an Abstract Syntax Tree (AST)
  • Translates Go constructs to WebAssembly representations
  • Compiles to WebAssembly bytecode
  • Supports function definitions with parameter and return types
  • Handles variable declarations and assignments
  • Supports control flow statements (if/else, loops)
  • Generates exportable WASM functions
  • Supports arithmetic and comparison operations
  • Handles increment/decrement operators
  • Support for struct types
  • Support for arrays and slices
  • Implement switch statements
  • Add support for string literals
  • Implement pointer types
  • Add garbage collection support
  • Support for goroutines and channels
  • Implement more standard library functions
  • Add support for package imports
  • Improve error reporting and diagnostics
  • Optimize generated WASM code
  • Add support for floating-point operations
  • Cli support goiaba main.go
  • Cli support produces the js with wasm-bindgen
  • Add LLVM-IR target for compilation

License

GPL-3.0 - Raphael Amorim

Commit count: 26

cargo fmt