plum

#treesitter#compiler#wasm

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

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


0f0c0a2 — Peter John 2026-11-09T10:19:00+05:30
Simpler language: rewrite the docs for the branch's language
Files changed (4) hide show
  1. README.md +2 -5
  2. docs/game.md +6 -5
  3. docs/language.md +140 -27
  4. docs/tools.md +1 -1
README.md CHANGED
@@ -20,10 +20,7 @@ enum Shape =
20
20
  Rect(w, h) => w * h
21
21
 
22
22
  fun firstEven(xs: List[Int]) -> Option[Int] =
23
- for x := range xs
24
- if x % 2 == 0
23
+ xs.find(|x| x % 2 == 0)
25
- return Some(x)
26
- None
27
24
 
28
25
  fun main(io: Io) =
29
26
  shape := Rect(w: 2.0, h: 3.0)
@@ -37,7 +34,7 @@ test "shapes"
37
34
 
38
35
  ## Features
39
36
 
40
- - **Easy to read:** blocks are indentation, and there is one way to loop over anything.
37
+ - **Easy to read:** blocks are indentation, and a function is one expression: no `return`, no reassigned locals, and `if` and `match` give values.
41
38
  - **Structs and enums:** `match` checks that you handled every case.
42
39
  - **Generics:** write `fun id[T](x: T) -> T`, and each type gets its own fast copy.
43
40
  - **No null, no exceptions:** absence is `Option`, failure is `Result`, and `?` passes an error up.
docs/game.md CHANGED
@@ -114,11 +114,12 @@ import std/game/scene
114
114
  struct Counter(clicks: Int) =
115
115
  fun update(self, io: Io, dt: Float) -> Counter =
116
116
  if !input.isMousePressed(io, Mouse.Left)
117
- return self
117
+ self
118
+ else
118
- p := input.mousePos(io)
119
+ p := input.mousePos(io)
119
- match self.render().hitId(p.x, p.y)
120
+ match self.render().hitId(p.x, p.y)
120
- Some(id) => Counter(clicks: self.clicks + 1)
121
+ Some(id) => Counter(clicks: self.clicks + 1)
121
- None => self
122
+ None => self
122
123
 
123
124
  fun render(self) -> SceneNode[Int] =
124
125
  Group(transform: Identity, children: [
docs/language.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  A tour of the language, one idea at a time. Every feature also has a runnable file in [`examples/`](../examples).
4
4
 
5
+ A Plum function is a tree of expressions. There is no `return`, `break` or `continue`, and a name keeps one value for its whole scope, so control always flows down a body and out at the bottom. Loops are for side effects, and values that change from step to step go through `fold`, `find` or recursion. Why the language is shaped this way is in [the design notes](decisions/2026-11-simpler-language-notes.md).
6
+
5
7
  - [First program](#first-program)
6
8
  - [Basics](#basics)
7
9
  - [Functions](#functions)
@@ -38,16 +40,18 @@ plum run hello.plum
38
40
 
39
41
  **Blocks** use indentation (2 spaces), with no braces or semicolons. **Comments** start with `#`.
40
42
 
41
- **Variables:** `:=` declares a new name and `=` changes an existing one.
43
+ **Variables:** `:=` declares a name, and a name keeps its value for the whole of its scope. To get a new value, give it a new name. `=` only assigns to a `mut` field (see [Structs](#structs)).
42
44
 
43
45
  ```plum
44
46
  fun main() =
45
47
  count := 1
46
- count = count + 1
48
+ next := count + 1
47
49
  a, b := 1, 2
48
50
  ratio: Float := 0.5 # with an explicit type
49
51
  ```
50
52
 
53
+ `count = 2` is an error: "locals can't be reassigned; use a new name with :=".
54
+
51
55
  **Values:**
52
56
 
53
57
  | Type | Examples |
@@ -83,7 +87,7 @@ fun total() -> Int =
83
87
 
84
88
  ## Functions
85
89
 
86
- The last expression is the return value. With no return type, a function returns `Unit`.
90
+ The last expression is the function's value. There is no `return`, so every path through a body ends in a value. With no return type, a function returns `Unit`.
87
91
 
88
92
  ```plum
89
93
  fun add(a: Int, b: Int) -> Int =
@@ -106,27 +110,41 @@ fun countUp(n: Int, acc: Int) -> Int =
106
110
 
107
111
  ## Control flow
108
112
 
109
- `if` comes in two forms. The block form is a statement, and the `then` form is an expression:
113
+ `if` and `match` are expressions, so each branch ends in the value it produces. `if` has a block form and a one-line `then` form, and both give a value:
110
114
 
111
115
  ```plum
112
116
  fun describe(n: Int) -> Str =
113
117
  if n < 0
114
- return "negative"
118
+ "negative"
115
119
  else if n == 0
116
- return "zero"
120
+ "zero"
121
+ else
117
- if n > 100 then "big" else "small"
122
+ if n > 100 then "big" else "small"
123
+
124
+ fun sign(n: Int) -> Int =
125
+ s := if n < 0
126
+ -1
127
+ else
128
+ 1
129
+ s * 10
118
130
  ```
119
131
 
120
- `while` loops as long as a condition holds, and `for` is covered under [Loops](#loops). `break` and `continue` work in both.
132
+ There is no early exit. Instead of returning from the middle of a function, branch with `if` or `match` and end each branch with its value.
133
+
134
+ `while` runs a body as long as a condition holds, and `for` is covered under [Loops](#loops). Both are for side effects: the body changes a `mut` field or calls something with `io`. Neither produces a value, and there is no `break` or `continue`.
121
135
 
122
136
  ```plum
137
+ struct Tally(mut n: Int)
138
+
123
139
  fun halve(start: Int) -> Int =
124
- i := start
140
+ t := Tally(n: start)
125
- while i > 1
141
+ while t.n > 1
126
- i = i / 2
142
+ t.n = t.n / 2
127
- i
143
+ t.n
128
144
  ```
129
145
 
146
+ To carry a value from step to step, use `fold`, `find` or recursion (see [Loops](#loops)).
147
+
130
148
  `assert n > 0` stops the program if the condition is false. `todo` marks a body you haven't written yet, and stops the program if it is reached.
131
149
 
132
150
  ## Structs
@@ -144,6 +162,20 @@ fun main() =
144
162
 
145
163
  Fields can't change unless they are marked `mut`. A spread, `User(..ann, age: 32)`, makes an updated value, and the compiler writes it in place when the old value isn't used again.
146
164
 
165
+ **Updating a field from its old value:** `.=` calls a method on a `mut` field and stores the result. `ann.age .= max(18)` means `ann.age = ann.age.max(18)`, and the path before `.=` is worked out only once. The method has to return the new value, so `.=` doesn't fit a method like `Slice.push` that changes the slice and returns `Unit`.
166
+
167
+ **Updating a nested field:** a spread can name a path through struct fields. Paths that start the same way share one copy of that part:
168
+
169
+ ```plum
170
+ struct Ship(x: Float, y: Float)
171
+ struct State(ship: Ship, score: Int)
172
+
173
+ fun moved(s: State) -> State =
174
+ State(..s, ship.x: 1.0, ship.y: 2.0) # State(..s, ship: Ship(..s.ship, x: 1.0, y: 2.0))
175
+ ```
176
+
177
+ A path can only go through struct fields. A path through an enum or a type parameter is a type error.
178
+
147
179
  **Copied or shared?** A small struct made only of numbers, like `Vec2` below, is copied when assigned. Any other struct is shared: two names point at the same value.
148
180
 
149
181
  ```plum
@@ -258,8 +290,9 @@ enum AgeError =
258
290
  fun parseAge(s: Str) -> Result[Int, AgeError] =
259
291
  n := Int.parse(s).mapErr(|e| NotANumber)?
260
292
  if n < 0
261
- return Err(Negative)
293
+ Err(Negative)
294
+ else
262
- Ok(n)
295
+ Ok(n)
263
296
 
264
297
  fun describe(s: Str) -> Str =
265
298
  match parseAge(s)
@@ -272,7 +305,7 @@ Each way of handling them:
272
305
 
273
306
  | You write | It means |
274
307
  |---|---|
275
- | `x?` | Use the value, or return the `Err`/`None` from this function |
308
+ | `x?` | Use the value, or make the `Err`/`None` this function's value |
276
309
  | `x!` | Use the value, or crash and report the file and line |
277
310
  | `x.unwrapOr(d)` | Use the value, or the default `d` |
278
311
  | `if Some(v) := x` | Run a block only when there is a value |
@@ -281,6 +314,16 @@ Each way of handling them:
281
314
  | `r.mapErr(f)` | Change the error, usually into your own error enum |
282
315
  | `o.okOr(e)`, `r.ok()` | Convert between `Option` and `Result` |
283
316
 
317
+ `?` is shorthand for a `match`. `x := parse(a)?` followed by the rest of the block means:
318
+
319
+ ```plum
320
+ match parse(a)
321
+ Err(error) => Err(error)
322
+ Ok(x) => ...the rest of the block
323
+ ```
324
+
325
+ So `?` can go at the level of a function's body, including inside the last `if` or `match` of the body. It can't go in an `if` branch that isn't last, or in a loop body, because a loop body is a function of its own. In those places, `match` on the value or move the code into a function.
326
+
284
327
  A good habit is one error enum per module. Use `?` and `mapErr` for failures you expect. Save `!` and `crash "message"` for bugs.
285
328
 
286
329
  ## Lists, slices and maps
@@ -309,27 +352,57 @@ A `List` is `Nil` or `Cons(head, tail)`, so you can match on it. Indexing a `Lis
309
352
 
310
353
  ## Loops
311
354
 
312
- `for x := range source` is the only `for`.
355
+ `for x := range source` is the only `for`. Like `while`, it is for side effects: the body acts through a `mut` field or `io`, and it can't reassign a local from outside the loop.
313
356
 
314
357
  ```plum
358
+ import std/Io
315
359
  import std/Int
316
360
  import std/Str
317
361
  import std/List
318
362
  import std/Map
319
363
  import std/Slice
320
364
 
365
+ struct Tally(mut n: Int)
366
+
321
- fun main() =
367
+ fun main(io: Io) =
322
- total := 0
368
+ total := Tally(n: 0)
323
369
  for i := range 10 # 0, 1, ... 9
324
- total = total + i
370
+ total.n = total.n + i
325
371
  for i := range 2.until(10) # 2 to 9; also to, downTo, stepBy
326
- total = total + i
372
+ total.n = total.n + i
327
373
  for x := range [1, 2, 3] # a List, Slice or Array
328
- total = total + x
374
+ total.n = total.n + x
329
375
  for e := range ["a" => 1].entries() # a Map: e.first and e.second
330
- total = total + e.second
376
+ io.printLn("{e.first} is {e.second}")
377
+ ```
378
+
379
+ **Carrying a value through a loop:** `fold` passes the value so far to each step, and each step returns the next one. `find` stops at the first match. Both work on ranges and lists, and a recursive function works for anything else:
380
+
381
+ ```plum
382
+ import std/Int
383
+ import std/List
384
+ import std/Option
385
+
386
+ fun sumSkippingThree(limit: Int) -> Int =
387
+ 0.until(limit).fold(0, |total, i|
388
+ if i == 3
389
+ total
390
+ else
391
+ total + i
392
+ )
393
+
394
+ fun firstEven(xs: List[Int]) -> Option[Int] =
395
+ xs.find(|x| x % 2 == 0)
396
+
397
+ fun sumDigits(n: Int, total: Int) -> Int =
398
+ if n == 0
399
+ total
400
+ else
401
+ sumDigits(n / 10, total + n % 10)
331
402
  ```
332
403
 
404
+ A `Slice` is folded over its indexes: `0.until(xs.length()).fold(0, |sum, i| sum + xs.at(i))`. `repeatUntil(start, done, step)` in `std/Int` applies `step` until `done` holds and gives back the last state. Every one of these runs in constant stack.
405
+
333
406
  Make your own type loopable by giving it `next`. Return `None` when done, or `Some` of the value and the remaining sequence:
334
407
 
335
408
  ```plum
@@ -361,6 +434,16 @@ fun main() -> Int =
361
434
  a + add1(2)
362
435
  ```
363
436
 
437
+ A longer body goes on indented lines below the parameters, and its last expression is the closure's value:
438
+
439
+ ```plum
440
+ fun main() -> Int =
441
+ f := |x|
442
+ doubled := x * 2
443
+ doubled + 1
444
+ f(3)
445
+ ```
446
+
364
447
  Add a type when the compiler can't work one out: `|b: Box| b.get()`. Passing a closure straight to a function that just calls it costs nothing, because the compiler inlines it.
365
448
 
366
449
  `adder(1)(2)` isn't supported yet, so bind the result to a name first.
@@ -410,7 +493,7 @@ struct Vec2(x: Float, y: Float) =
410
493
  self.x + self.y
411
494
  ```
412
495
 
413
- Constants and helper functions with no data live at the top level of a file. Another file reaches them through the module name, the last path segment of the import in lowercase:
496
+ Constants and helper functions with no data live at the top level of a file. Another file reaches them through the module name, the last path segment of the import in lowercase (see [Modules](#modules)):
414
497
 
415
498
  ```plum
416
499
  MaxRetries = 3
@@ -443,13 +526,43 @@ struct Vec2(x: Float, y: Float) =
443
526
 
444
527
  ## Modules
445
528
 
446
- A file starts with its imports. Nothing is implicit, not even `Int` or `Str` methods, and each file imports what it uses.
529
+ Each file is a module. A file starts with its imports. Nothing is implicit, not even `Int` or `Str` methods, and each file imports what it uses.
530
+
531
+ `import a/b` reads `a/b.plum`, with the path counted from the folder that holds `std`: the checkout, or the folder passed with `--lib-path`. That module's functions and constants are reached through its name, which is the last part of the path in lowercase. Its types and enum variants need no prefix:
447
532
 
448
533
  ```plum
449
- module shapes
534
+ # geo/shapes.plum
535
+ import std/Int
536
+
537
+ Limit = 100
450
538
 
539
+ struct Pt(x: Int)
540
+
541
+ fun clamp(x: Int) -> Int =
542
+ if x > Limit then Limit else x
543
+ ```
544
+
545
+ ```plum
546
+ # main.plum
451
547
  import std/Int
452
- import std/Str
548
+ import geo/shapes
549
+
550
+ fun main() -> Int =
551
+ p := Pt(x: 3)
552
+ shapes.clamp(500) + shapes.Limit + p.x
553
+ ```
554
+
555
+ This means two modules can each have a `clamp` without clashing, while two modules that define the same type name are an error. A module's own functions and constants need no prefix inside it, and a local with the same name as a module hides the module.
556
+
557
+ Module-level constants can be any pure expression, and they may refer to each other in any order. The same goes for std: `import std/math` gives `math.sqrt(2.0)`, and `import std/game/gfx` gives `gfx.rect(io, ..)`.
558
+
559
+ A module-level `extern fun` binds to a function the host provides. It takes `io: Io` first, because it reaches outside the program:
560
+
561
+ ```plum
562
+ import std/Io
563
+ import std/Float
564
+
565
+ extern fun rawNowMillis(io: Io) -> Float
453
566
  ```
454
567
 
455
568
  `test` blocks go at the end of the file.
@@ -499,6 +612,6 @@ This means a function that doesn't receive an `Io` can't have outside effects, w
499
612
  | nothing | exit code 0 |
500
613
  | `Int` | prints the value, exit code 0 |
501
614
  | `Result` | an `Err` stops the program and prints the error's name, exit code 1 |
502
- | a [`Game`](game.md) | `plum run` opens it in the browser |
615
+ | a type with `update` and `draw` methods | a [game](game.md): `plum run` opens it in the browser |
503
616
 
504
617
  `export fun` makes a top-level function callable from the host, for programs embedded in something else.
docs/tools.md CHANGED
@@ -6,7 +6,7 @@ Run these from a checkout of this repository, or pass `--lib-path`, so `import s
6
6
 
7
7
  | Command | What it does |
8
8
  |---|---|
9
- | `plum run file.plum` | Compile and run. A program whose `main` returns a [`Game`](game.md) opens in the browser |
9
+ | `plum run file.plum` | Compile and run. A program whose `main` returns a [game](game.md), a type with `update` and `draw`, opens in the browser |
10
10
  | `plum test file.plum dir/` | Run the `test` blocks in files and folders |
11
11
  | `plum compile file.plum` | Write `file.wasm` |
12
12
  | `plum build file.plum` | Make a native executable, or for a game, a static site |