plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


👾 Plum

A simple, statically typed programming language with enums, pattern matching and generics. It compiles to WebAssembly.

import std/Io
import std/Int
import std/Float
import std/Str
import std/collections/List
import std/Option
import std/App

enum Shape =
  | Circle(r: Float)
  | Rect(w: Float, h: Float)

  fun area(self) -> Float =
    match self
      Circle(r) => r * r * 3.14
      Rect(w, h) => w * h

primitive Shapes[App] =
  fun firstEven(xs: List<Int>) -> Option<Int> =
    xs.find(|x| x % 2 == 0)

  fun main(io: Io) =
    shape := Rect(w: 2.0, h: 3.0)
    even := Shapes.firstEven([1, 3, 4]).unwrapOr(0)
    io.printLn("area {shape.area()}, first even {even}")

Its tests, in tests/shapes.plum:

# tests/shapes.plum
import std/Test
import shapes

primitive ShapesTest[UnitTest] =
  fun areaAndFirstEven() =
    assert Rect(w: 2.0, h: 3.0).area() == 6.0
    assert Shapes.firstEven([1, 3]).isNone()

Features

  • Easy to read: blocks are indentation, and a function is one expression: no return, no reassigned locals, and if and match give values.
  • Structs and enums: match checks that you handled every case.
  • Generics: write fun id<T>(x: T) -> T, and each type gets its own fast copy.
  • Nothing global: every function is a method or a static of a type or a primitive, every constant is a const of one, and a program starts at the main of its [App] primitive (primitive Shapes[App]).
  • No null, no exceptions: absence is Option, failure is Result, and ? passes an error up.
  • Closures and tail calls: recursion runs in constant stack.
  • Tests built in: put a primitive ShapesTest[UnitTest] in the tests/ mirror of your code and run plum test.
  • Visible side effects: printing, files and network go through an Io value, so a function without one can't touch the outside world.
  • Fast by default: the compiler removes temporary lists and updates values in place when it is safe, with no change to what your code means.
  • Games: a built-in engine runs in the browser on WebGPU.
  • Editor support: syntax highlighting and a language server for Helix and VSCode.

Plum compiles to WebAssembly (wasm-gc), and plum build wraps the result in a native executable. True native targets are planned.

Install

Platform Download
macOS (Apple Silicon) plum-darwin-arm64
Linux (x86_64) plum-linux-x86_64
Windows (x86_64) plum-windows-x86_64.exe

Make it executable and put it on your PATH, e.g. on macOS/Linux:

curl -L -o plum https://git.pyrossh.dev/releases/plum-cli/v0.1.0/plum-darwin-arm64
chmod +x plum
mv plum ~/.cargo/bin/plum   # or anywhere else already on your PATH

Or build from source with cargo install --path compiler/cli.

Use it

Run these from a checkout of this repository, so import std/... can find the standard library.

plum run file.plum       # compile and run; a game opens in the browser and reloads as you edit
plum test tests          # run the tests under tests/
plum build file.plum     # make an executable, or a website for a game
plum editor helix        # set up your editor (or vscode)

Learn more