plum

#treesitter#compiler#wasm

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

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


plum-examples/control_flow.plum
import std/Str
import std/Bool
import std/Number

fun loopSum(limit: Int) -> Int =
  total := 0
  for i := range limit
    if i == 3
      continue
    if i == 8
      break
    total = total + i
  return total

fun classify(n: Int) -> Str =
  if n < 0
    return "negative"
  else if n == 0
    return "zero"
  else
    return "positive"

fun countdown(start: Int) -> Int =
  i := start
  while i > 0
    i = i - 1
  return i

fun placeholder() =
  todo

fun checkPositive(n: Int) =
  assert n > 0

# ---- tail-expression and assert regression tests ----

fun bindExample(n: Int) -> Int =
  match n
    x => x

fun abs(n: Int) -> Int =
  if n < 0
    -n
  else
    n

fun classifySign(n: Int) -> Int =
  match n
    0 => 1
    x =>
      if x < 0
        -1
      else
        2

fun describeMagnitude(n: Int) -> Int =
  match n
    0 =>
      return 100
    x => x * 2

enum TailOption =
  | TailSome(Int)
  | TailNone

fun unwrapTailOr(o: TailOption, default: Int) -> Int =
  match o
    TailSome(v) => v
    TailNone => default

fun checkPositiveAndReturn(n: Int) -> Int =
  assert n > 0
  n

test "tail match without return runs correctly"
  assert bindExample(5) == 5

test "tail if without return runs correctly"
  assert abs(-7) == 7

test "tail if nested inside match arm without return runs correctly"
  assert classifySign(-5) == -1

test "tail match mixing return and bare expr arms runs correctly"
  assert describeMagnitude(21) == 42

test "tail enum match without return runs correctly"
  assert unwrapTailOr(TailSome(9), 0) == 9

test "assert passes through on true"
  assert checkPositiveAndReturn(5) == 5