firefly-compiler 0.4.18 → 0.4.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/compiler/Builder.ff +1 -1
  2. package/compiler/Compiler.ff +6 -5
  3. package/compiler/Inference.ff +31 -19
  4. package/compiler/JsEmitter.ff +98 -71
  5. package/compiler/JsImporter.ff +1 -1
  6. package/compiler/LspHook.ff +4 -4
  7. package/compiler/Main.ff +6 -6
  8. package/compiler/Parser.ff +39 -39
  9. package/compiler/Patterns.ff +2 -0
  10. package/compiler/Syntax.ff +1 -1
  11. package/compiler/Tokenizer.ff +2 -2
  12. package/compiler/Workspace.ff +2 -2
  13. package/core/Array.ff +135 -294
  14. package/core/Buffer.ff +3 -3
  15. package/core/BuildSystem.ff +1 -1
  16. package/core/Equal.ff +36 -52
  17. package/core/HttpClient.ff +1 -1
  18. package/core/IntMap.ff +14 -18
  19. package/core/JsSystem.ff +1 -1
  20. package/core/JsValue.ff +6 -12
  21. package/core/Json.ff +19 -28
  22. package/core/List.ff +281 -312
  23. package/core/Map.ff +4 -8
  24. package/core/NodeSystem.ff +2 -2
  25. package/core/Option.ff +0 -4
  26. package/core/Ordering.ff +10 -6
  27. package/core/Pair.ff +0 -4
  28. package/core/Random.ff +12 -26
  29. package/core/RbMap.ff +216 -216
  30. package/core/Serializable.ff +9 -18
  31. package/core/Set.ff +0 -1
  32. package/core/SourceLocation.ff +1 -1
  33. package/core/Stack.ff +32 -45
  34. package/core/Stream.ff +10 -14
  35. package/core/String.ff +24 -6
  36. package/core/StringMap.ff +15 -19
  37. package/guide/Main.ff +20 -2
  38. package/lsp/CompletionHandler.ff +4 -4
  39. package/lsp/Handler.ff +44 -33
  40. package/lsp/HoverHandler.ff +2 -2
  41. package/lsp/LanguageServer.ff +2 -2
  42. package/lsp/SignatureHelpHandler.ff +1 -1
  43. package/lsp/SymbolHandler.ff +1 -1
  44. package/lux/Lux.ff +3 -3
  45. package/output/js/ff/compiler/Builder.mjs +19 -21
  46. package/output/js/ff/compiler/Compiler.mjs +18 -20
  47. package/output/js/ff/compiler/Dependencies.mjs +8 -10
  48. package/output/js/ff/compiler/Deriver.mjs +234 -236
  49. package/output/js/ff/compiler/Dictionaries.mjs +6 -8
  50. package/output/js/ff/compiler/Environment.mjs +42 -44
  51. package/output/js/ff/compiler/Inference.mjs +346 -304
  52. package/output/js/ff/compiler/JsEmitter.mjs +907 -833
  53. package/output/js/ff/compiler/JsImporter.mjs +0 -2
  54. package/output/js/ff/compiler/LspHook.mjs +10 -12
  55. package/output/js/ff/compiler/Main.mjs +109 -111
  56. package/output/js/ff/compiler/Parser.mjs +405 -407
  57. package/output/js/ff/compiler/Patterns.mjs +64 -50
  58. package/output/js/ff/compiler/Resolver.mjs +36 -38
  59. package/output/js/ff/compiler/Substitution.mjs +4 -6
  60. package/output/js/ff/compiler/Syntax.mjs +160 -162
  61. package/output/js/ff/compiler/Token.mjs +52 -54
  62. package/output/js/ff/compiler/Tokenizer.mjs +16 -18
  63. package/output/js/ff/compiler/Unification.mjs +24 -26
  64. package/output/js/ff/compiler/Wildcards.mjs +0 -2
  65. package/output/js/ff/compiler/Workspace.mjs +18 -20
  66. package/output/js/ff/core/Any.mjs +0 -2
  67. package/output/js/ff/core/Array.mjs +216 -613
  68. package/output/js/ff/core/AssetSystem.mjs +2 -4
  69. package/output/js/ff/core/Atomic.mjs +0 -2
  70. package/output/js/ff/core/Bool.mjs +0 -2
  71. package/output/js/ff/core/Box.mjs +0 -2
  72. package/output/js/ff/core/BrowserSystem.mjs +0 -2
  73. package/output/js/ff/core/Buffer.mjs +0 -2
  74. package/output/js/ff/core/BuildSystem.mjs +12 -14
  75. package/output/js/ff/core/Channel.mjs +0 -2
  76. package/output/js/ff/core/Char.mjs +0 -2
  77. package/output/js/ff/core/Core.mjs +0 -2
  78. package/output/js/ff/core/Duration.mjs +0 -2
  79. package/output/js/ff/core/Equal.mjs +0 -22
  80. package/output/js/ff/core/Error.mjs +0 -2
  81. package/output/js/ff/core/FileHandle.mjs +0 -2
  82. package/output/js/ff/core/Float.mjs +0 -2
  83. package/output/js/ff/core/HttpClient.mjs +2 -4
  84. package/output/js/ff/core/Instant.mjs +0 -2
  85. package/output/js/ff/core/Int.mjs +8 -10
  86. package/output/js/ff/core/IntMap.mjs +32 -42
  87. package/output/js/ff/core/JsSystem.mjs +1 -3
  88. package/output/js/ff/core/JsValue.mjs +5 -12
  89. package/output/js/ff/core/Json.mjs +23 -56
  90. package/output/js/ff/core/List.mjs +648 -1989
  91. package/output/js/ff/core/Lock.mjs +0 -2
  92. package/output/js/ff/core/Log.mjs +0 -2
  93. package/output/js/ff/core/Map.mjs +10 -20
  94. package/output/js/ff/core/NodeSystem.mjs +6 -8
  95. package/output/js/ff/core/Nothing.mjs +0 -2
  96. package/output/js/ff/core/Option.mjs +8 -18
  97. package/output/js/ff/core/Ordering.mjs +20 -98
  98. package/output/js/ff/core/Pair.mjs +6 -16
  99. package/output/js/ff/core/Path.mjs +12 -14
  100. package/output/js/ff/core/Random.mjs +24 -54
  101. package/output/js/ff/core/RbMap.mjs +54 -56
  102. package/output/js/ff/core/Serializable.mjs +19 -36
  103. package/output/js/ff/core/Set.mjs +0 -14
  104. package/output/js/ff/core/Show.mjs +0 -2
  105. package/output/js/ff/core/SourceLocation.mjs +0 -2
  106. package/output/js/ff/core/Stream.mjs +34 -44
  107. package/output/js/ff/core/String.mjs +31 -5
  108. package/output/js/ff/core/StringMap.mjs +32 -42
  109. package/output/js/ff/core/Task.mjs +0 -2
  110. package/output/js/ff/core/Try.mjs +0 -2
  111. package/output/js/ff/core/Unit.mjs +0 -2
  112. package/package.json +1 -1
  113. package/vscode/package.json +1 -1
  114. package/webserver/WebServer.ff +8 -8
  115. package/output/js/ff/core/Stack.mjs +0 -603
package/core/Map.ff CHANGED
@@ -42,10 +42,6 @@ extend self[K: Order, V]: Map[K, V] {
42
42
  self.redBlack.pairs()
43
43
  }
44
44
 
45
- toArray(): Array[Pair[K, V]] {
46
- self.redBlack.pairs().toArray() // TODO: Avoid going through List
47
- }
48
-
49
45
  toStream(cycle: Bool = False): Stream[Pair[K, V]] {
50
46
  self.redBlack.toStream(cycle)
51
47
  }
@@ -107,12 +103,12 @@ extend self[K: Order, V]: Map[K, List[V]] {
107
103
 
108
104
  }
109
105
 
110
- extend self[K: Order, V]: Map[K, Stack[V]] {
106
+ extend self[K: Order, V]: Map[K, Array[V]] {
111
107
 
112
- push(key: K, value: V): Map[K, Stack[V]] {
108
+ push(key: K, value: V): Map[K, Array[V]] {
113
109
  self.get(key).{
114
- | None => self.add(key, [value].toStack())
115
- | Some(stack) => stack.push(value); self
110
+ | None => self.add(key, [value].toArray())
111
+ | Some(array) => array.push(value); self
116
112
  }
117
113
  }
118
114
 
@@ -2,7 +2,7 @@ capability NodeSystem {}
2
2
 
3
3
  extend self: NodeSystem {
4
4
 
5
- arguments(): Array[String]
5
+ arguments(): List[String]
6
6
  target node async "return self_.array_"
7
7
 
8
8
  assets(): AssetSystem {
@@ -88,7 +88,7 @@ extend self: NodeSystem {
88
88
  internalAssets(system: NodeSystem): AssetSystem
89
89
  target node async "return system_.assets_"
90
90
 
91
- internalListDirectoryWithoutOpendir(system: NodeSystem, path: Path): Array[Path]
91
+ internalListDirectoryWithoutOpendir(system: NodeSystem, path: Path): List[Path]
92
92
  target node async """
93
93
  import * as fsPromises from 'fs/promises'
94
94
  import * as path from 'path'
package/core/Option.ff CHANGED
@@ -44,10 +44,6 @@ extend self[T]: Option[T] {
44
44
  }
45
45
  }
46
46
 
47
- toStack(): Stack[T] {
48
- self.toList().toStack()
49
- }
50
-
51
47
  toArray(): Array[T] {
52
48
  self.toList().toArray()
53
49
  }
package/core/Ordering.ff CHANGED
@@ -86,12 +86,16 @@ instance Pair[A: Order, B: Order]: Order {
86
86
 
87
87
  instance List[T: Order]: Order {
88
88
  compare(x: List[T], y: List[T]): Ordering {
89
- | [], [] => OrderingSame
90
- | [], _ => OrderingBefore
91
- | _, [] => OrderingAfter
92
- | [a, ...as], [b, ...bs] => compare(a, b).{
93
- | OrderingSame => compare(as, bs)
94
- | o => o
89
+ mutable ordering = OrderingSame
90
+ mutable i = 0
91
+ while {i < x.size() && i < y.size() && ordering == OrderingSame} {
92
+ ordering = compare(x.grab(i), y.grab(i))
93
+ i += 1
94
+ }
95
+ if(ordering != OrderingSame) {
96
+ fromInt(x.size() - y.size())
97
+ } else {
98
+ ordering
95
99
  }
96
100
  }
97
101
  }
package/core/Pair.ff CHANGED
@@ -32,10 +32,6 @@ extend self[A, A]: Pair[A, A] {
32
32
  self.toList().toArray()
33
33
  }
34
34
 
35
- toStack(): Stack[A] {
36
- self.toList().toStack()
37
- }
38
-
39
35
  }
40
36
 
41
37
  extend self[A: Order]: Pair[A, A] {
package/core/Random.ff CHANGED
@@ -100,48 +100,34 @@ extend self: Random {
100
100
  }
101
101
  """
102
102
 
103
- shuffleStack[T](stack: Stack[T]): Unit {
104
- 0.until(stack.size() - 1).each {i =>
105
- let j = self.nextInt(0, stack.size() - i) + i
106
- let value = stack.grab(i)
107
- stack.set(i, stack.grab(j))
108
- stack.set(j, value)
103
+ shuffleArray[T](array: Array[T]): Unit {
104
+ 0.until(array.size() - 1).each {i =>
105
+ let j = self.nextInt(0, array.size() - i) + i
106
+ let value = array.grab(i)
107
+ array.set(i, array.grab(j))
108
+ array.set(j, value)
109
109
  }
110
110
  }
111
111
 
112
- shuffleArray[T](array: Array[T]): Array[T] {
113
- let stack = array.toStack()
114
- self.shuffleStack(array.toStack())
115
- stack.drain()
116
- }
117
-
118
112
  shuffleList[T](list: List[T]): List[T] {
119
- let stack = list.toStack()
120
- self.shuffleStack(list.toStack())
121
- stack.toList()
113
+ let array = list.toArray()
114
+ self.shuffleArray(list.toArray())
115
+ array.drain()
122
116
  }
123
117
 
124
- sampleStack[T](count: Int, stack: Stack[T], body: T => Unit): Unit {
125
- self.shuffleArray(stack.toArray()).takeFirst(count).each {body(_)}
118
+ sampleArray[T](count: Int, array: Array[T], body: T => Unit): Unit {
119
+ self.shuffleList(array.toList()).takeFirst(count).each {body(_)}
126
120
  }
127
121
 
128
- sampleArray[T](count: Int, array: Array[T]): Array[T] {
129
- self.shuffleArray(array).takeFirst(count)
130
- }
131
-
132
122
  sampleList[T](count: Int, list: List[T]): List[T] {
133
123
  self.shuffleList(list).takeFirst(count)
134
124
  }
135
125
 
136
- grabStack[T](stack: Stack[T]): T {
137
- stack.grab(self.nextInt(0, stack.size()))
138
- }
139
-
140
126
  grabArray[T](array: Array[T]): T {
141
127
  array.grab(self.nextInt(0, array.size()))
142
128
  }
143
129
 
144
- grabList[T](list: Stack[T]): T {
130
+ grabList[T](list: List[T]): T {
145
131
  list.grab(self.nextInt(0, list.size()))
146
132
  }
147
133
 
package/core/RbMap.ff CHANGED
@@ -1,216 +1,216 @@
1
- // Based on https://www.cs.kent.ac.uk/people/staff/smk/redblack/Untyped.hs
2
-
3
- data Color {
4
- R
5
- B
6
- }
7
-
8
- data RB[K, V] {
9
- E
10
- T(color: Color, left: RB[K, V], key: K, value: V, right: RB[K, V])
11
- }
12
-
13
- insert[K: Order, V](x: K, xv: V, s: RB[K, V]): RB[K, V] {
14
- function ins(s: RB[K, V]): RB[K, V] {
15
- | E => T(R, E, x, xv, E)
16
- | T(B, a, y, yv, b) => Ordering.compare(x, y).{
17
- | OrderingBefore => balance(ins(a), y, yv, b)
18
- | OrderingAfter => balance(a, y, yv, ins(b))
19
- | OrderingSame => T(B, a, x, xv, b)
20
- }
21
- | T(R, a, y, yv, b) => Ordering.compare(x, y).{
22
- | OrderingBefore => T(R, ins(a), y, yv, b)
23
- | OrderingAfter => T(R, a, y, yv, ins(b))
24
- | OrderingSame => T(R, a, x, xv, b)
25
- }
26
- }
27
-
28
- ins(s).{
29
- | E => panic("Unexpected empty tree")
30
- | T(_, a, z, zv, b) => T(B, a, z, zv, b)
31
- }
32
- }
33
-
34
- member[K: Order, V](key: K, tree: RB[K, V]): Bool {
35
- | x, E => False
36
- | x, T(_, a, y, _, b,) => Ordering.compare(x, y).{
37
- | OrderingBefore => member(x, a)
38
- | OrderingAfter => member(x, b)
39
- | OrderingSame => True
40
- }
41
- }
42
-
43
- balance[K: Order, V](tree1: RB[K, V], key: K, value: V, tree2: RB[K, V]): RB[K, V] {
44
- | T(R, a, x, xv, b), y, yv, T(R, c, z, zv, d) => T(R, T(B, a, x, xv, b), y, yv, T(B, c, z, zv, d))
45
- | T(R, T(R, a, x, xv, b), y, yv, c), z, zv, d => T(R, T(B, a, x, xv, b), y, yv, T(B, c, z, zv, d))
46
- | T(R, a, x, xv, T(R, b, y, yv, c)), z, zv, d => T(R, T(B, a, x, xv, b), y, yv, T(B, c, z, zv, d))
47
- | a, x, xv, T(R, b, y, yv, T(R, c, z, zv, d)) => T(R, T(B, a, x, xv, b), y, yv, T(B, c, z, zv, d))
48
- | a, x, xv, T(R, T(R, b, y, yv, c), z, zv, d) => T(R, T(B, a, x, xv, b), y, yv, T(B, c, z, zv, d))
49
- | a, x, xv, b => T(B, a, x, xv, b)
50
- }
51
-
52
- delete[K: Order, V](x: K, t: RB[K, V]): RB[K, V] {
53
- function del(ss: RB[K, V]): RB[K, V] {
54
- | E => E
55
- | T(_, a, y, yv, b) => Ordering.compare(x, y).{
56
- | OrderingBefore => delformLeft(a, y, yv, b)
57
- | OrderingAfter => delformRight(a, y, yv, b)
58
- | OrderingSame => app(a, b)
59
- }
60
- }
61
-
62
- function delformLeft(left: RB[K, V], key: K, value: V, right: RB[K, V]): RB[K, V] {
63
- | T(B, _, _, _, _), y, yv, b => balleft(del(left), y, yv, b)
64
- | a, y, yv, b => T(R, del(a), y, yv, b)
65
- }
66
-
67
- function delformRight(left: RB[K, V], key: K, value: V, right: RB[K, V]): RB[K, V] {
68
- | a, y, yv, T(B, _, _, _, _) => balright(a, y, yv, del(right))
69
- | a, y, yv, b => T(R, a, y, yv, del(b))
70
- }
71
-
72
- del(t).{
73
- | T(_, a, y, yv, b) => T(B, a, y, yv, b)
74
- | _ => E
75
- }
76
- }
77
-
78
- balleft[K: Order, V](tree1: RB[K, V], key: K, value: V, tree2: RB[K, V]): RB[K, V] {
79
- | T(R, a, x, xv, b), y, yv, c => T(R, T(B, a, x, xv, b), y, yv, c)
80
- | bl, x, xv, T(B, a, y, yv, b) => balance(bl, x, xv, T(R, a, y, yv, b))
81
- | bl, x, xv, T(R, T(B, a, y, yv, b), z, zv, c) => T(R, T(B, bl, x, xv, a), y, yv, (balance(b, z, zv, sub1(c))))
82
- | _, _, _, _ => panic("Unexhaustive match in balleft")
83
- }
84
-
85
- balright[K: Order, V](tree1: RB[K, V], key: K, value: V, tree2: RB[K, V]): RB[K, V] {
86
- | a, x, xv, T(R, b, y, yv, c) => T(R, a, x, xv, T(B, b, y, yv, c))
87
- | T(B, a, x, xv, b), y, yv, bl => balance(T(R, a, x, xv, b), y, yv, bl)
88
- | T(R, a, x, xv, T(B, b, y, yv, c)), z, zv, bl => T(R, balance(sub1(a), x, xv, b), y, yv, T(B, c, z, zv, bl))
89
- | _, _, _, _ => panic("Unexhaustive match in balright")
90
- }
91
-
92
- sub1[K: Order, V](tree: RB[K, V]): RB[K, V] {
93
- | T(B, a, x, xv, b) => T(R, a, x, xv, b)
94
- | _ => panic("invariance violation")
95
- }
96
-
97
- app[K: Order, V](tree1: RB[K, V], tree2: RB[K, V]): RB[K, V] {
98
- | E, b => b
99
- | a, E => a
100
- | T(R, a, x, xv, b), T(R, c, y, yv, d) =>
101
- app(b, c).{
102
- | T(R, b2, z, zv, c2) => T(R, T(R, a, x, xv, b2), z, zv, T(R, c2, y, yv, d))
103
- | bc => T(R, a, x, xv, T(R, bc, y, yv, d))
104
- }
105
- | T(B, a, x, xv, b), T(B, c, y, yv, d) =>
106
- app(b, c).{
107
- | T(R, b2, z, zv, c2) => T(R, T(B, a, x, xv, b2), z, zv, T(B, c2, y, yv, d))
108
- | bc => balleft(a, x, xv, T(B, bc, y, yv, d))
109
- }
110
- | a, T(R, b, x, xv, c) => T(R, app(a, b), x, xv, c)
111
- | T(R, a, x, xv, b), c => T(R, a, x, xv, app(b, c))
112
- }
113
-
114
- extend self[K: Order, V]: RB[K, V] {
115
-
116
- get(key: K): Option[V] {
117
- self.{
118
- | E_ => None
119
- | T(_, a, y, yv, b) => Ordering.compare(key, y).{
120
- | OrderingBefore => a.get(key)
121
- | OrderingAfter => b.get(key)
122
- | OrderingSame => Some(yv)
123
- }
124
- }
125
- }
126
-
127
- size(): Int {
128
- self.{
129
- | E => 0
130
- | T(_, l, _, _, r) => l.size() + 1 + r.size()
131
- }
132
- }
133
-
134
- pairs(): List[Pair[K, V]] {
135
- mutable result = []
136
- self.each {k, v =>
137
- result = [Pair(k, v), ...result]
138
- }
139
- result.reverse()
140
- }
141
-
142
- toStream(cycle: Bool): Stream[Pair[K, V]] {
143
- mutable stack = [self]
144
- function next(): Option[Pair[K, V]] {
145
- stack.{
146
- | [] =>
147
- if(cycle && self.{| E => False | _ => True}) {
148
- stack = [self]
149
- tailcall next()
150
- } else {
151
- None
152
- }
153
- | [E, ...tail] =>
154
- stack = tail
155
- tailcall next()
156
- | [T(_, E, k, v, E), ...tail] =>
157
- stack = tail
158
- Some(Pair(k, v))
159
- | [T(_, l, k, v, r), ...tail] =>
160
- stack = [l, T(B, E, k, v, E), r, ...tail]
161
- tailcall next()
162
- }
163
- }
164
- Stream.make(next)
165
- }
166
-
167
- each(body: (K, V) => Unit): Unit {
168
- self.{
169
- | E =>
170
- | T(_, l, k, v, r) =>
171
- l.each(body)
172
- body(k, v)
173
- r.each(body)
174
- }
175
- }
176
-
177
- eachWhile(body: (K, V) => Bool): Bool {
178
- self.{
179
- | E =>
180
- True
181
- | T(_, l, k, v, r) =>
182
- l.eachWhile(body) &&
183
- body(k, v) &&
184
- r.eachWhile(body)
185
- }
186
- }
187
-
188
- map[K2: Order, V2](body: (K, V) => Pair[K2, V2]): RB[K2, V2] {
189
- mutable result = E
190
- self.each {k, v =>
191
- let pair = body(k, v)
192
- result = insert(pair.first, pair.second, result)
193
- }
194
- result
195
- }
196
-
197
- mapValues[V2](body: (K, V) => V2): RB[K, V2] {
198
- self.{
199
- | E => E
200
- | T(c, l, k, v, r) =>
201
- T(c, l.mapValues(body), k, body(k, v), r.mapValues(body))
202
- }
203
- }
204
-
205
- find(body: (K, V) => Bool): Option[Pair[K, V]] {
206
- mutable result = None
207
- self.eachWhile {k, v =>
208
- if(body(k, v)) {
209
- result = Some(Pair(k, v))
210
- False
211
- } else {True}
212
- }
213
- result
214
- }
215
-
216
- }
1
+ // Based on https://www.cs.kent.ac.uk/people/staff/smk/redblack/Untyped.hs
2
+
3
+ data Color {
4
+ R
5
+ B
6
+ }
7
+
8
+ data RB[K, V] {
9
+ E
10
+ T(color: Color, left: RB[K, V], key: K, value: V, right: RB[K, V])
11
+ }
12
+
13
+ insert[K: Order, V](x: K, xv: V, s: RB[K, V]): RB[K, V] {
14
+ function ins(s: RB[K, V]): RB[K, V] {
15
+ | E => T(R, E, x, xv, E)
16
+ | T(B, a, y, yv, b) => Ordering.compare(x, y).{
17
+ | OrderingBefore => balance(ins(a), y, yv, b)
18
+ | OrderingAfter => balance(a, y, yv, ins(b))
19
+ | OrderingSame => T(B, a, x, xv, b)
20
+ }
21
+ | T(R, a, y, yv, b) => Ordering.compare(x, y).{
22
+ | OrderingBefore => T(R, ins(a), y, yv, b)
23
+ | OrderingAfter => T(R, a, y, yv, ins(b))
24
+ | OrderingSame => T(R, a, x, xv, b)
25
+ }
26
+ }
27
+
28
+ ins(s).{
29
+ | E => panic("Unexpected empty tree")
30
+ | T(_, a, z, zv, b) => T(B, a, z, zv, b)
31
+ }
32
+ }
33
+
34
+ member[K: Order, V](key: K, tree: RB[K, V]): Bool {
35
+ | x, E => False
36
+ | x, T(_, a, y, _, b,) => Ordering.compare(x, y).{
37
+ | OrderingBefore => member(x, a)
38
+ | OrderingAfter => member(x, b)
39
+ | OrderingSame => True
40
+ }
41
+ }
42
+
43
+ balance[K: Order, V](tree1: RB[K, V], key: K, value: V, tree2: RB[K, V]): RB[K, V] {
44
+ | T(R, a, x, xv, b), y, yv, T(R, c, z, zv, d) => T(R, T(B, a, x, xv, b), y, yv, T(B, c, z, zv, d))
45
+ | T(R, T(R, a, x, xv, b), y, yv, c), z, zv, d => T(R, T(B, a, x, xv, b), y, yv, T(B, c, z, zv, d))
46
+ | T(R, a, x, xv, T(R, b, y, yv, c)), z, zv, d => T(R, T(B, a, x, xv, b), y, yv, T(B, c, z, zv, d))
47
+ | a, x, xv, T(R, b, y, yv, T(R, c, z, zv, d)) => T(R, T(B, a, x, xv, b), y, yv, T(B, c, z, zv, d))
48
+ | a, x, xv, T(R, T(R, b, y, yv, c), z, zv, d) => T(R, T(B, a, x, xv, b), y, yv, T(B, c, z, zv, d))
49
+ | a, x, xv, b => T(B, a, x, xv, b)
50
+ }
51
+
52
+ delete[K: Order, V](x: K, t: RB[K, V]): RB[K, V] {
53
+ function del(ss: RB[K, V]): RB[K, V] {
54
+ | E => E
55
+ | T(_, a, y, yv, b) => Ordering.compare(x, y).{
56
+ | OrderingBefore => delformLeft(a, y, yv, b)
57
+ | OrderingAfter => delformRight(a, y, yv, b)
58
+ | OrderingSame => app(a, b)
59
+ }
60
+ }
61
+
62
+ function delformLeft(left: RB[K, V], key: K, value: V, right: RB[K, V]): RB[K, V] {
63
+ | T(B, _, _, _, _), y, yv, b => balleft(del(left), y, yv, b)
64
+ | a, y, yv, b => T(R, del(a), y, yv, b)
65
+ }
66
+
67
+ function delformRight(left: RB[K, V], key: K, value: V, right: RB[K, V]): RB[K, V] {
68
+ | a, y, yv, T(B, _, _, _, _) => balright(a, y, yv, del(right))
69
+ | a, y, yv, b => T(R, a, y, yv, del(b))
70
+ }
71
+
72
+ del(t).{
73
+ | T(_, a, y, yv, b) => T(B, a, y, yv, b)
74
+ | _ => E
75
+ }
76
+ }
77
+
78
+ balleft[K: Order, V](tree1: RB[K, V], key: K, value: V, tree2: RB[K, V]): RB[K, V] {
79
+ | T(R, a, x, xv, b), y, yv, c => T(R, T(B, a, x, xv, b), y, yv, c)
80
+ | bl, x, xv, T(B, a, y, yv, b) => balance(bl, x, xv, T(R, a, y, yv, b))
81
+ | bl, x, xv, T(R, T(B, a, y, yv, b), z, zv, c) => T(R, T(B, bl, x, xv, a), y, yv, (balance(b, z, zv, sub1(c))))
82
+ | _, _, _, _ => panic("Unexhaustive match in balleft")
83
+ }
84
+
85
+ balright[K: Order, V](tree1: RB[K, V], key: K, value: V, tree2: RB[K, V]): RB[K, V] {
86
+ | a, x, xv, T(R, b, y, yv, c) => T(R, a, x, xv, T(B, b, y, yv, c))
87
+ | T(B, a, x, xv, b), y, yv, bl => balance(T(R, a, x, xv, b), y, yv, bl)
88
+ | T(R, a, x, xv, T(B, b, y, yv, c)), z, zv, bl => T(R, balance(sub1(a), x, xv, b), y, yv, T(B, c, z, zv, bl))
89
+ | _, _, _, _ => panic("Unexhaustive match in balright")
90
+ }
91
+
92
+ sub1[K: Order, V](tree: RB[K, V]): RB[K, V] {
93
+ | T(B, a, x, xv, b) => T(R, a, x, xv, b)
94
+ | _ => panic("invariance violation")
95
+ }
96
+
97
+ app[K: Order, V](tree1: RB[K, V], tree2: RB[K, V]): RB[K, V] {
98
+ | E, b => b
99
+ | a, E => a
100
+ | T(R, a, x, xv, b), T(R, c, y, yv, d) =>
101
+ app(b, c).{
102
+ | T(R, b2, z, zv, c2) => T(R, T(R, a, x, xv, b2), z, zv, T(R, c2, y, yv, d))
103
+ | bc => T(R, a, x, xv, T(R, bc, y, yv, d))
104
+ }
105
+ | T(B, a, x, xv, b), T(B, c, y, yv, d) =>
106
+ app(b, c).{
107
+ | T(R, b2, z, zv, c2) => T(R, T(B, a, x, xv, b2), z, zv, T(B, c2, y, yv, d))
108
+ | bc => balleft(a, x, xv, T(B, bc, y, yv, d))
109
+ }
110
+ | a, T(R, b, x, xv, c) => T(R, app(a, b), x, xv, c)
111
+ | T(R, a, x, xv, b), c => T(R, a, x, xv, app(b, c))
112
+ }
113
+
114
+ extend self[K: Order, V]: RB[K, V] {
115
+
116
+ get(key: K): Option[V] {
117
+ self.{
118
+ | E_ => None
119
+ | T(_, a, y, yv, b) => Ordering.compare(key, y).{
120
+ | OrderingBefore => a.get(key)
121
+ | OrderingAfter => b.get(key)
122
+ | OrderingSame => Some(yv)
123
+ }
124
+ }
125
+ }
126
+
127
+ size(): Int {
128
+ self.{
129
+ | E => 0
130
+ | T(_, l, _, _, r) => l.size() + 1 + r.size()
131
+ }
132
+ }
133
+
134
+ pairs(): List[Pair[K, V]] {
135
+ let result = [].toArray()
136
+ self.each {k, v =>
137
+ result.push(Pair(k, v))
138
+ }
139
+ result.drain()
140
+ }
141
+
142
+ toStream(cycle: Bool): Stream[Pair[K, V]] {
143
+ mutable array = [self]
144
+ function next(): Option[Pair[K, V]] {
145
+ array.{
146
+ | [] =>
147
+ if(cycle && self.{| E => False | _ => True}) {
148
+ array = [self]
149
+ tailcall next()
150
+ } else {
151
+ None
152
+ }
153
+ | [E, ...tail] =>
154
+ array = tail
155
+ tailcall next()
156
+ | [T(_, E, k, v, E), ...tail] =>
157
+ array = tail
158
+ Some(Pair(k, v))
159
+ | [T(_, l, k, v, r), ...tail] =>
160
+ array = [l, T(B, E, k, v, E), r, ...tail]
161
+ tailcall next()
162
+ }
163
+ }
164
+ Stream.make(next)
165
+ }
166
+
167
+ each(body: (K, V) => Unit): Unit {
168
+ self.{
169
+ | E =>
170
+ | T(_, l, k, v, r) =>
171
+ l.each(body)
172
+ body(k, v)
173
+ r.each(body)
174
+ }
175
+ }
176
+
177
+ eachWhile(body: (K, V) => Bool): Bool {
178
+ self.{
179
+ | E =>
180
+ True
181
+ | T(_, l, k, v, r) =>
182
+ l.eachWhile(body) &&
183
+ body(k, v) &&
184
+ r.eachWhile(body)
185
+ }
186
+ }
187
+
188
+ map[K2: Order, V2](body: (K, V) => Pair[K2, V2]): RB[K2, V2] {
189
+ mutable result = E
190
+ self.each {k, v =>
191
+ let pair = body(k, v)
192
+ result = insert(pair.first, pair.second, result)
193
+ }
194
+ result
195
+ }
196
+
197
+ mapValues[V2](body: (K, V) => V2): RB[K, V2] {
198
+ self.{
199
+ | E => E
200
+ | T(c, l, k, v, r) =>
201
+ T(c, l.mapValues(body), k, body(k, v), r.mapValues(body))
202
+ }
203
+ }
204
+
205
+ find(body: (K, V) => Bool): Option[Pair[K, V]] {
206
+ mutable result = None
207
+ self.eachWhile {k, v =>
208
+ if(body(k, v)) {
209
+ result = Some(Pair(k, v))
210
+ False
211
+ } else {True}
212
+ }
213
+ result
214
+ }
215
+
216
+ }
@@ -67,8 +67,8 @@ instance Bool: Serializable {
67
67
  }
68
68
  }
69
69
 
70
- instance Array[T: Serializable]: Serializable {
71
- serializeUsing(serialization: Serialization, value: Array[T]): Unit {
70
+ instance List[T: Serializable]: Serializable {
71
+ serializeUsing(serialization: Serialization, value: List[T]): Unit {
72
72
  if(value.size() < 255) {
73
73
  serialization.autoResize(1)
74
74
  serialization.buffer.setUint8(serialization.offset, value.size())
@@ -83,43 +83,34 @@ instance Array[T: Serializable]: Serializable {
83
83
  }
84
84
  value.each {serializeUsing(serialization, _)}
85
85
  }
86
- deserializeUsing(serialization: Serialization): Array[T] {
86
+ deserializeUsing(serialization: Serialization): List[T] {
87
87
  let smallSize = serialization.buffer.grabUint8(serialization.offset)
88
88
  if(smallSize != 255) {
89
89
  serialization.offset += 1
90
- Array.fillBy(smallSize) {_ => deserializeUsing(serialization)}
90
+ List.fillBy(smallSize) {_ => deserializeUsing(serialization)}
91
91
  } else {
92
92
  let size = serialization.buffer.grabUint32(serialization.offset + 1)
93
93
  serialization.offset += 1 + 4
94
- Array.fillBy(size) {_ => deserializeUsing(serialization)}
94
+ List.fillBy(size) {_ => deserializeUsing(serialization)}
95
95
  }
96
96
  }
97
97
  }
98
98
 
99
- instance List[T: Serializable]: Serializable {
100
- serializeUsing(serialization: Serialization, value: List[T]): Unit {
101
- serializeUsing(serialization, value.toArray())
102
- }
103
- deserializeUsing(serialization: Serialization): List[T] {
104
- deserializeUsing[Array[T]](serialization).toList()
105
- }
106
- }
107
-
108
99
  instance Set[T: Serializable: Order]: Serializable {
109
100
  serializeUsing(serialization: Serialization, value: Set[T]): Unit {
110
- serializeUsing(serialization, value.toArray())
101
+ serializeUsing(serialization, value.toList())
111
102
  }
112
103
  deserializeUsing(serialization: Serialization): Set[T] {
113
- deserializeUsing[Array[T]](serialization).toSet()
104
+ deserializeUsing[List[T]](serialization).toSet()
114
105
  }
115
106
  }
116
107
 
117
108
  instance Map[K: Serializable: Order, V: Serializable]: Serializable {
118
109
  serializeUsing(serialization: Serialization, value: Map[K, V]): Unit {
119
- serializeUsing(serialization, value.toArray())
110
+ serializeUsing(serialization, value.toList())
120
111
  }
121
112
  deserializeUsing(serialization: Serialization): Map[K, V] {
122
- deserializeUsing[Array[Pair[K, V]]](serialization).toMap()
113
+ deserializeUsing[List[Pair[K, V]]](serialization).toMap()
123
114
  }
124
115
  }
125
116
 
package/core/Set.ff CHANGED
@@ -12,7 +12,6 @@ extend self[T: Order]: Set[T] {
12
12
  contains(value: T): Bool {self.map.contains(value)}
13
13
  size(): Int {self.map.size()}
14
14
  toList(): List[T] {self.map.toList().map {_.first}}
15
- toArray(): Array[T] {self.map.toArray().map {_.first}}
16
15
  toStream(cycle: Bool = False): Stream[T] {self.map.toStream(cycle).map {_.first }}
17
16
  each(body: T => Unit): Unit {self.map.each {| k, _ => body(k)}}
18
17
  eachWhile(body: T => Bool): Bool {self.map.eachWhile {| k, _ => body(k)}}
@@ -34,7 +34,7 @@ extend self: SourceLocation {
34
34
  self.location.dropWhile {_ != ':'}.dropFirst().dropWhile {_ != ':'}.dropFirst().takeWhile {_ != ','}.grabInt()
35
35
  }
36
36
 
37
- breadcrumbs(): Array[String] {
37
+ breadcrumbs(): List[String] {
38
38
  self.location.dropWhile {_ != '>'}.dropFirst().split('>')
39
39
  }
40
40