~repos /plum

#treesitter#compiler#wasm

git clone https://pyrossh.dev/repos/plum.git
Discussions: https://groups.google.com/g/rust-embed-devs

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


33c64378 pyrossh

1 year ago
docs
Files changed (2) hide show
  1. readme.md +55 -78
  2. std/list.mi +115 -4
readme.md CHANGED
@@ -8,14 +8,14 @@ Here is some sample code, please enjoy.
8
8
  ```rs
9
9
  module lambda
10
10
 
11
- import pacos/list
11
+ import ca/list
12
12
  import pacos/math
13
13
  import pacos/http
14
14
 
15
- const start_year = 2101
15
+ START_YEAR = 2101
16
- const end_year = 2111
16
+ END_YEAR = 2111
17
- const name: String = "Gleam"
17
+ NAME = "Gleam"
18
- const size: Int = 100
18
+ SIZE = 100
19
19
 
20
20
  fn sum(a: int, b: int): int = a + b
21
21
 
@@ -59,13 +59,7 @@ class Cat[A: Comparable & Stringable] is Stringable:
59
59
  name: str
60
60
  age: int
61
61
 
62
- fn Cat():
63
- pass
64
-
65
- fn ~Cat():
66
- pass
67
-
68
- fn Cat.with_name(name: str):
62
+ fn Cat.with_name(name: str):
69
63
  Cat(name: name, age: 0)
70
64
 
71
65
  fn fullname() -> str:
@@ -77,20 +71,14 @@ class Cat[A: Comparable & Stringable] is Stringable:
77
71
  fn to_str() -> str:
78
72
  "Cat<{fullname()}, ${age}>"
79
73
 
80
- record Cat[A](
74
+ record Cat[A: Comparable & Stringable](
81
75
  name: str
82
76
  age: int
83
- ) where A is Comparable & Stringable
77
+ ) is Stringable
84
78
 
85
79
  fn Cat.with_name(name: str) =
86
80
  Cat(name: name, age: 0)
87
81
 
88
- fn (c: Cat) init(): str =
89
- c.name + c.age.to_str()
90
-
91
- fn (c: Cat) deinit(): str =
92
- c.name + c.age.to_str()
93
-
94
82
  fn (c: Cat) fullname(): str =
95
83
  c.name + c.age.to_str()
96
84
 
@@ -232,30 +220,65 @@ println("Name ${name} age ${age}")
232
220
  **list**
233
221
 
234
222
  ```rs
223
+ import pacos/list
224
+
235
225
  a := [1, 2, 3] // list[int]
236
226
  b := [[1, 2], [3, 4], [5, 6]] // list[list[int]]
227
+
228
+ actors := List.new("Krabs", "Squidward")
229
+ actors.add("Spongebob")
230
+ actors.length() // ==> 3
231
+ actors.contains("Krabs") // ==> true
232
+ actors.get(0) // => "Krabs"
233
+ actors.get(5) // => nil
237
234
  ```
238
235
 
239
236
  **map**
240
237
 
241
238
  ```rs
239
+ import pacos/map
240
+
241
+ nums := Map.new(:one => 1, :two => 2)
242
+ map.get(:one) // =>`
243
+ map.get(:unknown) // => nil
244
+
242
- tree = [
245
+ friends_tree := [
243
- value: "Fred",
246
+ :value => "Fred",
244
- left: [
247
+ :left => [
245
- value: "Jim",
248
+ :value => "Jim",
246
249
  ],
247
- right: [
250
+ :right => [
248
- value: "Shiela",
251
+ :value => "Shiela",
249
- left: [
252
+ :left => [
250
- value: "Alice",
253
+ :value => "Alice",
251
254
  },
252
- right: [
255
+ :right => [
253
- value: "Bob"
256
+ :value => "Bob"
254
257
  ],
255
258
  ],
256
259
  ]
257
260
  ```
258
261
 
262
+ **Constant statement**
263
+
264
+ Constants can be declared at the top level of a program. They cannot be reassigned.
265
+ * Primitive values like`int, float, str` are directly replaced in code.
266
+ * Reference values like `list, map, records` are initialized at program start and passed by reference when used. Their data can be modified.
267
+
268
+ ```rs
269
+ PI = 3.14159f
270
+ ERR_MESSAGE = "An unknown error occured"
271
+ COUNT = count(10)
272
+ COUNTRIES_LIST = ["US", "INDIA", "CANADA"]
273
+ COUNTRY_CODES = [
274
+ :in => "INDIA",
275
+ :us => "United States",
276
+ :ca => "Canada"
277
+ ]
278
+
279
+ fn count(n: int): int = n * 1
280
+ ```
281
+
259
282
 
260
283
  **Assignment statement**
261
284
 
@@ -265,7 +288,7 @@ x := 10
265
288
  y := 20
266
289
  xy_list := [x, y]
267
290
  xy_map := [x: x, y: y]
268
- assoc_list = [a: 1, b: 2]
291
+ assoc_list = [:a => 1, :b => 2]
269
292
  assoc_list[:a]
270
293
  assoc_list["b"]
271
294
  ```
@@ -279,11 +302,6 @@ while low < high
279
302
  high = cmp < 0 ? mid : high
280
303
  if cmp == 0
281
304
  return mid, true
282
-
283
- while (eventuallyErrorSequence()) |value| {
284
- sum1 += value;
285
- else |err|
286
- try expect(err == error.ReachedZero);
287
305
  ```
288
306
 
289
307
  **For statement**
@@ -305,17 +323,7 @@ let list = for filter(players_list) |v|
305
323
  items
306
324
  .map(|v| v + 1)
307
325
  .each(|v| println("v", v))
308
-
309
- items
310
- .each(v =>
311
- if v == 2
312
- break
313
- if v == 0
314
- continue
315
- println(v)
316
- )
317
- .map {v => v * 2}
318
- .reduce(0, |v| => v + 1)
326
+ .reduce(0, |v| v + 1)
319
327
 
320
328
  fn range(start: int, end: int, cb: (v: T) -> IterateResult) =
321
329
 
@@ -323,9 +331,6 @@ range(0, 5, |v| =>
323
331
  sum3 += v
324
332
  )
325
333
 
326
- // Iterate over multiple objects.
327
- // All lengths must be equal at the start of the loop, otherwise detectable
328
- // illegal behavior occurs.
329
334
  for items, items2 |i, j|
330
335
  count += i + j
331
336
  ```
@@ -354,34 +359,6 @@ match xs
354
359
  [a] -> "This list has 1 element"
355
360
  [a, b] -> "This list has 2 elements"
356
361
  _ -> "This list has more than 2 elements"
357
-
358
- import palm/list
359
-
360
- list.new("Krabs")
361
- |> list.add("Spongebob")
362
- |> list.length() // ==> 3
363
- |> list.contains("Krabs") // ==> true
364
- |> list.get(0) // => Some("Krabs")
365
- |> list.get(5) // => None
366
-
367
- let x = list.new(2, 3)
368
- let y = list.new(1, ..x)
369
-
370
- import palm/map
371
-
372
- let nums = map.new(:one => 1, :two => 2) // => Map(k, v)
373
- nums |> map.get(:one) // => Some(1)
374
- nums |> map.get(:unknown) // => None
375
-
376
- import palm/io.{println}
377
- import palm/result.{Ok, Error}
378
-
379
- let connect_res = Error("Connection failed")
380
-
381
- case connect_res {
382
- Ok(a) -> println("Connection succeeded")
383
- Err(a) -> println("Error occurred: {a}")
384
- }
385
362
  ```
386
363
 
387
364
  Arithmetic (+, -, /, *, @divFloor, @sqrt, @ceil, @log, etc.)
std/list.mi CHANGED
@@ -5,10 +5,10 @@ module std
5
5
  record list[V](
6
6
  head: node[V]?
7
7
  tail: node[V]?
8
- size: int
8
+ _size: int
9
9
  )
10
10
 
11
- `A node captures the data value in a list and contains pointers to the previous and next sibling nodes
11
+ `A node stores the data in a list and contains pointers to the previous and next sibling nodes
12
12
  record node[V](
13
13
  value: V
14
14
  prev: node[V]?
@@ -30,18 +30,22 @@ fn (l: list) set(i: int, v: V): A =
30
30
  `sets the element at i'th index of the list
31
31
  pass
32
32
 
33
+ fn (l: list) length(): int =
34
+ `returns the no of elements in the list
35
+ _size
36
+
33
37
  fn (l: list) add(c: V) =
34
38
  `adds the specified elements to the start of the list
35
39
  l.head = node(value: v, prev: nil, next: l.head)
36
40
  l.head.next.prev = l.head
37
- l.size += 1
41
+ l._size += 1
38
42
 
39
43
  fn (l: list) remove(i int) =
40
44
  `removes the element at i'th index of the list
41
45
  l.tail?.prev?.next = nil
42
46
  `old tail node gets deallocated due to zero reference count
43
47
  l.tail = list.tail?.prev
44
- l.size -= 1
48
+ l._size -= 1
45
49
 
46
50
  fn (l: list) reverse(v: fn(v: V): true): list[A] =
47
51
  `returns a new list with the elements in reverse order.
@@ -154,3 +158,110 @@ fn (l: list) to_str(): str =
154
158
  else
155
159
  res.write(@TypeToString(v))
156
160
  res.to_str()
161
+
162
+
163
+
164
+ class List[V]:
165
+ head: node[V]?
166
+ tail: node[V]?
167
+ size: int
168
+
169
+ fn get(i: int): A =
170
+ `gets the element at i'th index of the list
171
+ current := l.head
172
+ index := 0
173
+ while current != nil
174
+ if index == i
175
+ current.value
176
+ else
177
+ current = current.next
178
+ index += 1
179
+
180
+ fn set(i: int, v: V): A =
181
+ `sets the element at i'th index of the list
182
+ pass
183
+
184
+ fn add(c: V) =
185
+ `adds the specified elements to the start of the list
186
+ l.head = node(value: v, prev: nil, next: l.head)
187
+ l.head.next.prev = l.head
188
+ l._size += 1
189
+
190
+ fn remove(i int) =
191
+ `removes the element at i'th index of the list
192
+ l.tail?.prev?.next = nil
193
+ `old tail node gets deallocated due to zero reference count
194
+ l.tail = list.tail?.prev
195
+ l._size -= 1
196
+
197
+ fn reverse(v: fn(v: V): true): list[A] =
198
+ `returns a new list with the elements in reverse order.
199
+ pass
200
+
201
+ fn sort(sorter: fn(v: V): true): list[A] =
202
+ `returns a new list with the elements sorted by sorter
203
+ pass
204
+
205
+ fn find(search: V): V? =
206
+ `returns an item in the list if the item is is equal to search item
207
+ pass
208
+
209
+ fn find_index(search: V): int? =
210
+ `returns the index of an item in the list if present and comparable otherwise nil
211
+ pass
212
+
213
+ fn contains(s: V): bool =
214
+ `returns whether s is present in the list or not
215
+ pass
216
+
217
+ fn concat(ol: list): list[A] =
218
+ `returns the index of an item in the list if present and comparable otherwise nil
219
+ pass
220
+
221
+ fn sub_list(start: int, end: int?): list[A] =
222
+ `returns the index of an item in the list if present and comparable otherwise nil
223
+ pass
224
+
225
+ fn each(cb: fn(v: V)): void =
226
+ `calls f for each elem in the list
227
+ current := l.head
228
+ while current != nil
229
+ cb(current.value)
230
+ current = current.next
231
+
232
+ fn map[B](cb: fn(v: V): B): list[A] =
233
+ `returns a list made up of B elements for each elem in the list
234
+ nl := []
235
+ current := l.head
236
+ while current != nil
237
+ item := cb(current.value)
238
+ nl.push(item)
239
+ nl
240
+
241
+ fn flat_map() =
242
+ `returns a new list with all elements shuffled`
243
+ pass
244
+
245
+ fn retain(predicate: fn(v: V): A): list[A] =
246
+ `returns a new list with the elements that matched the predicate
247
+ pass
248
+
249
+ fn reject(predicate: fn(v: V): A): list[A] =
250
+ `returns a new list with the elements that matched the predicate removed
251
+ pass
252
+
253
+ fn any(predicate: fn(v: V): bool) =
254
+ `returns true if any element in the list satisfies the predicate
255
+ pass
256
+
257
+ fn all(predicate: fn(v: V): bool) =
258
+ `returns true if all of the elements in the list satisfies the predicate
259
+ pass
260
+
261
+ fn reduce[B](acc: B, cb: fn(v: V): A): B =
262
+ `returns the accumulated value of all the elements in the list
263
+ pass
264
+
265
+ fn first(): A? =
266
+ `returns the first element in the list
267
+ l.head?.value