-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprim.el
831 lines (704 loc) · 23 KB
/
prim.el
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
(eval-when (load compile eval)
(require 'shadchen)
(provide 'prim))
(defvar prim:*mangle-cache* (make-hash-table :test 'equal))
(defun prim:really-mangle (s)
"Take a symbol S and return the mangled version of the symbol
for transcoding. See `match-lambda` below for a list of
manglings. Additionally, dashed ids are replaced by camel case."
(let* ((s (if (symbolp s) (symbol-name s) s))
(s1 (replace-regexp-in-string
(regexp-quote "->") "-to-" s))
(s1 (replace-regexp-in-string
(regexp-quote "===") "-triple-equal-" s1))
(s1 (replace-regexp-in-string
(regexp-quote "::") "-of-type-" s1))
(s1 (replace-regexp-in-string
(regexp-quote "{}") "-braces-" s1))
(s1 (replace-regexp-in-string "-\\([a-zA-Z0-9]\\)"
(lambda (x)
(upcase (substring x 1)))
s1))
(s1 (replace-regexp-in-string (regexp-quote "%")
"modsign"
s1)))
(replace-regexp-in-string
(rx
(| "|" "~" "+" "-" "*" "%" "$" "&" "^" "!" ":" "/" "\\" "#" "@" "?" "="
"<" ">" "{" "}"))
(match-lambda
("|" "pipe")
("+" "plus")
("-" "minus")
("*" "times")
("<" "lessThan")
(">" "greaterThan")
("$" "$")
("=" "equal")
("%" "modsign")
("!" "bang")
("?" "Predicate")
(":" "colon")
("&" "ampersand")
("^" "caret")
("/" "divide")
("\\" "mdivide")
("#" "hash")
("~" "tilda")
("@" "at")
("{" "openBrace")
("}" "closeBrace"))
s1)))
(defun prim:mangle (s)
(let ((m (gethash s prim:*mangle-cache*)))
(if m m
(let ((m (prim:really-mangle s)))
(setf (gethash s prim:*mangle-cache*) m)
m))))
(defmacro* prim:in-parens (&body body)
(let ((val (gensym)))
`(let ((,val nil))
(prim:insert "(")
(setq ,val (progn ,@body))
(prim:insert ")")
,val)))
(defmacro* prim:in-brackets (&body body)
(let ((val (gensym)))
`(let ((,val nil))
(prim:insert "[")
(setq ,val (progn ,@body))
(prim:insert "]")
,val)))
(defun prim:kw->symbol (kw)
(intern (substring (symbol-name kw) 1)))
(defun prim:insertf (fs &rest args)
"Insert a formatted string with ARGS into the current transcoding buffer."
(insert (apply #'format fs args)))
(defun prim:insert (fs)
"Insert a non-formatted string."
(insert fs))
(defun prim:newline ()
"Insert a newline into the transcoding buffer."
(prim:insertf "\n")
(loop for i from 1 to prim:indent-depth do
(prim:insert " ")))
(defmacro* prim:with-tab+ (&body body)
"Execute the body BODY with additional indentation."
`(let ((prim:indent-depth (+ prim:indent-depth 2)))
(loop for i from 1 to prim:indent-depth do
(prim:insert " "))
,@body))
(defun prim:transcode-block (statements)
"Transcode a {} block."
(prim:with-tab+
(prim:insert "{")
(prim:newline)
(prim:transcode-newline-sequence statements)
(prim:insert "}")))
(defun-match- prim:transcode-csvs ((list))
"Transcode a list of expressions into a comman separated list of transcodings. (base case)"
nil)
(defun-match prim:transcode-csvs ((list item))
"Transcode a list of expressions into a comman separated list of transcodings. (base case)"
(prim:transcode item))
(defun-match prim:transcode-csvs ((list-rest item rest))
"Transcode a list of expressions into a comman separated list of transcodings. (recursion case)"
(prim:transcode item)
(prim:insert ", ")
(recur rest))
(defun-match- prim:transcode-newline-sequence ((list))
"Transcode a list of expressions into a transcoded, newline
semicolon delimited list of transcodings. (base case)"
nil)
(defun-match prim:transcode-newline-sequence ((list item))
"Transcode a list of expressions into a transcoded, newline
semicolon delimited list of transcodings. (base case)"
(prim:transcode item)
(prim:insert ";")
(prim:newline))
(defun-match prim:transcode-newline-sequence ((list-rest item rest))
"Transcode a list of expressions into a transcoded, newline
semicolon delimited list of transcodings. (recursion case)"
(prim:transcode item)
(prim:insert ";")
(prim:newline)
(recur rest))
(defun-match- prim:transcode-csvs ((list))
"Transcode a list of expressions into a transcoded, newline
semicolon delimited list of transcodings. (base case)"
nil)
(defun-match prim:transcode-csvs ((list item))
"Transcode a list of expressions into a transcoded, newline
semicolon delimited list of transcodings. (base case)"
(prim:transcode item))
(defun-match prim:transcode-csvs ((list-rest item rest))
"Transcode a list of expressions into a transcoded, newline
semicolon delimited list of transcodings. (recursion case)"
(prim:transcode item)
(prim:insert ", ")
(recur rest))
(defvar prim:indent-depth 0 "Indentation tracking.")
(defun-match- prim:transcode ('_false)
(prim:insert "false"))
(defun-match prim:transcode ('_true)
(prim:insert "true"))
(defun-match prim:transcode ('_null)
(prim:insert "null"))
(defun-match prim:transcode ('_undefined)
(prim:insert "undefined"))
(defun-match prim:transcode ('_break)
(prim:insert "break"))
(defun-match prim:transcode ('_continue)
(prim:insert "continue"))
(defun-match prim:transcode ('_{})
(prim:insert "{}"))
(defun-match prim:transcode ((non-kw-symbol s))
(prim:insert (prim:mangle s)))
(defun-match prim:transcode->string (expr)
(with-temp-buffer
(prim:transcode expr)
(buffer-substring-no-properties (point-min) (point-max))))
(defun-match prim:transcode ((keyword k))
(prim:insert "\"")
(prim:insert (prim:mangle (prim:kw->symbol k)))
(prim:insert "\""))
(defun-match prim:transcode ((number n))
(prim:insertf "%s" n))
(defun prim:message (s)
(message (format "prim: %S" s)))
(defun prim:block-string-p (str)
(and (stringp str)
(> (length (split-string str (regexp-quote (format "\n"))))
1)))
(defun prim:transcode-block-string (str)
(let ((parts (split-string str (regexp-quote (format "\n")))))
(prim:insert "[")
(prim:with-tab+
(loop for (part . rest) on parts do
(prim:transcode-string part)
(if rest
(progn
(prim:insert ",")
(prim:newline))
(progn
(prim:insert "].join(\"\\n\")")))))))
(defun prim:transcode-normal-string (string)
(prim:insert "\"")
(loop for character in (coerce string 'list) do
(match character
(?\n (insert "\\
"))
(?\t (insert "\\t"))
(?\" (insert "\\\""))
(?\\ (insert "\\\\"))
(?\s
(insert " "))
(else (insert else))))
(prim:insert "\""))
(defun prim:transcode-string (string)
(if (prim:block-string-p string)
(prim:transcode-block-string string)
(prim:transcode-normal-string string)))
(defun-match prim:transcode ((string s))
(prim:transcode-string s))
;; (assert (string= "\"cats and dogs\""
;; (prim:transcode->string "cats and dogs")))
;; (assert (string= "stringToNumber"
;; (prim:transcode->string 'string->number)))
;; (assert (string= "\"stringToNumber\""
;; (prim:transcode->string :string->number)))
(defun-match prim:transcode ((list '_var (non-kw-symbol s) expr
(tail tail-of-var)))
(prim:insert "var ")
(prim:transcode s)
(prim:insert " = ")
(prim:transcode expr)
(if tail-of-var (prim:insert ", "))
(prim:transcode-tail-of-var tail-of-var))
(defun-match- prim:transcode-tail-of-var ((list))
nil)
(defun-match prim:transcode-tail-of-var ((list (non-kw-symbol s) expr))
(prim:transcode s)
(prim:insert " = ")
(prim:transcode expr))
(defun-match prim:transcode-tail-of-var ((list (non-kw-symbol s) expr
(tail tail-of-var)))
(prim:transcode s)
(prim:insert " = ")
(prim:transcode expr)
(prim:insert ", ")
(recur tail-of-var))
(assert (string= "var someValue = \"someValue\""
(prim:transcode->string '(_var some-value :some-value))))
(defun-match- prim:transcode-tail-of-if (nil)
nil)
(defun-match prim:transcode-tail-of-if ((list '_else (list-rest expressions)))
(prim:insert " else ")
(prim:transcode-block expressions))
(defun-match prim:transcode-tail-of-if ((list '_elseif condition (list-rest expressions)))
(prim:insert " else if ")
(prim:in-parens
(prim:transcode condition))
(prim:transcode-block
expressions))
(defun-match prim:transcode-tail-of-if ((list-rest
'_else
(list-rest expressions)
rest))
(prim:transcode-tail-of-if (list '_else expressions))
(recur rest))
(defun-match prim:transcode-tail-of-if ((list-rest
'_elseif
condition
(list-rest expressions)
rest))
(prim:transcode-tail-of-if (list '_elseif condition expressions))
(recur rest))
(defun-match prim:transcode ((list-rest '_if condition (list-rest expressions) rest))
(prim:insert "if ")
(prim:in-parens
(prim:transcode condition))
(prim:transcode-block expressions)
(prim:transcode-tail-of-if rest))
(assert (string= (prim:transcode->string '(_if :test-value ((_var x :x) (_var y :y))
_elseif :test-two
((_var z :z)
(_var q :q))
_elseif :test-three
((_var z :z)
(_var q :q))))
"if (\"testValue\") {
var x = \"x\";
var y = \"y\";
} else if (\"testTwo\") {
var z = \"z\";
var q = \"q\";
} else if (\"testThree\") {
var z = \"z\";
var q = \"q\";
}"))
(defun-match- prim:split-at-_case (nil acc)
(list (reverse acc) nil))
(defun-match prim:split-at-_case ((list (and which (or '_default '_case)) (tail rest)) acc)
(list (reverse acc) (cons which rest)))
(defun-match prim:split-at-_case ((list other-expr (tail rest)) acc)
(recur rest (cons other-expr acc)))
(defun-match prim:split-at-_case (o)
(recur o nil))
(defun-match- prim:remove-one-case-statement ((list (and which
(or '_default '_case)) (tail rest)))
(match (prim:split-at-_case rest)
((list case1 rest)
(list `(,which ,@case1) rest))))
(defun-match- prim:split-switch-body (nil acc)
(reverse acc))
(defun-match prim:split-switch-body (body acc)
(match (prim:remove-one-case-statement body)
((list case-statement rest)
(recur rest (cons case-statement acc)))))
(defun-match prim:split-switch-body (body)
(recur body nil))
(defun-match- prim:transcode-case-statement ((list '_case
value
(tail body)))
(prim:insert "case ")
(prim:transcode value)
(prim:insert ":")
(prim:newline)
(prim:transcode-newline-sequence body))
(defun-match prim:transcode-case-statement ((list '_default
(tail body)))
(prim:insert "default ")
(prim:insert ":")
(prim:newline)
(prim:transcode-newline-sequence body))
(defun-match- prim:transcode-tail-of-switch ((list (tail body)))
(let ((statements (prim:split-switch-body body)))
(loop for statement in statements do
(prim:transcode-case-statement statement))))
(defun-match prim:transcode ((list '_switch vexpr (tail tail-of-switch)))
(prim:insert "switch ")
(prim:transcode-in-parens vexpr)
(prim:insert "{")
(prim:newline)
(prim:with-tab+
(prim:transcode-tail-of-switch tail-of-switch))
(prim:newline)
(prim:insert "}"))
(defun-match prim:transcode ((list '_? cond true-branch false-branch))
(prim:in-parens
(prim:in-parens
(prim:transcode cond))
(prim:insert "?")
(prim:in-parens
(prim:transcode true-branch))
(prim:insert " : ")
(prim:in-parens
(prim:transcode false-branch))))
(eval-when (compile load eval)
(defun-match- prim:non-kw-symbol ((non-kw-symbol s))
t)
(defun-match prim:non-kw-symbol (_)
nil)
(defun-match- prim:all-satisfy (p nil)
t)
(defun-match prim:all-satisfy (p (list-rest hd tl))
(if (funcall p hd)
(recur p tl)
nil))
(defpattern prim:argument-list (pattern)
(let ((val (gensym)))
`(p #'(lambda (,val)
(and (listp ,val)
(prim:all-satisfy #'prim:non-kw-symbol ,val)))
,pattern))))
(defun-match prim:transcode ((list-rest '_function (prim:argument-list args) body))
(prim:insert "(function ")
(prim:in-parens
(prim:transcode-csvs args))
(prim:transcode-block body)
(prim:insert ")"))
(defun-match prim:transcode ((list-rest '_function
(non-kw-symbol name)
(prim:argument-list args)
body))
(prim:insert "function ")
(prim:transcode name)
(prim:in-parens
(prim:transcode-csvs args))
(prim:transcode-block body))
(defun-match prim:transcode ((list '_return (list (and which (or '_if '_for '_while '_try '_var '_=)) (tail body))))
(recur `(,which ,@body)))
(defun prim:make-last-return (sequence)
(let* ((r (reverse sequence))
(last (car r))
(new-last `(_return ,last)))
(reverse (cons new-last (cdr r)))))
(defun-match prim:transcode ((list '_return (list '_newline-sequence (tail body))))
(recur `(_newline-sequence ,@(prim:make-last-return body))))
(defun-match prim:transcode ((list '_return (list (and which (or '_delete '_throw '_continue '_break)) expression)))
(recur `(,which ,expression)))
(defun-match prim:transcode ((list '_return expression))
(prim:insert "return ")
(prim:transcode expression))
(defun-match prim:transcode ((list '_return))
(prim:insert "return ")
(prim:transcode '_undefined))
(defun-match prim:transcode ((list-rest '_while expression body))
(prim:insert "while ")
(prim:in-parens
(prim:transcode expression))
(prim:transcode-block body))
(defun-match prim:transcode ((list-rest '_for (list (non-kw-symbol name)
'_in expression) body))
(prim:insert "for ")
(prim:in-parens
(prim:transcode name)
(prim:insert " in ")
(prim:in-parens
(prim:transcode expression)))
(prim:transcode-block body))
(defun-match prim:transcode ((list-rest '_for (list init cond update) body))
(prim:insert "for ")
(prim:in-parens
(prim:transcode init)
(prim:insert ";")
(prim:transcode cond)
(prim:insert ";")
(prim:transcode update))
(prim:transcode-block body))
(defun-match prim:transcode ((list '_delete expr))
(prim:insert "delete ")
(prim:in-parens (prim:transcode expr)))
(defun-match prim:transcode ((list '_throw expr))
(prim:insert "throw ")
(prim:in-parens
(prim:transcode expr)))
;; (defun-match prim:transcode ((list '_break expr))
;; (prim:insert "break ")
;; (prim:in-parens
;; (prim:transcode expr)))
;; (defun-match prim:transcode ((list '_continue expr))
;; (prim:insert "continue ")
;; (prim:in-parens
;; (prim:transcode expr)))
(defun-match prim:transcode ((list '_instanceof object-expr cons-expr))
(prim:in-parens
(prim:transcode object-expr)
(prim:insert " instanceof ")
(prim:transcode cons-expr)))
(defun-match prim:transcode ((list '_typeof object-expr))
(prim:in-parens
(prim:insert "typeof ")
(prim:transcode object-expr)))
(defun-match prim:transcode-tail-of-try (nil)
nil)
(defun-match prim:transcode-tail-of-try ((list-rest '_catch
(list (non-kw-symbol name))
(list-rest catch-body)
tail-of-try))
(prim:insert "catch ")
(prim:in-parens
(prim:transcode name))
(prim:transcode-block catch-body)
(prim:transcode-tail-of-try tail-of-try))
(defun-match prim:transcode-tail-of-try ((list-rest '_catch
(list (non-kw-symbol name) '_if expression)
(list-rest catch-body)
tail-of-try))
(prim:insert "catch ")
(prim:in-parens
(prim:transcode name)
(prim:insert " if ")
(prim:transcode expression))
(prim:transcode-block catch-body)
(prim:transcode-tail-of-try tail-of-try))
(defun-match prim:transcode-tail-of-try ((list '_finally
(list-rest finally-body)))
(prim:insert "finally ")
(prim:transcode-block finally-body))
(defun-match prim:transcode ((list-rest '_try
(list-rest body)
tail-of-try))
(prim:insert "try")
(prim:transcode-block body)
(prim:transcode-tail-of-try tail-of-try))
(defun prim:transcode-in-parens (expr)
(prim:in-parens
(prim:transcode expr)))
(defun-match- prim:transcode-tail-of-dot-expr (nil)
nil)
(defun-match prim:transcode-tail-of-dot-expr ((list-rest (non-kw-symbol s)
tail-of-dot-expr))
(prim:transcode s)
(if tail-of-dot-expr (prim:insert "."))
(recur tail-of-dot-expr))
(defun-match prim:transcode-tail-of-dot-expr ((list-rest (list-rest (non-kw-symbol m) args) tail-of-dot-expr))
(prim:transcode m)
(prim:in-parens
(prim:transcode-csvs args))
(if tail-of-dot-expr (prim:insert "."))
(recur tail-of-dot-expr))
(defun-match prim:transcode-tail-of-dot-expr ((list-rest [ (non-kw-symbol name) (tail exprs) ] tail-of-dot-expr))
(prim:transcode name)
(loop for expr in exprs do
(prim:in-brackets
(prim:transcode expr)))
(if tail-of-dot-expr (prim:insert "."))
(recur tail-of-dot-expr))
(defun-match prim:transcode ((list-rest '_. (non-kw-symbol expr) tail-of-dot-expr))
(prim:transcode expr)
(if tail-of-dot-expr
(progn
(prim:insert ".")
(prim:transcode-tail-of-dot-expr tail-of-dot-expr))))
(defun-match prim:transcode
((list-rest '_.
expr
tail-of-dot-expr))
(prim:transcode expr)
(if tail-of-dot-expr
(progn
(prim:insert ".")
(prim:transcode-tail-of-dot-expr tail-of-dot-expr))))
(defun-match prim:transcode ((list '_new constructor (tail arguments)))
(prim:in-parens
(prim:insert "new ")
(prim:transcode-in-parens constructor)
(prim:in-parens
(prim:transcode-csvs arguments))))
(defun-match prim:transcode ((list '_=
(and
(or (non-kw-symbol _)
(list-rest '_. _)
[_ (tail _)])
set-this)
value-expr))
(prim:transcode set-this)
(prim:insert " = ")
(prim:transcode value-expr))
(defun-match prim:transcode ((list '_=
(and
(or (non-kw-symbol _)
(list-rest '_. _)
[_ (tail _)])
set-this)
value-expr
(tail others)))
(prim:transcode set-this)
(prim:insert " = ")
(prim:transcode value-expr)
(prim:insert ", ")
(recur `(_= ,@others)))
(defun-match prim:transcode ((list '_value value-expr))
(prim:in-parens
(prim:transcode value-expr)))
(defun-match prim:transcode ((list '_! expr))
(prim:in-parens
(prim:insert "!")
(prim:transcode-in-parens expr)))
(defun-match prim:transcode ((list '_- expr))
(prim:in-parens
(prim:insert "-")
(prim:transcode-in-parens expr)))
(defun-match prim:transcode ((list '_~ expr))
(prim:in-parens
(prim:insert "~")
(prim:transcode-in-parens expr)))
(defun-match prim:transcode ((list '_in key value))
(prim:in-parens
(prim:in-parens
(prim:transcode key))
(prim:insert " in ")
(prim:in-parens
(prim:transcode value))))
(defun-match prim:transcode ((list '_include-js (! (string file))))
(let ((contents (with-temp-buffer
(insert-file file)
(buffer-substring-no-properties (point-min)
(point-max)))))
(prim:insert (format "// inserting contents of %s" file))
(prim:newline)
(prim:insert contents)))
(defun-match- prim:commentify (object)
(match object
((string s)
(replace-regexp-in-string "^" "// " s))
(anything-else
(recur (format "%S" anything-else)))))
(defun-match prim:transcode ((list '_comment (tail comments)))
(loop for comment in comments do
(prim:insert (prim:commentify comment))
(prim:newline)))
(eval-when (compile load eval)
(defpattern prim:binop (pattern)
`(and
(or '_+
'_-
'_*
'_/
'_&&
'_||
'_==
'_===
'_%
'_&
'_|
'_^
'_<<
'_>>
'_<
'_>
'_<=
'_>=
'_>>>)
,pattern)))
(defun prim:binop-sym->transcode-string (s)
(substring (symbol-name s) 1))
(defun prim:transcode-in-parens-when-needed (expr)
(match expr
((or (keyword k)
(symbol k)
(number k))
(prim:transcode expr))
((list '_. (tail tl))
(prim:transcode expr))
((list '_value expr)
(prim:transcode-in-parens-when-needed expr))
(anything-else
(prim:in-parens
(prim:transcode expr)))))
(defun-match prim:transcode ((list '_+ (tail args)))
(prim:in-parens
(loop for (element . rest) on args
do (prim:in-parens (prim:transcode element))
when rest do (prim:insert "+"))))
(defun-match prim:transcode ((list '_- (tail args)))
(prim:in-parens
(loop for (element . rest) on args
do (prim:in-parens (prim:transcode element))
when rest do (prim:insert "-"))))
(defun-match prim:transcode ((list '_* (tail args)))
(prim:in-parens
(loop for (element . rest) on args
do (prim:in-parens (prim:transcode element))
when rest do (prim:insert "*"))))
(defun-match prim:transcode ((list '_/ first (tail args)))
(prim:in-parens
(cond ((= 0 (length args))
(recur first))
(:otherwise
(prim:in-parens (prim:transcode first))
(prim:insert "/")
(prim:in-parens (loop for (element . rest) on args
do (prim:in-parens (prim:transcode element))
when rest do (prim:insert "*")))))))
(defun-match prim:transcode ((list '_&& (tail args)))
(prim:in-parens
(loop for (element . rest) on args
do (prim:in-parens (prim:transcode element))
when rest do (prim:insert "&&"))))
(defun-match prim:transcode ((list '_|| (tail args)))
(prim:in-parens
(loop for (element . rest) on args
do (prim:in-parens (prim:transcode element))
when rest do (prim:insert "||"))))
(defun-match prim:transcode ((list (prim:binop op) e1 e2))
(prim:in-parens
(prim:transcode-in-parens-when-needed e1)
(prim:insert (prim:binop-sym->transcode-string op))
(prim:transcode-in-parens-when-needed e2)))
(defun-match prim:transcode ((list-rest '_comma-sequence exprs))
(prim:in-parens (prim:transcode-csvs exprs)))
(defun-match prim:transcode ((list-rest '_newline-sequence exprs))
(prim:transcode-newline-sequence exprs))
(defun-match prim:transcode (['_: (tail elements)])
(prim:in-brackets
(prim:transcode-csvs elements)))
(defun-match prim:transcode ([hd (tail elements)])
(prim:transcode-in-parens-when-needed hd)
(loop for element in elements do
(prim:in-brackets
(prim:transcode element))))
(eval-when (compile load eval)
(defun-match- prim:flat-seq-of-sym-val-pairs (nil)
t)
(defun-match prim:flat-seq-of-sym-val-pairs ((list odd))
nil)
(defun-match prim:flat-seq-of-sym-val-pairs ((list (or (symbol s)
(string s))
value-expr (tail rest)))
(recur rest))
(defun-match prim:flat-seq-of-sym-val-pairs (anything-else)
nil))
(eval-when (compile load eval)
(defpattern prim:{}-body (&optional (pattern (gensym)))
`(p #'prim:flat-seq-of-sym-val-pairs ,pattern)))
(defun-match- prim:group-by-2 (nil acc)
(reverse acc))
(defun-match prim:group-by-2 ((list a) acc)
(error "Odd number of elements in prim:group-by-two."))
(defun-match prim:group-by-2 ((list a b (tail rest)) acc)
(recur rest (cons (list a b) acc)))
(defun-match prim:group-by-2 ((list-rest lst))
(recur lst nil))
(defun-match prim:transcode ((list '_{} (tail (prim:{}-body tail))))
(prim:insert "{")
(prim:with-tab+
(prim:newline)
(let ((first true))
(loop for (sym val) in (prim:group-by-2 tail) do
(if first (setq first nil)
(progn (prim:insert ",")
(prim:newline)))
(prim:transcode sym)
(prim:insert ":")
(prim:transcode val))))
(prim:newline)
(prim:insert "}"))
(defun-match prim:transcode ((list 'quote expr))
(recur (prim:transcode->string expr)))
(defun-match prim:transcode ((list-rest head arguments))
(prim:transcode-in-parens-when-needed head)
(prim:in-parens
(prim:transcode-csvs arguments)))