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
@@ -0,0 +1,173 @@
1
+ data DeserializationChecksumException()
2
+
3
+ serialize[T: Serializable](value: T, initialBufferSize: Int = 1024): Buffer {
4
+ let serialization = Serialization(Buffer.make(initialBufferSize), 0, 0)
5
+ serializeUsing(serialization, value)
6
+ serialization.autoResize(4)
7
+ serialization.buffer.setInt32(serialization.offset, serialization.checksum)
8
+ serialization.offset += 4
9
+ serialization.buffer.view(0, serialization.offset)
10
+ }
11
+
12
+ deserialize[T: Serializable](buffer: Buffer): T {
13
+ let serialization = Serialization(buffer, 0, 0)
14
+ let result = deserializeUsing(serialization)
15
+ let checksum = serialization.buffer.grabInt32(serialization.offset)
16
+ if(checksum != serialization.checksum) {
17
+ throw(DeserializationChecksumException)
18
+ }
19
+ result
20
+ }
21
+
22
+ trait T: Serializable {
23
+ serializeUsing(serialization: Serialization, value: T): Unit
24
+ deserializeUsing(serialization: Serialization): T
25
+ }
26
+
27
+ class Serialization(
28
+ mutable buffer: Buffer
29
+ mutable offset: Int
30
+ mutable checksum: Int
31
+ )
32
+
33
+ extend self: Serialization {
34
+ autoResize(minSpareCapacity: Int) {
35
+ if(self.offset + minSpareCapacity > self.buffer.size()) {
36
+ let minSize = self.buffer.size() + minSpareCapacity
37
+ let newBuffer = Buffer.make((self.buffer.size() * 2).max(minSize))
38
+ newBuffer.setAll(0, self.buffer)
39
+ self.buffer = newBuffer
40
+ }
41
+ }
42
+ }
43
+
44
+ instance Int: Serializable {
45
+ serializeUsing(serialization: Serialization, value: Int): Unit {
46
+ serialization.autoResize(8)
47
+ serialization.buffer.setInt64(serialization.offset, value)
48
+ serialization.offset += 8
49
+ }
50
+ deserializeUsing(serialization: Serialization): Int {
51
+ let result = serialization.buffer.grabInt64(serialization.offset)
52
+ serialization.offset += 8
53
+ result
54
+ }
55
+ }
56
+
57
+ instance Bool: Serializable {
58
+ serializeUsing(serialization: Serialization, value: Bool): Unit {
59
+ serialization.autoResize(1)
60
+ serialization.buffer.setUint8(serialization.offset, if(value) {1} else {0})
61
+ serialization.offset += 1
62
+ }
63
+ deserializeUsing(serialization: Serialization): Bool {
64
+ let result = serialization.buffer.grabUint8(serialization.offset)
65
+ serialization.offset += 1
66
+ result == 1
67
+ }
68
+ }
69
+
70
+ instance Array[T: Serializable]: Serializable {
71
+ serializeUsing(serialization: Serialization, value: Array[T]): Unit {
72
+ if(value.size() < 255) {
73
+ serialization.autoResize(1)
74
+ serialization.buffer.setUint8(serialization.offset, value.size())
75
+ serialization.offset += 1
76
+ } elseIf {value.size() < 1073741824} {
77
+ serialization.autoResize(1 + 4)
78
+ serialization.buffer.setUint8(serialization.offset, 255)
79
+ serialization.buffer.setUint32(1 + serialization.offset, value.size())
80
+ serialization.offset += 1 + 4
81
+ } else {
82
+ panic("Can't serialize arrays where size() >= 1073741824")
83
+ }
84
+ value.each {serializeUsing(serialization, _)}
85
+ }
86
+ deserializeUsing(serialization: Serialization): Array[T] {
87
+ let smallSize = serialization.buffer.grabUint8(serialization.offset)
88
+ if(smallSize != 255) {
89
+ serialization.offset += 1
90
+ Array.fillBy(smallSize) {_ => deserializeUsing(serialization)}
91
+ } else {
92
+ let size = serialization.buffer.grabUint32(serialization.offset + 1)
93
+ serialization.offset += 1 + 4
94
+ Array.fillBy(size) {_ => deserializeUsing(serialization)}
95
+ }
96
+ }
97
+ }
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
+ instance Set[T: Serializable: Order]: Serializable {
109
+ serializeUsing(serialization: Serialization, value: Set[T]): Unit {
110
+ serializeUsing(serialization, value.toArray())
111
+ }
112
+ deserializeUsing(serialization: Serialization): Set[T] {
113
+ deserializeUsing[Array[T]](serialization).toSet()
114
+ }
115
+ }
116
+
117
+ instance Map[K: Serializable: Order, V: Serializable]: Serializable {
118
+ serializeUsing(serialization: Serialization, value: Map[K, V]): Unit {
119
+ serializeUsing(serialization, value.toArray())
120
+ }
121
+ deserializeUsing(serialization: Serialization): Map[K, V] {
122
+ deserializeUsing[Array[Pair[K, V]]](serialization).toMap()
123
+ }
124
+ }
125
+
126
+ instance String: Serializable {
127
+ serializeUsing(serialization: Serialization, value: String): Unit {
128
+ serialization.autoResize(1 + value.size())
129
+ serialization.buffer.setUint8(serialization.offset, value.size())
130
+ if(value.size() < 255 && internalSetLatin1(serialization.buffer, serialization.offset + 1, value)) {
131
+ serialization.offset += 1 + value.size()
132
+ } elseIf {value.size() < 1073741824} {
133
+ let stringBuffer = value.toBuffer()
134
+ serialization.autoResize(5 + stringBuffer.size())
135
+ serialization.buffer.setUint8(serialization.offset, 255)
136
+ serialization.buffer.setUint32(serialization.offset + 1, stringBuffer.size())
137
+ serialization.buffer.setAll(serialization.offset + 5, stringBuffer)
138
+ serialization.offset += 5 + stringBuffer.size()
139
+ } else {
140
+ panic("Can't serialize strings where size() >= 1073741824")
141
+ }
142
+ }
143
+ deserializeUsing(serialization: Serialization): String {
144
+ let smallSize = serialization.buffer.grabUint8(serialization.offset)
145
+ if(smallSize != 255) {
146
+ let result = internalGrabLatin1(serialization.buffer, serialization.offset + 1, smallSize)
147
+ serialization.offset += 1 + smallSize
148
+ result
149
+ } else {
150
+ let size = serialization.buffer.grabUint32(serialization.offset + 1)
151
+ let stringBuffer = serialization.buffer.view(serialization.offset + 5, serialization.offset + 5 + size)
152
+ serialization.offset += 5 + size
153
+ stringBuffer.toString()
154
+ }
155
+ }
156
+ }
157
+
158
+
159
+ internalSetLatin1(self: Buffer, byteOffset: Int, value: String): Bool
160
+ target js sync """
161
+ for(let i = 0; i < value_.length; i++) {
162
+ let charCode = value_.charCodeAt(i)
163
+ if(charCode >= 256) return false
164
+ self_.setUint8(byteOffset_ + i, charCode)
165
+ }
166
+ return true
167
+ """
168
+
169
+ internalGrabLatin1(self: Buffer, byteOffset: Int, size: Int): String
170
+ target js sync """
171
+ const codeUnits = new Uint8Array(self_.buffer, self_.byteOffset + byteOffset_, size_)
172
+ return String.fromCharCode.apply(null, codeUnits)
173
+ """
package/core/Set.ff ADDED
@@ -0,0 +1,38 @@
1
+ newtype Set[T](map: Map[T, Unit])
2
+
3
+ empty[T](): Set[T] {
4
+ Set(Map.empty())
5
+ }
6
+
7
+ extend self[T: Order]: Set[T] {
8
+ add(value: T): Set[T] {Set(self.map.add(value, Unit))}
9
+ addAll(that: Set[T]): Set[T] {Set(self.map.addAll(that.map))}
10
+ remove(value: T): Set[T] {Set(self.map.remove(value))}
11
+ removeAll(that: Set[T]): Set[T] {Set(self.map.removeAll(that.map))}
12
+ contains(value: T): Bool {self.map.contains(value)}
13
+ size(): Int {self.map.size()}
14
+ toList(): List[T] {self.map.toList().map {_.first}}
15
+ toArray(): Array[T] {self.map.toArray().map {_.first}}
16
+ toStream(cycle: Bool = False): Stream[T] {self.map.toStream(cycle).map {_.first }}
17
+ each(body: T => Unit): Unit {self.map.each {| k, _ => body(k)}}
18
+ eachWhile(body: T => Bool): Bool {self.map.eachWhile {| k, _ => body(k)}}
19
+ find(body: T => Bool): Option[T] {self.map.find {| k, _ => body(k)}.map {_.first}}
20
+ }
21
+
22
+ instance Set[A: Equal: Order]: Equal {
23
+ equals(x: Set[A], y: Set[A]): Bool {
24
+ Equal.equals(x.toList(), y.toList())
25
+ }
26
+ }
27
+
28
+ instance Set[A: Order]: Order {
29
+ compare(x: Set[A], y: Set[A]): Ordering {
30
+ Ordering.compare(x.toList(), y.toList())
31
+ }
32
+ }
33
+
34
+ instance Set[A: Show: Order]: Show {
35
+ show(value: Set[A]): String {
36
+ Show.show(value.toList()) + ".toSet()"
37
+ }
38
+ }
package/core/Show.ff ADDED
@@ -0,0 +1,43 @@
1
+ trait T: Show {
2
+ show(value: T): String
3
+ }
4
+
5
+ instance Bool: Show {
6
+ show(value: Bool): String {
7
+ | False => "False"
8
+ | True => "True"
9
+ }
10
+ }
11
+
12
+ instance Nothing: Show {
13
+ show(value: Nothing): String {""}
14
+ }
15
+
16
+ instance Char: Show {
17
+ show(value: Char): String {value.toString()}
18
+ }
19
+
20
+ instance Int: Show {
21
+ show(value: Int): String {"" + value}
22
+ }
23
+
24
+ instance Float: Show {
25
+ show(value: Float): String {"" + value}
26
+ }
27
+
28
+ instance String: Show {
29
+ show(value: String): String
30
+ target js sync "return JSON.stringify(value_);"
31
+ }
32
+
33
+ instance List[T: Show]: Show {
34
+ show(value: List[T]): String {
35
+ "[" + value.map(show).join(", ") + "]"
36
+ }
37
+ }
38
+
39
+ instance Pair[A: Show, B: Show]: Show {
40
+ show(value: Pair[A, B]): String {
41
+ "Pair(" + show(value.first) + ", " + show(value.second) + ")"
42
+ }
43
+ }
package/core/Stack.ff ADDED
@@ -0,0 +1,263 @@
1
+ class Stack[T] {}
2
+
3
+ make[T](): Stack[T]
4
+ target js sync "return {array: []}"
5
+
6
+ fill[T](size: Int, value: T): Array[T]
7
+ target js sync """
8
+ return {array: new Array(size_).fill(value_)};
9
+ """
10
+
11
+ fillBy[T](size: Int, body: Int => T): Array[T]
12
+ target js sync """
13
+ return {array: Array.from({length: size_}, (_, i) => body_(i))};
14
+ """
15
+
16
+ range(size: Int): Array[Int]
17
+ target js sync """
18
+ return {array: Array.from({length: size_}, (_, i) => i)};
19
+ """
20
+
21
+ extend self[T]: Stack[T] {
22
+
23
+ isEmpty(): Bool
24
+ target js sync "return self_.array.length === 0"
25
+
26
+ size(): Int
27
+ target js sync "return self_.array.length"
28
+
29
+ get(index: Int): Option[T]
30
+ target js sync """
31
+ return index_ >= 0 && index_ < self_.array.length
32
+ ? ff_core_Option.Some(self_.array[index_])
33
+ : ff_core_Option.None()
34
+ """
35
+
36
+ grab(index: Int): T
37
+ target js sync """
38
+ if(index_ < 0 || index_ >= self_.array.length) {
39
+ ff_core_Try.internalThrowGrabException_()
40
+ }
41
+ return self_.array[index_]
42
+ """
43
+
44
+ grabFirst(): T { self.grab(0) }
45
+
46
+ grabLast(): T { self.grab(self.size() - 1) }
47
+
48
+ first(): Option[T]
49
+ target js sync """
50
+ return self_.array.length > 0
51
+ ? ff_core_Option.Some(self_.array[0])
52
+ : ff_core_Option.None()
53
+ """
54
+
55
+ last(): Option[T]
56
+ target js sync """
57
+ return self_.array.length > 0
58
+ ? ff_core_Option.Some(self_.array[self_.array.length - 1])
59
+ : ff_core_Option.None()
60
+ """
61
+
62
+ push(value: T): Unit
63
+ target js sync "self_.array.push(value_)"
64
+
65
+ pushAll(value: Stack[T]): Unit
66
+ target js sync "self_.array.push(...value_.array)"
67
+
68
+ pushArray(value: Array[T]): Unit
69
+ target js sync "self_.array.push(...value_)"
70
+
71
+ pop(): Option[T]
72
+ target js sync """
73
+ return self_.array.length > 0
74
+ ? ff_core_Option.Some(self_.array.pop())
75
+ : ff_core_Option.None()
76
+ """
77
+
78
+ set(index: Int, value: T): Unit
79
+ target js sync """
80
+ if(index_ < 0 || index_ > self_.array.length) {
81
+ ff_core_Try.internalThrowGrabException_()
82
+ }
83
+ self_.array[index_] = value_
84
+ """
85
+
86
+ modify(index: Int, body: T => T): Unit
87
+ target js sync """
88
+ if(index_ < 0 || index_ >= self_.array.length) {
89
+ ff_core_Try.internalThrowGrabException_()
90
+ }
91
+ self_.array[index_] = body_(self_.array[index_])
92
+ """
93
+ target js async """
94
+ if(index_ < 0 || index_ >= self_.array.length) {
95
+ ff_core_Try.internalThrowGrabException_()
96
+ }
97
+ self_.array[index_] = await body_(self_.array[index_], $task)
98
+ """
99
+
100
+ fill(value: T, start: Int = 0, end: Int = 9007199254740991): Unit
101
+ target js sync """
102
+ self.array.fill(value_, start, end);
103
+ """
104
+
105
+ copy(target: Int, start: Int, end: Int): Unit
106
+ target js sync """
107
+ self.array.copyWithin(target, start, end);
108
+ """
109
+
110
+ delete(start: Int, deleteCount: Int): Unit
111
+ target js sync """
112
+ self.array.splice(start, deleteCount_);
113
+ """
114
+
115
+ insert(start: Int, value: T, deleteCount: Int = 0): Unit
116
+ target js sync """
117
+ self.array.splice(start, deleteCount_, value_);
118
+ """
119
+
120
+ insertAll(start: Int, value: Stack[T], deleteCount: Int = 0): Unit
121
+ target js sync """
122
+ self.array.splice(start, deleteCount_, ...value_.array);
123
+ """
124
+
125
+ insertArray(start: Int, value: Array[T], deleteCount: Int = 0): Unit
126
+ target js sync """
127
+ self.array.splice(start, deleteCount_, ...value_);
128
+ """
129
+
130
+ each(body: T => Unit): Unit
131
+ target js sync """
132
+ return self_.array.forEach(body_);
133
+ """
134
+ target js async """
135
+ for(let i = 0; i < self_.array.length; i++) {
136
+ await body_(self_.array[i], $task)
137
+ }
138
+ """
139
+
140
+ eachWhile(body: T => Bool): Unit
141
+ target js sync "for(const value of self_.array) if(!body_(value)) break"
142
+ target js async "for(const value of self_.array) if(!await body_(value, $task)) break"
143
+
144
+ all(body: T => Bool): Bool {
145
+ mutable result = True
146
+ self.eachWhile {x =>
147
+ result = body(x)
148
+ result
149
+ }
150
+ result
151
+ }
152
+
153
+ any(body: T => Bool): Bool {
154
+ mutable result = False
155
+ self.eachWhile {x =>
156
+ result = body(x)
157
+ (!result)
158
+ }
159
+ result
160
+ }
161
+
162
+ indexWhere(body: T => Bool): Option[Int] {
163
+ mutable i = -1
164
+ mutable result = False
165
+ self.eachWhile {x =>
166
+ i += 1
167
+ result = body(x)
168
+ (!result)
169
+ }
170
+ if(result) {i}
171
+ }
172
+
173
+ drain(): Array[T]
174
+ target js sync "const result = self_.array; self_.array = []; return result"
175
+
176
+ toArray(start: Int = 0, end: Int = 9007199254740991): Array[T]
177
+ target js sync """return self_.array.slice(start_, end_)"""
178
+
179
+ toList(start: Int = 0, end: Int = 9007199254740991): List[T]
180
+ target js sync """
181
+ if(start_ < 0) start_ = self_.array.length + start_
182
+ if(end_ < 0) end_ = self_.array.length + start_
183
+ start_ = Math.max(0, Math.min(start_, self_.array.length))
184
+ end_ = Math.max(0, Math.min(end_, self_.array.length))
185
+ let result = ff_core_List.Empty();
186
+ for(let i = end_ - 1; i >= start_; i--) {
187
+ result = ff_core_List.Link(self_.array[i], result);
188
+ }
189
+ return result;
190
+ """
191
+
192
+ toStream(start: Int = 0, end: Int = 9007199254740991): Stream[T] {
193
+ self.toArray(start, end).toStream()
194
+ }
195
+
196
+ reverse(): Unit
197
+ target js sync "self_.array.reverse()"
198
+
199
+ sortBy[S: Order](body: T => S): Unit {
200
+ self.sortWith {Ordering.compare(body(_), body(_))}
201
+ }
202
+
203
+ sortWith(ordering: (T, T) => Ordering): Unit {
204
+ sortRange(self, ordering, 0, self.size())
205
+ }
206
+ target js sync "self_.array.sort((x, y) => ff_core_Ordering.Ordering_toInt(ordering_(x, y)))"
207
+
208
+ }
209
+
210
+ extend self[T: Order]: Stack[T] {
211
+
212
+ sort(): Unit {
213
+ self.sortWith(Ordering.compare)
214
+ }
215
+
216
+ }
217
+
218
+ extend self: Stack[String] {
219
+
220
+ join(separator: String = ""): String
221
+ target js sync "return self_.array.join(separator_)"
222
+
223
+ }
224
+
225
+ instance Stack[T: Show]: Show {
226
+ show(value: Stack[T]): String {
227
+ let stack = Stack.make()
228
+ stack.push("[")
229
+ value.each {x =>
230
+ if(stack.size() > 1) {stack.push(", ")}
231
+ stack.push(Show.show(x))
232
+ }
233
+ stack.push("].toStack()")
234
+ stack.join()
235
+ }
236
+ }
237
+
238
+ sortRange[T](stack: Stack[T], compare: (T, T) => Ordering, start: Int, end: Int): Unit {
239
+ if(end - start < 2) {} else:
240
+
241
+ mutable middle = start + ((end - start) / 2)
242
+ sortRange(stack, compare, start, middle)
243
+ sortRange(stack, compare, middle, end)
244
+
245
+ mutable i = start
246
+ mutable j = middle
247
+ while {i < middle && j < end} {
248
+ if(compare(stack.grab(i), stack.grab(j)) != OrderingAfter) {
249
+ i += 1
250
+ } else {
251
+ let value = stack.grab(j)
252
+ mutable k = j
253
+ while {k > i} {
254
+ stack.set(k, stack.grab(k - 1))
255
+ k -= 1
256
+ }
257
+ stack.set(i, value)
258
+ i += 1
259
+ middle += 1
260
+ j += 1
261
+ }
262
+ }
263
+ }