-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_js.wax
497 lines (469 loc) · 13.5 KB
/
target_js.wax
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
; ----------------------------------------------------------------------------
; - JavaScript language target -
; ----------------------------------------------------------------------------
(func node_to_js (param depth int) (param node (struct node)) (result str)
(let s str (alloc str ""))
(set depth (+ depth 1))
(set node (call node_copy node))
(if (= (get node type) @NODE_ROOT) (then
(let has_main int 0)
(for i 0 (< i (# (get node child))) 1 (do
(if (&&
(= (get node child i value) "func")
(= (get node child i child 0 value) "main")
) (then
(set has_main 1)
))
(let child str (call node_to_js 0 (get node child i)))
(<< s child)
(<< s "\n")
))
(if has_main (then
(<< s "(typeof process == 'undefined') ? main([]) : process.exit(main(process.argv.slice(1)));\n")
))
))
(if (||
(= (get node value) "+")
(= (get node value) "&&")
(= (get node value) "||")
) (then
(set node (call node_expand node))
))
(if (= (get node type) @NODE_EXPR) (then
(let block_start str (alloc str "("))
(<< block_start (get node value))
(let block_end str (alloc str ")"))
(let block_seperator str (alloc str " "))
(if (= (get node value) "") (then
(set block_start "{")
(set block_end "}")
))
(if (= (get node value) "@define") (then
(set block_start "const ")
(<< block_start (get node child 0 value))
(<< block_start " = ")
(<< block_start (get node child 1 value))
(<< block_start ";")
(set block_end "")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "@if") (then
(set block_start "")
(set block_end "")
(if (= (get node child 0 value) "TARGET_JS") (then
(remove (get node child) 0 1)
(remove (get node child) 0 1)
) (else
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
))
(if (= (get node value) "extern") (then
(set block_start "")
(set block_end "")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "asm") (then
(let quotedline str (get node child 0 value))
(let line str "")
(for j 1 (< j (- (# quotedline) 1)) 1 (do
(<< line (get quotedline j))
))
(set block_start line)
(set block_end "\n")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "struct") (then
(set block_start "")
(set block_end "")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "func") (then
(set block_start "function ")
(if (= (get node child 0 type) @NODE_STR) (then
(let fname_node (struct node) (get node child 0))
(<< block_start (get fname_node value))
(remove (get node child) 0 1)
))
(<< block_start "(")
(let param_count int 0)
(let i int 0)
(while (< i (# (get node child))) (do
(let child_node (struct node) (get node child i))
(if (= (get child_node value) "param") (then
(if (= (# (get child_node child)) 2) (then
(let param_node (struct node) (get child_node child 0))
(if (> param_count 0) (then
(<< block_start ", ")
))
(<< block_start (get param_node value))
(set param_count (+ param_count 1))
))
(remove (get node child) i 1)
(set i (- i 1))
))
(if (= (get child_node value) "result") (then
(remove (get node child) i 1)
(set i (- i 1))
))
(set i (+ i 1))
))
(<< block_start ") {")
(set block_end "}")
))
(if (= (get node value) "call") (then
(let fname_node (struct node) (get node child 0))
(set block_start (get fname_node value))
(<< block_start "(")
(set block_end ")")
(remove (get node child) 0 1)
(set block_seperator ", ")
))
(if (||
(= (get node value) "let")
(= (get node value) "local")
) (then
(set block_start "let ")
(set block_end "")
(remove (get node child) 1 1)
(if (= (# (get node child)) 2) (then
(insert (get node child) 1 (call node_new @NODE_STR "="))
))
))
(if (= (get node value) "alloc") (then
(set block_start "")
(set block_end "")
(let child (struct node) (get node child 0))
(if (= (get child type) @NODE_EXPR) (then
(if (&&
(= (get node child 0 value) "vec")
(= (# (get node child)) 1))
(then
(set block_start "new Array(")
(<< block_start (call node_to_js depth (get node child 0 child 0)))
(<< block_start ").fill(0)")
) (else
(if (||
(= (get node child 0 value) "arr")
(= (get node child 0 value) "vec")
) (then
(set block_start "[")
(for i 1 (< i (# (get node child))) 1 (do
(<< block_start (call node_to_js depth (get node child i)))
(if (< i (- (# (get node child)) 1)) (then
(<< block_start ",")
))
))
(<< block_start "]")
))
))
(if (||
(= (get node child 0 value) "map")
(= (get node child 0 value) "struct")
) (then
(set block_start "{}")
))
))
(if (= (get child type) @NODE_STR) (then
(if (>= (# (get node child)) 2) (then
(set block_start (call node_to_js depth (get node child 1)))
) (else
(set block_start "\"\"")
))
))
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "free") (then
(set block_start "/*GC*/")
(set block_end "")
(remove (get node child) 0 1)
))
(if (= (get node value) "#") (then
(set block_start "")
(set block_end ".length")
))
(if (= (get node value) "insert") (then
(set block_start (call node_to_js depth (get node child 0)))
(<< block_start ".splice(")
(<< block_start (call node_to_js depth (get node child 1)))
(<< block_start ",0,")
(<< block_start (call node_to_js depth (get node child 2)))
(set block_end ")")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "remove") (then
(if (<> (get node child 1 type) @NODE_STR_QUOTE) (then
(set block_start (call node_to_js depth (get node child 0)))
(<< block_start ".splice(")
(<< block_start (call node_to_js depth (get node child 1)))
(<< block_start ",")
(<< block_start (call node_to_js depth (get node child 2)))
(set block_end ")")
))
(if (= (get node child 1 type) @NODE_STR_QUOTE) (then
(set block_start "delete ")
(<< block_start (call node_to_js depth (get node child 0)))
(<< block_start "[")
(<< block_start (call node_to_js depth (get node child 1)))
(set block_end "]")
))
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "slice") (then
(set block_start (call node_to_js depth (get node child 0)))
(<< block_start ".slice(")
(<< block_start (call node_to_js depth (get node child 1)))
(<< block_start ",(")
(<< block_start (call node_to_js depth (get node child 2)))
(<< block_start " + ")
(<< block_start (call node_to_js depth (get node child 1)))
(set block_end "))")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "get") (then
(set block_start (call node_to_js depth (get node child 0)))
(set block_end "")
(for i 1 (< i (# (get node child))) 1 (do
(let child (struct node) (get node child i))
(if (&& (= (get child type) @NODE_STR) (> (# (get child value)) 1))
(then
(<< block_end ".")
(<< block_end (call node_to_js depth (get node child i)))
(<< block_end "")
) (else
(<< block_end "[")
(<< block_end (call node_to_js depth (get node child i)))
(<< block_end "]")
))
))
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "set") (then
(set block_start (call node_to_js depth (get node child 0)))
(set block_end "")
(let n int (- (# (get node child)) 1))
(for i 1 (< i n) 1 (do
(let child (struct node) (get node child i))
(if (&& (= (get child type) @NODE_STR) (> (# (get child value)) 1))
(then
(<< block_end ".")
(<< block_end (call node_to_js depth (get node child i)))
(<< block_end "")
) (else
(<< block_end "[")
(<< block_end (call node_to_js depth (get node child i)))
(<< block_end "]")
))
))
(<< block_end " = ")
(<< block_end (call node_to_js depth (get node child n)))
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "cast") (then
(if (= (get node child 1 value) "str") (then
(set block_start "\"\"+")
(set block_end (call node_to_js depth (get node child 0)))
))
(if (= (get node child 1 value) "int") (then
(set block_start (call node_to_js depth (get node child 0)))
(set block_end "|0")
))
(remove (get node child) 0 1)
(remove (get node child) 0 1)
))
(if (= (get node value) "then") (then
(set block_start "{")
(set block_end "}")
))
(if (= (get node value) "else") (then
(set block_start "else {")
(set block_end "}")
))
(if (= (get node value) "for") (then
(if (= (# (get node child)) 5) (then
(set block_start "for (let ")
(<< block_start (call node_to_js depth (get node child 0)))
(<< block_start " = ")
(<< block_start (call node_to_js depth (get node child 1)))
(<< block_start "; ")
(<< block_start (call node_to_js depth (get node child 2)))
(<< block_start "; ")
(<< block_start (call node_to_js depth (get node child 0)))
(<< block_start " += ")
(<< block_start (call node_to_js depth (get node child 3)))
(<< block_start ") ")
(remove (get node child) 0 4)
(set block_end "")
))
(if (= (# (get node child)) 4) (then
(set block_start "for (const [")
(<< block_start (call node_to_js depth (get node child 0)))
(<< block_start ",")
(<< block_start (call node_to_js depth (get node child 1)))
(<< block_start "] of Object.entries(")
(<< block_start (call node_to_js depth (get node child 2)))
(<< block_start "))")
(remove (get node child) 0 3)
(set block_end "")
))
))
(if (||
(= (get node value) "if")
(= (get node value) "while")
) (then
(set block_start (get node value))
(<< block_start " ")
(let block_simple int (<> (get node child 0 type) @NODE_EXPR))
(if block_simple (then
(<< block_start "(")
))
(<< block_start (call node_to_js depth (get node child 0)))
(if block_simple (then
(<< block_start ")")
))
(<< block_start " ")
(set block_end "")
(remove (get node child) 0 1)
))
(if (= (get node value) "do") (then
(set block_start "{")
(set block_end "}")
))
(if (= (get node value) "break") (then
(set block_start "break")
(set block_end "")
))
(if (= (get node value) "?") (then
(set block_start (call node_to_js depth (get node child 0)))
(<< block_start " ? ")
(<< block_start (call node_to_js depth (get node child 1)))
(<< block_start " : ")
(<< block_start (call node_to_js depth (get node child 2)))
(set block_end "")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "print") (then
(set block_start "console.log(")
))
(if (||
(= (get node value) ">>")
(= (get node value) "<<")
(= (get node value) "=")
(= (get node value) "&&")
(= (get node value) "||")
(= (get node value) ">=")
(= (get node value) "<=")
(= (get node value) "<>")
(= (get node value) "+")
(= (get node value) "-")
(= (get node value) "*")
(= (get node value) "/")
(= (get node value) "^")
(= (get node value) "%")
(= (get node value) "&")
(= (get node value) "|")
(= (get node value) "~")
(= (get node value) "<")
(= (get node value) ">")
) (then
(set block_start "(")
(set block_end ")")
(if (= (get node value) "=") (then
(set node value "==")
))
(if (= (get node value) "<>") (then
(set node value "!=")
))
(if (&&
(= (get node value) "<<")
(= (get node child 0 type) @NODE_STR)
) (then
(set node value "+=")
))
(insert (get node child) 1 (call node_new @NODE_STR (get node value)))
))
(if (= (get node value) "return") (then
(if (> (# (get node child)) 0) (then
(set block_start "return(")
(set block_end ")")
) (else
(set block_start "return")
(set block_end "")
))
))
(<< s block_start)
(for i 0 (< i (# (get node child))) 1 (do
(let node_child (struct node) (get node child i))
(let indent_needed int (call node_indent_needed node node_child))
(let indent_depth int (? indent_needed depth (- depth 1)))
(let indent str " ")
(if indent_needed (then
(<< s "\n")
(for i 0 (< i indent_depth) 1 (do
(<< s indent)
))
) (else (if (> i 0) (then
(<< s block_seperator)
))))
(let child str (call node_to_js indent_depth node_child))
(<< s child)
(if (||
(= (get node value) "")
(= (get node value) "func")
(= (get node value) "do")
(= (get node value) "then")
(= (get node value) "else")
) (then
(<< s ";")
))
(if (&&
indent_needed
(= i (- (# (get node child)) 1))
) (then
(<< s "\n")
(for i 0 (< i (- indent_depth 1)) 1 (do
(<< s indent)
))
))
))
(<< s block_end)
))
(if (<> (get node type) @NODE_EXPR) (then
(let value str (get node value))
(if (= (get node type) @NODE_PREPROCESS) (then
(set value "")
(for i 1 (< i (# (get node value))) 1 (do
(<< value (get node value i))
))
))
(if (= (get node type) @NODE_COMMENT) (then
(set value "// ")
(<< value (get node value))
))
(<< s value)
))
(return s)
)