Skip to main content

Muni compiler frontend targeting WebAssembly

Project description

muni2wasm

A compiler that turns Muni source code into WebAssembly.

  • Written in Python, ships with a CLI
  • No system tools required — uses the Python WABT package to assemble
  • Optional: run the resulting .wasm with Wasmtime (Python binding)

Table of contents

Installation

pip install muni2wasm

Python 3.10+ recommended.

Quick start

# Compile .mun → .wasm
muni2wasm compile hello.mun out.wasm

# Run .wasm (requires wasmtime)
muni2wasm run out.wasm

Use --debug to see full Python tracebacks on errors.

The Muni language

Types

  • Primitives: int, boolean, char
  • Special: void (functions that don’t return a value)
  • Generic array: array<T>
  • User types: structure (fields + methods, can be generic)

Internally targets WebAssembly i32 for now.

Structures & generics

structure List<T> {
    T element;
    List<T> next;

    List<T>(T element) {     # constructor
        this.element = element;
        this.next = null;
    }

    void append(T element) {
        List<T> cur = this;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = List<T>(element);
    }
}

void main() {
    List<int> xs = List<int>(3);
    xs.append(4);
}

Type aliases

alias numbers   = array<int>;
alias index<T>  = pair<int, T>;

Control flow

  • if / else
  • for (init; cond; post) { ... }
  • while (cond) { ... }
  • until (cond) { ... }
  • do <X> { ... } <while (cond)>

Operators

  • Arithmetic: + - * / % (ints)
  • Comparisons: > < >= <= == != (mostly ints)
  • Logical: && || !
  • Unary: - (negate), ! (not)

Literals

123
true / false
'x'   # char
"hello!\n"  # string
[1, 2, 3]  # array literal
null

Imports

  1. File imports (inlines another .mun file; path is relative):
import <vector.mun>;
  1. Library imports (inlines a library file)
import <math.lib>; # imports the math.mun lib
  1. Host imports (declare a host function available at runtime):
import env.write_int(int) -> void;
import env.write_chr(int) -> void;

Strings

string is an alias for vec<char> (and char is just an alias for int).

  • vec<T> is structure that represents a growable vector around array<T> with size, capacity, get, set, push_back, …
  • array<T> layout is { length, buffer_ptr } in linear memory.

The standard helpers live in lib/std.mun (e.g., the definition of vec, a print(string) that calls the host env.write_chr per character).

CLI

muni2wasm compile <input.mun> <output.(wat|wasm)> [--debug]
muni2wasm run     <module.wasm>                  [--debug]
  • compile emits .wat or .wasm (based on the output suffix), assembling via Python WABT.

  • run loads the module with Wasmtime and wires a minimal host environment:

    • env.write_int(i32) — print integer
    • env.write_chr(i32) — print a single character code (ASCII/UTF-8 byte)

You can define your own host imports and call them from Muni via import module.name(...) -> ...;.

Notes

  • Currently uses i32 only (no floats yet).
  • char is an int under the hood; escape sequences like '\n' are supported.
  • Strings are vec<char>; printing strings typically iterates chars and calls write_chr.
  • Type aliases are expanded before type-checking; generic arity is validated.
  • Diagnostics include file:line:col.

Contributing

PRs and discussions welcome! Ideas & roadmap:

  • Add f32
  • Better diagnostics
  • More stdlib (strings, I/O helpers)
  • JS/Web runner
  • Lambdas / closures (and lambda lifting)
  • More tests

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

muni2wasm-0.1.3.post0.tar.gz (38.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

muni2wasm-0.1.3.post0-py3-none-any.whl (39.5 kB view details)

Uploaded Python 3

File details

Details for the file muni2wasm-0.1.3.post0.tar.gz.

File metadata

  • Download URL: muni2wasm-0.1.3.post0.tar.gz
  • Upload date:
  • Size: 38.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for muni2wasm-0.1.3.post0.tar.gz
Algorithm Hash digest
SHA256 db1af05020d45a0a50c2171c039ae92dc0a4447fa0f1da69bc68eb51ae5308dc
MD5 ec136476a919f1274a593b79fa141cc6
BLAKE2b-256 85cff1e3970ff839a1ac17bdce281534b6c64a0c7ba3ec1db7c99a92141040fa

See more details on using hashes here.

File details

Details for the file muni2wasm-0.1.3.post0-py3-none-any.whl.

File metadata

File hashes

Hashes for muni2wasm-0.1.3.post0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a0bc7b7cfefd22df3ee1d34cf3a75366aaf5ed2a4223a7273a07e21f61b7029
MD5 3c7f12ae1f86496e4e61b195d24b15c1
BLAKE2b-256 83bcaff78d6e2444616090e5e56edd8067c863eb508ec3097796c0cd100d0180

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page