firefly-compiler 0.4.4

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 (221) hide show
  1. package/.firefly-workspace +1 -0
  2. package/.vscode/settings.json +5 -0
  3. package/LICENSE.txt +21 -0
  4. package/README.md +96 -0
  5. package/bin/firefly.mjs +2 -0
  6. package/compiler/.firefly/package.ff +1 -0
  7. package/compiler/Builder.ff +218 -0
  8. package/compiler/Compiler.ff +241 -0
  9. package/compiler/Dependencies.ff +179 -0
  10. package/compiler/Deriver.ff +647 -0
  11. package/compiler/Dictionaries.ff +205 -0
  12. package/compiler/Environment.ff +166 -0
  13. package/compiler/Inference.ff +1117 -0
  14. package/compiler/JsEmitter.ff +861 -0
  15. package/compiler/JsImporter.ff +56 -0
  16. package/compiler/LspHook.ff +188 -0
  17. package/compiler/Main.ff +237 -0
  18. package/compiler/Parser.ff +1383 -0
  19. package/compiler/Patterns.ff +111 -0
  20. package/compiler/Resolver.ff +620 -0
  21. package/compiler/Substitution.ff +178 -0
  22. package/compiler/Syntax.ff +299 -0
  23. package/compiler/Token.ff +180 -0
  24. package/compiler/Tokenizer.ff +278 -0
  25. package/compiler/Unification.ff +220 -0
  26. package/compiler/Wildcards.ff +50 -0
  27. package/compiler/Workspace.ff +88 -0
  28. package/core/.firefly/package.ff +2 -0
  29. package/core/Any.ff +30 -0
  30. package/core/Array.ff +249 -0
  31. package/core/AssetSystem.ff +61 -0
  32. package/core/Atomic.ff +64 -0
  33. package/core/Bool.ff +13 -0
  34. package/core/BrowserSystem.ff +14 -0
  35. package/core/Buffer.ff +211 -0
  36. package/core/BuildSystem.ff +144 -0
  37. package/core/Channel.ff +131 -0
  38. package/core/Char.ff +18 -0
  39. package/core/Core.ff +58 -0
  40. package/core/Duration.ff +15 -0
  41. package/core/Equal.ff +52 -0
  42. package/core/Error.ff +20 -0
  43. package/core/FileHandle.ff +41 -0
  44. package/core/Float.ff +41 -0
  45. package/core/HttpClient.ff +84 -0
  46. package/core/Instant.ff +9 -0
  47. package/core/Int.ff +61 -0
  48. package/core/IntMap.ff +85 -0
  49. package/core/JsSystem.ff +66 -0
  50. package/core/JsValue.ff +240 -0
  51. package/core/List.ff +440 -0
  52. package/core/Lock.ff +144 -0
  53. package/core/Log.ff +24 -0
  54. package/core/Map.ff +126 -0
  55. package/core/NodeSystem.ff +88 -0
  56. package/core/Nothing.ff +1 -0
  57. package/core/Option.ff +133 -0
  58. package/core/Ordering.ff +157 -0
  59. package/core/Pair.ff +55 -0
  60. package/core/Path.ff +393 -0
  61. package/core/RbMap.ff +216 -0
  62. package/core/Serializable.ff +173 -0
  63. package/core/Set.ff +38 -0
  64. package/core/Show.ff +43 -0
  65. package/core/Stack.ff +263 -0
  66. package/core/Stream.ff +406 -0
  67. package/core/String.ff +175 -0
  68. package/core/StringMap.ff +85 -0
  69. package/core/Task.ff +138 -0
  70. package/core/Try.ff +81 -0
  71. package/core/Unit.ff +3 -0
  72. package/experimental/random/AltGeneric.ff +44 -0
  73. package/experimental/random/Async.ff +68 -0
  74. package/experimental/random/Buffer2.ff +77 -0
  75. package/experimental/random/Cat.ff +12 -0
  76. package/experimental/random/Dictionary.ff +52 -0
  77. package/experimental/random/Example.ff +46 -0
  78. package/experimental/random/Generic.ff +102 -0
  79. package/experimental/random/HappyEyeballs.ff +40 -0
  80. package/experimental/random/HashMap.ff +72 -0
  81. package/experimental/random/IfElseUnit.ff +9 -0
  82. package/experimental/random/InputOutput.ff +23 -0
  83. package/experimental/random/ListVsArray.ff +45 -0
  84. package/experimental/random/Main.ff +44 -0
  85. package/experimental/random/MapTest.ff +67 -0
  86. package/experimental/random/OldTaskSystem.ff +210 -0
  87. package/experimental/random/PatternTest.ff +39 -0
  88. package/experimental/random/Patterns.ff +226 -0
  89. package/experimental/random/ReadBytesTest.ff +10 -0
  90. package/experimental/random/RunLength.ff +65 -0
  91. package/experimental/random/Scrape.ff +51 -0
  92. package/experimental/random/Serialization.ff +217 -0
  93. package/experimental/random/SerializationTest.ff +46 -0
  94. package/experimental/random/Serializer.ff +36 -0
  95. package/experimental/random/StdInOutErr.ff +4 -0
  96. package/experimental/random/Symbols.ff +74 -0
  97. package/experimental/random/Tag.ff +49 -0
  98. package/experimental/random/Tensor.ff +52 -0
  99. package/experimental/random/Try.ff +56 -0
  100. package/experimental/random/Tsv.ff +9 -0
  101. package/experimental/random/TypesAreModules.ff +87 -0
  102. package/experimental/random/blueprints/Blueprint.ff +52 -0
  103. package/experimental/random/blueprints/Main.ff +11 -0
  104. package/experimental/random/blueprints/Pretty.ff +58 -0
  105. package/experimental/random/blueprints/User.ff +64 -0
  106. package/experimental/random/blueprintsystem/BlueprintSystem.ff +48 -0
  107. package/experimental/random/blueprintsystem/Deserialize.ff +53 -0
  108. package/experimental/random/blueprintsystem/ReadJs.ff +13 -0
  109. package/experimental/random/blueprintsystem/User.ff +2 -0
  110. package/experimental/random/kahrs/Kahrs.ff +112 -0
  111. package/experimental/random/kahrs/TestKahrs.ff +22 -0
  112. package/experimental/random/kahrs/TestMap.ff +18 -0
  113. package/experimental/random/streaming/Gzip.ff +3 -0
  114. package/experimental/random/streaming/Main.ff +34 -0
  115. package/experimental/random/streaming/S3Bucket.ff +11 -0
  116. package/experimental/random/streaming/Tar.ff +5 -0
  117. package/experimental/rhymeapp/Main.ff +81 -0
  118. package/experimental/rhymeapp/index.html +14 -0
  119. package/firefly.sh +5 -0
  120. package/fireflysite/Main.ff +13 -0
  121. package/httpserver/.firefly/package.ff +1 -0
  122. package/httpserver/HttpServer.ff +184 -0
  123. package/lsp/.firefly/package.ff +1 -0
  124. package/lsp/CompletionHandler.ff +814 -0
  125. package/lsp/Handler.ff +551 -0
  126. package/lsp/HoverHandler.ff +82 -0
  127. package/lsp/LanguageServer.ff +229 -0
  128. package/lsp/SignatureHelpHandler.ff +55 -0
  129. package/lsp/SymbolHandler.ff +167 -0
  130. package/output/js/ff/compiler/Builder.mjs +483 -0
  131. package/output/js/ff/compiler/Compiler.mjs +410 -0
  132. package/output/js/ff/compiler/Dependencies.mjs +388 -0
  133. package/output/js/ff/compiler/Deriver.mjs +1166 -0
  134. package/output/js/ff/compiler/Dictionaries.mjs +1305 -0
  135. package/output/js/ff/compiler/Environment.mjs +1005 -0
  136. package/output/js/ff/compiler/Inference.mjs +4264 -0
  137. package/output/js/ff/compiler/JsEmitter.mjs +5353 -0
  138. package/output/js/ff/compiler/JsImporter.mjs +262 -0
  139. package/output/js/ff/compiler/LspHook.mjs +789 -0
  140. package/output/js/ff/compiler/Main.mjs +1695 -0
  141. package/output/js/ff/compiler/Parser.mjs +4004 -0
  142. package/output/js/ff/compiler/Patterns.mjs +923 -0
  143. package/output/js/ff/compiler/Resolver.mjs +2303 -0
  144. package/output/js/ff/compiler/Substitution.mjs +1146 -0
  145. package/output/js/ff/compiler/Syntax.mjs +12430 -0
  146. package/output/js/ff/compiler/Token.mjs +3092 -0
  147. package/output/js/ff/compiler/Tokenizer.mjs +589 -0
  148. package/output/js/ff/compiler/Unification.mjs +1748 -0
  149. package/output/js/ff/compiler/Wildcards.mjs +604 -0
  150. package/output/js/ff/compiler/Workspace.mjs +683 -0
  151. package/output/js/ff/core/Any.mjs +139 -0
  152. package/output/js/ff/core/Array.mjs +594 -0
  153. package/output/js/ff/core/AssetSystem.mjs +270 -0
  154. package/output/js/ff/core/Atomic.mjs +186 -0
  155. package/output/js/ff/core/Bool.mjs +141 -0
  156. package/output/js/ff/core/BrowserSystem.mjs +122 -0
  157. package/output/js/ff/core/Buffer.mjs +467 -0
  158. package/output/js/ff/core/BuildSystem.mjs +320 -0
  159. package/output/js/ff/core/Channel.mjs +268 -0
  160. package/output/js/ff/core/Char.mjs +145 -0
  161. package/output/js/ff/core/Core.mjs +300 -0
  162. package/output/js/ff/core/Duration.mjs +112 -0
  163. package/output/js/ff/core/Equal.mjs +175 -0
  164. package/output/js/ff/core/Error.mjs +138 -0
  165. package/output/js/ff/core/FileHandle.mjs +164 -0
  166. package/output/js/ff/core/Float.mjs +214 -0
  167. package/output/js/ff/core/HttpClient.mjs +210 -0
  168. package/output/js/ff/core/Instant.mjs +105 -0
  169. package/output/js/ff/core/Int.mjs +254 -0
  170. package/output/js/ff/core/IntMap.mjs +282 -0
  171. package/output/js/ff/core/JsSystem.mjs +234 -0
  172. package/output/js/ff/core/JsValue.mjs +678 -0
  173. package/output/js/ff/core/List.mjs +2335 -0
  174. package/output/js/ff/core/Lock.mjs +322 -0
  175. package/output/js/ff/core/Log.mjs +159 -0
  176. package/output/js/ff/core/Map.mjs +358 -0
  177. package/output/js/ff/core/NodeSystem.mjs +288 -0
  178. package/output/js/ff/core/Nothing.mjs +100 -0
  179. package/output/js/ff/core/Option.mjs +1002 -0
  180. package/output/js/ff/core/Ordering.mjs +734 -0
  181. package/output/js/ff/core/Pair.mjs +318 -0
  182. package/output/js/ff/core/Path.mjs +768 -0
  183. package/output/js/ff/core/RbMap.mjs +1936 -0
  184. package/output/js/ff/core/Serializable.mjs +434 -0
  185. package/output/js/ff/core/Set.mjs +250 -0
  186. package/output/js/ff/core/Show.mjs +201 -0
  187. package/output/js/ff/core/Stack.mjs +595 -0
  188. package/output/js/ff/core/Stream.mjs +1300 -0
  189. package/output/js/ff/core/String.mjs +433 -0
  190. package/output/js/ff/core/StringMap.mjs +282 -0
  191. package/output/js/ff/core/Task.mjs +345 -0
  192. package/output/js/ff/core/Try.mjs +503 -0
  193. package/output/js/ff/core/Unit.mjs +103 -0
  194. package/package.json +29 -0
  195. package/postgresql/.firefly/include/package-lock.json +250 -0
  196. package/postgresql/.firefly/include/package.json +5 -0
  197. package/postgresql/.firefly/include/prepare.sh +2 -0
  198. package/postgresql/.firefly/package.ff +3 -0
  199. package/postgresql/Pg.ff +530 -0
  200. package/unsafejs/.firefly/package.ff +1 -0
  201. package/unsafejs/UnsafeJs.ff +19 -0
  202. package/vscode/.vscode/launch.json +18 -0
  203. package/vscode/.vscode/tasks.json +33 -0
  204. package/vscode/LICENSE.txt +21 -0
  205. package/vscode/Prepublish.ff +15 -0
  206. package/vscode/README.md +17 -0
  207. package/vscode/client/package-lock.json +544 -0
  208. package/vscode/client/package.json +22 -0
  209. package/vscode/client/src/extension.ts +64 -0
  210. package/vscode/client/tsconfig.json +12 -0
  211. package/vscode/icons/firefly-icon.png +0 -0
  212. package/vscode/icons/firefly-icon.svg +10 -0
  213. package/vscode/icons/firefly-logo-notext.png +0 -0
  214. package/vscode/icons/firefly-logo.png +0 -0
  215. package/vscode/language-configuration.json +39 -0
  216. package/vscode/package-lock.json +3623 -0
  217. package/vscode/package.json +144 -0
  218. package/vscode/snippets-none.json +1 -0
  219. package/vscode/snippets.json +241 -0
  220. package/vscode/syntaxes/firefly.tmLanguage.json +294 -0
  221. package/vscode/tsconfig.json +20 -0
package/core/List.ff ADDED
@@ -0,0 +1,440 @@
1
+ data List[T] {
2
+ Empty
3
+ Link(head: T, tail: List[T])
4
+ }
5
+
6
+ range(size: Int): List[Int]
7
+ target js sync """
8
+ let result = ff_core_List.Empty();
9
+ for(let i = size_ - 1; i >= 0; i--) {
10
+ result = ff_core_List.Link(i, result);
11
+ }
12
+ return result;
13
+ """
14
+
15
+ build[A, B](initial: A, body: A => Option[Pair[A, B]]): List[B] {
16
+ function go(state: A, result: List[B]): List[B] {
17
+ body(state).{
18
+ | None => result.reverse()
19
+ | Some(Pair(s, x)) => tailcall go(s, [x, ...result])
20
+ }
21
+ }
22
+ go(initial, [])
23
+ }
24
+
25
+ extend self[T]: List[T] {
26
+
27
+ addAll(list: List[T]): List[T] {
28
+ [self, list].flatten()
29
+ }
30
+
31
+ toStack(): Stack[T]
32
+ target js sync """
33
+ let current = self_;
34
+ let result = [];
35
+ while(current.Link) {
36
+ result.push(current.head_);
37
+ current = current.tail_;
38
+ }
39
+ return {array: result};
40
+ """
41
+
42
+ toArray(): Array[T] {
43
+ self.toStack().drain()
44
+ }
45
+
46
+ grab(index: Int): T {
47
+ function go(list: List[T], i: Int): T {
48
+ list.{
49
+ | [] => Try.internalThrowGrabException()
50
+ | [head, ...] {i == 0} => head
51
+ | [_, ...tail] => tailcall go(tail, i - 1)
52
+ }
53
+ }
54
+ go(self, index)
55
+ }
56
+
57
+ first(): Option[T] {
58
+ self.{
59
+ | [] => None
60
+ | [head, ...] => Some(head)
61
+ }
62
+ }
63
+
64
+ last(): Option[T] {
65
+ self.{
66
+ | [] => None
67
+ | [head] => Some(head)
68
+ | [_, ...tail] => tailcall tail.last()
69
+ }
70
+ }
71
+
72
+ grabFirst(): T {
73
+ self.first().else {Try.internalThrowGrabException()}
74
+ }
75
+
76
+ grabLast(): T {
77
+ self.last().else {Try.internalThrowGrabException()}
78
+ }
79
+
80
+ dropFirst(count: Int = 1): List[T] {
81
+ self.{
82
+ | _ {count <= 0} => self
83
+ | [] => self
84
+ | [_, ...tail] => tailcall tail.dropFirst(count - 1)
85
+ }
86
+ }
87
+
88
+ dropLast(count: Int = 1): List[T] {
89
+ self.reverse().dropFirst(count).reverse()
90
+ }
91
+
92
+ takeFirst(count: Int = 1): List[T] {
93
+ function go(list: List[T], count: Int, result: List[T]): List[T] {
94
+ list.{
95
+ | _ {count <= 0} => result.reverse()
96
+ | [] => result.reverse()
97
+ | [head, ...tail] => tailcall go(tail, count - 1, [head, ...result])
98
+ }
99
+ }
100
+ go(self, count, [])
101
+ }
102
+
103
+ takeLast(count: Int = 1): List[T] {
104
+ self.reverse().takeFirst(count).reverse()
105
+ }
106
+
107
+ pairs(): List[Pair[Int, T]] {
108
+ mutable i = 0
109
+ self.map {x =>
110
+ let r = Pair(i, x)
111
+ i += 1
112
+ r
113
+ }
114
+ }
115
+
116
+ slice(from: Int, until: Int): List[T] {
117
+ self.dropFirst(from).takeFirst(until - from)
118
+ }
119
+
120
+ isEmpty(): Bool {
121
+ self.{
122
+ | [] => True
123
+ | _ => False
124
+ }
125
+ }
126
+
127
+ size(): Int {
128
+ function go(list: List[T], result: Int): Int {
129
+ list.{
130
+ | [] => result
131
+ | [_, ...tail] => tailcall go(tail, result + 1)
132
+ }
133
+ }
134
+ go(self, 0)
135
+ }
136
+
137
+ each(body: T => Unit): Unit {
138
+ self.{
139
+ | [] =>
140
+ | [head, ...tail] =>
141
+ body(head)
142
+ tailcall tail.each(body)
143
+ }
144
+ }
145
+
146
+ all(body: T => Bool): Bool {
147
+ self.{
148
+ | [] => True
149
+ | [head, ...] {!body(head)} => False
150
+ | [_, ...tail] => tailcall tail.all(body)
151
+ }
152
+ }
153
+
154
+ any(body: T => Bool): Bool {
155
+ self.{
156
+ | [] => False
157
+ | [head, ...] {body(head)} => True
158
+ | [_, ...tail] => tailcall tail.any(body)
159
+ }
160
+ }
161
+
162
+ find(body: T => Bool): Option[T] {
163
+ self.{
164
+ | [] => None
165
+ | [head, ...] {body(head)} => Some(head)
166
+ | [_, ...tail] => tailcall tail.find(body)
167
+ }
168
+ }
169
+
170
+ filter(body: T => Bool): List[T] {
171
+ function go(list: List[T], result: List[T]): List[T] {
172
+ list.{
173
+ | [] =>
174
+ result.reverse()
175
+ | [head, ...tail] {body(head)} =>
176
+ tailcall go(tail, [head, ...result])
177
+ | [_, ...tail] =>
178
+ tailcall go(tail, result)
179
+ }
180
+ }
181
+ go(self, [])
182
+ }
183
+
184
+ partition(body: T => Bool): Pair[List[T], List[T]] {
185
+ Pair(self.filter(body), self.filter {!body(_)})
186
+ }
187
+
188
+ map[R](body: T => R): List[R] {
189
+ function go(list: List[T], result: List[R]): List[R] {
190
+ list.{
191
+ | [] =>
192
+ result.reverse()
193
+ | [head, ...tail] =>
194
+ tailcall go(tail, [body(head), ...result])
195
+ }
196
+ }
197
+ go(self, [])
198
+ }
199
+
200
+ flatMap[R](body: T => List[R]): List[R] {
201
+ function go(list: List[T], result: List[List[R]]): List[R] {
202
+ list.{
203
+ | [] =>
204
+ result.reverse().flatten()
205
+ | [head, ...tail] =>
206
+ tailcall go(tail, [body(head), ...result])
207
+ }
208
+ }
209
+ go(self, [])
210
+ }
211
+
212
+ collect[R](body: T => Option[R]): List[R] {
213
+ self.toStream().collect(body).toList()
214
+ }
215
+
216
+ collectFirst[R](body: T => Option[R]): Option[R] {
217
+ self.{
218
+ | [] =>
219
+ None
220
+ | [head, ...tail] =>
221
+ body(head).{
222
+ | None => tailcall tail.collectFirst(body)
223
+ | Some(value) => Some(value)
224
+ }
225
+ }
226
+ }
227
+
228
+ foldLeft[R](initial: R, body: (R, T) => R): R {
229
+ function go(state: R, list: List[T]): R {
230
+ list.{
231
+ | [] =>
232
+ state
233
+ | [head, ...tail] =>
234
+ tailcall go(body(state, head), tail)
235
+ }
236
+ }
237
+ go(initial, self)
238
+ }
239
+
240
+ updated(index: Int, value: T): List[T] {
241
+ function go(list: List[T], i: Int, result: List[T]): List[T] {
242
+ list.{
243
+ | [] =>
244
+ result.reverse()
245
+ | [head, ...tail] {i == 0} =>
246
+ tailcall go(tail, i - 1, [value, ...result])
247
+ | [head, ...tail] =>
248
+ tailcall go(tail, i - 1, [head, ...result])
249
+ }
250
+ }
251
+ go(self, index, [])
252
+ }
253
+
254
+ modify(index: Int, body: T => T): List[T] {
255
+ function go(list: List[T], i: Int, result: List[T]): List[T] {
256
+ list.{
257
+ | [] =>
258
+ result.reverse()
259
+ | [head, ...tail] {i == 0} =>
260
+ tailcall go(tail, i - 1, [body(head), ...result])
261
+ | [head, ...tail] =>
262
+ tailcall go(tail, i - 1, [head, ...result])
263
+ }
264
+ }
265
+ go(self, index, [])
266
+ }
267
+
268
+ zip[S](that: List[S]): List[Pair[T, S]] {
269
+ function go(list1: List[T], list2: List[S], result: List[Pair[T, S]]): List[Pair[T, S]] {
270
+ Pair(list1, list2).{
271
+ | Pair([x, ...xs], [y, ...ys]) =>
272
+ tailcall go(xs, ys, [Pair(x, y), ...result])
273
+ | _ =>
274
+ result.reverse()
275
+ }
276
+ }
277
+ go(self, that, [])
278
+ }
279
+
280
+ sortBy[O: Order](body: T => O): List[T] {
281
+ if(self.size() <= 1) {self} else:
282
+ let stack = self.toStack()
283
+ stack.sortBy(body)
284
+ stack.toList()
285
+ }
286
+
287
+ sortWith(compare: (T, T) => Ordering): List[T] {
288
+ if(self.size() <= 1) {self} else:
289
+ let stack = self.toStack()
290
+ stack.sortWith(compare)
291
+ stack.toList()
292
+ }
293
+
294
+ reverse(): List[T] {
295
+ function go(list: List[T], result: List[T]): List[T] {
296
+ list.{
297
+ | [] =>
298
+ result
299
+ | [head, ...tail] =>
300
+ tailcall go(tail, [head, ...result])
301
+ }
302
+ }
303
+ go(self, [])
304
+ }
305
+
306
+ chunk(chunkSize: Int): List[List[T]] {
307
+ mutable results = []
308
+ mutable result = []
309
+ mutable added = 0
310
+ self.each {item =>
311
+ if(added < chunkSize) {
312
+ result = [item, ...result]
313
+ added += 1
314
+ } else {
315
+ results = [result.reverse(), ...results]
316
+ result = [item]
317
+ added = 1
318
+ }
319
+ }
320
+ if(added != 0) {
321
+ results = [result.reverse(), ...results]
322
+ }
323
+ results.reverse()
324
+ }
325
+
326
+ toStream(cycle: Bool = False): Stream[T] {
327
+ mutable remaining = self
328
+ Stream(
329
+ next = {
330
+ remaining.{
331
+ | [head, ...tail] =>
332
+ remaining = tail
333
+ Some(head)
334
+ | [] {self | [head, ...tail]} {cycle} =>
335
+ remaining = tail
336
+ Some(head)
337
+ | [] =>
338
+ None
339
+ }
340
+ }
341
+ close = {
342
+ remaining = []
343
+ }
344
+ )
345
+ }
346
+
347
+ insertBetween(separator: List[T]): List[T] {
348
+ self.flatMap {e => [...separator, e]}.dropFirst(separator.size())
349
+ }
350
+
351
+ }
352
+
353
+ extend self[T: Order]: List[T] {
354
+
355
+ sort(): List[T] {
356
+ if(self.size() <= 1) {self} else:
357
+ let stack = self.toStack()
358
+ stack.sort()
359
+ stack.toList()
360
+ }
361
+
362
+ toSet(): Set[T] {
363
+ self.foldLeft(Set.empty[T]()) {set, value => set.add(value)}
364
+ }
365
+
366
+ distinct(): List[T] {
367
+ mutable seen = [].toSet()
368
+ self.filter {
369
+ | item {!seen.contains(item)} =>
370
+ seen = seen.add(item)
371
+ True
372
+ | _ =>
373
+ False
374
+ }
375
+ }
376
+
377
+ }
378
+
379
+ extend self[T: Show]: List[T] {
380
+
381
+ show(): String {
382
+ Show.show(self)
383
+ }
384
+
385
+ }
386
+
387
+ extend self[T]: List[List[T]] {
388
+
389
+ flatten(): List[T] {
390
+ function finish(list: List[T], result: List[T]): List[T] {
391
+ | as, [] => as
392
+ | as, [x, ...xs] => tailcall finish([x, ...as], xs)
393
+ }
394
+ function go(lists: List[List[T]], result: List[T]): List[T] {
395
+ lists.{
396
+ | [] => []
397
+ | [as] => finish(as, result)
398
+ | [[], ...aas] => tailcall go(aas, result)
399
+ | [[a, ...as], ...aas] => tailcall go([as, ...aas], [a, ...result])
400
+ }
401
+ }
402
+ go(self, [])
403
+ }
404
+
405
+ }
406
+
407
+ extend self[K: Order, V]: List[Pair[K, V]] {
408
+
409
+ toMap(): Map[K, V] {
410
+ self.foldLeft(Map.empty[K, V]()) {map, pair => map.add(pair.first, pair.second)}
411
+ }
412
+
413
+ group(): Map[K, List[V]] {
414
+ let initial: List[Pair[K, List[V]]] = []
415
+ self.foldLeft(initial.toMap()) {map, pair => map.addToList(pair.first, pair.second)}
416
+ }
417
+
418
+ }
419
+
420
+ extend self[K, V]: List[Pair[K, V]] {
421
+
422
+ unzip(): Pair[List[K], List[V]] {
423
+ function go(pairs: List[Pair[K, V]], ks: List[K], vs: List[V]): Pair[List[K], List[V]] {
424
+ pairs.{
425
+ | [] =>
426
+ Pair(ks.reverse(), vs.reverse())
427
+ | [Pair(k, v), ...tail] =>
428
+ tailcall go(tail, [k, ...ks], [v, ...vs])
429
+ }
430
+ }
431
+ go(self, [], [])
432
+ }
433
+
434
+ }
435
+
436
+ extend self: List[String] {
437
+
438
+ join(separator: String = ""): String {self.toArray().join(separator)}
439
+
440
+ }
package/core/Lock.ff ADDED
@@ -0,0 +1,144 @@
1
+ capability Lock {}
2
+ capability LockCondition {}
3
+
4
+ extend self: Lock {
5
+
6
+ condition(): LockCondition
7
+ target js async """
8
+ return {lock: self_, stack: [], queue: []}
9
+ """
10
+
11
+ acquire(): Unit
12
+ target js async """
13
+ if(self_.level === 0) {
14
+ self_.owner = $task
15
+ self_.level += 1
16
+ } else {
17
+ if(self_.owner !== $task) {
18
+ try {
19
+ await new Promise((resolve, reject) => {
20
+ $task.controller.signal.addEventListener('abort', reject)
21
+ try {
22
+ self_.queue.push({owner: $task, resolve: resolve})
23
+ } finally {
24
+ $task.controller.signal.removeEventListener('abort', reject)
25
+ }
26
+ })
27
+ } finally {
28
+ if($task.controller.signal.aborted) $task.controller = new AbortController()
29
+ }
30
+ } else {
31
+ self_.level += 1
32
+ }
33
+ }
34
+ """
35
+
36
+ release(): Unit
37
+ target js async """
38
+ if(self_.owner !== $task) {
39
+ throw new Error("Tried to release a lock without acquiring it first.")
40
+ } else if(self_.level > 1) {
41
+ self_.level -= 1
42
+ } else {
43
+ self_.owner = null
44
+ self_.level = 0
45
+ if(self_.stack.length === 0) {
46
+ const empty = self_.stack
47
+ self_.stack = self_.queue.reverse()
48
+ self_.queue = empty
49
+ }
50
+ if(self_.stack.length !== 0) {
51
+ const pending = self_.stack.pop()
52
+ self_.owner = pending.owner
53
+ self_.level = 1
54
+ pending.resolve()
55
+ }
56
+ }
57
+ """
58
+
59
+ do[T](body: () => T): T {
60
+ self.acquire()
61
+ try {
62
+ body()
63
+ } finally {
64
+ self.release()
65
+ } grab()
66
+ }
67
+
68
+ }
69
+
70
+ extend self: LockCondition {
71
+
72
+ sleep(): Unit
73
+ target js async """
74
+ if(self_.lock.owner !== $task) {
75
+ throw new Error("Tried to sleep on a condition without acquiring it first.")
76
+ }
77
+ const level = self_.lock.level
78
+ self_.lock.level = 1
79
+ await ff_core.Lock_release$(self_.lock)
80
+ try {
81
+ await new Promise((resolve, reject) => {
82
+ $task.controller.signal.addEventListener('abort', reject)
83
+ try {
84
+ self_.queue.push(resolve)
85
+ } finally {
86
+ $task.controller.signal.removeEventListener('abort', reject)
87
+ if($task.controller.signal.aborted) $task.controller = new AbortController()
88
+ }
89
+ })
90
+ } finally {
91
+ let exception = null
92
+ let acquired = false
93
+ while(!acquired) {
94
+ try {
95
+ await ff_core.Lock_acquire$(self_.lock)
96
+ self_.lock.level = level
97
+ acquired = true
98
+ } catch(e) {
99
+ exception = e
100
+ }
101
+ }
102
+ if(e !== null) throw e;
103
+ }
104
+ """
105
+
106
+ sleepUntil[T](body: () => Option[T]): T {
107
+ body().{
108
+ | Some(value) =>
109
+ value
110
+ | None =>
111
+ self.sleep()
112
+ tailcall self.sleepUntil(body)
113
+ }
114
+ }
115
+
116
+ wakeOne(): Unit
117
+ target js async """
118
+ if(self_.stack.length === 0) {
119
+ const empty = self_.stack
120
+ self_.stack = self_.queue.reverse()
121
+ self_.queue = empty
122
+ }
123
+ if(self_.stack.length !== 0) {
124
+ const resolve = self_.stack.pop()
125
+ resolve()
126
+ }
127
+ """
128
+
129
+ wakeAll(): Unit
130
+ target js async """
131
+ while(self_.stack.length !== 0) {
132
+ const resolve = self_.stack.pop()
133
+ resolve()
134
+ }
135
+ const empty = self_.stack
136
+ self_.stack = self_.queue.reverse()
137
+ self_.queue = empty
138
+ while(self_.stack.length !== 0) {
139
+ const resolve = self_.stack.pop()
140
+ resolve()
141
+ }
142
+ """
143
+
144
+ }
package/core/Log.ff ADDED
@@ -0,0 +1,24 @@
1
+ show[T: Show](value: T): Unit {
2
+ trace(Show.show(value))
3
+ }
4
+
5
+ debugDynamic[T](value: T): Unit
6
+ target js sync "console.debug(value_)"
7
+
8
+ debug(value: String): Unit
9
+ target js sync "console.debug(value_)"
10
+
11
+ verbose(value: String): Unit
12
+ target js sync "console.log(value_)"
13
+
14
+ info(value: String): Unit
15
+ target js sync "console.info(value_)"
16
+
17
+ warn(value: String): Unit
18
+ target js sync "console.warn(value_)"
19
+
20
+ error(value: String): Unit
21
+ target js sync "console.error(value_)"
22
+
23
+ trace(value: String): Unit
24
+ target js sync "process.stderr.write(value_ + String.fromCharCode(10))"