~repos /plum

#treesitter#compiler#wasm

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

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


e2157e48 Peter John

1 year ago
improve grammar
Files changed (4) hide show
  1. sample.ks +19 -0
  2. std/bool.ks +41 -13
  3. std/map.mi +4 -3
  4. std/str.mi +34 -32
sample.ks CHANGED
@@ -2,6 +2,11 @@ module sample
2
2
 
3
3
  import std.{List, Map, Math, Random}
4
4
 
5
+ Int.random()
6
+ Float.random()
7
+
8
+ Float.PI
9
+
5
10
  trait BinaryOp<T> {
6
11
  fn add(rhs: T) -> T
7
12
  fn sub(rhs: T) -> T
@@ -194,3 +199,17 @@ fn handler(req: Request) -> Response {
194
199
  fn hasFlag(b: bool, d: List) {
195
200
  return data
196
201
  }
202
+
203
+ record Greeter(name: String) {
204
+ fn greet() {
205
+ println("Hello, ${name}")
206
+ }
207
+ }
208
+
209
+ fn main(args: List[String]) {
210
+ let g = Greeter(name: "abc")
211
+ g = g.copy(
212
+ name = "123"
213
+ )
214
+ Greeter(...g, name: "123")
215
+ }
std/bool.ks CHANGED
@@ -1,25 +1,53 @@
1
1
  module std
2
2
 
3
3
  trait Bool : ToStr permits True, False {
4
- operator fn eq(other: Bool) {
4
+ operator fn eq(other: Bool) -> Bool
5
+ operator fn neq(other: Bool) -> Bool
6
+ operator fn and(other: Bool) -> Bool
7
+ operator fn or(other: Bool) -> Bool
8
+ operator fn not() -> Bool
9
+ override fn toStr() -> Str
10
+ }
11
+
12
+ tuple True() : Bool {
5
- return (self is True && other is True) || (self is False && other is False)
13
+ operator fn eq(other: Bool) -> Bool {
14
+ return other is True
6
15
  }
7
- operator fn neq(other: Bool) {
16
+ operator fn neq(other: Bool) -> Bool {
8
17
  return !self.eq()
9
18
  }
10
- operator fn and(other: Bool) {
19
+ operator fn and(other: Bool) -> Bool {
11
- return self is False || other is False ? False : True
20
+ return other is False ? False : True
12
21
  }
13
- operator fn or(other: Bool) {
22
+ operator fn or(other: Bool) -> Bool {
14
- return self is True || other is True ? True : False
23
+ return True
15
24
  }
16
- operator fn not(): Bool {
25
+ operator fn not() -> Bool {
17
- return self is True ? False : True
26
+ return False
18
27
  }
19
- override fn toStr() {
28
+ override fn toStr() -> Str {
20
- return self is True ? "true" : "false"
29
+ return "True"
21
30
  }
22
31
  }
23
32
 
24
- tuple True() : Bool {}
33
+
25
- tuple False() : Bool {}
34
+ tuple False() : Bool {
35
+ operator fn eq(other: Bool) -> Bool {
36
+ return other is False
37
+ }
38
+ operator fn neq(other: Bool) -> Bool {
39
+ return !self.eq()
40
+ }
41
+ operator fn and(other: Bool) -> Bool {
42
+ return False
43
+ }
44
+ operator fn or(other: Bool) -> Bool {
45
+ return other is True ? True : False
46
+ }
47
+ operator fn not() -> Bool {
48
+ return True
49
+ }
50
+ override fn toStr() -> Str {
51
+ return "False"
52
+ }
53
+ }
std/map.mi CHANGED
@@ -4,10 +4,11 @@ record pair[K: Comparable, V] where
4
4
  | k: K
5
5
  | v: V
6
6
 
7
+ `A Map is a data structure describing a contiguous section of an array stored separately from the slice variable itself.
8
+ `A Map is not an array. A slice describes a piece of an array.
7
9
  struct Map[K, V] where
8
- `A Map is a data structure describing a contiguous section of an array stored separately from the slice variable itself.
10
+
9
- `A Map is not an array. A slice describes a piece of an array.
10
- | items: list[pair[K, V]]
11
+ let items: list[pair[K, V]]
11
12
 
12
13
  static fn of[K, V](kvs: ...pair[K, V]): Map[K, V] =
13
14
  Map[K, V]().add(kvs)
std/str.mi CHANGED
@@ -5,38 +5,40 @@ trait ToStr(
5
5
  fn toStr(): Str
6
6
  )
7
7
 
8
- record Str(
9
- `A str is an array of contiguous data stored in memory with a null termination using hex 0x00 or ASCII 0x00.
8
+ `A str is an array of contiguous data stored in memory with a null termination using hex 0x00 or ASCII 0x00.
10
- `It is immutable and cannot be modified. It is copied for any changes and saved to a new memory location.
9
+ `It is immutable and cannot be modified. It is copied for any changes and saved to a new memory location.
11
- `The previous str is freed if its reference count is 0 within the block.
10
+ `The previous str is freed if its reference count is 0 within the block.
12
- data: list[byte]
13
- ): Comparable, ToStr
11
+ record Str(data: Buffer): Comparable, ToStr {
14
-
15
- fn strOf(bs ...byte): Str =
16
- data.add(bs)
17
-
18
- fn<str> get(i: int) =
12
+ fn get(i: int) {
19
- data[i]
13
+ return data.get(i)
20
-
14
+ }
15
+
21
- fn (str) contains(search: str): bool =
16
+ fn contains(search: Str) -> Bool {
22
- pass
17
+ pass
23
-
18
+ }
19
+
24
- fn (str) index_of(sub: str): int =
20
+ fn index_of(sub: Str) -> Int {
25
- pass
21
+ pass
26
-
22
+ }
23
+
27
- fn (str) test(pattern: Regex): []str =
24
+ fn test(pattern: Regex) -> Bool {
28
- pass
25
+ pass
29
-
26
+ }
27
+
30
- fn (str) search(key str): int, bool =
28
+ fn search(key: Str) -> (Int, Bool) {
31
- low, mid, high := 0, 0, n.numItems
29
+ val (low, mid, high) = (0, 0, n.numItems)
32
- while low < high
30
+ while low < high {
33
- mid = (low + high) / 2
31
+ mid = (low + high) / 2
34
- cmp := key > n.items[mid].key
32
+ val cmp := key > n.items[mid].key
35
- low = cmp > 0 ? mid + 1 : low
33
+ low = cmp > 0 ? mid + 1 : low
36
- high = cmp < 0 ? mid : high
34
+ high = cmp < 0 ? mid : high
37
- if cmp == 0
35
+ if cmp == 0 {
38
- return mid, true
36
+ return (mid, True)
37
+ }
38
+ }
39
- low, false
39
+ return (low, False)
40
+ }
41
+ }
40
42
 
41
43
  fn (t BTree) delete(key str): bool =
42
44
  if t.root == nil