plum

#treesitter#compiler#wasm

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

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


26f8c23 — Peter John 2026-11-10T14:52:55+05:30
Docs: the built-ins in one place
Files changed (4) hide show
  1. compiler/core/src/drive.rs +1 -1
  2. docs/game.md +2 -2
  3. docs/language.md +51 -3
  4. docs/std.md +2 -1
compiler/core/src/drive.rs CHANGED
@@ -2172,7 +2172,7 @@ impl Driver {
2172
2172
  }
2173
2173
 
2174
2174
  /// No effect in `stmts` that a moved pure value could observe: no field write, no
2175
- /// `todo`, and every call is read-only (it may draw or stop, never mutate) or takes the
2175
+ /// `@native`, and every call is read-only (it may draw or stop, never mutate) or takes the
2176
2176
  /// value `p` directly.
2177
2177
  fn quiet(&self, stmts: &[Stmt], p: &str) -> bool {
2178
2178
  let isP = |e: &Expr| matches!(&e.kind, ExprKind::Var(n) if n == p);
docs/game.md CHANGED
@@ -108,7 +108,7 @@ Each tick, `Game.update(io, input, dt)` does this, in this order:
108
108
 
109
109
  ### Screens and transitions
110
110
 
111
- The screens are the children of the tree's root, and each is a `Screen[S, M]` ([`std/game/Game.plum`](../std/game/Game.plum)). A screen's name is its type's: `PlayScreen.name()` is `"PlayScreen"` (the trait's static `name()` defaults to `@typeName(Self)`, see [Compile-time built-ins](language.md#compile-time-built-ins)), so game code never writes it as a string. The state keeps the name of the screen to show and its reducer sets it: `Start => State(..self, on: PlayScreen.name())`, with `fun screen(self) -> Str = self.on`. After a tick whose messages changed it, `Game` switches to that screen.
111
+ The screens are the children of the tree's root, and each is a `Screen[S, M]` ([`std/game/Game.plum`](../std/game/Game.plum)). A screen's name is its type's: `PlayScreen.name()` is `"PlayScreen"` (the trait's static `name()` defaults to `@typeName(Self)`, see [Built-ins](language.md#built-ins)), so game code never writes it as a string. The state keeps the name of the screen to show and its reducer sets it: `Start => State(..self, on: PlayScreen.name())`, with `fun screen(self) -> Str = self.on`. After a tick whose messages changed it, `Game` switches to that screen.
112
112
 
113
113
  Each method gets `t`, the screen's own subtree (its root is the screen), and returns it changed:
114
114
 
@@ -272,7 +272,7 @@ struct Target(id: Int, x: Float, y: Float) is Actor[Msg], Clickable =
272
272
  _ => self
273
273
  ```
274
274
 
275
- Keep an actor's position in plain fields (`x: Float, y: Float`) and build its `Place` in `place`, as `place.at(self.x, self.y)`: a `Place` field costs a step about 1.7 times as much. An actor that doesn't change should return `self` from `step` and `input` (the defaults do), not a copy: an `input` that returns the actor as it was leaves the tree as it was (`ref.same`, see [std](std.md#utilities)), and a screen's `react` that returns its tree as it was (the default) leaves the game's tree as it was. std gives `Group` (a parent that paints nothing) and `Box` (`Clickable`, hit inside it); [`std/game/ui.plum`](../std/game/ui.plum) gives `Label` and `Button` (`Clickable`) (`ui.button(id, x, y, w, h, label, onClick)`). A button shows the pointer over it and a press on it, and a press and a release on it send its `onClick` message.
275
+ Keep an actor's position in plain fields (`x: Float, y: Float`) and build its `Place` in `place`, as `place.at(self.x, self.y)`: a `Place` field costs a step about 1.7 times as much. An actor that doesn't change should return `self` from `step` and `input` (the defaults do), not a copy: an `input` that returns the actor as it was leaves the tree as it was (`@sameRef`, see [Built-ins](language.md#reference-identity)), and a screen's `react` that returns its tree as it was (the default) leaves the game's tree as it was. std gives `Group` (a parent that paints nothing) and `Box` (`Clickable`, hit inside it); [`std/game/ui.plum`](../std/game/ui.plum) gives `Label` and `Button` (`Clickable`) (`ui.button(id, x, y, w, h, label, onClick)`). A button shows the pointer over it and a press on it, and a press and a release on it send its `onClick` message.
276
276
 
277
277
  The tree's `step(ctx, dt)` steps every actor, parents before children, then applies the spawns and kills; `input(ctx, e)` routes one pointer event. `Game` calls both. `add`, `update`, `kill` and `find` work by id, and `updateAs[T](id, f)` and `findAs[T](id)` act on an actor of the type `T` only, which is how a screen's `rules` and `react` read and change one kind's own fields in its subtree. `allAs[T]()` is every actor of the type `T`, in tree order, and `kidsAs[T](parent)` the `T`s among one actor's children (a layer: `t.kidsAs[Balloon](BalloonLayer)`), which is how `rules` reads one kind's actors. `subtree(id)` is an actor with its subtree as a tree of its own, and `withTop(a)` a tree with its root actor replaced. A pattern `Orb as o` on an `Actor[M]` matches an `Orb`.
278
278
 
docs/language.md CHANGED
@@ -19,6 +19,7 @@ A Plum function is a tree of expressions. There is no `return`, `break` or `cont
19
19
  - [Methods, statics and constants](#methods-statics-and-constants)
20
20
  - [Operators on your own types](#operators-on-your-own-types)
21
21
  - [Traits](#traits)
22
+ - [Built-ins](#built-ins)
22
23
  - [Modules](#modules)
23
24
  - [Tests](#tests)
24
25
  - [Talking to the outside world](#talking-to-the-outside-world)
@@ -671,13 +672,60 @@ A value of an implementing type becomes a trait object, with no cast, where one
671
672
  - a bound not met at a use, naming the missing trait;
672
673
  - a method whose `Self` inside a parameter or the result can't be checked with the trait object there (`xs: List[Rect] := ..; xs` as a `List[Self]`): build the value where it is returned.
673
674
 
674
- ## Compile-time built-ins
675
+ ## Built-ins
675
676
 
676
- `@name(args)` calls a built-in the compiler evaluates while it compiles. Each built-in says what its arguments are, types or values. An unknown `@name`, or the wrong arguments, is an error that lists what exists.
677
+ `@name(args)` calls a built-in: something the compiler itself provides, with no declaration in std and nothing to import. Each built-in says what its arguments are, types or values. An unknown `@name`, or the wrong arguments, is an error that lists what exists. A built-in call is an expression like any call.
677
678
 
678
679
  | Built-in | Gives |
679
680
  |---|---|
680
- | `@typeName(T)` | the name of the type `T` as a `Str`, without type arguments: `@typeName(Button[Msg])` is `"Button"`. With a generic `T` it is the type of each instantiation, and `@typeName(Self)` in a trait's default is each implementer's own name (see [Statics](#statics)). |
681
+ | `@typeName(T)` | the name of the type `T` as a `Str`, without type arguments: `@typeName(Button[Msg])` is `"Button"`. The compiler evaluates it while it compiles. With a generic `T` it is the type of each instantiation, and `@typeName(Self)` in a trait's default is each implementer's own name (see [Statics](#statics)). |
682
+ | `@sameRef(a, b)` | whether `a` and `b` are the very same object, not just equal ones, as a `Bool`. See [Reference identity](#reference-identity). |
683
+ | `@memSize()` | the size of the program's linear memory in bytes, as an `Int`. See [Linear memory](#linear-memory). |
684
+ | `@memGrow(pages)` | adds `pages` 64 KiB pages to the memory; gives the old page count, or -1 when it can't grow. |
685
+ | `@memStoreI32(addr, v)` | stores the low 32 bits of the `Int` `v` at byte `addr`. |
686
+ | `@memLoadI32(addr)` | the 32 bits at `addr` as an `Int`, sign-extended. |
687
+ | `@memStoreU8(addr, v)` | stores the low 8 bits of the `Int` `v` at `addr`. |
688
+ | `@memLoadU8(addr)` | the byte at `addr` as an `Int`, 0 to 255. |
689
+ | `@memStoreF32(addr, v)` | stores the `Float` `v` at `addr` as a 32-bit float. |
690
+ | `@memLoadF32(addr)` | the 32-bit float at `addr` as a `Float`. |
691
+ | `@native` | not a call: the whole body of a std function the compiler supplies. See [`@native`](#native). |
692
+
693
+ ### Reference identity
694
+
695
+ ```plum
696
+ @sameRef(a, a) # true
697
+ @sameRef(a, Box(..a)) # false: a copy is another object, however equal
698
+ ```
699
+
700
+ A persistent structure uses `@sameRef` to keep what didn't change: a walk that rebuilds a node only when a part of it is another object returns the old node otherwise, so an unchanged tree comes back as the very same tree.
701
+
702
+ Both arguments have the same type, and it must be a type whose values live on the heap: a struct that isn't flat, an enum, a trait object, `Str`, `Slice`, `List`, `Array` and the like. The checker rejects a scalar (`Int`, `Float`, `Bool`, `Byte`), `Unit`, a flat struct (it is copied by value, so it has no identity), a function, and a generic type that one of its instantiations makes any of those. Compare values with `==`.
703
+
704
+ In-place updates never fool it: the compiler never updates a variable passed to `@sameRef` in place, so a method that changes a field of it always returns another object.
705
+
706
+ ### Linear memory
707
+
708
+ The `@mem` built-ins read and write the program's linear memory: one growable wasm memory, 16 pages of 64 KiB to start, exported to the host as `memory`. It exists only in a program that uses one of them. Addresses are byte offsets from 0; every access is little-endian and stops the program when it is out of bounds.
709
+
710
+ ```plum
711
+ @memStoreI32(16, -5)
712
+ @memLoadI32(16) # -5
713
+ @memStoreU8(40, 300)
714
+ @memLoadU8(40) # 44: the low 8 bits
715
+ @memSize() # 1048576
716
+ @memGrow(1) # 16, and the memory is one page longer
717
+ ```
718
+
719
+ ### `@native`
720
+
721
+ ```plum
722
+ primitive Int =
723
+ fun popCount(self) -> Int = @native
724
+ ```
725
+
726
+ `@native` as a function's whole body says the compiler supplies the body: a wasm instruction (`Float.sqrt`, `Int.popCount`), an array operation (`Array.get`), or what a built-in does. Only std declares such functions, and the compiler knows each by its name (`Int.popCount`, `math.sqrt`), not by the file it is in.
727
+
728
+ It is an error to write `@native` anywhere else: inside an expression, in a block with other statements, in a closure, a constant or a test, or in a file outside std. A `@native` function the compiler has no implementation for is an error at the function.
681
729
 
682
730
  ## Modules
683
731
 
docs/std.md CHANGED
@@ -214,7 +214,8 @@ fun main(io: Io) =
214
214
  - **`Uuid`:** `Uuid.v4(rng)`, `Uuid.fromStr`, `Uuid.isValid`
215
215
  - **`base64`:** `base64.encode`, `base64.decode`, `base64.encodeURL`
216
216
  - **`Aabb`:** an axis-aligned box, corners included: `Aabb.of(x, y, w, h)`, `width`, `height`, `center`, `overlaps`, `contains`, `containsPoint`, `union`, `distSq`, `rayEntry`
217
- - **`ref`:** `ref.same(a, b)` is true when `a` and `b` are the very same object, not just equal ones: `ref.same(a, Box(..a))` is false. Only for values on the heap (a struct that isn't flat, an enum, a trait object, `Str`, `List`, ...); the checker rejects a scalar, a flat struct or a function. A persistent structure uses it to keep what didn't change: the scene tree's `input` and `Game`'s tick return a node as it was when its actor came back as the same object (see [the game engine](game.md)). The compiler never updates a value compared by `ref.same` in place, so an update always shows up as another object
217
+ - **Reference identity:** the built-in `@sameRef(a, b)` is true when `a` and `b` are the very same object, not just equal ones (see [Built-ins](language.md#built-ins)); there is nothing to import. A persistent structure uses it to keep what didn't change: the scene tree's `input` and `Game`'s tick return a node as it was when its actor came back as the same object (see [the game engine](game.md))
218
+ - **Linear memory:** the built-ins `@memSize`, `@memGrow`, `@memStoreI32`, `@memLoadI32`, `@memStoreU8`, `@memLoadU8`, `@memStoreF32` and `@memLoadF32` read and write the program's wasm memory (see [Built-ins](language.md#linear-memory))
218
219
  - **`io.args()`:** the program's arguments as a `Slice[Str]`: what follows `--` in `plum run app.plum -- a b`, or a built executable's own arguments (empty under `plum test` and in the browser)
219
220
  - **`io.nowMicros()`:** a wall clock in microseconds since the Unix epoch, as a `Float`, on every host
220
221