-
Notifications
You must be signed in to change notification settings - Fork 38
/
c.lisp
1486 lines (1375 loc) · 47.4 KB
/
c.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
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Copyright Jonathan Baca, 2016
(if (not (boundp ' ***C/LISP-SYSTEM-LOADED***))
(progn
(format t "Welcome to LISP/C. Loading constants...")
(setf *lbrac* #\[)
(setf *file-out* nil)
(setf *exec-out* nil)
(setf *last-compiled* nil)
(setf *c-synonyms* (make-hash-table))
(setf *macrolist* (make-hash-table))
(setf *templatelist* (make-hash-table))))
(setf ***C/LISP-SYSTEM-LOADED*** T)
; ; returns (list key key-exists rest)
; (defun getkey (lst key)
; (setf key (addsyms #\: key))
(defun replace-fn (old-fn new-fn form)
(labels ((helper (form s)
(if (atom form)
(if (and s (eq form old-fn)) new-fn form)
(if (null form)
nil
(if (atom (car form))
(cond
((eq (car form) 'function)
(cons 'function (helper (cdr form) t)))
((eq (car form) 'quote)
(print (cadr form))
`(quote ,(cadr form)))
(t
(cons (helper (car form) t)
(mapcar #'(lambda (x)
(helper x nil)) (cdr form)))))
(cons (helper (car form) t)
(mapcar #'(lambda (x)
(helper x nil)) (cdr form))))))))
(helper form t)))
(defmacro lisp/c-macro (nym llist &rest code)
(let ((helper (addsyms nym '-background))
(args (gensym)))
`(progn
(defun ,helper ,llist ,@(replace-fn nym helper code))
(cdefun ,nym (&rest ,args) (c (apply #',helper ,args))))))
; (DEFMACRO LISP/C-MACRO (NYM LLIST &REST CODE)
; (LET ((HELPER (GENSYM)))
; `(CDEFUN ,NYM (REST ,ARGS)
; (LABELS ((,HELPER ,LLIST
; ,@(REPLACE-FN NYM HELPER CODE)))
; (CDEFUN ,NYM (&REST ,ARGS)
; (C ,(APPLY HELPER ARGS)))))))
(defun pairify (xs)
(if (null xs)
nil
(cons
(list (car xs) (cadr xs))
(pairify (cddr xs)))))
(defmacro macropairs (m &rest xs)
`(progn
,@(mapcar #'(lambda (x) `(,m ,@x)) (pairify xs))))
(defmacro sethash (k v hash)
`(setf (gethash ,k ,hash) ,v))
(defmacro inhash (k hash)
`(nth-value 1 (gethash ,k ,hash)))
(defmacro csyn (k v)
`(sethash ,k ,v *c-synonyms*))
(defmacro cunsyn (k)
`(remhash ,k *c-synonyms*))
(defun write-out (str)
(if *file-out*
(with-open-file (stream *file-out* :direction :output :if-exists :append :if-does-not-exist :create)
(format stream str))))
(defun change-file (file &optional is-h)
(setf *exec-out* (c-strify file))
(setf *file-out* (format nil "~a.~a" *exec-out* (if is-h #\h #\c))))
(defun change-exec (nym)
(setf *exec-out* (c-strify nym)))
(defun compile-c ()
(ext:run-shell-command (format nil "gcc ~a -o ~a" *file-out* *exec-out*)))
(defun strof (x)
(format nil "~a" x))
(defun f/list (x)
(if (listp x) x (list x)))
(defun f/list/n (x &optional (n 1))
(if (zerop n) x
(if (eq 1 n) (f/list x)
(mapcar #'(lambda (y) (f/list/n y (1- n))) (f/list x)))))
(defmacro decr (x)
`(setf ,x (1- ,x)))
(defun f/list//n (x &optional (n 1))
(if (<= n 0) x
(if (atom x)
(list (f/list//n x (1- n)))
(if (null (cdr x))
(list (f/list//n (car x) (- n 2)))
(list (f/list//n x (1- n)))))))
(defun strsof (xs)
(format nil "~{~a~}" xs))
(defun chs->str (x)
(strsof x))
(defun str->chs (x)
(loop for c across x collect c))
(defun replace-char (before after str)
(chs->str (mapcar #'(lambda (x) (if (eq x before) after x)) (str->chs str))))
(defun numeric-string (x)
(ignore-errors (numberp (read-from-string x))))
(defun alphap (x)
(member (char-upcase x) (str->chs "ABCDEFGHIJKLMNOPQRSTUVWXYZ")))
(defun c-strify (x &optional leavemostlyalone)
(if (stringp x) x
(let ((s (strof x)))
(if leavemostlyalone
(string-downcase s)
(if (numeric-string s) s
(if (eq (char s 0) #\!) (replace-char #\- #\_ (cof (subseq s 1)))
(if (eq (char s 0) #\=) (camelcase-c (cof (subseq s 1)))
(if (eq (char s 0) #\-) (Lcamelcase-c (cof (subseq s 1)))
(replace-char #\- #\_ (string-downcase s))))))))))
(defmacro sethash (k v hash)
`(setf (gethash ,k ,hash) ,v))
(defun addsyms (&rest syms)
(read-from-string (strsof syms)))
(defun macn (x &optional n)
(def n 1)
(if (zerop n) x (macn (macroexpand-1 x) (1- n))))
(defmacro def (a b) `(setf ,a (if ,a ,a ,b)))
(defmacro deff (x f)
`(setf ,x (,f ,x)))
(defmacro func-syn (func syn)
`(progn
(defun ,syn (&rest args)
(apply #',func args))
(compile ',syn)))
(defmacro cfunc-syn (func syn)
`(func-syn ,(cnym func) ,(cnym syn)))
(defmacro func-syns (func syns &rest yns)
(deff syns f/list)
(setf syns (append syns yns))
`(progn ,@(mapcar #'(lambda (syn) `(func-syn ,func ,syn)) syns)))
(defmacro cfunc-syns (func syns &rest yns)
(deff syns f/list)
(setf syns (append syns yns))
`(progn ,@(mapcar #'(lambda (syn) `(cfunc-syn ,func ,syn)) syns)))
(defmacro un (x) `(setf ,x (not ,x)))
(defun cnym (nym)
(nth-value 0 (addsyms nym '-c)))
(defmacro incr (x)
`(setf ,x (1+ ,x)))
(defmacro cdefun (f args &body body)
`(progn
(defun ,(cnym f) ,args ,@body)
(compile ',(cnym f))))
(defmacro binop2 (oper &key nlp nrp nym)
(def nym oper)
(un nlp)
(un nrp)
(labels ((helper (a b) (if a `(format nil "(~a)" (cof ,b)) `(cof ,b))))
`(cdefun ,nym (x y) (format nil "~a~a~a" ,(helper nlp 'x) ',oper ,(helper nrp 'y)))))
; does a left reduce
(defmacro lredop (oper &key nym nparen)
(def nym oper)
(let ((lp (if nparen "" "(")) (rp (if nparen "" ")")))
`(cdefun ,nym (&rest xs)
(if (null xs) nil
(if (= 1 (length xs))
(format nil "~a~a~a" ,lp (cof (car xs)) ,rp)
(format nil "~a~a~a~a~a~a~a"
,lp
,lp (cof (car xs)) ,rp
',oper
(apply (function ,(cnym nym)) (cdr xs)) ,rp))))))
(defmacro rredop (oper &key nym nparen)
(def nym oper)
(let ((lp (if nparen "" "(")) (rp (if nparen "" ")")))
`(cdefun ,nym (&rest xs)
(if (null xs) nil
(if (= 1 (length xs))
(format nil "~a~a~a" ,lp (cof (car xs)) ,rp)
(format nil "~a~a~a~a~a~a~a" ,lp (apply (function ,(cnym nym)) (butlast xs)) ',oper ,lp (cof (car (last xs))) ,rp ,rp))))))
(defun parenify (x)
(format nil "(~a)" x))
(defmacro binop (oper &key nlp nrp nym nyms l r nparen)
;;; (format t "OPER:~a NYM:~a NYMS:~a NPAREN:~a~%" oper nym nyms nparen)
(if nyms
`(progn ,@(mapcar #'(lambda (x) `(binop ,oper :nlp ,(un nlp) :nrp ,(un nrp) :nym ,x :l l :r r :nparen ,nparen)) nyms))
(if (or l r)
(if l `(lredop ,oper :nym ,nym :nparen ,nparen) `(rredop ,oper :nym ,nym :nparen ,nparen))
`(binop2 ,oper :nlp ,nlp :nrp ,nrp :nym ,nym))))
(defmacro pre (oper &key nym nparen)
`(cdefun ,nym (x) (format nil "~a~a~a~a" ',oper ,(if nparen "" "(") (cof x) ,(if nparen "" ")") )))
(defmacro post (oper &key nym nparen)
`(cdefun ,nym (x) (format nil "~a~a~a~a" ,(if nparen "" "(") (cof x) ,(if nparen "" ")") ',oper)))
(defmacro prepost (oper &key post nym nparen nyms)
(setf nym (if nym nym oper))
(if nyms
`(progn ,@(mapcar #'(lambda (x) `(prepost ,oper :post ,post :nym ,x :nparen ,nparen)) nyms))
(if post
`(post ,oper :nym ,nym :nparen ,nparen)
`(pre ,oper :nym ,nym :nparen ,nparen))))
(defmacro preposts (&rest opers)
`(progn ,@(mapcar #'(lambda (oper) `(prepost ,@(f/list oper))) opers)))
(defmacro binops (&rest opers)
`(progn ,@(mapcar #'(lambda (oper) `(binop ,@(f/list oper))) opers)))
(defmacro swap (a b)
(let ((c (gensym)))
`(let ((,c ,a))
(setf ,a ,b)
(setf ,b ,c)
(setf ,c ,a))))
(defmacro cfun (nym llisp &body body)
`(cdefun ,nym ,llisp ,@body))
(defmacro cfuns (&body defs)
`(progn ,@(mapcar #'(lambda (def) `(cfun ,@def)) defs)))
(defun c (&rest xs)
(format nil "~{~a~^~(;~%~%~)~}" (mapcar #'cof xs)))
(defun pc (&rest xs)
(format t "~a" (apply #'c xs)))
(defun repeatnrepeatnrepeatn (x &optional (n 1))
(format nil "~{~a~}"
(loop for i from 1 to n collect x)))
(defmacro cwrite (&rest xs)
`(write-out (format nil "~a;~%" (c ,@xs))))
(defun symtrim (x n)
(read-from-string (subseq (strof x) n)))
(defun capitalize-c (str)
(format nil "~a~a"
(string-upcase (char (strof str) 0))
(string-downcase (subseq (strof str) 1))))
(defun uncapitalize-c (str)
(format nil "~a~a"
(string-downcase (char (strof str) 0))
(subseq (strof str) 1)))
(defun flatten (xs)
(if (atom xs) (list xs) (mapcan #'flatten xs)))
(defun divide-at (seq elem)
(labels ((helper (seq elem curr res)
(if (null seq) (cons (reverse curr) res)
(if (eq (car seq) elem)
(helper
(cdr seq) elem nil (cons (reverse curr) res))
(helper
(cdr seq) elem (cons (car seq) curr) res)))))
(reverse (helper seq elem nil nil))))
(defun split-str (str ch)
(remove-if #'(lambda (x) (eq (length x) 0))
(mapcar #'chs->str (divide-at (str->chs str) ch))))
(defun lowercase-c (&rest strs)
(format nil "~{~a~}" (mapcar #'string-downcase (mapcar #'strof strs))))
(defun uppercase-c (&rest strs)
(format nil "~{~a~}" (mapcar #'string-upcase (mapcar #'strof strs))))
(defun camelcase-c (&rest strs)
(setf strs
(flatten (mapcan #'(lambda (x) (split-str x #\-)) (mapcar #'strof strs))))
(setf strs
(flatten (mapcan #'(lambda (x) (split-str x #\_)) (mapcar #'strof strs))))
(format nil "~{~a~}" (mapcar #'capitalize-c strs)))
(defun dashify-c (&rest strs)
(format nil "~{~a~^-~}" (mapcar #'cof strs)))
(defun lcamelcase-c (&rest strs)
(setf strs
(flatten (mapcan #'(lambda (x) (split-str x #\-)) (mapcar #'strof strs))))
(format nil "~a~{~a~}" (string-downcase (car strs)) (mapcar #'capitalize-c (cdr strs))))
(defmacro with-optional-first-arg (args nym default-value possible-values &body body)
(let ((other (gensym)))
`(let ((,nym (if (member (car ,args) ',possible-values)
(car ,args)
',other)))
(if (eq ,nym ',other)
(setf ,nym ,default-value)
(setf ,args (cdr ,args)))
,@body)))
(defun gensym-n (&optional (n 1))
(loop for i from 1 to n collect (gensym)))
(defun bar (&rest xs)
(with-optional-first-arg xs atmos 'cloudy (cloudy sunny rainy)
(with-optional-first-arg xs deg 0 (0 1 2 3 4 5)
(list atmos deg xs))))
(defmacro fib (n)
(if (< n 2) 1 `(+ (fib ,(1- n)) (fib ,(- n 2)))))
(defun macnx (macro-form &optional (n 1))
(if (zerop n)
macro-form
(if (listp macro-form)
(if (atom (car macro-form))
(if (equal (macroexpand-1 macro-form) macro-form)
(mapcar #'(lambda (x) (macnx x n)) macro-form)
(macnx (macroexpand-1 macro-form) (1- n)))
(mapcar #'(lambda (x) (macnx x n)) macro-form))
macro-form)))
(defun padleft (lst item len)
(if (>= (length lst) len)
lst
(append (padleft lst item (1- len)) (list item))))
(defun cof (x)
(if (null x)
""
(if (atom x)
(if (inhash x *c-synonyms*)
(cof (gethash x *c-synonyms*))
(c-strify x))
(if (atom (car x))
(if (and
(> (length (strof (car x))) 1)
(not (fboundp (cnym (car x)))))
(case (char (strof (car x)) 0)
(#\@ (apply #'call-c (cof (symtrim (car x) 1)) (cdr x)))
(#\[ (apply #'nth-c (cof (symtrim (car x) 2)) (cdr x)))
(#\] (apply #'arr-c (cof (symtrim (car x) 1)) (cdr x)))
(#\& (apply #'addr-c (cof (symtrim (car x) 1)) (cdr x)))
(#\^ (apply #'cast-c (cof (symtrim (car x) 1)) (cdr x)))
(#\* (apply #'ptr-c (cof (symtrim (car x) 1)) (cdr x)))
(#\. (apply #'mem-c (cof (symtrim (car x) 1)) (cdr x)))
(#\> (apply #'slot-c (cof (symtrim (car x) 1)) (cdr x)))
(#\= (apply #'camelcase-c (strof (symtrim (car x) 1)) (mapcar #'strof (cdr x))))
(#\% (apply #'lcamelcase-c (strof (symtrim (car x) 1)) (mapcar #'strof (cdr x))))
(#\- (apply #'lcamelcase-c (strof (symtrim (car x) 1)) (mapcar #'strof (cdr x))))
(otherwise (apply (cnym (car x)) (cdr x))))
(apply (cnym (car x)) (cdr x)))
(format nil "~{~a~^~(;~%~)~}" (mapcar #'cof x))))))
(defmacro cofy (x) `(setf ,x (cof ,x)))
(defmacro cofsy (x) `(setf ,x (mapcar #'cof (f/list ,x))))
(defun replacify (vars subs template)
(labels ((helper (v s temp)
(if (eq temp v) s
(if (atom temp) temp
(mapcar #'(lambda (x) (helper v s x)) temp)))))
(if (null vars) template
(replacify (cdr vars) (cdr subs) (helper (car vars) (car subs) template)))))
(defmacro replacify-lambda (vars template)
(let ((varlist (loop for i from 1 to (length vars) collect (gensym))))
`(lambda ,varlist (replacify ',vars (list ,@varlist) ',template))))
;; ## NOW DEFINE THE C LANGUAGE
(binops (= :l t :nyms (= set let <- ":="))
(!= :l t :nyms (!= neq diff different))
(== :r t :nyms (== eq same))
(< :r t :nyms (< lt))
(> :r t :nyms (> gt))
(<= :r t :nyms (<= leq le))
(>= :r t :nyms (>= geq ge))
(&& :r t :nyms (&& and et und y))
(& :r t :nyms (& bit-and band .and bit-et bet .et bit-und bund .und bit-y by .y ))
(&= :l t :nyms (&= &-eq bit-and-eq band-eq .and-eq bit-et-eq bet-eq .et-eq bit-und-eq bund-eq
.und-eq bit-y-eq by-eq .y-eq &= bit-and= band= .and= bit-et= bet=
.et= bit-und= bund= .und= bit-y= by= .y= ))
("||":r t :nyms (or uel oder o))
("|" :r t :nyms (bit-or .or bor bit-uel .uel buel bit-oder .oder boder bit-o .o bo))
("|=":l t :nyms (bit-or-eq .or-eq bor-eq bit-uel-eq .uel-eq buel-eq bit-oder-eq
.oder-eq boder-eq bit-o-eq .o-eq bo-eq bit-or= .or=
bor= bit-uel= .uel= buel= bit-oder= .oder= boder= bit-o= .o= bo=))
(+ :r t :nyms (+ plus add sum))
(+= :l t :nyms (+= plus-eq add-eq sum-eq plus= add= sum=))
(- :r t :nyms (- minus subtract sub))
(-= :l t :nyms (-= minus-eq subtract-eq sub-eq minus= subtract= sub=))
(* :r t :nyms (* times product mul multiply))
(*= :l t :nyms (*= times-eq product-eq mul-eq multiply-eq times= product= mul= multiply=))
(/ :r t :nyms (/ quotient ratio div divide))
(/= :l t :nyms (/= quotient-eq ratio-eq div-eq divide-eq quotient= ratio= div= divide=))
(% :r t :nyms (% modulo mod remainder))
(%= :l t :nyms (%-eq modulo-eq mod-eq remainder-eq %= modulo= mod= remainder=))
(<< :r t :nyms (<< l-shift shift-left shl))
(" << " :l t :nparen t :nym <<+) ;; for C++
(" >> " :l t :nparen t :nym >>+) ;; for C++
(= :l t :nparen t :nym =!)
(<<= :l t :nyms (<<= l-shift-eq shift-left-eq shl-eq l-shift= shift-left= shl=))
(>> :r t :nyms (>> r-shift shift-right shr))
(>>= :l t :nyms (>>= r-shift-eq shift-right-eq shr-eq >>= r-shift= shift-right= shr=))
)
(preposts (++ :post nil :nyms (++ inc +inc incr pre++ +1 ++n))
(++ :post t :nyms (+++ pinc inc+ pincr post++ 1+ n++))
(-- :post nil :nyms (-- dec -dec decr pre-- -1 --n))
(-- :post t :nyms (--- pdec dec- pdecr post-- 1- n--))
(- :post nil :nyms (neg))
(! :post nil :nyms (! not un a flip))
(~ :post nil :nyms (~ bit-not bit-un bit-a bit-flip))
(* :post t :nyms (ptrtyp arg*) :nparen t))
(cfuns
(arr-decl (&rest xs)
(format nil "{~{~a~^~(, ~)~}}" (mapcar #'cof xs)))
(struct-decl (&optional nym &rest xs)
(cofy nym)
(format nil "(~a){~{~a~^~(, ~)~}}" nym (mapcar #'cof xs)))
(sym/add (&rest xs)
(cofsy xs)
(strsof xs))
(slot (a &rest bs)
(cofy a)
(cofsy bs)
(format nil "(~a)~a~{~a~^~(->~)~}" a (if bs "->" "") bs))
(mem (a &rest bs)
(cofy a)
(cofsy bs)
(format nil "(~a)~a~{~a~^.~}" a (if bs "." "") bs))
(typ* (x &optional (n 1))
(cofy x)
(format nil "~a~{~a~}" x (loop for i from 1 to n collect #\*)))
(const (&rest xs)
(format nil "const ~a" (apply #'var-c
(if (= 1 (length xs)) (list (car xs) nil) xs))))
(syn (a b)
(progn
(csyn a b) ""))
(unsyn (a)
(progn
(cunsyn a) ""))
(progn (&rest xs)
(format nil "~{ ~a;~^~%~}" (mapcar #'cof xs)))
(? (test ifyes ifno)
(cofy test)
(cofy ifyes)
(cofy ifno)
(format nil "(~a)?~a:(~a)" test ifyes ifno))
(if (test &optional ifyes ifno)
(cofy test)
(cofy ifyes)
(format nil "if(~a) {~% ~a;~%}~a" test ifyes (if ifno (format nil "else{~% ~a;~%}"(cof ifno)) "")))
(cond (&rest pairs)
(format nil "if(~a) {~{~% ~a;~}~%}~{~a~}" (cof (caar pairs)) (mapcar #'cof (cdar pairs))
(mapcar #'(lambda (pair) (format nil "else if(~a){~{~% ~a;~}~%}"
(cof (car pair)) (mapcar #'cof (cdr pair)))) (cdr pairs))))
(ifs (&rest pairs)
(format nil "if(~a) {~{~% ~a;~}~%}~{~a~}" (cof (caar pairs)) (mapcar #'cof (cdar pairs))
(mapcar #'(lambda (pair) (format nil "if(~a){~{~% ~a;~}~%}"
(cof (car pair)) (mapcar #'cof (cdr pair)))) (cdr pairs))))
(main (&rest body)
(format nil "int main(int argc,char **argv)~a" (block-c body)))
(for (a b c &rest lines)
(cofy a) (cofy b) (cofy c)
(format nil "for(~a;~a;~a)~a" a b c (block-c lines)))
(while (test &rest lines)
(cofy test)
(format nil "while(~a) ~a" test (block-c lines)))
(do-while (test &rest lines)
(cofy test)
(format nil "do~awhile(~a)" (block-c lines) test))
(switch (var &rest pairs)
(cofy var)
(labels ((helper (pairs)
(format nil "~a:~% ~a~%~a"
(cof (caar pairs))
(block-c (cdar pairs) NIL)
(if (cdr pairs)
(helper (cdr pairs))
""))))
(format nil "switch(~a){~a}" var (helper pairs))))
(addr (x &optional (n 1))
(cofy x)
(format nil "~a(~a)" (repeatnrepeatnrepeatn #\& n) x))
(ptr (x &optional (n 1))
(format nil "~{~a~}(~a)" (loop for i from 1 to n collect #\*) (cof x)))
(pt (x &optional (n 1))
(format nil "~{~a~}~a" (loop for i from 1 to n collect #\*) (cof x)))
(nth (x &optional (n 0) &rest ns)
(format nil "(~a)[~a]~a" (cof x) (cof n)
(if ns
(format nil "~{[~a]~}" (mapcar #'cof ns)) "")))
(arr (x &optional n &rest ns)
(format nil "~a[~a]~a" (cof x) (cof n)
(if ns
(format nil "~{[~a]~}" (mapcar #'cof ns)) "")))
(call (nym &rest args)
(format nil "~a(~{~a~^,~})" (cof nym) (mapcar #'cof args)))
(cuda/call (nym ijk &rest args)
(cofy nym) (cofsy ijk)
(format nil "~a<<<~{~a~^,~}>>>(~{~a~^,~})" nym ijk (mapcar #'cof args)))
(str (&rest x)
(cofsy x)
(format nil "\"~{~a~^ ~}\"" x))
(char (x)
(cofy x)
(format nil "'~a'" x))
(cast (nym &optional (typ 'int) &rest typs)
(if typs
(apply #'cast-c (cast-c nym typ) typs)
(format nil "((~a)(~a))" (cof typ) (cof nym))))
(var (x &optional type init &rest modifiers)
(cofy x)
(cofy type)
(format nil "~a~a~{~a~^,~}~a"
(if modifiers
(format nil "~{~a ~}" (mapcar #'cof modifiers))
"")
(if type (format nil "~a " type) "")
(f/list x) (if init (format nil "=~a" (cof init)) "")))
(vars (x &optional (inter #\,) (newline t))
(setf x (mapcar #'(lambda (y) (apply #'var-c (f/list y))) (f/list/n x 1)))
(format nil (format nil "~~{~~a~~^~(~a~a~)~~}" inter (if newline #\Newline "")) x))
(varlist (args)
(vars-c args #\;))
(struct (nym &optional vars)
(cofy nym)
(csyn '***curr-class*** nym)
(if vars
(format nil "struct ~a{~% ~a;~%}" nym (vars-c vars #\;))
(format nil "struct ~a" nym)))
(union (nym &optional vars)
(cofy nym)
(if vars
(format nil "union ~a{~% ~a;~%}" nym (vars-c vars #\;))
(format nil "union ~a" nym)))
(block (&optional lines (bracket t))
(let ((preq "")
(unempty (and lines (not (equal '(nil) lines)))))
(if (eq 'const (car lines))
(progn
(setf preq " const ")
(setf lines (cdr lines))))
(if (listp (car lines))
(if (eq '-> (caar lines))
(progn
(setf preq
(format nil "~a -> ~a" preq (cof (cadar lines))))
(setf lines (cdr lines)))))
(format nil "~a~a~a~{ ~a~(;~%~)~}~a"
preq
(if bracket #\{ "")
(if unempty #\Newline "")
(if unempty
(mapcar #'cof (f/list lines))
())
(if bracket #\} "") )))
(func (nym &optional typ vars &rest body)
(cofy nym)
(cofy typ)
(format nil "~a ~a(~a)~a" typ nym (vars-c vars #\, nil)
(if body (block-c body) "")))
(inline (arg)
(format nil "inline ~a" (cof arg)))
(cuda/global (&rest args)
(format nil "__global__ ~a" (apply #'func-c args)))
(cuda/device (&rest args)
(format nil "__device__ ~a" (apply #'func-c args)))
(funcarg (nym typ &optional varforms)
(cofy nym)
(cofy typ)
(cofsy varforms)
(format nil "~a(*~a)(~{~a~^,~})" typ nym varforms))
(return (&optional x &rest ys)
(cofy x)
(format nil "return ~a~a~{~^ ~a~}"
x (if ys #\; "")
(if ys (mapcar #'cof ys) ())))
(typedef (x &optional y)
(cofy x)
(format nil "typedef ~a ~a;~%" x (if y (cof y) "")))
(enum (nym &rest mems)
(cofy nym)
(cofsy mems)
(format nil "enum ~a{~{~a~^~(, ~)~}};~%" nym mems))
(h-file (nym)
(cofy nym)
(format nil "~a.h" nym))
(str/add (&rest xs)
(format nil "~{~a~}" (cof xs)))
(include (filename &key local)
(cofy filename)
(format nil "#include ~a~a~a~%" (if local #\" #\<) filename (if local #\" #\>)))
(import (filename)
(setf filename (if (stringp filename) filename (format nil "~a.cl" (cof filename))))
(progn (c-whole-file filename)) (format nil "/* ~a LOADED */" filename))
(macro (nym &rest xs)
(cofy nym)
(format nil "~a(~{~a~^,~})" nym (mapcar #'cof (f/list xs))))
(unsigned (x)
(cofy x)
(format nil "unsigned ~a" x))
(define (a b)
(cofy a)
(cofy b)
(format nil "#define ~a ~a~%" a b))
(ifdef (expr)
(cofy expr)
(format nil "#ifdef ~~%" expr))
(ifndef (expr)
(cofy expr)
(format nil "#ifndef ~~%" expr))
(if# (expr)
(cofy expr)
(format nil "#if ~a~%" expr))
(else# ()
"#else~%")
(endif ()
"#endif~%")
(pragma (&rest xs)
(cofsy xs)
(format nil "#pragma ~{~a~^ ~}" xs))
(paren (x)
(cofy x)
(format nil "(~a)" x))
(comment (&rest xs)
(let* ((small (eq (car xs) 's))
(s (format nil "/* ~{~a~^ ~} */~%" (mapcar #'cof (if small (cdr xs) xs))))
(v (if small "" (format nil "/**~a**/~%" (repeatnrepeatnrepeatn #\* (- (length s) 7))))))
(format nil "~%~a~a~a~%" v s v)))
(header (nym &key local)
(include-c (h-file-c nym) :local local))
(headers (&rest xs)
(format nil "~{~a~}" (mapcar #'(lambda (x) (apply #'header-c (f/list x))) xs)))
(cpp (&rest xs)
(cofsy xs)
(format nil "#~{~a~^ ~}" xs))
(lisp (x)
(let ((s (eval x)))
(if (stringp s) s "")))
(lispmacro (f llist &rest body)
(if (and
(fboundp (cnym f))
(not (inhash f *macrolist*)))
(format nil "/**ERROR: \"~a\" ALREADY DEFINED.**/" f)
(progn
(eval `(cdefun ,f ,llist ,@body))
(sethash f t *macrolist*)
(format nil "/**DEFINED: \"~a\" (lispmacro)**/" f))))
(lisp/c-macro (nym llist &rest body)
(progn
(eval `(lisp/c-macro ,nym ,llist ,@body))
(format nil "/**LISP/C MACRO \"~a\"**/" nym)))
(lambda (llist template &rest args)
(cof (eval `(apply (replacify-lambda ,llist ,template) ',args))))
(template (f vars template)
(progn (eval `(cdefun ,f (&rest args)
(cof
(apply (replacify-lambda ,vars ,template)
(mapcar #'cof args)))))
(sethash f t *templatelist*)
(format nil "/**DEFINED: \"~a\" (template)**/" f) ))
(templates (f vars template)
(progn
(eval `(cdefun ,f (&rest argss)
(apply #'progn-c (mapcar #'cof (mapcar #'(lambda (args)
(apply
(replacify-lambda ,vars ,template)
(mapcar #'cof (f/list args))))
argss))))) ""))
(cuda/dim3 (typ x y)
(cofy typ)
(cofy x)
(cofy y)
(format nil "dim3 ~a(~a,~a)" typ x y))
(cuda/dim/block (x y)
(cuda/dim3-c 'dim/block x y))
(cuda/dim/grid (x y)
(cuda/dim3-c 'dim/grid x y))
(cuda/shared (&rest xs)
(format nil "__shared__ ~a" (apply #'var-c xs)))
(repeat (x &optional (n 1))
(cofy x)
(format nil "~{~a~^ ~}" (loop for i from 1 to n collect x)))
(funcall (func &rest args)
(apply (cnym func) args))
(apply (func &rest args)
(setf args (append (butlast args) (car (last args))))
(apply (cnym func) args))
;(args nym default-value possible-values &body body)
(mapcar (&rest argss)
(with-optional-first-arg argss brackets? nil (t nil)
(let ((func (car argss)))
(setf argss (cdr argss))
(block-c (apply #'mapcar (cnym func) argss) brackets?))))
(mapargs (&rest argss)
(with-optional-first-arg argss brackets? nil (t nil)
(let ((func (car argss)))
(setf argss (cdr argss))
(block-c
(mapcar
#'(lambda (args) (apply-c func args))
argss) brackets?))))
(car (&rest args)
(car args))
(cdr (&rest args)
(cdr args))
(cadr (&rest args)
(cadr args))
(cdar (&rest args)
(cdar args))
(cddr (&rest args)
(cddr args))
(caar (&rest args)
(caar args))
(binop (opr &rest xs)
(cofsy xs)
(format nil
(format nil "(~~{(~~a)~~^~~(~a~~)~~})" opr) xs))
(funcall-if (test func &rest args)
(if test
(apply #'funcall-c func args)
(strsof (mapcar #'cof args))))
(apply-if (test func args)
(if test
(apply #'funcall-c func args)
(strsof (mapcar #'cof args))))
(test-eq (a b)
(eq a b))
(test-not (a)
(not a))
(test-and (&rest xs)
(eval `(and ,@xs)))
(test-or (&rest xs)
(eval `(or ,@xs)))
(code-list (&rest xs)
(mapcar #'cof xs))
(list (&rest xs) xs)
)
;; C++ Stuff
(cfuns
(hh-file (nym)
(cofy nym)
(format nil "~a.hh" nym))
(header++ (nym &key local)
(if local
(include-c (hh-file-c nym) :local local)
(include-c nym)))
(headers++ (&rest xs)
(format nil "~{~a~}" (mapcar #'(lambda (x) (apply #'header++-c (f/list x))) xs)))
(tridot (x)
(cofy x)
(format nil "~a..." x))
(struct++ (&optional nym &rest xs)
(cofy nym)
(csyn '***curr-class*** nym)
(format nil "struct ~a~a" nym (if xs (block-c xs) "")))
(virtual (&optional x y)
(cofy x)
(format nil "virtual ~a~a" x
(if y (format nil " = ~a" (cof y)) "")))
(deprecated (&optional x &rest msg)
(cofy x)
(format nil "[[deprecated~a]] ~a"
(if msg (format nil "(\"~{~a~^ ~}\")" (mapcar #'cof msg)) "")
x))
(delete (&optional x)
(cofy x)
(format nil "delete ~a" x))
(lambda++ (&optional capture-list params attribs ret &rest body)
(if (eq capture-list '[])
(setf capture-list ()))
(setf capture-list (mapcar
#'(lambda (x) (if (atom x) (c-strify x t) (cof x))) (f/list capture-list)))
; (setf attribs (mapcar #'(lambda (x) (c-strify x t)) (f/list attribs)))
(setf attribs (mapcar #'cof (f/list attribs)))
(format nil "[~{~a~^,~}]~a~{~^ ~a~}~a~a"
capture-list
(if (or params attribs ret) (parenify (vars-c params #\, nil)) "")
attribs
(if ret
(format nil " -> ~a " (cof ret)) "")
(block-c body)))
(lambda++* (&optional args &rest body)
(apply #'lambda++-c (append (padleft (f/list args) nil 4) body)))
(namespace (&rest terms)
(cofsy terms)
(format nil "~{~a~^~(::~)~}" terms))
(namespacedecl (nym &rest terms)
(cofy nym)
(format nil "namespace ~a~a" nym (block-c terms)))
(typ& (&optional nym (n 1) const)
(cofy nym)
(if (not (numberp n))
(progn
(setf n 1)
(setf const 'const)))
(format nil "~a~a~a" nym
(if const (format nil " ~a" (cof const)) "")
(repeatnrepeatnrepeatn #\& n)))
(ptr& (&optional nym (n 1))
(cofy nym)
(format nil "~a~a" (repeatnrepeatnrepeatn #\& n) nym))
(typ[&] (&optional nym (n 1))
(cofy nym)
(format nil "~a(~a)" nym (repeatnrepeatnrepeatn #\& n)))
(ptr[&] (&optional nym (n 1))
(cofy nym)
(format nil "(~a)~a" (repeatnrepeatnrepeatn #\& n) nym))
(class (&optional nym &rest terms)
(cofy nym)
(csyn '***curr-class*** nym)
(if (listp (car terms))
(if (member (caar terms) '(inherits inh))
(progn
(setf nym (format nil "~a : ~{~a~^ ~}"
nym
(mapcar #'cof (cdar terms))))
(setf terms (cdr terms)))))
(format nil "class~a~a~a" (if nym " " "") nym (if terms (block-c terms) "")))
(protected (&rest terms)
(cofsy terms)
(format nil "protected:~%~a" (block-c terms nil)))
(private (&rest terms)
(cofsy terms)
(format nil "private:~%~a" (block-c terms nil)))
(public (&rest terms)
(cofsy terms)
(format nil "public:~%~a" (block-c terms nil)))
(construct (&optional args init-pairs &rest code)
(format nil "~a(~a)~a~a"
(cof '***curr-class***)
(vars-c args)
(if init-pairs
(format nil " : ~{~a~^~(, ~)~}"
(mapcar #'(lambda (xs)
(format nil "~a(~a)"
(cof (car xs))
(if (cadr xs)
(cof (cadr xs))
(cof (car xs)))))
init-pairs))
"")
(if code (block-c code) "" )))
(destroy (&optional args &rest code)
(format nil "~~~a(~a)~a"
(cof '***curr-class***)
(vars-c args)
(if code (block-c code) "")))
(constructor ()
(format nil "~a" (cof '***curr-class***)))
(destructor ()
(format nil "~~~a" (cof '***curr-class***)))
(suffix (x y)
(format nil "~a~a" (cof x) (c-strify y)))
(operator (oper &optional typ args &rest code)
(let ((opr "operator") (constif ""))
(if (listp oper)
(if (member (car oper) '(s su suf suffix))
(setf oper (format nil "\"\"_~a" (c-strify (cadr oper))))))
(cofy typ)
(if (listp oper)
(if (member (car oper) '(@ ns namespace n/c))
(progn
(setf opr
(apply
#'namespace-c
(append (butlast (cdr oper)) (list opr))))
(setf oper (car (last oper))))))
(if (null oper) (setf oper "()"))
(setf oper (c-strify oper t))
(if (eq (car code) 'const)
(progn
(setf constif " const ")
(setf code (cdr code))))
(format nil "~a ~a~a~a(~a)~a~a"
typ
opr
(if (alphap (char (strof oper) 0)) " " "")
oper
(vars-c args)
constif
(if code (block-c code) ""))))
(friend (code)
(cofy code)
(format nil "friend ~a" code))
(decltemp (&optional var typ &rest code)
(if (listp var)
(progn
(setf var (mapcar #'f/list var))
(setf code (cons typ code)))
(setf var (f/list/n (list (list var typ)))))
(cofy typ)
(setf var (format nil "~{~a~^,~}"
(mapcar #'(lambda (pair) (format nil "~{~a~^ ~}" (reverse (mapcar #'cof pair)))) var)))
(format nil "template ~a~{~^ ~a~}" (if (or typ var)
(format nil "<~a>" var) "<>")
(if code (mapcar #'cof code) '(""))))
(temp (&optional var &rest typs)
(cofy var) (cofsy typs)
(format nil "~a<~{~a~^,~}>" var typs))
(using (namespace)
(format nil "using namespace ~a" (cof namespace)))
(usevar (&rest args)
(format nil "~a" (apply #'var-c (car args) 'using (cdr args))))
(comment++ (&rest comments)
(cofsy comments)
(format nil "//~{~a~^ ~}" comments))
(new (&rest xs)
(cofsy xs)
(format nil "new ~{~a~}" xs))
(try/catch (catch &optional trybody catchbody)
(setf catch (apply #'var-c (f/list catch)))
(format nil "try~acatch(~a)~a" (block-c (f/list trybody))
catch (if catchbody (block-c (f/list catchbody)) "")))
(strlit (&rest xs)
(format nil "~a" (apply #'str-c xs)))
(explicit (&rest xs)
(cofsy xs)
(format nil "explicit ~{~a~}"))
)
(macropairs cfunc-syn
func f{}
funcarg arg{}
funcarg fa{}
namespace n/s
namespace ns
namespace @
slot ->
mem .>
typ* t*
typ& t&
typ[&] [t&]
typ[&] t[&]
typ[&] t&[]
ptr p*
ptr& p&
ptr[&] [p&]
ptr[&] p[&]
ptr[&] p&[]
ptr& var&
var v
delete del
class c.
class d/c
operator op
operator opr
construct cx
constructor cxr