~repos /plum

#treesitter#compiler#wasm

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

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


e6e9e7c0 pyrossh

1 year ago
improve
std/map.mi CHANGED
@@ -1,40 +1,74 @@
1
1
  module std
2
2
 
3
- record pair[K: Comparable, V](k: K, v: V)
3
+ record pair[K: Comparable, V] where
4
+ | k: K
5
+ | v: V
4
6
 
5
- record map[K, V](
7
+ struct Map[K, V] where
6
- `A slice is a data structure describing a contiguous section of an array stored separately from the slice variable itself.
8
+ `A Map is a data structure describing a contiguous section of an array stored separately from the slice variable itself.
7
- `A slice is not an array. A slice describes a piece of an array.
9
+ `A Map is not an array. A slice describes a piece of an array.
8
- items: list[pair[K, V]]
10
+ | items: list[pair[K, V]]
11
+
12
+ static fn of[K, V](kvs: ...pair[K, V]): Map[K, V] =
13
+ Map[K, V]().add(kvs)
14
+
15
+ fn add(kvs: ...pair[K, V]) =
16
+ `adds the specified elements to the start of the list
17
+
18
+ fn op_range(yld: fn(k: K, v: V): bool): bool =
19
+ `allows the range operator to iterate over the Map
20
+ for item := range items
21
+ if !yld(item.k, item.v)
22
+ false
23
+ true
24
+
25
+ fn get(k K): Option[V] =
26
+ `get a value from the Map using key k
27
+ for k, v := range m
28
+ if k == k
29
+ return v
9
- )
30
+ nil
31
+
32
+ fn set(k K, v V) =
33
+ `put a value into the Map
34
+ list.add(pair(k, v))
10
35
 
36
+ fn putIf(k K, v V) =
37
+ `put a value into the Map if its not already present
38
+ pass
39
+
40
+
41
+
42
+ `A Map is a data structure describing a contiguous section of an array stored separately from the slice variable itself.
11
- fn Map.of[K, V](kvs: ...pair[K, V]): map[K, V] =
43
+ `A Map is not an array. A slice describes a piece of an array.
12
- map[K, V]().add(kvs)
44
+ record Map[K, V](
45
+ items: List[Pair[K, V]]
46
+ )
13
47
 
14
- fn (m: Map) add(kvs: ...pair[K, V]) =
48
+ fn Map.add(kvs: ...pair[K, V]) =
15
49
  `adds the specified elements to the start of the list
16
50
  items.add(kvs)
17
51
 
18
- fn (m: Map[K, V]) op_range(yld: fn(k: K, v: V): bool): bool =
19
- for item := range items
20
- if !yld(item.k, item.v)
21
- false
22
- true
23
-
24
- fn (m: Map[K, V]) get(k K): V? =
52
+ fn (Map) get(k K): V? =
25
53
  for k, v := range m
26
54
  if k == k
27
55
  return v
28
56
  nil
29
57
 
30
- fn (m: Map) put(k K, v V) =
58
+ fn (Map) put(k K, v V) =
31
59
  list.add(pair(k, v))
32
60
 
33
- fn (m: Map) put_if(k K, v V) =
61
+ fn (Map) put_if(k K, v V) =
34
62
  if
35
63
 
36
- fn (m: Map) update() =
64
+ fn (Map) update() =
37
65
  pass
38
66
 
39
- fn (m: Map) update_if() =
67
+ fn (Map) update_if() =
40
68
  pass
69
+
70
+ fn (Map->Range) range(yld: fn(k: K, v: V): bool): bool =
71
+ for item := range items
72
+ if !yld(item.k, item.v)
73
+ false
74
+ true
std/option.mi CHANGED
@@ -24,7 +24,7 @@ fn (o option[T]) getOrElse(default: T): T =
24
24
  some(val) -> val
25
25
  none -> d
26
26
 
27
- fn (o option[T: ToStr]) toStr(): str =
27
+ fn (Option->ToStr) toStr(): str =
28
28
  match o
29
29
  some(v) -> v.to_str()
30
30
  none -> "none"
website/src/content/docs/declarations/enum.md CHANGED
@@ -6,24 +6,25 @@ description: These are basic types
6
6
  An Algebraic Data Type
7
7
 
8
8
  ```rs
9
- enum Ordering is Stringable =
9
+ enum Ordering =
10
10
  | LT
11
11
  | EQ
12
12
  | GT
13
13
 
14
- fn toStr(): str =
14
+ fn (Ordering->ToStr) toStr(): str =
15
- match self
15
+ match self
16
- LT -> "LT"
16
+ LT -> "LT"
17
- EQ -> "EQ"
17
+ EQ -> "EQ"
18
- GT -> "GT"
18
+ GT -> "GT"
19
19
 
20
- enum Shape where
20
+ enum Shape =
21
21
  | Circle(int)
22
22
  | Square(int)
23
23
  | Rectangle(int, int)
24
24
 
25
- enum Shape where
25
+ fn (Shape) toStr() =
26
- Circle(int)
26
+ match self
27
+ Circle(r) -> printLn("Circle(${r})")
27
- Square(int)
28
+ Square(s) -> printLn("Square(${s})")
28
- Rectangle(int, int)
29
+ Rectangle(w, h) -> printLn("Rectangle(${w}, ${h})")
29
- ```
30
+ ```
website/src/content/docs/declarations/records.md CHANGED
@@ -7,10 +7,10 @@ A record is a collect of data indexed by fields. It is a reference type and refe
7
7
 
8
8
  ```rs
9
9
  struct Cat is Stringable where
10
- | name: str
10
+ name: str
11
- | age: int
11
+ age: int
12
12
 
13
- fn Cat.withName(name: str): Cat =
13
+ static fn withName(name: str): Cat =
14
14
  Cat(name: name, age: 0)
15
15
 
16
16
  fn fullname(): str =
@@ -19,6 +19,119 @@ struct Cat is Stringable where
19
19
  fn talk() =
20
20
  printLn("cat ${name} says meow")
21
21
 
22
+ override fn plus(other: Cat): Cat =
23
+ Cat(...self, age: self.age + other.age)
24
+
22
- fn toStr(): str =
25
+ override fn toStr(): str =
23
26
  "Cat<${fullname()}, ${age}>"
27
+
28
+ struct Rand(seed: int) =
29
+ self.seed = seed
30
+
31
+ fn Rand.int(): int =
32
+ 1 * 2 * seed
33
+
34
+ struct Cat(name: str, age: int) =
35
+ self.name = name
36
+ self.age = age
37
+
38
+ fn Cat::withName(name: str): Cat =
39
+ Cat(name: name, age: 0)
40
+
41
+ fn Cat.fullname(): str =
42
+ name + age.toStr()
43
+
44
+ fn Cat.talk() =
45
+ printLn("cat ${name} says meow")
46
+
47
+ fn Cat.plus(other: Cat): Cat =
48
+ Cat(...self, age: self.age + other.age)
49
+
50
+ fn Cat.toStr(): str =
51
+ "Cat<${fullname()}, ${age}>"
52
+
53
+ record Response
54
+ field _body: Buffer
55
+ field _headers: Map[str, str]
56
+ field _status: int
57
+
58
+ fn _init() =
59
+ _body = Buffer()
60
+ _headers = Map[str, str]()
61
+ _status = 0
62
+
63
+ fn header(kv Pair): Self =
64
+ _headers.add(kv)
65
+
66
+ fn body(b Buffer): Self =
67
+ _body = b
68
+
69
+ fn status(v int): Self =
70
+ _status = v
71
+
72
+ struct Map[K, V](kvs: ...Pair[K, V]) =
73
+ `A Map is a data structure describing a contiguous section of an array stored separately from the slice variable itself.
74
+ `A Map is not an array. A slice describes a piece of an array.
75
+ self._items = List[Pair[K, V]](kvs)
76
+ self.index = int(0)
77
+ self.length = int(kvs.length)
78
+
79
+ fn Map.range(yld: fn(k: K, v: V): bool): bool =
80
+ `provide a range operation for map
81
+ for pair := range self._items
82
+ if !yld(pair.k, pair.v)
83
+ false
84
+ true
85
+
86
+ fn Map.get(k: K): Option[V] =
87
+ for pair := range self._items
88
+ if pair.k == k
89
+ return v
90
+ None
91
+
92
+ fn Map.set(k: K, v: V) =
93
+ for pair := range self._items
94
+ if pair.k == k
95
+ pair.v = v
96
+ return
97
+ self._items.add(k, v)
98
+
99
+ fn Map.putIf(k: K, v: V) =
100
+ `put a value into the Map if its not already present
101
+ return
102
+
103
+ struct Map[K, V] where
104
+ _items: List[Pair[K, V]]
105
+
106
+ static fn of[K, V](kvs: ...Pair[K, V]): Map[K, V] =
107
+ Map[K, V]().add(kvs)
108
+
109
+ fn range(yld: fn(k: K, v: V): bool): bool =
110
+ `allows the range operator to iterate over the Map
111
+ for item := range self._items
112
+ if !yld(item.k, item.v)
113
+ false
114
+ true
115
+
116
+ fn add(kvs: ...Pair[K, V]) =
117
+ `adds the specified elements to the start of the list
118
+
119
+ fn get(k: K): Option[V] =
120
+ `get a value from the Map using key k
121
+ for k, v := range m
122
+ if k == k
123
+ return v
124
+ None
125
+
126
+ fn set(k: K, v: V) =
127
+ `put a value into the Map
128
+ for pair := range self._items
129
+ if pair.k == k
130
+ pair.v = v
131
+ return
132
+ self._items.add(k, v)
133
+
134
+ fn putIf(k: K, v: V) =
135
+ `put a value into the Map if its not already present
136
+ pass
24
- ```
137
+ ```
website/src/content/docs/declarations/traits.md CHANGED
@@ -6,17 +6,38 @@ description: These are basic types
6
6
  Traits are used to implement static/dynamic dispatch
7
7
 
8
8
  ```rs
9
- trait HasEq[A] where
10
- fn eq(other: A): bool
11
-
12
9
  trait Equatable[A] where
13
10
  fn eq(other: A): bool
14
11
  fn ne(other: A): bool
15
12
 
16
- trait Comparable[A: Ord] is Equatable where
13
+ trait Comparable[A: Ord] where
17
- fn lt(other: A): bool
18
- fn le(other: A): bool
19
- fn ge(other: A): bool
20
- fn gt(other: A): bool
21
- fn compare(other: A): int
14
+ fn compareTo(other: A): int
15
+
16
+ trait ArithmeticOperator[A] where
17
+ fn add(other: A): A
18
+ fn subtract(other: A): A
19
+ fn multiply(other: A): A
20
+ fn divide(other: A): A
21
+ fn remainder(other: A): A
22
+
23
+ trait UnaryOperator[A] where
24
+ fn unaryPlus(): A
25
+ fn unaryMinus(): A
26
+ fn increment(): A
27
+ fn decrement(): A
28
+
29
+ trait LogicalOperator[A] where
30
+ fn and(other: A): A
31
+ fn or(other: A): A
32
+ fn not(): A
33
+
34
+ trait AssignmentOperator[A] where
35
+ fn assign(other: A) A
36
+
37
+ trait BitwiseOperator[A] where
38
+ fn bitwiseAnd(other: A): A
39
+ fn bitwiseOr(other: A): A
40
+ fn bitwiseXor(): A
41
+ fn bitwiseLeftShift(): A
42
+ fn bitwiseRightShift(): A
22
43
  ```
website/src/content/docs/operators/logical.md CHANGED
@@ -13,18 +13,8 @@ description: These are basic types
13
13
  **assignment operator**
14
14
 
15
15
  ```go
16
- x := 5
16
+ x, y := 5, 3
17
- y := 3
18
- x += y // 8
17
+ z := x + y // 8
19
- x -= y // 5
20
- x *= y // 15
21
- x /= y // 5
22
- x %= y // 2
23
- x &= y // 2
24
- x |= y // 3
25
- x <<= y // 24
26
- x >>= y // 3
27
- a ?= 2 // elvis assignment operator
28
18
  ```
29
19
 
30
20
  **not operator**
@@ -68,24 +58,26 @@ v := list.of(1, 2, 3)
68
58
  **range operator**
69
59
 
70
60
  ```rs
71
- type Seq0 = fn(yield: fn(): bool): bool
61
+ trait Rangeble where
72
- type Seq1[V] = fn(yield: fn(V): bool): bool
62
+ fn range[V](yield: fn(V): bool): bool
73
- type Seq2[K, V] = fn(yield: fn(K, V): bool): bool
74
63
 
75
- struct Tree[E] =
64
+ trait RangebleKV where
76
- | value E
77
- | left: Option[Tree]
78
- | right: Option[Tree]
65
+ fn rangeKV[K, V](yield: fn(K, V): bool): bool
79
66
 
67
+ record Tree[E](
68
+ value: E
69
+ left: Option[Tree] = None
70
+ right: Option[Tree] = None
71
+ )
72
+
80
- fn op_range(yld: fn(E): bool): bool =
73
+ fn (Tree->Rangeble) range(yld: fn(E): bool): bool =
81
- t ? true : t.left?.in_order(yld) && yld(t.val) && t.right?.in_order(yld)
74
+ t.left?.range(yld) && yld(t.val) && t.right?.range(yld)
82
75
 
83
76
  let tree = Tree(
84
77
  value: 10,
85
78
  left: Tree(20, Tree(30), Tree(39)),
86
79
  right: Tree(40),
87
80
  )
88
-
89
- for t := range tree:
81
+ for t := range tree
90
82
  printLn(v)
91
83
  ```