-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.lisp
802 lines (749 loc) · 25.4 KB
/
go.lisp
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
(in-package :cl-golang-generator)
(setf (readtable-case *readtable*) :invert)
(defparameter *file-hashes* (make-hash-table))
(defun write-source (name code &optional (dir (user-homedir-pathname))
ignore-hash)
(let* ((fn (merge-pathnames (format nil "~a.go" name)
dir))
(code-str (emit-go
:code code))
(fn-hash (sxhash fn))
(code-hash (sxhash code-str)))
(multiple-value-bind (old-code-hash exists) (gethash fn-hash *file-hashes*)
(when (or (not exists) ignore-hash (/= code-hash old-code-hash))
;; store the sxhash of the c source in the hash table
;; *file-hashes* with the key formed by the sxhash of the full
;; pathname
(setf (gethash fn-hash *file-hashes*) code-hash)
(with-open-file (s fn
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(write-sequence code-str s))
#+sbcl
(sb-ext:run-program
;"/usr/local/go/bin/go"
"/usr/bin/go"
(list "fmt" (namestring fn))
)))))
;; http://clhs.lisp.se/Body/s_declar.htm
;; http://clhs.lisp.se/Body/d_type.htm
;; go through the body until no declare anymore
(defun consume-declare (body)
"take a list of instructions (body), parse type declarations,
return the body without them and a hash table with an environment. the
entry return-values contains a list of return values"
;; (declare (type int a b) (type float c)
;; (declare (values int &optional))
;; (declare (values int float &optional))
;; FIXME doesnt handle documentation strings
(let ((env (make-hash-table))
(looking-p t)
(new-body nil))
(loop for e in body do
(if looking-p
(if (listp e)
(if (eq (car e) 'declare)
(loop for declaration in (cdr e) do
(when (eq (first declaration) 'type)
(destructuring-bind (symb type &rest vars) declaration
(declare (ignorable symb))
(loop for var in vars do
(setf (gethash var env) type))))
(when (eq (first declaration) 'values)
(destructuring-bind (symb &rest types-opt) declaration
(declare (ignorable symb))
(let ((types nil))
;; only collect types until occurrance of &optional
(loop for type in types-opt do
(unless (eq #\& (aref (format nil "~a" type) 0))
(push type types)))
(setf (gethash 'return-values env) (reverse types))))))
(progn
(push e new-body)
(setf looking-p nil)))
(progn
(setf looking-p nil)
(push e new-body)))
(push e new-body)))
(values (reverse new-body) env)))
(defun lookup-type (name &key env)
"get the type of a variable from an environment"
(gethash name env))
(defun parse-let (code emit)
"let ({var | (var [init-form])}*) declaration* form*"
(destructuring-bind (decls &rest body) (cdr code)
(multiple-value-bind (body env) (consume-declare body)
(with-output-to-string (s)
(format s "~a"
(funcall emit
`(do0
,@(loop for decl in decls collect
(if (listp decl) ;; split into name and initform
(destructuring-bind (name &optional value) decl
(format nil "var ~a~@[ ~a~]~@[ = ~a~]"
name
(lookup-type name :env env)
(funcall emit value)))
(format nil "var ~a ~a"
decl
(let ((type (lookup-type decl :env env)))
(if type
type
(break "type ~a not defined." decl))))))
,@body)))))))
(defun parse-defun (code emit)
;; defun function-name lambda-list [declaration*] form*
;; https://golang.org/ref/spec#Function_declarations
;; func(x float, y int) int {
(destructuring-bind (name lambda-list &rest body) (cdr code)
(multiple-value-bind (body env) (consume-declare body) ;; py
(multiple-value-bind (req-param opt-param res-param
key-param other-key-p
aux-param key-exist-p)
(parse-ordinary-lambda-list lambda-list)
(declare (ignorable req-param opt-param res-param
key-param other-key-p aux-param key-exist-p))
(with-output-to-string (s)
(format s "func ~a~a ~@[~a ~]"
name
(funcall emit `(paren
,@(loop for p in req-param collect
(let ((type (gethash p env)))
(if type
(format nil "~a ~a"
p type)
(format nil "~a"
p)))
)))
(let ((r (gethash 'return-values env)))
(if (< 1 (length r))
(funcall emit `(paren ,@r))
(car r))))
(format s "~a" (funcall emit `(progn ,@body))))))))
(defun parse-defun-declaration (code emit)
;; only emit the declaration of the function
(destructuring-bind (name lambda-list &rest body) (cdr code)
(multiple-value-bind (body env) (consume-declare body)
(multiple-value-bind (req-param opt-param res-param
key-param other-key-p
aux-param key-exist-p)
(parse-ordinary-lambda-list lambda-list)
(declare (ignorable req-param opt-param res-param
key-param other-key-p aux-param key-exist-p))
(with-output-to-string (s)
(format s "~a~a ~@[~a ~]"
name
(funcall emit `(paren
,@(loop for p in req-param collect
(let ((type (gethash p env)))
(if type
(format nil "~a ~a"
p type)
(format nil "~a"
p))))))
(let ((r (gethash 'return-values env)))
(if (< 1 (length r))
(funcall emit `(paren ,@r))
(car r)))))))))
(defun parse-defmethod (code emit)
;; defmethod function-name specialized-lambda-list [declaration*] form*
;; specialized-lambda-list::= ({var | (var parameter-specializer-name)}*
;; first element of lambda-list declares the object (the methods receiver)
;; (defmethod Distance ((p Point) q) ...
;; => func (p Point) Distance(q Point) float64 { ...
(destructuring-bind (name lambda-list &rest body) (cdr code)
(multiple-value-bind (body env) (consume-declare body)
(destructuring-bind (receiver-name receiver-type) (car lambda-list)
(multiple-value-bind (req-param opt-param res-param
key-param other-key-p
aux-param key-exist-p)
(parse-ordinary-lambda-list (cdr lambda-list))
(declare (ignorable req-param opt-param res-param
key-param other-key-p aux-param key-exist-p))
(with-output-to-string (s)
(format s "func (~a ~a) ~a~a ~@[~a ~]"
receiver-name
receiver-type
name
(funcall emit `(paren
,@(loop for p in req-param
collect
(let ((type (gethash p env)))
(if type
(format nil "~a ~a"
p type)
(format nil "~a"
p)))
#+nil (format nil "~a ~a"
p
(let ((type (gethash p env)))
(if type
type
(break "can't find type for ~a in defun"
p))))
)))
(let ((r (gethash 'return-values env)))
(if (< 1 (length r))
(funcall emit `(paren ,@r))
(car r))))
(format s "~a" (funcall emit `(progn ,@body)))))))))
(defun parse-defmethod-declaration (code emit)
;; only emit declaration
(destructuring-bind (name lambda-list &rest body) (cdr code)
(multiple-value-bind (body env) (consume-declare body)
(destructuring-bind (receiver-name receiver-type) (car lambda-list)
(multiple-value-bind (req-param opt-param res-param
key-param other-key-p
aux-param key-exist-p)
(parse-ordinary-lambda-list (cdr lambda-list))
(declare (ignorable req-param opt-param res-param
key-param other-key-p aux-param key-exist-p))
(with-output-to-string (s)
(format s "func (~a ~a) ~a~a ~@[~a ~]"
receiver-name
receiver-type
name
(funcall emit `(paren
,@(loop for p in req-param collect
(let ((type (gethash p env)))
(if type
(format nil "~a ~a"
p type)
(format nil "~a"
p)))
#+nil
(format nil "~a ~a"
p
(let ((type (gethash p env)))
(if type
type
(break "can't find type for ~a in defun"
p)))))))
(let ((r (gethash 'return-values env)))
(if (< 1 (length r))
(funcall emit `(paren ,@r))
(car r))))))))))
(defun parse-defmethod-interface (code emit)
;; only emit declaration that can be used in an interface
(destructuring-bind (name lambda-list &rest body) (cdr code)
(multiple-value-bind (body env) (consume-declare body)
(destructuring-bind (receiver-name receiver-type) (car lambda-list)
(multiple-value-bind (req-param opt-param res-param
key-param other-key-p
aux-param key-exist-p)
(parse-ordinary-lambda-list (cdr lambda-list))
(declare (ignorable req-param opt-param res-param
key-param other-key-p aux-param key-exist-p))
(with-output-to-string (s)
(format s "~a~a ~@[~a ~]"
name
(funcall emit `(paren
,@(loop for p in req-param collect
(let ((type (gethash p env)))
(if type
(format nil "~a ~a"
p type)
(format nil "~a"
p)))
#+nil
(format nil "~a ~a"
p
(let ((type (gethash p env)))
(if type
type
(break "can't find type for ~a in defun"
p)))))))
(let ((r (gethash 'return-values env)))
(if (< 1 (length r))
(funcall emit `(paren ,@r))
(car r))))))))))
(defun parse-lambda (code emit)
;; lambda lambda-list [declaration*] form*
(destructuring-bind (lambda-list &rest body) (cdr code)
(multiple-value-bind (body env) (consume-declare body)
(multiple-value-bind (req-param opt-param res-param
key-param other-key-p
aux-param key-exist-p)
(parse-ordinary-lambda-list lambda-list)
(declare (ignorable req-param opt-param res-param
key-param other-key-p aux-param key-exist-p))
(with-output-to-string (s)
(format s "func ~a ~@[~a ~]"
(funcall emit `(paren
,@(loop for p in req-param
collect
(let ((type (gethash p env)))
(if type
(format nil "~a ~a"
p type)
(format nil "~a"
p))))))
(let ((r (gethash 'return-values env)))
(if (< 1 (length r))
(funcall emit `(paren ,@r))
(car r))))
(format s "~a" (funcall emit `(progn ,@body))))))))
(defun parse-setf (code emit)
"setf {pair}*"
(let ((args (cdr code)))
(format nil "~a"
(funcall emit
`(do0
,@(loop for i below (length args) by 2 collect
(let ((a (elt args i))
(b (elt args (+ 1 i))))
`(= ,a ,b))))))))
(defun parse-const (code emit)
"const {pair}*"
(let ((args (cdr code)))
(with-output-to-string (s)
(format s "const (")
(format s "~a~%"
(funcall emit
`(do0
,@(loop for i below (length args) by 2 collect
(let ((a (elt args i))
(b (elt args (+ 1 i))))
`(= ,a ,b))))))
(format s ")"))))
(defun print-sufficient-digits-f64 (f)
"print a double floating point number as a string with a given nr. of
digits. parse it again and increase nr. of digits until the same bit
pattern."
(let* ((ff (coerce f 'double-float))
(s (format nil "~E" ff)))
#+nil (assert (= 0d0 (- ff
(read-from-string s))))
(assert (< (abs (- ff
(read-from-string s)))
1d-12))
(substitute #\e #\d s)))
(progn
(defun emit-go (&key code (str nil) (level 0))
(flet ((emit (code &optional (dl 0))
"change the indentation level. this is used in do"
(emit-go :code code :level (+ dl level))))
(if code
(if (listp code)
(case (car code)
(ntuple (let ((args (cdr code)))
(format nil "~{~a~^, ~}" (mapcar #'emit args))))
(space
;; space {args}*
(let ((args (cdr code)))
(format nil "~{~a~^ ~}" (mapcar #'emit args))))
(paren
;; paren {args}*
(let ((args (cdr code)))
(format nil "(~{~a~^, ~})" (mapcar #'emit args))))
(braces
;; braces {args}*
(let ((args (cdr code)))
(format nil "{~{~a~^, ~}}" (mapcar #'emit args))))
(curly ;; name{arg1, args}
;; or name{key1:arg1, key2:arg2}
(destructuring-bind (name &rest args) (cdr code)
(emit `(cast ,name
(braces
,@(if (keywordp (car args))
(loop for i below (length args) by 2 collect
(let ((a (elt args i))
(b (elt args (+ 1 i))))
(format nil "~a: ~a" (emit a) (emit b))))
args))))))
(cast ;; cast type value
(destructuring-bind (type value) (cdr code)
(format nil "~a ~a" (emit type) (emit value)))
)
(comment (format nil "// ~a~%" (cadr code)))
(comments (let ((args (cdr code)))
(format nil "~{// ~a~%~}" args)))
(dict
;; dict {pair}*
(let* ((args (cdr code)))
(let ((str (with-output-to-string (s)
(loop for (e f) in args
do
(format s "~a: ~a," (emit e) (emit f))))))
(format nil "{~a}" ;; remove trailing comma
(subseq str 0 (- (length str) 1))))))
(go (format nil "go ~a" (emit (car (cdr code)))))
(range (format nil "range ~a" (emit (car (cdr code)))))
(chan (format nil "chan ~a" (emit (car (cdr code)))))
(defer (format nil "defer ~a" (emit (car (cdr code)))))
(return (format nil "return ~a" (emit (car (cdr code)))))
(indent
;; indent form
(format nil "~{~a~}~a"
;; print indentation characters
(loop for i below level collect " ")
(emit (cadr code))))
(do (with-output-to-string (s)
;; do {form}*
;; print each form on a new line with one more indentation.
(format s "~{~&~a~}" (mapcar #'(lambda (x) (emit `(indent ,x) 1)) (cdr code)))))
(progn (with-output-to-string (s)
;; progrn {form}*
;; like do but surrounds forms with braces.
(format s "{~{~&~a~}~&}" (mapcar #'(lambda (x) (emit `(indent ,x) 1)) (cdr code)))))
(do0 (with-output-to-string (s)
;; do0 {form}*
;; write each form into a newline, keep current indentation level
(format s "~&~a~{~&~a~}"
(emit (cadr code))
(mapcar #'(lambda (x) (emit `(indent ,x) 0)) (cddr code)))))
(let (parse-let code #'emit))
(defun (parse-defun code #'emit))
(defun-declaration (parse-defun-declaration code #'emit))
(lambda (parse-lambda code #'emit))
(defmethod (parse-defmethod code #'emit))
(defmethod-interface (parse-defmethod-interface code #'emit))
(defmethod-declaration (parse-defmethod-declaration code #'emit))
#+nil (defstruct
;; defstruct name {slot-description}*
;; slot-description::= slot-name | (slot-name [slot-initform [[slot-option]]])
;; slot-option::= :type slot-type
(destructuring-bind (name &rest slot-descriptions) (cdr code)
(format
nil "type ~a struct ~a"
name
(emit
`(progn
,@(loop for desc in slot-descriptions collect
(destructuring-bind (slot-name ;; &optional init
;; init doesnt really fit into go semantics
&key type) desc
(format nil "~a~@[ ~a~]" slot-name type))))))))
(deftype
;; deftype name lambda-list {form}*
;; only the first form of the body is used, lambda list is ignored
(destructuring-bind (name lambda-list &rest body) (cdr code)
(declare (ignore lambda-list))
(format nil "type ~a ~a" name (emit (car body)))))
(defstruct0
;; defstruct without init-form
;; defstruct name {slot-description}*
;; slot-description::= slot-name | (slot-name [slot-type])
;; a slot-name without type can be used to create a
;; composed type with a struct embedding
(destructuring-bind (name &rest slot-descriptions) (cdr code)
(format nil "type ~a struct ~a"
name
(emit
`(progn
,@(loop for desc in slot-descriptions collect
(destructuring-bind (slot-name &optional type) desc
(format nil "~a~@[ ~a~]" slot-name type))))))))
(definterface
;; definterface name {slot-description}*
;; slot-description::= other-interface-name | method-interface-declaration
(destructuring-bind (name &rest slot-descriptions) (cdr code)
(format nil "type ~a interface ~a"
name
(emit
`(progn
,@(mapcar #'emit slot-descriptions))))))
(setf (parse-setf code #'emit))
(const (parse-const code #'emit))
(assign
;; assign {pair}*
(let ((args (cdr code)))
(format nil "~a~%"
(emit
`(do0
,@(loop for i below (length args) by 2 collect
(let ((a (elt args i))
(b (elt args (+ 1 i))))
`(:= ,a ,b))))))))
(if (destructuring-bind (condition true-statement &optional false-statement) (cdr code)
(with-output-to-string (s)
(format s "if ( ~a ) ~a"
(emit condition)
(emit `(progn ,true-statement)))
(when false-statement
(format s " else ~a"
(emit `(progn ,false-statement)))))))
(when (destructuring-bind (condition &rest forms) (cdr code)
(emit `(if ,condition
(do0
,@forms)))))
(unless (destructuring-bind (condition &rest forms) (cdr code)
(emit `(if (not ,condition)
(do0
,@forms)))))
(ecase
;; ecase keyform {normal-clause}*
;; normal-clause::= (keys form*)
(destructuring-bind (keyform &rest clauses)
(cdr code)
(format
nil "switch ~a ~a"
(emit keyform)
(emit
`(progn
,@(loop for c in clauses collect
(destructuring-bind (key &rest forms) c
(format nil "case ~a:~&~a"
(emit key)
(emit
`(do0
,@(mapcar #'emit
forms)))))))))))
(case
;; case keyform {normal-clause}* [otherwise-clause]
;; normal-clause::= (keys form*)
;; otherwise-clause::= (t form*)
(destructuring-bind (keyform &rest clauses)
(cdr code)
(format
nil "switch ~a ~a"
(emit keyform)
(emit
`(progn
,@(loop for c in clauses collect
(destructuring-bind (key &rest forms) c
(if (eq key t)
(format nil "default:~&~a"
(emit
`(do0
,@(mapcar #'emit
forms))))
(format nil "case ~a:~&~a"
(emit key)
(emit
`(do0
,@(mapcar #'emit
forms))))))))))))
(typecase
;; typecase keyform {normal-clause}* [otherwise-clause]
;; normal-clause::= (keys form*)
;; otherwise-clause::= (t form*)
(destructuring-bind (keyform &rest clauses)
(cdr code)
(format
nil "switch ~a := ~a.(type) ~a"
(emit keyform)
(emit keyform)
(emit
`(progn
,@(loop for c in clauses collect
(destructuring-bind (key &rest forms) c
(if (eq key t)
(format nil "default:~&~a"
(emit
`(do0
,@(mapcar #'emit
forms))))
(format nil "case ~a:~&~a"
(emit key)
(emit
`(do0
,@(mapcar #'emit
forms))))))))))))
(for
;; for init condition update {forms}* .. for loop
;; for .. infinite loop (for () ...)
(let* ((args (cdr code))
(params (first args))
(body (cdr args)))
(with-output-to-string (s)
(cond ((eq 0 (length params))
(format s "for "))
((eq 3 (length params))
(destructuring-bind (init condition update) params
(format s "for ~a ; ~a; ~a "
(emit init)
(emit condition)
(emit update))))
(t (break "unsupported number of arguments in for loop")))
(format s "~a" (emit `(progn ,@body))))
))
(foreach
;; foreach [var] range {forms}*
;; foreach range {forms}*
(destructuring-bind ((&rest decl) &rest body) (cdr code)
(with-output-to-string (s)
(format s "for ~a "
(if (< 1 (length decl))
(destructuring-bind (var range) decl
(emit `(:= ,var ,range)))
(emit (car decl))))
(format s "~a" (emit `(progn ,@body))))))
(while
;; while condition {forms}*
(destructuring-bind (condition &rest body) (cdr code)
(with-output-to-string (s)
(format s "for ~a "
(emit condition))
(format s "~a" (emit `(progn ,@body))))))
(dotimes (destructuring-bind ((var end) &rest body) (cdr code)
(emit `(for ((:= ,var 0)
(< ,var ,end)
(incf ,var))
,@body))))
(not (format nil "(!(~a))" (emit (car (cdr code)))))
(ref (format nil "(&(~a))" (emit (car (cdr code)))))
(deref (format nil "(*(~a))" (emit (car (cdr code)))))
(package (format nil "package ~a" (car (cdr code))))
(import (let ((args (cdr code)))
;; import {(name|pair)}*
;; pair := nickname name
(with-output-to-string (s)
(format s "import (")
(loop for e in args do
(if (listp e)
(destructuring-bind (nick name) e
(format s "~&~a \"~a\""
nick name))
(format s "~&\"~a\"" e))
)
(format s "~&)"))))
(+ (let ((args (cdr code)))
;; + {summands}*
(format nil "(~{(~a)~^+~})" (mapcar #'emit args))))
(- (let ((args (cdr code)))
(if (eq 1 (length args))
(format nil "(-(~a))" (emit (car args))) ;; py
(format nil "(~{(~a)~^-~})" (mapcar #'emit args)))))
(* (let ((args (cdr code)))
(format nil "(~{(~a)~^*~})" (mapcar #'emit args))))
(/ (let ((args (cdr code)))
(if (eq 1 (length args))
(format nil "(1.0/(~a))" (emit (car args))) ;; py
(format nil "(~{(~a)~^/~})" (mapcar #'emit args)))))
(logior (let ((args (cdr code))) ;; py
(format nil "(~{(~a)~^||~})" (mapcar #'emit args))))
(logand (let ((args (cdr code))) ;; py
(format nil "(~{(~a)~^&&~})" (mapcar #'emit args))))
(or (let ((args (cdr code)))
(format nil "(~{(~a)~^|~})" (mapcar #'emit args))))
(and (let ((args (cdr code)))
(format nil "(~{(~a)~^&~})" (mapcar #'emit args))))
(= (destructuring-bind (a b) (cdr code)
;; = pair
(format nil "~a=~a" (emit a) (emit b))))
(% (destructuring-bind (a b) (cdr code)
(format nil "((~a)%(~a))" (emit a) (emit b))))
(:= (destructuring-bind (a b) (cdr code)
(format nil "~a:=~a" (emit a) (emit b))))
(/= (destructuring-bind (a b) (cdr code)
(format nil "~a/=(~a)" (emit a) (emit b))))
(*= (destructuring-bind (a b) (cdr code)
(format nil "~a*=(~a)" (emit a) (emit b))))
(^= (destructuring-bind (a b) (cdr code)
(format nil "(~a)^=(~a)" (emit a) (emit b))))
(<= (destructuring-bind (a b) (cdr code)
(format nil "(~a)<=(~a)" (emit a) (emit b))))
(!= (destructuring-bind (a b) (cdr code)
(format nil "(~a)!=(~a)" (emit a) (emit b))))
(== (destructuring-bind (a b) (cdr code)
(format nil "(~a)==(~a)" (emit a) (emit b))))
(<- ;; send to channel channel
(destructuring-bind (a &optional b) (cdr code)
(if b
(format nil "~a<-~a" (emit a) (emit b))
(format nil "<-~a" (emit a)))))
(< (destructuring-bind (a b) (cdr code)
(format nil "~a<~a" (emit a) (emit b))))
(<< (destructuring-bind (a b) (cdr code)
(format nil "~a<<~a" (emit a) (emit b))))
(>> (destructuring-bind (a b) (cdr code)
(format nil "~a>>~a" (emit a) (emit b))))
(incf (destructuring-bind (a &optional b) (cdr code) ;; py
(if b
(format nil "(~a)+=(~a)" (emit a) (emit b))
(format nil "(~a)++" (emit a)))))
(decf (destructuring-bind (a &optional b) (cdr code)
(if b
(format nil "(~a)-=(~a)" (emit a) (emit b))
(format nil "(~a)--" (emit a)))))
(string (format nil "\"~a\"" (cadr code)))
(string-raw (format nil "`~a`" (cadr code)))
(char (format nil "'~a'" (cadr code)))
(slice (let ((args (cdr code)))
(if (null args)
(format nil ":")
(format nil "~{~a~^:~}" (mapcar #'emit args)))))
(aref (destructuring-bind (name &rest indices) (cdr code)
(format nil "~a~{[~a]~}" (emit name) (mapcar #'emit indices))))
(dot (let ((args (cdr code)))
(format nil "~{~a~^.~}" (mapcar #'emit args))))
(hex (let ((arg (car (cdr code))))
(format nil "0x~x" arg)))
(bin (let ((arg (car (cdr code))))
(format nil "0b~b" arg)))
(oct (let ((arg (car (cdr code))))
(format nil "0o~o" arg)))
#+nil (-> (let ((forms (cdr code)))
;; clojure's thread first macro, thrush operator
;; http://blog.fogus.me/2010/09/28/thrush-in-clojure-redux/
;; -> {form}*
(emit (reduce #'(lambda (x y) (list (emit x) (emit y))) forms))))
(t (destructuring-bind (name &rest args) code
(if (listp name)
;; lambda call and similar complex constructs
(format nil "(~a)(~a)"
(emit name)
(if args
(emit `(paren ,@args))
""))
;; function call
(progn ;if
#+nil(and
(= 1 (length args))
(eq (aref (format nil "~a" (car args)) 0) #\.))
#+nil (format nil "~a~a" name
(emit args))
(format nil "~a~a" name
(emit `(paren ,@args))))))))
(cond
((or (symbolp code)
(stringp code)) ;; print variable
(format nil "~a" code))
((numberp code) ;; print constants
(cond ((integerp code) (format str "~a" code))
((floatp code) ;; FIXME arbitrary precision?
(format str "(~a)" (print-sufficient-digits-f64 code)))))))
"")))
#-nil
(defparameter *bla*
(emit-go :code `(let ((a (+ 40 2))
(b 3))
(declare (type int64 a))
(setf a1 3
b2 (logior a 5))
(:= stai (string "hello"))
(incf a 4)
(incf b)
(/= a (- b))
(/ q)
(setf q (sin (atan (aref a (slice 3 4))))
; f
#+nil (-> sin
(atan a)))
(defun bla (a b c)
(declare (type int a b)
(type float c)
(values float &optional))
(if a (return c)
v))
#+nil (->
(string "bla")
(.strip)
(.split (string " "))
(aref 0))
(defstruct0 Employee
(ID int)
(Name string))
(lambda (items)
(declare (type "[]string" items))
)
(defstruct0 Point
(X float64)
(Y float64))
(defmethod Distance ((p Point) q)
(declare (type Point q)
(values float64 &optional))
(return (math.Hypot (- q.X p.X)
(- q.Y p.Y))))))))