plum

#treesitter#compiler#wasm

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

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


e9f7d66 — Peter John 2026-11-10T12:11:02+05:30
std: compare on Int, Float, Str, Bool and Byte; Str gets < <= > >=
Files changed (5) hide show
  1. std/Bool.plum +5 -0
  2. std/Byte.plum +4 -0
  3. std/Float.plum +15 -0
  4. std/Int.plum +5 -2
  5. std/Str.plum +31 -0
std/Bool.plum CHANGED
@@ -1,6 +1,7 @@
1
1
  module std
2
2
 
3
3
  import std/Str
4
+ import std/Int
4
5
  import std/Option
5
6
  import std/Result
6
7
 
@@ -14,6 +15,10 @@ primitive Bool =
14
15
  fun toStr(self) -> Str =
15
16
  if self then "true" else "false"
16
17
 
18
+ # `false` before `true`: the order of `Map` keys.
19
+ fun compare(self, o: Bool) -> Int =
20
+ if self == o then 0 else if self then 1 else -1
21
+
17
22
  # Parses exactly `"true"` or `"false"`.
18
23
  fun parse(s: Str) -> Result[Bool, BoolError] =
19
24
  if s == "true"
std/Byte.plum CHANGED
@@ -17,3 +17,7 @@ primitive Byte =
17
17
 
18
18
  fun toStr(self) -> Str =
19
19
  Int(self).toStr()
20
+
21
+ # -1, 0 or 1 as `self` is below, equal to or above `o`: the order of `Map` keys.
22
+ fun compare(self, o: Byte) -> Int =
23
+ Int(self).compare(Int(o))
std/Float.plum CHANGED
@@ -67,6 +67,21 @@ primitive Float =
67
67
  # they do an Int's. Floats that are `==` hash the same: `0.0` and `-0.0` both hash to 0.
68
68
  # Every NaN hashes the same (to the bits of `0.0 / 0.0`, as wasm makes it), though a NaN is
69
69
  # never `==` to anything, so it never finds itself as a key.
70
+ # A total order for `Map` keys: -1, 0 or 1 as `self` is below, equal to or above `o`.
71
+ # `-0.0` and `0.0` compare equal, and every NaN equals every other NaN and comes after
72
+ # `Infinity`.
73
+ fun compare(self, o: Float) -> Int =
74
+ if self < o
75
+ -1
76
+ else if self > o
77
+ 1
78
+ else if self == o
79
+ 0
80
+ else if self != self
81
+ if o != o then 0 else 1
82
+ else
83
+ -1
84
+
70
85
  fun hash(self) -> Int =
71
86
  if self == 0.0f
72
87
  0
std/Int.plum CHANGED
@@ -57,11 +57,14 @@ primitive Int =
57
57
  fun sign(self) -> Int =
58
58
  if self > 0 then 1 else if self < 0 then -1 else 0
59
59
 
60
- # An `Int` already IS its own well-distributed bit pattern — used by
60
+ # An `Int` already IS its own well-distributed bit pattern (FHamt buckets `Int` keys by it).
61
- # `Map[K, V]` to bucket `Int` keys.
62
61
  fun hash(self) -> Int =
63
62
  self
64
63
 
64
+ # -1, 0 or 1 as `self` is below, equal to or above `o`: the order of `Map` keys.
65
+ fun compare(self, o: Int) -> Int =
66
+ if self < o then -1 else if self > o then 1 else 0
67
+
65
68
  # An Int is always already whole, so trunc/floor/ceil/round are all just
66
69
  # the value itself widened to Float.
67
70
  fun trunc(self) -> Float =
std/Str.plum CHANGED
@@ -181,6 +181,31 @@ struct Str(data: Array[Byte], start: Int, len: Int) =
181
181
  else
182
182
  Array.equal(self.data, self.start, o.data, o.start, n)
183
183
 
184
+ # -1, 0 or 1 as `self` sorts before, equal to or after `o`, byte by byte (for UTF-8 that
185
+ # is code point order; a prefix sorts first): the order of `Map` keys and of `<`.
186
+ fun compare(self, o: Str) -> Int =
187
+ n := self.length()
188
+ m := o.length()
189
+ compareFrom(self, o, 0, if n < m then n else m, n - m)
190
+
191
+ # `compare` from byte `i` on, with `common` bytes to look at and `tie` the length difference.
192
+ fun compareFrom(a: Str, b: Str, i: Int, common: Int, tie: Int) -> Int =
193
+ if i >= common
194
+ if tie < 0 then -1 else if tie > 0 then 1 else 0
195
+ else
196
+ x := a.byteAtUnchecked(i)
197
+ y := b.byteAtUnchecked(i)
198
+ if x < y then -1 else if x > y then 1 else compareFrom(a, b, i + 1, common, tie)
199
+
200
+ # `<`, `<=`, `>` and `>=` follow `compare`.
201
+ fun lt(self, o: Str) -> Bool = self.compare(o) < 0
202
+
203
+ fun le(self, o: Str) -> Bool = self.compare(o) <= 0
204
+
205
+ fun gt(self, o: Str) -> Bool = self.compare(o) > 0
206
+
207
+ fun ge(self, o: Str) -> Bool = self.compare(o) >= 0
208
+
184
209
  fun toStr(self) -> Str =
185
210
  self
186
211
 
@@ -914,3 +939,9 @@ test "clone detaches a substring and Buffer.toStr copies"
914
939
  b.write("z")
915
940
  assert first == "xy"
916
941
  assert b.toStr() == "xyz"
942
+
943
+ test "compare and < go byte by byte, a prefix first"
944
+ assert "a".compare("b") == -1 && "b".compare("a") == 1 && "ab".compare("ab") == 0
945
+ assert "a" < "ab" && "ab" < "b" && "" < "a" && !{"b" < "b"} && "b" <= "b" && "c" > "b" && "c" >= "c"
946
+ assert "Z" < "a" && "é" > "z"
947
+ assert "xab".sub(1, 3).compare("ab") == 0