plum

#treesitter#compiler#wasm

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

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


f92e171Peter John 2026-07-24T11:45:33+05:30
feat(libs/std): migrate stdlib generics to bracket syntax
libs/std/list.plum CHANGED
@@ -3,23 +3,23 @@ module std
3
3
  import std/option
4
4
 
5
5
  # A node stores the data in a list and contains pointers to the previous and next sibling nodes
6
- type Node(a) =
6
+ type Node[T] =
7
- value: a
7
+ value: T
8
8
  prev: Option[Node]
9
9
  next: Option[Node]
10
10
 
11
11
  # A list is a data structure describing a contiguous section of an array stored separately from the slice variable itself.
12
12
  # It contains the pointers to the start and end nodes (head, tail) and maintains the size as well
13
- type List(Stringable)(a: Stringable) =
13
+ type List[T: Stringable](Stringable) =
14
14
  head: Option[Node]
15
15
  tail: Option[Node]
16
16
  size: Int
17
17
 
18
- init<List>(self, values: ...a) -> List =
18
+ makeList(values: ...T) -> List =
19
- List().add(values)
19
+ List(None, None, 0).add(values)
20
20
 
21
21
  # gets the element at i'th index of the list
22
- get<List>(self, i: Int) -> Option(a) =
22
+ get<List>(self, i: Int) -> Option[T] =
23
23
  current = self.head
24
24
  index = 0
25
25
  while current != None
@@ -34,7 +34,7 @@ get<List>(self, i: Int) -> Option(a) =
34
34
  None
35
35
 
36
36
  # sets the element at i'th index of the list
37
- set<List>(self, i: Int, v: a) -> Option(a) =
37
+ set<List>(self, i: Int, v: T) -> Option[T] =
38
38
  todo
39
39
 
40
40
  # returns the no of elements in the list
@@ -42,7 +42,7 @@ length<List>(self) -> Int =
42
42
  self.size
43
43
 
44
44
  # adds the specified elements to the start of the list
45
- add<List>(self, values: ...a) =
45
+ add<List>(self, values: ...T) =
46
46
  todo
47
47
 
48
48
  # removes the element at i'th index of the list
@@ -50,7 +50,7 @@ removeAt<List>(self, i: Int) =
50
50
  todo
51
51
 
52
52
  # removes the element v from list
53
- remove<List>(self, v: a) =
53
+ remove<List>(self, v: T) =
54
54
  todo
55
55
 
56
56
  # removes all objects from this list
@@ -58,23 +58,23 @@ clear<List>(self) =
58
58
  todo
59
59
 
60
60
  # returns a new list with the elements in reverse order.
61
- reverse<List>(self, v: fn(a) -> Bool) -> List =
61
+ reverse<List>(self, v: fn(T) -> Bool) -> List =
62
62
  todo
63
63
 
64
64
  # returns a new list with the elements sorted by sorter
65
- sort<List>(self, sorter: fn(a) -> Bool) -> List =
65
+ sort<List>(self, sorter: fn(T) -> Bool) -> List =
66
66
  todo
67
67
 
68
68
  # returns an item and index in the list if the item is is equal to search item
69
- find<List>(self, search: a) -> Option(a) =
69
+ find<List>(self, search: T) -> Option[T] =
70
70
  todo
71
71
 
72
72
  # returns the index of an item in the list if present and comparable otherwise None
73
- contains<List>(self, v: a) -> Bool =
73
+ contains<List>(self, v: T) -> Bool =
74
74
  todo
75
75
 
76
76
  # calls f for each elem in the list
77
- each<List>(self, cb: fn(a)) -> Unit =
77
+ each<List>(self, cb: fn(T)) -> Unit =
78
78
  current = self.head
79
79
  while current != None
80
80
  match current
@@ -85,7 +85,7 @@ each<List>(self, cb: fn(a)) -> Unit =
85
85
  break
86
86
 
87
87
  # returns a list made up of b elements for each elem in the list
88
- map<List>(self, cb: fn(a) -> b) -> List(b) =
88
+ map<List>(self, cb: fn(T) -> U) -> List[U] =
89
89
  nl = List()
90
90
  current = self.head
91
91
  while current != None
@@ -103,27 +103,27 @@ flatMap<List>(self) =
103
103
  todo
104
104
 
105
105
  # returns a new list with the elements that matched the predicate
106
- retain<List>(self, predicate: fn(a) -> a) -> List =
106
+ retain<List>(self, predicate: fn(T) -> T) -> List =
107
107
  todo
108
108
 
109
109
  # returns a new list with the elements that matched the predicate removed
110
- reject<List>(self, predicate: fn(a) -> a) -> List =
110
+ reject<List>(self, predicate: fn(T) -> T) -> List =
111
111
  todo
112
112
 
113
113
  # returns true if any element in the list satisfies the predicate
114
- any<List>(self, predicate: fn(a) -> Bool) -> Bool =
114
+ any<List>(self, predicate: fn(T) -> Bool) -> Bool =
115
115
  todo
116
116
 
117
117
  # returns true if all of the elements in the list satisfies the predicate
118
- every<List>(self, predicate: fn(a) -> Bool) -> Bool =
118
+ every<List>(self, predicate: fn(T) -> Bool) -> Bool =
119
119
  todo
120
120
 
121
121
  # returns the accumulated value of all the elements in the list
122
- reduce<List>(self, acc: b, cb: fn(a) -> a) -> Option(b) =
122
+ reduce<List>(self, acc: U, cb: fn(T) -> T) -> Option[U] =
123
123
  todo
124
124
 
125
125
  # returns the first element in the list
126
- first<List>(self) -> Option(a) =
126
+ first<List>(self) -> Option[T] =
127
127
  match self.head
128
128
  Some(node) =>
129
129
  Some(node.value)
@@ -131,7 +131,7 @@ first<List>(self) -> Option(a) =
131
131
  None
132
132
 
133
133
  # returns the last element in the list
134
- last<List>(self) -> Option(a) =
134
+ last<List>(self) -> Option[T] =
135
135
  match self.tail
136
136
  Some(node) =>
137
137
  Some(node.value)
libs/std/map.plum CHANGED
@@ -1,14 +1,14 @@
1
1
  module std
2
2
 
3
3
  # A Pair is a grouping of a key with a value
4
- type Pair(a, b) =
4
+ type Pair[K, V] =
5
- key: a
5
+ key: K
6
- val: b
6
+ val: V
7
7
 
8
8
  # A Map is a data structure describing a contiguous section of an array stored separately from the slice variable itself.
9
9
  # A Map is not an array. A slice describes a piece of an array.
10
- type Map(a, b) =
10
+ type Map[K, V] =
11
- items: List(Pair(a, b))
11
+ items: List[Pair[K, V]]
12
12
 
13
13
  init<Map>(self, kvs: ...Pair) -> Map =
14
14
  Map().add(kvs)
@@ -18,19 +18,19 @@ add<Map>(self, kvs: ...Pair) =
18
18
  self.items.add(kvs)
19
19
 
20
20
  # gets a value from the Map using key k
21
- get<Map>(self, k: a) -> Option(b) =
21
+ get<Map>(self, k: K) -> Option[V] =
22
22
  for p in self.items
23
23
  if p.key == k
24
24
  return Some(p.val)
25
25
  None
26
26
 
27
27
  # puts a value into the Map
28
- set<Map>(self, k: a, v: b) =
28
+ set<Map>(self, k: K, v: V) =
29
29
  self.items.add(Pair(key: k, val: v))
30
30
 
31
31
  # puts a value into the Map if its not already present
32
- putIfAbsent<Map>(self, k: a, v: b) =
32
+ putIfAbsent<Map>(self, k: K, v: V) =
33
33
  todo
34
34
 
35
- map<Map>(self, cb: fn(Pair(a, b)) -> Pair(c, d)) -> Map(c, d) =
35
+ map<Map>(self, cb: fn(Pair[K, V]) -> Pair[X, Y]) -> Map[X, Y] =
36
36
  self.items.map(cb)
libs/std/option.plum CHANGED
@@ -1,5 +1,5 @@
1
1
  module std
2
2
 
3
3
  enum Option =
4
- | Some(a)
4
+ | Some[T]
5
5
  | None
libs/std/result.plum CHANGED
@@ -1,8 +1,8 @@
1
1
  module std
2
2
 
3
3
  enum Result =
4
- | Ok(a)
4
+ | Ok[T]
5
- | Err(b)
5
+ | Err[E]
6
6
 
7
7
  # checks whether the result is an Ok value
8
8
  isOk<Result>(self) -> Bool =