-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathtop_level.cr
420 lines (338 loc) · 11.1 KB
/
top_level.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# TODO: Refactor this file into a different class because it should not be
# in the compiler.
module Mint
class Compiler
alias Options = NamedTuple(
web_components: Hash(String, String),
css_prefix: String?,
optimize: Bool,
relative: Bool,
build: Bool,
)
DEFAULT_OPTIONS = Options.new(
web_components: {} of String => String,
css_prefix: nil,
optimize: false,
relative: false,
build: false,
)
# Compiles the application with the runtime and the rendering of the $Main
# component.
def self.compile(artifacts : TypeChecker::Artifacts, options = DEFAULT_OPTIONS) : String
compiler =
new(artifacts, **options)
main =
compiler.ast.components.find(&.name.value.==("Main")).try do |component|
globals =
compiler
.ast
.components
.select(&.global?)
.each_with_object({} of String => String) do |item, memo|
name =
compiler.js.class_of(item)
memo[name] = "$#{name}"
end
main_class =
compiler.js.class_of(component)
globals_object =
compiler.js.object(globals)
"\n_program.render(#{main_class}, #{globals_object})"
end || ""
compiler.wrap_runtime(compiler.compile, main)
end
def self.compile_embed(artifacts : TypeChecker::Artifacts, options = DEFAULT_OPTIONS) : String
compiler =
new(artifacts, **options)
main =
compiler.ast.components.find(&.name.value.==("Main")).try do |component|
globals =
compiler
.ast
.components
.select(&.global?)
.each_with_object({} of String => String) do |item, memo|
name =
compiler.js.class_of(item)
memo[name] = "$#{name}"
end
main_class =
compiler.js.class_of(component)
globals_object =
compiler.js.object(globals)
"\n Mint.embed = (base) => (new mint.EmbeddedProgram(base)).render(#{main_class}, #{globals_object})"
end || ""
compiler.wrap_runtime(compiler.compile, main)
end
# Compiles the application without the runtime.
def self.compile_bare(artifacts : TypeChecker::Artifacts, options = DEFAULT_OPTIONS) : String
compiler =
new(artifacts, **options)
compiler.compile
end
# Compiles the application with the runtime and the tests
def self.compile_with_tests(artifacts : TypeChecker::Artifacts) : String
compiler =
new(artifacts)
compiler.wrap_runtime(compiler.compile(include_tests: true))
end
# Compiles the application
def compile(include_tests : Bool = false) : String
records =
compile ast.records
providers =
compile ast.providers
components =
compile ast.components
modules =
compile ast.unified_modules
stores =
compile ast.stores
routes =
compile ast.routes
enums =
compile ast.enums
all_css =
style_builder.compile
footer =
unless all_css.empty?
["_insertStyles(`\n#{all_css}\n`)"]
end
suites =
if include_tests
["SUITES = [#{compile(ast.suites, ",")}]"]
end
static =
static_components.map do |name, compiled|
js.const("$#{name}", "_m(() => #{compiled})")
end
elements =
(%w[] &+ enums &+ records &+ modules &+ providers &+ routes &+ components &+ static &+ stores &+ footer &+ suites &+ compiled_web_components)
.reject!(&.empty?)
replace_skipped(js.statements(elements))
end
# --------------------------------------------------------------------------
def maybe
ast.enums.find!(&.name.value.==("Maybe"))
end
def just
node =
maybe.options.find!(&.value.value.==("Just"))
js.class_of(node)
end
def nothing
node =
maybe.options.find!(&.value.value.==("Nothing"))
js.class_of(node)
end
# --------------------------------------------------------------------------
def result
ast.enums.find!(&.name.value.==("Result"))
end
def ok
node =
result.options.find!(&.value.value.==("Ok"))
js.class_of(node)
end
def err
node =
result.options.find!(&.value.value.==("Err"))
js.class_of(node)
end
def compiled_web_components
@web_components.compact_map do |component, tagname|
node =
ast.components.find(&.name.value.==(component))
next unless node
name =
js.class_of(node)
prefixed_name =
if node.global?
"$#{name}"
else
name
end
properties =
compile node.properties.reject(&.name.value.==("children"))
"_wc(#{prefixed_name}, '#{tagname}', #{js.array(properties)})"
end
end
# --------------------------------------------------------------------------
# Wraps the application with the runtime
def wrap_runtime(body, main = "")
html_event_module =
ast.unified_modules.find!(&.name.value.==("Html.Event"))
from_event =
html_event_module.functions.find!(&.name.value.==("fromEvent"))
from_event_call =
"#{js.class_of(html_event_module)}.#{js.variable_of(from_event)}"
minimized_class_warning =
unless build
<<-JS
console.warn("%c!!!DO NOT TARGET ELEMENTS WITH SELECTORS BECAUSE THE SELECTORS WILL BE MINIMIZED IN THE PRODUCTION BUILD!!!", "font-size: 2em")
JS
end
<<-RESULT
(() => {
const _enums = {}
const mint = Mint(_enums)
const _normalizeEvent = (event) => {
return #{from_event_call}(mint.normalizeEvent(event))
}
const _R = mint.createRecord
const _h = mint.createElement
const _createPortal = mint.createPortal
const _insertStyles = mint.insertStyles
const _navigate = mint.navigate
const _compare = mint.compare
const _program = mint.program
const _encode = mint.encode
const _style = mint.style
const _array = mint.array
const _wc = mint.register
const _u = mint.update
const _at = mint.at
window.TestContext = mint.TestContext
const TestContext = mint.TestContext
const ReactDOM = mint.ReactDOM
const Decoder = mint.Decoder
const Encoder = mint.Encoder
const DateFNS = mint.DateFNS
const Record = mint.Record
const React = mint.React
const _C = mint.Component
const _P = mint.Provider
const _M = mint.Module
const _S = mint.Store
const _E = mint.Enum
const _PR = (patterns) => new RecordPattern(patterns)
const _PE = (x, pattern) => new Pattern(x, pattern)
const _PV = Symbol("Variable")
const _PS = Symbol("Spread")
class RecordPattern {
constructor(patterns) {
this.patterns = patterns
}
}
class Pattern {
constructor(x,pattern) {
this.pattern = pattern;
this.x = x;
}
}
const __match = (value, pattern, values = []) => {
if (pattern === null) {
} else if (pattern === _PV) {
values.push(value)
} else if (Array.isArray(pattern)) { // This covers tuples and arrays (they are the same)
const hasSpread = pattern.some((item) => item === _PS)
if (hasSpread && value.length >= (pattern.length - 1)) {
let endValues = []
let startIndex = 0
while (pattern[startIndex] !== _PS && startIndex < pattern.length) {
if (!__match(value[startIndex], pattern[startIndex], values)) {
return false
}
startIndex++
}
let endIndex = 1
while (pattern[pattern.length - endIndex] !== _PS && endIndex < pattern.length) {
if (!__match(value[value.length - endIndex], pattern[pattern.length - endIndex], endValues)) {
return false
}
endIndex++
}
// Add in the spread
values.push(value.slice(startIndex, value.length - (endIndex - 1)))
// Add in the end values
for (let item of endValues) {
values.push(item)
}
} else {
if (pattern.length !== value.length) {
return false
} else {
for (let index in pattern) {
if (!__match(value[index], pattern[index], values)) {
return false
}
}
}
}
} else if (pattern instanceof Pattern) {
if (value instanceof pattern.x) {
for (let index in pattern.pattern) {
if (!__match(value[`_${index}`], pattern.pattern[index], values)) {
return false
}
}
} else {
return false
}
} else if (pattern instanceof RecordPattern && value instanceof Record) {
for (let index in pattern.patterns) {
const item = pattern.patterns[index];
if (!__match(value[item[0]], item[1], values)) {
return false
}
}
} else {
if (!_compare(value, pattern)) {
return false
}
}
return values;
}
const _match = (value, patterns) => {
for (let pattern of patterns) {
if (pattern[0] === null) {
return pattern[1]()
} else {
const values = __match(value, pattern[0]);
if (values) {
return pattern[1].apply(null, values)
}
}
}
}
const _m = (method) => {
let value
return () => {
if (value) { return value }
value = method()
return value
}
}
const _o = (item, value) => {
if (item !== undefined && item !== null) {
return item;
} else {
return value;
}
}
const _s = (item, callback) => {
if (item instanceof #{nothing}) {
return item
} else if (item instanceof #{just}) {
return new #{just}(callback(item._0))
} else {
return callback(item)
}
}
class DoError extends Error {}
#{body}
const Nothing = #{nothing}
const Just = #{just}
const Err = #{err}
const Ok = #{ok}
_enums.nothing = #{nothing}
_enums.just = #{just}
_enums.err = #{err}
_enums.ok = #{ok}
#{minimized_class_warning}
#{main}
})()
RESULT
end
end
end