Repository navigation
Expand file tree
/
Copy pathtranspiler-tests.lisp
More file actions
1556 lines (1363 loc) · 52 KB
/
Copy pathtranspiler-tests.lisp
File metadata and controls
1556 lines (1363 loc) · 52 KB
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
;;;; transpiler-tests.lisp --- unit tests for the s-expression -> Rust transpiler
;;;;
;;;; Run with ./run-tests.sh, or from a REPL:
;;;; (load "transpiler-tests.lisp")
;;;; (cl-rust-generator::run-transpiler-tests)
;;;; (cl-rust-generator::generate-documentation)
;;;;
;;;; NOTE: the tests live *inside* the CL-RUST-GENERATOR package on purpose.
;;;; EMIT-RS dispatches with CASE on the head symbol, so a form like (dot a b)
;;;; read in another package would produce a different, non-matching symbol and
;;;; silently fall through to the "function call" branch.
(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload :cl-rust-generator)
(ql:quickload "uiop")
(ql:quickload "cl-ppcre"))
(in-package :cl-rust-generator)
;; rs.lisp inverts the readtable case; make sure that is also true while this
;; file is read, otherwise `Ok' and `dot' would not round-trip.
(eval-when (:compile-toplevel :load-toplevel :execute)
(setf (readtable-case *readtable*) :invert))
;;; ===================================================================
;;; Tier 1 + 2: transpilation tests
;;;
;;; Each entry is a plist:
;;; :name short identifier
;;; :description what the form does (also used for SUPPORTED_FORMS.md)
;;; :lisp the s-expression (unquoted, *test-cases* is quoted as a whole)
;;; :rust expected output, compared after whitespace normalisation
;;; :tags list of tags, the first one groups the documentation
;;; :item when non-NIL the expected output is a complete Rust item and
;;; is additionally handed to rustfmt as a syntax check
;;; ===================================================================
(defparameter *test-cases*
'(;; ---------------- literals ----------------
(:name "integer-literal"
:description "Positive integers are emitted verbatim."
:lisp 42
:rust "42"
:tags (:literal))
(:name "negative-integer-literal"
:description "Negative integers are wrapped in parentheses so that they can
be used as an operand without gluing to a preceding operator."
:lisp -42
:rust "(-42)"
:tags (:literal))
(:name "f32-literal"
:description "Single floats print with just enough digits to round-trip.
Note that no f32 suffix is added; the Rust type is inferred from the context."
:lisp 0.5s0
:rust "0.50"
:tags (:literal))
(:name "f32-zero-literal"
:description "Zero must not degenerate to `0.', which the Rust lexer of the
generator's own test harness would still accept but which reads badly."
:lisp 0.0s0
:rust "0.0"
:tags (:literal))
(:name "f64-literal"
:description "Double floats print with enough digits to round-trip."
:lisp 3.14159265358979d0
:rust "3.14159265359"
:tags (:literal))
(:name "f64-exponent-literal"
:description "Large doubles use exponent notation."
:lisp 1.2345678d10
:rust "1.23456780e+10"
:tags (:literal))
(:name "string-literal"
:description "(string x) emits a Rust string literal."
:lisp (string "hello")
:rust "\"hello\""
:tags (:literal))
(:name "raw-string-literal"
:description "(string-r x) emits a raw string literal. Enough hashes
are added so that the payload may contain quote-hash."
:lisp (string-r "a\"b")
:rust "r#\"a\"b\"#"
:tags (:literal))
(:name "byte-string-literal"
:description "(string-b x) emits a byte string literal."
:lisp (string-b "abc")
:rust "b\"abc\""
:tags (:literal))
(:name "char-literal"
:description "(char x) emits a character literal."
:lisp (char a)
:rust "'a'"
:tags (:literal))
(:name "byte-literal"
:description "(byte x) emits a byte literal."
:lisp (byte a)
:rust "b'a'"
:tags (:literal))
(:name "hex-literal"
:description "(hex n) prints n in hexadecimal."
:lisp (hex 255)
:rust "0xFF"
:tags (:literal))
;; ---------------- symbols ----------------
(:name "symbol-double-dash-is-path"
:description "A single - in a symbol becomes :, so -- becomes the Rust path
separator ::. This is why identifiers in generated code use underscores."
:lisp (std--sync--Arc--new x)
:rust "std::sync::Arc::new(x)"
:tags (:core))
(:name "string-escape-hatch"
:description "Strings are passed through unchanged. This is the escape
hatch for everything the generator has no form for."
:lisp (do0 "#[derive(Clone)]" (defstruct0 Point (x f64)))
:rust "#[derive(Clone)]
struct Point { x: f64, }"
:tags (:core))
(:name "angle"
:description "(angle args*) emits turbofish brackets <...>."
:lisp (angle Vec T)
:rust "<VecT>"
:tags (:core))
(:name "scope-turbofish"
:description "(scope a b c) joins with ::, so (scope name (angle type))
emits the turbofish call syntax name::<type>."
:lisp (scope parse_pair (angle i32))
:rust "parse_pair::<i32>"
:tags (:core))
;; ---------------- arithmetic ----------------
(:name "addition"
:description "(+ a b ...) emits a parenthesised sum."
:lisp (+ 1 2 3)
:rust "(1+2+3)"
:tags (:operator :arithmetic))
(:name "subtraction"
:description "(- a b) emits a difference, (- a) a unary minus."
:lisp (- 5 3)
:rust "(5-3)"
:tags (:operator :arithmetic))
(:name "negation"
:description "A single argument to - is a unary minus."
:lisp (- (+ a b))
:rust "(-(a+b))"
:tags (:operator :arithmetic))
(:name "multiplication"
:description "(* a b ...) parenthesises every factor."
:lisp (* 2 3)
:rust "((2)*(3))"
:tags (:operator :arithmetic))
(:name "division"
:description "(/ a b) divides, (/ a) emits the reciprocal."
:lisp (/ 10 2)
:rust "((10)/(2))"
:tags (:operator :arithmetic))
(:name "reciprocal"
:description "A single argument to / yields 1.0/x."
:lisp (/ x)
:rust "(1.0/(x))"
:tags (:operator :arithmetic))
(:name "modulo"
:description "(% a b) is the remainder operator. The result is
parenthesised, otherwise (dot (% a b) c) would regroup into (a)%((b).c)."
:lisp (% a b)
:rust "((a)%(b))"
:tags (:operator :arithmetic))
(:name "modulo-in-dot"
:description "Regression test: a binary operator used as the receiver of a
method call must keep its own parentheses."
:lisp (dot (% a b) (to_string))
:rust "((a)%(b)).to_string()"
:tags (:operator :precedence))
;; ---------------- omit-parens ----------------
;; Each case below is emitted with *OMIT-REDUNDANT-PARENS* bound (see
;; :omit-parens). The fully parenthesised output stays the default.
(:name "omit-add-mul"
:description "With elision * binds tighter than +, so no parentheses
are needed."
:lisp (+ 1 (* 2 3))
:rust "1 + 2 * 3"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-call-tight-operand"
:description "Calls bind tightest: no parentheses as ? operand or
dot receiver."
:lisp (do0 (? (fetch row))
(dot (fetch row) (to_string)))
:rust "fetch(row)?; fetch(row).to_string();"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-mul-add"
:description "A looser operand keeps its parentheses."
:lisp (* (+ 1 2) 3)
:rust "(1 + 2) * 3"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-sub-left"
:description "Left-nested subtraction stays flat (left associative)."
:lisp (- (- a b) c)
:rust "a - b - c"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-sub-right"
:description "Right-nested subtraction is parenthesised: a-(b-c) is
not (a-b)-c."
:lisp (- a (- b c))
:rust "a - (b - c)"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-div-right"
:description "Same-level mixed * and / never stay flat on the right:
a*(b/c) is not (a*b)/c for integers."
:lisp (* a (/ b c))
:rust "a * (b / c)"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-compare-nested"
:description "Comparisons cannot be chained in Rust (a==b==c is a
compile error), so a nested comparison is always parenthesised."
:lisp (== a (== b c))
:rust "a == (b == c)"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-bitand-compare"
:description "Unlike C, Rust's bitwise & binds tighter than ==, so no
parentheses are needed here."
:lisp (== (logand x mask) 0)
:rust "x & mask == 0"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-and-or"
:description "&& binds tighter than ||, so this stays flat."
:lisp (or (and a b) c)
:rust "a && b || c"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-neg-sum"
:description "Unary minus keeps parentheses around a looser operand."
:lisp (- (+ a b))
:rust "-(a + b)"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-neg-neg"
:description "Minus inside minus would glue into the invalid --x."
:lisp (- (- x))
:rust "-(-x)"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-not-compare"
:description "The == operand binds looser than unary !, so it keeps
its parentheses."
:lisp (not (== a b))
:rust "!(a == b)"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-dot-receiver"
:description "Method call binds tightest, so a binary receiver keeps
its parentheses even with elision."
:lisp (dot (% a b) (to_string))
:rust "(a % b).to_string()"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-deref-dot"
:description "*(p).x parses as *((p).x), so the deref keeps its
parentheses as a dot receiver."
:lisp (dot (deref p) x)
:rust "(*p).x"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-coerce-sum"
:description "`as' binds tighter than +, so the sum keeps its
parentheses."
:lisp (coerce (+ a b) i64)
:rust "(a + b) as i64"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-ref-add"
:description "& binds tighter than +, but the operand is looser, so
&(a+b) keeps its parentheses."
:lisp (ref (+ a b))
:rust "&(a + b)"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-shift-add"
:description "+ binds tighter than <<, so the sum needs no parentheses."
:lisp (<< (+ 1 1) 3)
:rust "1 + 1 << 3"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-shift-right-nested"
:description "Shifts are left associative but not flattenable, so a
right-nested shift keeps its parentheses: 64>>(8>>1) is not (64>>8)>>1."
:lisp (>> 64 (>> 8 1))
:rust "64 >> (8 >> 1)"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-reciprocal"
:description "The single-argument / reciprocal keeps a tight operand
bare."
:lisp (/ x)
:rust "1.0 / x"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-question"
:description "A call needs no parentheses under ?."
:lisp (? (dot f (read_to_string "&mut s")))
:rust "f.read_to_string(&mut s)?"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-logior-chain"
:description "Left-nested bitwise | stays flat."
:lisp (logior (logior a b) c)
:rust "a | b | c"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-logxor-right"
:description "logxor is associative for integers, so a right-nested
chain stays flat like + and * do."
:lisp (logxor a (logxor b c))
:rust "a ^ b ^ c"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-logxor-mixed"
:description "& binds tighter than ^, so no parentheses are needed."
:lisp (logxor (logand a b) c)
:rust "a & b ^ c"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-shl-left"
:description "Left-nested shifts stay flat (left associative)."
:lisp (<< (<< a 1) 2)
:rust "a << 1 << 2"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-shr-left"
:description "Same for >>."
:lisp (>> (>> a 1) 2)
:rust "a >> 1 >> 2"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-or-right"
:description "|| is associative, so a right-nested chain stays flat."
:lisp (or a (or b c))
:rust "a || b || c"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-and-or-nested"
:description "|| binds looser than &&, so it keeps its parentheses
as an && operand."
:lisp (and (or a b) c)
:rust "(a || b) && c"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-add-shift"
:description "<< binds looser than +, so the shift keeps its
parentheses as a + operand."
:lisp (+ (<< a 1) b)
:rust "(a << 1) + b"
:omit-parens t
:tags (:operator :precedence))
(:name "omit-add-right"
:description "+ is associative, so a right-nested sum stays flat
(up to floating point rounding)."
:lisp (+ 1 (+ 2 3))
:rust "1 + 2 + 3"
:omit-parens t
:tags (:operator :precedence))
;; ---------------- bit operations ----------------
(:name "bitwise-and"
:description "(logand a b) emits Rust's bitwise &."
:lisp (logand a b)
:rust "((a) & (b))"
:tags (:operator :bitwise))
(:name "bitwise-or"
:description "(logior a b) emits Rust's bitwise |."
:lisp (logior a b)
:rust "((a) | (b))"
:tags (:operator :bitwise))
(:name "bitwise-xor"
:description "(logxor a b) emits Rust's bitwise ^."
:lisp (logxor a b)
:rust "((a) ^ (b))"
:tags (:operator :bitwise))
(:name "shift-left"
:description "(<< a b) shifts left."
:lisp (<< a 3)
:rust "((a)<<(3))"
:tags (:operator :bitwise))
(:name "shift-right"
:description "(>> a b) shifts right."
:lisp (>> a 3)
:rust "((a)>>(3))"
:tags (:operator :bitwise))
;; ---------------- comparison / logic ----------------
(:name "equality"
:description "(== a b) compares for equality. Note that Common Lisp's =
means assignment here and /= means division-assignment."
:lisp (== a 5)
:rust "((a)==(5))"
:tags (:operator :comparison))
(:name "inequality"
:description "(!= a b) compares for inequality."
:lisp (!= a 5)
:rust "((a)!=(5))"
:tags (:operator :comparison))
(:name "less-than"
:description "(< a b) compares."
:lisp (< 3 5)
:rust "((3)<(5))"
:tags (:operator :comparison))
(:name "greater-than"
:description "(> a b) compares."
:lisp (> 3 5)
:rust "((3)>(5))"
:tags (:operator :comparison))
(:name "less-equal"
:description "(<= a b) compares."
:lisp (<= 3 5)
:rust "((3)<=(5))"
:tags (:operator :comparison))
(:name "greater-equal"
:description "(>= a b) compares."
:lisp (>= 3 5)
:rust "((3)>=(5))"
:tags (:operator :comparison))
(:name "logical-and"
:description "(and a b) emits Rust's short circuiting &&."
:lisp (and a b)
:rust "((a)&&(b))"
:tags (:operator :comparison))
(:name "logical-or"
:description "(or a b) emits Rust's short circuiting ||."
:lisp (or a b)
:rust "((a)||(b))"
:tags (:operator :comparison))
(:name "logical-not"
:description "(not a) emits !a. The parentheses enclose the whole
expression so that (dot (not a) b) does not turn into !((a).b)."
:lisp (not a)
:rust "(!a)"
:tags (:operator :comparison))
;; ---------------- assignment ----------------
(:name "assignment"
:description "(= place value) assigns."
:lisp (= x 5)
:rust "x=5"
:tags (:assignment))
(:name "setf-multiple"
:description "(setf a 1 b 2) expands into a sequence of assignments."
:lisp (setf a 1 b 2)
:rust "a=1;
b=2;"
:tags (:assignment))
(:name "incf"
:description "(incf place [delta]) emits +=, delta defaults to 1."
:lisp (incf i)
:rust "i += 1"
:tags (:assignment))
(:name "decf"
:description "(decf place [delta]) emits -=."
:lisp (decf i 2)
:rust "i -= 2"
:tags (:assignment))
(:name "divide-assign"
:description "(/= place value) is division-assignment, NOT `not equal'."
:lisp (/= i 2)
:rust "i/=(2)"
:tags (:assignment))
(:name "times-assign"
:description "(*= place value) is multiplication-assignment."
:lisp (*= i 2)
:rust "i*=(2)"
:tags (:assignment))
(:name "xor-assign"
:description "(^= place value) is xor-assignment."
:lisp (^= i 2)
:rust "(i)^=(2)"
:tags (:assignment))
(:name "rem-assign"
:description "(%= place [delta]) emits remainder-assignment %=."
:lisp (%= i 2)
:rust "i %= 2"
:tags (:assignment))
(:name "shl-assign"
:description "(<<= place value) is shift-left-assignment."
:lisp (<<= x 2)
:rust "x<<=(2)"
:tags (:assignment))
(:name "shr-assign"
:description "(>>= place value) is shift-right-assignment."
:lisp (>>= x 2)
:rust "x>>=(2)"
:tags (:assignment))
(:name "bitand-assign"
:description "(&= place value) is bitwise-and-assignment."
:lisp (&= x mask)
:rust "x&=(mask)"
:tags (:assignment))
(:name "bitor-assign"
:description "(\|= place value) is bitwise-or-assignment."
:lisp (\|= x mask)
:rust "x|=(mask)"
:tags (:assignment))
;; ---------------- references / casts ----------------
(:name "reference"
:description "(ref x) takes a shared reference."
:lisp (ref x)
:rust "(&x)"
:tags (:reference))
(:name "mutable-reference"
:description "(ref-mut x) takes a mutable reference."
:lisp (ref-mut x)
:rust "(&mut x)"
:tags (:reference))
(:name "dereference"
:description "(deref x) dereferences."
:lisp (deref x)
:rust "(*x)"
:tags (:reference))
(:name "dereference-in-dot"
:description "Regression test: *(p).x parses as *((p).x) in Rust, so the
parentheses have to enclose the deref, not its operand."
:lisp (dot (deref p) x)
:rust "(*p).x"
:tags (:reference :precedence))
(:name "coerce"
:description "(coerce value type) emits Rust's `as' cast."
:lisp (coerce x u8)
:rust "(x as u8)"
:tags (:reference))
(:name "try-operator"
:description "(? expr) appends Rust's ? error propagation operator."
:lisp (? (dot f (read_to_string "&mut s")))
:rust "f.read_to_string(&mut s)?"
:tags (:reference))
;; ---------------- aggregates ----------------
(:name "paren"
:description "(paren a b) emits a comma separated, parenthesised list; also
used for tuples."
:lisp (paren a b)
:rust "(a, b)"
:tags (:collection))
(:name "values-is-paren"
:description "(values ...) is an alias of paren, useful for tuples."
:lisp (values a b)
:rust "(a, b)"
:tags (:collection))
(:name "bracket"
:description "(bracket a b) and (list a b) emit a Rust array literal."
:lisp (list 1 2)
:rust "[1, 2]"
:tags (:collection))
(:name "array-repeat"
:description "(array-repeat value count) emits the repeat array
literal [value; count]."
:lisp (array-repeat 0 50)
:rust "[0; 50]"
:tags (:collection))
(:name "curly"
:description "(curly a b) emits braces, e.g. for a use-list."
:lisp (curly Read Write)
:rust "{Read, Write}"
:tags (:collection))
(:name "comma"
:description "(comma a b) joins with commas but adds no brackets."
:lisp (comma a b)
:rust "a, b"
:tags (:collection))
(:name "semicolon"
:description "(semicolon a b) joins with semicolons."
:lisp (semicolon a b)
:rust "a; b"
:tags (:collection))
(:name "space"
:description "(space a b) joins with blanks; the generic way to build
Rust syntax the generator has no dedicated form for."
:lisp (space pub (defstruct0 P (x f64)))
:rust "pub struct P { x: f64, }"
:tags (:collection))
(:name "aref"
:description "(aref a i) indexes."
:lisp (aref arr 0)
:rust "arr[0]"
:tags (:indexing))
(:name "aref-multi"
:description "Several indices are separated by commas (for types whose
Index impl takes a tuple-like argument)."
:lisp (aref arr i j)
:rust "arr[i,j]"
:tags (:indexing))
(:name "range"
:description "(range a b) emits the end-exclusive Rust range a..b."
:lisp (range 0 n)
:rust "(0..n)"
:tags (:indexing))
(:name "range-inclusive"
:description "(range-inclusive a b) emits the end-inclusive range a..=b."
:lisp (range-inclusive 0 n)
:rust "(0..=n)"
:tags (:indexing))
(:name "range-from"
:description "(range-from a) emits the open-ended range a.. ."
:lisp (range-from 2)
:rust "(2..)"
:tags (:indexing))
(:name "range-to"
:description "(range-to b) emits the left-open range ..b."
:lisp (range-to 5)
:rust "(..5)"
:tags (:indexing))
(:name "range-to-inclusive"
:description "(range-to-inclusive b) emits ..=b."
:lisp (range-to-inclusive 5)
:rust "(..=5)"
:tags (:indexing))
(:name "range-full"
:description "(range-full) emits the full range .. ."
:lisp (range-full)
:rust "(..)"
:tags (:indexing))
(:name "range-in-for"
:description "A range used as a for collection loses its redundant
parentheses."
:lisp (for (x (range 0 n)) (f x))
:rust "for x in 0..n { f(x) }"
:tags (:indexing))
(:name "dot"
:description "(dot a b c) chains field accesses and method calls."
:lisp (dot v (iter) (map f) (collect))
:rust "v.iter().map(f).collect()"
:tags (:accessor))
(:name "dot-tuple-field"
:description "A numeric field access is Rust's tuple field access."
:lisp (dot pair 0)
:rust "pair.0"
:tags (:accessor))
;; ---------------- control flow ----------------
(:name "if-then"
:description "(if c then) emits an if with a block. The redundant
parentheses that the comparison operators add are stripped from the condition so
that rustc's unused_parens lint stays quiet."
:lisp (if (< x 0) (return 0))
:rust "if (x)<(0) { return 0 }"
:tags (:control-flow))
(:name "if-then-else"
:description "(if c then else) emits both branches."
:lisp (if (< x 0) (return 0) (return 1))
:rust "if (x)<(0) { return 0 } else { return 1 }"
:tags (:control-flow))
(:name "when"
:description "(when c form*) is an if without an else branch that accepts
several body forms."
:lisp (when (< x 0) (f) (g))
:rust "if (x)<(0) { f(); g() }"
:tags (:control-flow))
(:name "unless"
:description "(unless c form*) negates the condition."
:lisp (unless x (f))
:rust "if !x { f() }"
:tags (:control-flow))
(:name "if-single-let"
:description "A branch holding a single let needs no extra block: the
let block becomes the branch block instead of nesting inside another one."
:lisp (if c (let ((x 1)) (f x)) (g))
:rust "if c { let x = 1; f(x) } else { g() }"
:tags (:control-flow))
(:name "when-single-let"
:description "Same splicing for when: (when c (let ...)) emits one block,
not a block in a block."
:lisp (when c (let ((x 1)) (f x)))
:rust "if c { let x = 1; f(x) }"
:tags (:control-flow))
(:name "if-single-progn"
:description "A single explicit progn as a branch is spliced the same
way, so hand-grouped statements do not nest either."
:lisp (if c (progn (setf a 1) (setf b 2)))
:rust "if c { a=1; b=2; }"
:tags (:control-flow))
(:name "loop-keeps-inner-let-block"
:description "Only singleton branch bodies splice. A let that shares its
body with other statements keeps its own block, so its bindings stay scoped."
:lisp (loop (f) (let ((x 1)) (g x)))
:rust "loop { f(); { let x = 1; g(x) } }"
:tags (:control-flow))
(:name "match-single-let-arm"
:description "Match arms splice a singleton let the same way branches do."
:lisp (case x
(1 (let ((a 1)) (f a)))
(t (g)))
:rust "match x { 1 => { let a = 1; f(a) }, _ => { g() }, }"
:tags (:control-flow))
(:name "if-let"
:description "(if-let (pattern scrutinee) then else) emits Rust's if
let. A list pattern such as (Some x) emits the tuple-struct pattern
Some(x); None and _ work as written."
:lisp (if-let ((Some x) y) (return x) (return 0))
:rust "if let Some(x) = y { return x } else { return 0 }"
:tags (:control-flow))
(:name "if-let-no-else"
:description "Without an else form if-let emits a bare if let."
:lisp (if-let ((Some x) y) (return x))
:rust "if let Some(x) = y { return x }"
:tags (:control-flow))
(:name "while-let"
:description "(while-let (pattern scrutinee) form*) emits Rust's
while let loop."
:lisp (while-let ((Some x) (dot it (next))) (f x))
:rust "while let Some(x) = it.next() { f(x) }"
:tags (:control-flow))
(:name "let-else"
:description "(let-else (pattern scrutinee) form*) emits Rust's
let-else. The form terminates itself with a semicolon (Rust requires
it), so it is safe in any statement position."
:lisp (let-else ((Some x) y) (return 1))
:rust "let Some(x) = y else { return 1 };"
:tags (:binding))
(:name "let-else-in-do0"
:description "let-else in statement position needs no help: the
following statement still gets its own semicolon and nothing is doubled."
:lisp (do0 (let-else ((Some x) y) (return 1)) (g))
:rust "let Some(x) = y else { return 1 }; g();"
:tags (:binding))
(:name "while"
:description "(while c form*) emits a while loop."
:lisp (while (< i n) (incf i))
:rust "while (i)<(n) { i += 1 }"
:tags (:control-flow))
(:name "loop"
:description "(loop form*) emits Rust's unconditional loop."
:lisp (loop (break))
:rust "loop { break }"
:tags (:control-flow))
(:name "break-with-value"
:description "(break value) breaks out of a loop with a value."
:lisp (loop (break 3))
:rust "loop { break 3 }"
:tags (:control-flow))
(:name "continue"
:description "(continue) emits continue."
:lisp (loop (continue))
:rust "loop { continue }"
:tags (:control-flow))
(:name "for-in"
:description "(for (item collection) form*) emits Rust's for-in loop."
:lisp (for (x v) (f x))
:rust "for x in v { f(x) }"
:tags (:control-flow))
(:name "dotimes"
:description "(dotimes (i n) form*) iterates over the range 0..n."
:lisp (dotimes (i 4) (f i))
:rust "for i in 0..4 { f(i) }"
:tags (:control-flow))
(:name "dotimes-step"
:description "A third element in the dotimes head becomes step_by."
:lisp (dotimes (i 10 2) (f i))
:rust "for i in (0..10).step_by(2) { f(i) }"
:tags (:control-flow))
(:name "match"
:description "(case keyform (key form*) ... (t form*)) emits a Rust match.
A key of t becomes the wildcard arm _."
:lisp (case x
(1 (f))
(t (g)))
:rust "match x { 1 => { f() }, _ => { g() }, }"
:tags (:control-flow))
(:name "match-pattern"
:description "A list key is emitted as a pattern, so enum variants with
bindings work."
:lisp (case r
((Ok v) v)
((Err e) (return (Err e))))
:rust "match r { Ok(v) => { v }, Err(e) => { return Err(e) }, }"
:tags (:control-flow))
(:name "match-arm-statements"
:description "Regression test: several forms in one match arm must be
separated by semicolons."
:lisp (case x
(1 (f) (g)))
:rust "match x { 1 => { f(); g() }, }"
:tags (:control-flow))
(:name "return"
:description "(return x) returns from a function. Redundant enclosing
parentheses are removed so that rustc's unused_parens lint stays quiet."
:lisp (return 0)
:rust "return 0"
:tags (:control-flow))
(:name "return-expression-unwrapped"
:description "The parentheses that the arithmetic forms add around their
result are stripped from a return value."
:lisp (return (+ a b))
:rust "return a+b"
:tags (:control-flow :precedence))
(:name "return-tuple-kept"
:description "Regression test for the paren stripping: a tuple must keep
its parentheses, so the stripping bails out on a top level comma."
:lisp (return (paren a b))
:rust "return (a, b)"
:tags (:control-flow :precedence))
(:name "assign-expression-unwrapped"
:description "Same stripping on the right hand side of an assignment."
:lisp (setf m (% m n))
:rust "m=(m)%(n);"
:tags (:control-flow :precedence))
(:name "assign-tuple-kept"
:description "A tuple on the right hand side keeps its parentheses."
:lisp (setf m (paren a b))
:rust "m=(a, b);"
:tags (:control-flow :precedence))
(:name "match-tuple-kept"
:description "Matching on a tuple keeps the parentheses."
:lisp (case (paren a b)
((paren 1 2) (f)))
:rust "match (a, b) { (1, 2) => { f() }, }"
:tags (:control-flow :precedence))
;; ---------------- blocks ----------------
(:name "do0"
:description "(do0 form*) emits one form per line at the current
indentation and terminates each with a semicolon where needed."
:lisp (do0 (f) (g))
:rust "f(); g();"
:tags (:block))
(:name "progn"
:description "(progn form*) is do0 in braces, but without a semicolon after
the last form, so the block has the value of that form (Rust's implicit return)."
:lisp (progn (f) (g))
:rust "{ f(); g() }"
:tags (:block))
(:name "block"
:description "(block form*) is like progn but terminates the last form too,
so the block evaluates to ()."
:lisp (block (f) (g))
:rust "{ f(); g(); }"
:tags (:block))
(:name "unsafe"
:description "(unsafe form*) emits an unsafe block."
:lisp (unsafe (f))
:rust "unsafe { f() }"
:tags (:block))
(:name "extern-block"
:description "(extern form*) emits an extern block."
:lisp (extern "fn puts(s: *const u8) -> i32;")
:rust "extern { fn puts(s: *const u8) -> i32; }"
:tags (:block))
(:name "do"
:description "(do form*) sequences statements like do0 but indented one
level deeper; it is what function and block bodies are built from."
:lisp (do (f) (g))
:rust "f(); g();"
:tags (:block))
(:name "stmt"
:description "(stmt form) forces statement termination with a semicolon.
It is the explicit override for escape-hatch forms such as (space ...)
in statement position, where the semicolon heuristic cannot know whether
the expansion is a statement or an expression."
:lisp (do0 (stmt (space foo bar)) (g))
:rust "foo bar; g();"
:tags (:block))
(:name "expr"
:description "(expr form) is the counterpart of stmt: it forces
expression position, i.e. no semicolon is added. Use it for the tail