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,226 @@
1
+ data Body(term: Int)
2
+
3
+ data Constructor(
4
+ name: String
5
+ arity: Int
6
+ span: Int
7
+ )
8
+
9
+ data Pattern {
10
+ PVariable(name: String)
11
+ PConstructor(
12
+ constructor: Constructor
13
+ patterns: List[Pattern]
14
+ )
15
+ }
16
+
17
+ data Match(
18
+ rules: List[Pair[Pattern, Body]]
19
+ )
20
+
21
+ data TermDescription {
22
+ Positive(
23
+ constructor: Constructor
24
+ descriptions: List[TermDescription]
25
+ )
26
+ Negative(constructors: Set[String])
27
+ }
28
+
29
+ data Context(
30
+ list: List[Pair[Constructor, List[TermDescription]]]
31
+ )
32
+
33
+ data MatchResult {
34
+ Yes
35
+ No
36
+ Maybe
37
+ }
38
+
39
+ data Access{
40
+ Object
41
+ Select(
42
+ index: Int
43
+ access: Access
44
+ )
45
+ }
46
+
47
+ data Decision {
48
+ Failure
49
+ Success(body: Body)
50
+ IfEqual(
51
+ access: Access
52
+ constructor: Constructor
53
+ left: Decision
54
+ right: Decision
55
+ )
56
+ }
57
+
58
+ data Work(
59
+ patterns: List[Pattern]
60
+ objects: List[Access]
61
+ descriptions: List[TermDescription]
62
+ )
63
+
64
+ staticMatch(
65
+ constructor: Constructor
66
+ description: TermDescription
67
+ ) : MatchResult {
68
+ | c1, Positive(c2, _) { c1 == c2 } => Yes
69
+ | c1, Positive(_, _) => No
70
+ | c1, Negative(noneSet) { noneSet.contains(c1.name) } => No
71
+ | c1, Negative(noneSet) { c1.span == 1 + noneSet.size() } => Yes
72
+ | _, Negative(_) => Maybe
73
+ }
74
+
75
+ augment(context: Context, description: TermDescription): Context {
76
+ | Context([]), _ => Context([])
77
+ | Context([Pair(c, ds), ...rest]), d => Context([Pair(c, [d, ...ds]), ...rest])
78
+ }
79
+
80
+ norm(context: Context): Context {
81
+ | Context([Pair(c, ds), ...rest]) => augment(Context(rest), Positive(c, ds.reverse()))
82
+ | _ => panic("Norm is unexhaustive(!)")
83
+ }
84
+
85
+ buildDescription(
86
+ context: Context
87
+ description: TermDescription
88
+ works: List[Work]
89
+ ): TermDescription {
90
+ | Context([]), d, [] => d
91
+ | Context([Pair(c, ds1), ...rest]), d, [Work(_, _, ds2), ...works] =>
92
+ buildDescription(Context(rest), Positive(c, ds1.reverse() ++ [d, ...ds2]), works)
93
+ | _, _, _ => panic("buildDescription is unexhaustive: buildDescription(\n" +
94
+ " context = " + magicShow(context) + "\n" +
95
+ " description = " + magicShow(description) + "\n" +
96
+ " works = " + magicShow(works) + "\n)"
97
+ )
98
+ }
99
+
100
+ addNegative(description : TermDescription, constructor : Constructor) : TermDescription {
101
+ | Negative(noneSet), c {
102
+ !noneSet.contains(c.name) && noneSet.add(c.name).size() < c.span
103
+ } => Negative(noneSet.add(constructor.name))
104
+ | _, _ => panic("Negative TermDescription expected, got " + magicShow(description))
105
+ }
106
+
107
+ fail(
108
+ description: TermDescription
109
+ rules: List[Pair[Pattern, Body]]
110
+ ): Decision {
111
+ | _, [] => Failure
112
+ | d, [Pair(p, body), ...rules] =>
113
+ match(
114
+ pattern = p
115
+ object = Object
116
+ description = d
117
+ context = Context([])
118
+ works = []
119
+ body = body
120
+ rules = rules
121
+ )
122
+ | _, rs => panic("fail is unexhaustive:: rs=" + magicShow(rs))
123
+
124
+ }
125
+
126
+ succeed(
127
+ context: Context
128
+ works: List[Work]
129
+ body: Body
130
+ rules: List[Pair[Pattern, Body]]
131
+ ): Decision {
132
+ works.{
133
+ | [] => Success(body)
134
+ | [Work([], [], []), ...ws] => succeed(norm(context), ws, body, rules)
135
+ | [Work([p, ...ps], [o, ...os], [d, ...ds]), ...ws] =>
136
+ match(
137
+ pattern = p
138
+ object = o
139
+ description = d
140
+ context = context
141
+ works = [Work(ps, os, ds), ...ws]
142
+ body = body
143
+ rules = rules
144
+ )
145
+ | ws => panic("succeed is unexhaustive: ws=" + magicShow(ws))
146
+ }
147
+ }
148
+
149
+ match(
150
+ pattern: Pattern
151
+ object: Access
152
+ description: TermDescription
153
+ context: Context
154
+ works : List[Work]
155
+ body: Body
156
+ rules: List[Pair[Pattern, Body]]
157
+ ): Decision {
158
+ pattern.{
159
+ | PVariable(_) =>
160
+ succeed(augment(context, description), works, body, rules)
161
+ | PConstructor(c, ps) =>
162
+ function args[R](f: Int => R): List[R] {
163
+ List.range(c.arity).map(f)
164
+ }
165
+
166
+ function getDescriptionArguments(description: TermDescription) : List[TermDescription] {
167
+ | Negative(_) => args {_ => Negative(Set.empty())}
168
+ | Positive(c, ps) => ps
169
+ }
170
+
171
+ function getoargs(): List[Access] {
172
+ args {i => Select(i + 1, object)}
173
+ }
174
+
175
+ function doSucceed() : Decision {
176
+ let work = Work(ps, getoargs(), getDescriptionArguments(description))
177
+ succeed(
178
+ Context([Pair(c, []), ...context.list]),
179
+ [work, ...works],
180
+ body,
181
+ rules
182
+ )
183
+ }
184
+
185
+ function doFail(newDescription : TermDescription): Decision {
186
+ Log.debug("doFail: " + magicShow(newDescription))
187
+ fail(buildDescription(context, newDescription, works), rules)
188
+ }
189
+
190
+ staticMatch(c, description).{
191
+ | Yes => doSucceed()
192
+ | No => doFail(description)
193
+ | Maybe => IfEqual(
194
+ access = object,
195
+ constructor = c,
196
+ left = doSucceed(),
197
+ right = doFail(addNegative(description, c))
198
+ )
199
+ }
200
+ }
201
+ }
202
+
203
+ compile(input: Match): Decision {
204
+ fail(Negative(Set.empty()), input.rules)
205
+ }
206
+
207
+ main(system: NodeSystem): Unit {
208
+ //type Color {Red, Green, Blue}
209
+ let red = PConstructor(Constructor("Red", 0, 3), [])
210
+ let green = PConstructor(Constructor("Green", 0, 3), [])
211
+ let blue = PConstructor(Constructor("Blue", 0, 3), [])
212
+ function pair(first : Pattern, second : Pattern) : Pattern {
213
+ PConstructor(Constructor("()", 2, 1), [first, second])
214
+ }
215
+ let match = Match(rules = [
216
+ //Pair(red, Body(1))
217
+ //Pair(pair(red, blue), Body(1)),
218
+ //Pair(pair(green, blue), Body(2))
219
+ Pair(pair(blue, green), Body(3)),
220
+ Pair(pair(PVariable("x"), PVariable("y")), Body(4))
221
+ //Pair(red, Body(1)),
222
+ //Pair(PVariable("x"), Body(2))
223
+ ])
224
+
225
+ Log.debug(compile(match))
226
+ }
@@ -0,0 +1,10 @@
1
+ main(system: NodeSystem) {
2
+ read(read(system.readStream()))
3
+ }
4
+
5
+ read(input: Stream[Buffer]): Stream[Buffer] {
6
+ let pair = input.readBytes(10)
7
+ let arrayBuffer = pair.first
8
+ Log.show(arrayBuffer)
9
+ pair.second
10
+ }
@@ -0,0 +1,65 @@
1
+ main(system: NodeSystem) {
2
+ let inBuffers = 1.to(100).map {_ => Buffer.fromByteArray([5, 5, 3, 1, 2].toArray())}
3
+ let inStream = inBuffers.toStream()
4
+ let outStream = toRunLength(inStream, 3)
5
+ printRunLength(outStream)
6
+ }
7
+
8
+ printRunLength(outStream: Stream[Buffer]) {
9
+ let buffer = Buffer.fromBufferArray(outStream.toArray())
10
+ Log.debug(buffer.toHex())
11
+ let stack = Stack.make()
12
+ mutable i = 0
13
+ while {i < buffer.size()} {
14
+ 1.to(buffer.grabUint8(i)).each {_ => stack.push(buffer.grabUint8(i + 1))}
15
+ i += 2
16
+ }
17
+ Log.debug(Buffer.fromByteArray(stack.drain()).toHex())
18
+ }
19
+
20
+ toRunLength(stream: Stream[Buffer], bufferSize: Int = 65536): Stream[Buffer] {
21
+ mutable outBuffer = Buffer.make(bufferSize)
22
+ mutable outOffset = 0
23
+ let outBuffers = Stack.make()
24
+ function writeByte(byte: Int): Unit {
25
+ if(outOffset >= outBuffer.size()) {
26
+ outBuffers.push(outBuffer)
27
+ outBuffer = Buffer.make(bufferSize)
28
+ outOffset = 0
29
+ }
30
+ outBuffer.setUint8(outOffset, byte)
31
+ outOffset += 1
32
+ }
33
+ mutable extraCount = 0
34
+ mutable value = 0
35
+ let result = stream.flatMap {inBuffer =>
36
+ mutable i = 0
37
+ while {i < inBuffer.size()} {
38
+ value = if(extraCount > 0) {value} else {inBuffer.grabUint8(i)}
39
+ mutable j = if(extraCount > 0) {-1} else {0}
40
+ doWhile {
41
+ j += 1
42
+ j + extraCount < 256 && i + j < inBuffer.size() && inBuffer.grabUint8(i + j) == value
43
+ }
44
+ i += j
45
+ if(i < inBuffer.size() || j + extraCount == 256) {
46
+ writeByte(j + extraCount)
47
+ writeByte(value)
48
+ extraCount = 0
49
+ } else {
50
+ extraCount += j
51
+ }
52
+ }
53
+ outBuffers.drain().toStream()
54
+ }
55
+ result.addAll(Stream.init {
56
+ if(extraCount > 0) {
57
+ writeByte(extraCount)
58
+ writeByte(value)
59
+ }
60
+ if(outOffset > 0) {
61
+ outBuffers.push(outBuffer.view(0, outOffset))
62
+ }
63
+ outBuffers.drain().toStream()
64
+ })
65
+ }
@@ -0,0 +1,51 @@
1
+
2
+ nodeMain(system: NodeSystem) {
3
+
4
+ let urlFile = system.arguments().grab(0)
5
+ let urls = system.path(urlFile).readText().lines()
6
+
7
+ let urlChannel = system.mainTask().channel(urls.size())
8
+ let htmlChannel = system.mainTask().channel(urls.size())
9
+ let resultChannel = system.mainTask().channel(urls.size())
10
+
11
+ 1.to(1000).each {_ =>
12
+ system.mainTask().spawn {task =>
13
+ let url = urlChannel.read()
14
+ let html = system.httpClient().fetch(url).readText()
15
+ htmlChannel.write(html)
16
+ }
17
+ }
18
+
19
+ 1.to(10).each {_ =>
20
+ system.mainTask().spawn {task =>
21
+ let html = htmlChannel.read()
22
+ let result = scrape(html)
23
+ resultChannel.write(result)
24
+ }
25
+ }
26
+
27
+ urls.each {url => urlChannel.write(url)}
28
+ let results = urls.map {_ => resultChannel.read()}
29
+ system.writeText(results.join("\n"))
30
+
31
+ }
32
+
33
+ scrape(html: String): String {
34
+ html // Uhm...
35
+ }
36
+
37
+ foo() {
38
+
39
+ let result = urls.toStream()
40
+ .parallelMap(threads = 1000) {url => download(url)}
41
+ .parallelMap(threads = 10) {result => result.map(scrape)}
42
+
43
+ let result = urls.toStream()
44
+ .parallelMap(threads = 1000) {task, url => task.timeout(Duration(5)) {download(url)}}
45
+ .parallelMap(threads = 10) {_, result => result.map(scrape)}
46
+
47
+ let result = urls.toStream()
48
+ .parallelMap(1000) {task, url => task.deadline(task.now().add(Duration(5))) {download(url)}}
49
+ .parallelMap(10) {_, result => result.map(scrape)}
50
+
51
+ }
@@ -0,0 +1,217 @@
1
+ data Person(id: Int, male: Bool, name: String)
2
+
3
+ instance Person: Serializable {
4
+ serializeUsing(serialization: Serialization, value: Person): Unit {
5
+ serialization.checksum = (31 * serialization.checksum + 7020).bitOr(0)
6
+ serialization.buffer.setUint8(serialization.offset, 1)
7
+ serialization.offset += 1
8
+ serializeUsing(serialization, value.id)
9
+ serializeUsing(serialization, value.male)
10
+ serializeUsing(serialization, value.name)
11
+ }
12
+ deserializeUsing(serialization: Serialization): Person {
13
+ let constructorNumber = serialization.buffer.grabUint8(serialization.offset)
14
+ serialization.offset += 1
15
+ constructorNumber.{
16
+ | 0 =>
17
+ serialization.checksum = (31 * serialization.checksum + 7020).bitOr(0)
18
+ Person(
19
+ deserializeUsing(serialization)
20
+ deserializeUsing(serialization)
21
+ deserializeUsing(serialization)
22
+ )
23
+ }
24
+ }
25
+ }
26
+
27
+ data DeserializationChecksumException()
28
+
29
+ main(system: BrowserSystem) {
30
+ let p = Person(5, True, "Hans")
31
+ Log.show(p)
32
+ Log.show(serialize(p))
33
+ Log.show(deserialize[Person](serialize(p)))
34
+
35
+ let numbers = 1.to(1000).toArray()
36
+
37
+ let serialized = serialize(numbers)
38
+ let deserialized = deserialize(serialized)
39
+ Log.show(numbers == deserialized)
40
+
41
+ function logDuration[T](label: String, body: () => T): T {
42
+ let started = system.time().elapsed()
43
+ let result = body()
44
+ Log.debug(label + ": " + Show.show(Duration(system.time().elapsed().seconds - started.seconds)))
45
+ result
46
+ }
47
+
48
+ function test() {
49
+ let persons = logDuration("create") {
50
+ Array.fillBy(1000000) {n =>
51
+ Person(n, n % 2 == 0, "Person called " + n)
52
+ }
53
+ }
54
+
55
+ let started = system.time().elapsed()
56
+ let personsSerialized = logDuration("serialize") {
57
+ serialize(persons)
58
+ }
59
+ let personsDeserialized = logDuration("deserialize") {
60
+ deserialize[Array[Person]](personsSerialized)
61
+ }
62
+ Log.show(persons == personsDeserialized)
63
+ //Log.show(personsDeserialized1 == personsDeserialized2)
64
+ //Log.show(persons.toList().takeFirst(2))
65
+ //Log.show(personsDeserialized.toList().takeFirst(2))
66
+ }
67
+ test()
68
+ //system.tasks().sleep(Duration(10.0))
69
+ //test()
70
+ //system.tasks().sleep(Duration(10.0))
71
+ //test()
72
+ }
73
+
74
+ serialize[T: Serializable](value: T, initialBufferSize: Int = 1024): Buffer {
75
+ let serialization = Serialization(Buffer.make(initialBufferSize), 0, 0)
76
+ serializeUsing(serialization, value)
77
+ serialization.autoResize(4)
78
+ serialization.buffer.setInt32(serialization.offset, serialization.checksum)
79
+ serialization.offset += 4
80
+ serialization.buffer.view(0, serialization.offset)
81
+ }
82
+
83
+ deserialize[T: Serializable](buffer: Buffer): T {
84
+ let serialization = Serialization(buffer, 0, 0)
85
+ let result = deserializeUsing(serialization)
86
+ let checksum = serialization.buffer.grabInt32(serialization.offset)
87
+ if(checksum != serialization.checksum) {
88
+ throw(DeserializationChecksumException)
89
+ }
90
+ result
91
+ }
92
+
93
+ trait T: Serializable {
94
+ serializeUsing(serialization: Serialization, value: T): Unit
95
+ deserializeUsing(serialization: Serialization): T
96
+ }
97
+
98
+ data Serialization(
99
+ mutable buffer: Buffer
100
+ mutable offset: Int
101
+ mutable checksum: Int
102
+ )
103
+
104
+ extend self: Serialization {
105
+ autoResize(minSpareCapacity: Int) {
106
+ if(self.offset + minSpareCapacity > self.buffer.size()) {
107
+ let minSize = self.buffer.size() + minSpareCapacity
108
+ let newBuffer = Buffer.make((self.buffer.size() * 2).max(minSize))
109
+ newBuffer.setAll(0, self.buffer)
110
+ self.buffer = newBuffer
111
+ }
112
+ }
113
+ }
114
+
115
+ instance Int: Serializable {
116
+ serializeUsing(serialization: Serialization, value: Int): Unit {
117
+ serialization.autoResize(8)
118
+ serialization.buffer.setInt64(serialization.offset, value)
119
+ serialization.offset += 8
120
+ }
121
+ deserializeUsing(serialization: Serialization): Int {
122
+ let result = serialization.buffer.grabInt64(serialization.offset)
123
+ serialization.offset += 8
124
+ result
125
+ }
126
+ }
127
+
128
+ instance Bool: Serializable {
129
+ serializeUsing(serialization: Serialization, value: Bool): Unit {
130
+ serialization.autoResize(1)
131
+ serialization.buffer.setUint8(serialization.offset, if(value) {1} else {0})
132
+ serialization.offset += 1
133
+ }
134
+ deserializeUsing(serialization: Serialization): Bool {
135
+ let result = serialization.buffer.grabUint8(serialization.offset)
136
+ serialization.offset += 1
137
+ result == 1
138
+ }
139
+ }
140
+
141
+ instance Array[T: Serializable]: Serializable {
142
+ serializeUsing(serialization: Serialization, value: Array[T]): Unit {
143
+ if(value.size() < 255) {
144
+ serialization.autoResize(1)
145
+ serialization.buffer.setUint8(serialization.offset, value.size())
146
+ serialization.offset += 1
147
+ } elseIf {value.size() < 1073741824} {
148
+ serialization.autoResize(1 + 4)
149
+ serialization.buffer.setUint8(serialization.offset, 255)
150
+ serialization.buffer.setUint32(1 + serialization.offset, value.size())
151
+ serialization.offset += 1 + 4
152
+ } else {
153
+ panic("Can't serialize arrays where size() >= 1073741824")
154
+ }
155
+ value.each {serializeUsing(serialization, _)}
156
+ }
157
+ deserializeUsing(serialization: Serialization): Array[T] {
158
+ let smallSize = serialization.buffer.grabUint8(serialization.offset)
159
+ if(smallSize != 255) {
160
+ serialization.offset += 1
161
+ Array.fillBy(smallSize) {_ => deserializeUsing(serialization)}
162
+ } else {
163
+ let size = serialization.buffer.grabUint32(serialization.offset + 1)
164
+ serialization.offset += 1 + 4
165
+ Array.fillBy(size) {_ => deserializeUsing(serialization)}
166
+ }
167
+ }
168
+ }
169
+
170
+ instance String: Serializable {
171
+ serializeUsing(serialization: Serialization, value: String): Unit {
172
+ serialization.autoResize(1 + value.size())
173
+ serialization.buffer.setUint8(serialization.offset, value.size())
174
+ if(value.size() < 255 && internalSetLatin1(serialization.buffer, serialization.offset + 1, value)) {
175
+ serialization.offset += 1 + value.size()
176
+ } elseIf {value.size() < 1073741824} {
177
+ let stringBuffer = value.toBuffer()
178
+ serialization.autoResize(5 + stringBuffer.size())
179
+ serialization.buffer.setUint8(serialization.offset, 255)
180
+ serialization.buffer.setUint32(serialization.offset + 1, stringBuffer.size())
181
+ serialization.buffer.setAll(serialization.offset + 5, stringBuffer)
182
+ serialization.offset += 5 + stringBuffer.size()
183
+ } else {
184
+ panic("Can't serialize strings where size() >= 1073741824")
185
+ }
186
+ }
187
+ deserializeUsing(serialization: Serialization): String {
188
+ let smallSize = serialization.buffer.grabUint8(serialization.offset)
189
+ if(smallSize != 255) {
190
+ let result = internalGrabLatin1(serialization.buffer, serialization.offset + 1, smallSize)
191
+ serialization.offset += 1 + smallSize
192
+ result
193
+ } else {
194
+ let size = serialization.buffer.grabUint32(serialization.offset + 1)
195
+ let stringBuffer = serialization.buffer.view(serialization.offset + 5, serialization.offset + 5 + size)
196
+ serialization.offset += 5 + size
197
+ stringBuffer.toString()
198
+ }
199
+ }
200
+ }
201
+
202
+
203
+ internalSetLatin1(self: Buffer, byteOffset: Int, value: String): Bool
204
+ target js sync """
205
+ for(let i = 0; i < value_.length; i++) {
206
+ let charCode = value_.charCodeAt(i)
207
+ if(charCode >= 256) return false
208
+ self_.setUint8(byteOffset_ + i, charCode)
209
+ }
210
+ return true
211
+ """
212
+
213
+ internalGrabLatin1(self: Buffer, byteOffset: Int, size: Int): String
214
+ target js sync """
215
+ const codeUnits = new Uint8Array(self_.buffer, self_.byteOffset + byteOffset_, size_)
216
+ return String.fromCharCode.apply(null, codeUnits)
217
+ """
@@ -0,0 +1,46 @@
1
+ data Person(id: Int, male: Bool, name: String)
2
+
3
+ main(system: BrowserSystem) {
4
+ let p = Person(5, True, "Hans")
5
+ Log.show(p)
6
+ Log.show(Serializable.serialize(p))
7
+ Log.show(Serializable.deserialize[Person](Serializable.serialize(p)))
8
+
9
+ let numbers = 1.to(1000).toArray()
10
+
11
+ let serialized = Serializable.serialize(numbers)
12
+ let deserialized = Serializable.deserialize(serialized)
13
+ Log.show(numbers == deserialized)
14
+
15
+ function logDuration[T](label: String, body: () => T): T {
16
+ let started = system.time().elapsed()
17
+ let result = body()
18
+ Log.debug(label + ": " + Show.show(Duration(system.time().elapsed().seconds - started.seconds)))
19
+ result
20
+ }
21
+
22
+ function test() {
23
+ let persons = logDuration("create") {
24
+ Array.fillBy(1000000) {n =>
25
+ Person(n, n % 2 == 0, "Person called " + n)
26
+ }
27
+ }
28
+
29
+ let started = system.time().elapsed()
30
+ let personsSerialized = logDuration("serialize") {
31
+ Serializable.serialize(persons)
32
+ }
33
+ let personsDeserialized = logDuration("deserialize") {
34
+ Serializable.deserialize[Array[Person]](personsSerialized)
35
+ }
36
+ Log.show(persons == personsDeserialized)
37
+ //Log.show(personsDeserialized1 == personsDeserialized2)
38
+ //Log.show(persons.toList().takeFirst(2))
39
+ //Log.show(personsDeserialized.toList().takeFirst(2))
40
+ }
41
+ test()
42
+ //system.tasks().sleep(Duration(10.0))
43
+ //test()
44
+ //system.tasks().sleep(Duration(10.0))
45
+ //test()
46
+ }
@@ -0,0 +1,36 @@
1
+ // Binary format each starts with a single byte.
2
+ // tttvvvvv - the ttt bits describe the type and the vvvvv bits describe the value.
3
+ // If vvvvv is 111cc, then 2^cc bytes follow with the actual value. Otherwise the value is vvvvv-3.
4
+ // If it's a string (UTF-8) or a buffer, the value describes the length, and the raw bytes then follow.
5
+
6
+ capability Serializer[T](
7
+ writeBool: Bool => Unit
8
+ writeChar: Char => Unit
9
+ writeInt: Int => Unit
10
+ writeFloat: Float => Unit
11
+ writeString: String => Unit
12
+ writeBuffer: Buffer => Unit
13
+ grab: () => T
14
+ )
15
+
16
+ trait T: Serializable {
17
+ makeSerializer(): Serializer[T]
18
+ serialize[R](serializer: Serializer[R], value: T): Unit
19
+ }
20
+
21
+
22
+
23
+
24
+
25
+ data Drawing {
26
+ Circle(radius: Float) // 0
27
+ Rectangle(width: Float, height: Float) // 1
28
+ Rotate(drawing: Drawing, angle: Float) // 2
29
+ }
30
+
31
+ // Then Rotate(Rectangle(5.0, 7.0), 0.3) could be serialized as:
32
+ serializer.writeInt(2) // Tag for the third constructor
33
+ serializer.writeInt(1) // Tag for the second constructor
34
+ serializer.writeFloat(5.0)
35
+ serializer.writeFloat(7.0)
36
+ serializer.writeFloat(0.3)
@@ -0,0 +1,4 @@
1
+ main(system: NodeSystem) {
2
+ //let stream = system.readStream()
3
+ //system.writeStream(stream)
4
+ }