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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
//! Implementation of the evaluator.
//!
//! Source code inside [`rukt`](crate::rukt) blocks is evaluated and expanded
//! entirely using declarative macros.
//!
//! The evaluator is a simple [TT
//! muncher](https://veykril.github.io/tlborm/decl-macros/patterns/tt-muncher.html)
//! with a continuation stack that can handle a rich subset of Rust syntax.
//!
//! # Calling convention
//!
//! All macros invoked by the Rukt evaluator follow a unified calling convention
//! that encodes the complete evaluation state.
//!
//! ```
//! macro_rules! example {
//! ($T:tt $S:tt $N:tt $P:tt $V:tt $D:tt) => {
//! };
//! }
//! ```
//!
//! For brevity, macros invoked by the evaluator use a conventional
//! single-letter metavariable name to bind each specific fragment.
//!
//! The `$D:tt` metavariable at the very end is not part of the evaluation
//! state. It's always bound to the dollar-sign token `$`, which can be useful
//! for generating intermediate `macro_rules` definitions.
//!
//! All macros expand to a call to a continuation. This can be a predetermined
//! continuation or the [next dynamic continuation](#next-continuation).
//!
//! ```
//! macro_rules! example {
//! ($T:tt $S:tt $N:tt $P:tt $V:tt $D:tt) => {
//! continuation!($T $S $N $P $V $);
//! };
//! }
//! ```
//!
//! ## Remaining tokens
//!
//! The `$T:tt` metavariable matches the source tokens that we still need to
//! evaluate, enclosed in braces `{}`.
//!
//! Macros responsible for parsing the input will consume tokens then pass the
//! remainder to the next continuation unmodified.
//!
//! ```
//! macro_rules! example {
//! ({ let $L:tt = $($T:tt)* } $S:tt $N:tt $P:tt $V:tt $D:tt) => {
//! continuation!({ $($T)* } () $N $P $V $);
//! };
//! }
//! ```
//!
//! ## Current subject
//!
//! The `$S:tt` metavariable matches the token corresponding to the last
//! evaluated expression.
//!
//! It's essentially an accumulator that individual macros are free to consume
//! or ignore depending on the specific context.
//!
//! When a macro decides to discard the current subject it should invoke the
//! next continuation with the unit token `()`.
//!
//! ```
//! macro_rules! example {
//! ($T:tt $S:tt $N:tt $P:tt $V:tt $D:tt) => {
//! continuation!($T () $N $P $V $);
//! };
//! }
//! ```
//!
//! ## Next continuation
//!
//! The `$N:tt` metavariable matches the next dynamic continuation.
//!
//! After evaluating part of an expression, most macros will need to invoke the
//! next continuation to dispatch depending on the previous caller.
//!
//! ```
//! macro_rules! example {
//! ($T:tt $S:tt ($F:path; $($C:tt)*) $P:tt $V:tt $D:tt) => {
//! $F!($T "hello" $($C)* $P $V $);
//! };
//! }
//! ```
//!
//! The pattern for destructuring the continuation is `($F:path; $($C:tt)*)`,
//! where `$F:path` matches an arbitrary Rust path to a declarative macro that
//! follows the calling convention of the evaluator, and `$($C:tt)*` matches
//! additional context information that was saved when the continuation was
//! pushed.
//!
//! The context information `$C` includes the previous continuation. As such, it
//! must be forwarded after the current subject `$S` and before the execution
//! environment patterns `$P`, which is where the next macro will be expecting
//! to receive its next continuation.
//!
//! When expecting a sub-expression as part of a larger construct, pushing a
//! continuation makes it so that the evaluator can call you back once the
//! sub-expressions is evaluated.
//!
//! ```
//! macro_rules! example {
//! ({ let $L:tt = $($T:tt)* } $S:tt $N:tt $P:tt $V:tt $D:tt) => {
//! expression!({ $($T)* } () (let_binding; $L $N) $P $V $);
//! };
//! }
//! ```
//! ```
//! macro_rules! let_binding {
//! ({ ; $($T:tt)* } $S:tt $I:ident $N:tt [$($P:tt)*] [$($V:tt)*] $D:tt) => {
//! block!({ $($T)* } () $N [$($P)* $D$I:tt] [$($V)* $S] $);
//! };
//! }
//! ```
//!
//! ## Environment
//!
//! The `$P:tt` and `$V:tt` metavariables represent the current execution
//! environment. The execution environment defines the variables accessible in
//! the current scope and their respective values.
//!
//! The `$P:tt` metavariable matches every variable's corresponding pattern,
//! enclosed in brackets `[]`.
//!
//! The `$V:tt` metavariable matches every variable's associated value, enclosed
//! in brackets `[]`.
//!
//! You can define a new variable in the current scope by pushing a pattern and
//! its matching value when calling the next continuation.
//!
//! ```
//! macro_rules! example {
//! ($T:tt $S:tt $N:tt [$($P:tt)*] [$($V:tt)*] $D:tt) => {
//! continuation!($T () $N [$($P)* $message:tt] [$($V)* "hello"] $);
//! };
//! }
//! ```
//!
//! To substitute variables defined in the current scope, you can generate and
//! expand an intermediate macro.
//!
//! ```
//! macro_rules! example {
//! ($T:tt $S:tt $N:tt$P:tt $V:tt $D:tt) => {
//! macro_rules! __transcribe {
//! ($P $TT:tt $NN:tt $PP:tt $VV:tt) => {
//! continuation!($TT $S $NN $PP $VV $);
//! };
//! }
//! __transcribe!($V $T $N $P $V);
//! };
//! }
//! ```
//!
//! By pasting the environment patterns into the signature of the generated
//! macro and matching them with the associated environment values, the expanded
//! metavariables will bind all the accessible local variables.
//!
//! Make sure to forward the rest of the evaluation state through intermediate
//! metavariables passed to the generated macro. In this case for example,
//! variable substitution should only occur within the current subject `$S`
//! before passing it to the next continuation.
;
=> ;
=> ;
=> ;
=> ;
=> ;
=> ;
=> ;
=> ;
=>
}
;
}
/// Evaluate statements within blocks.
///
/// Rukt blocks can contain the following statements:
///
/// - [Expression statements](#expression-statements)
/// - [Let bindings](#let-bindings)
/// - [Expand statements](#expand-statements)
/// - [If statements](#if-statements)
/// - [Function definitions](#function-definitions)
/// - [Exports](#exports)
/// - [Imports](#imports)
/// - [Function exports](#function-exports)
///
/// # Expression statements
///
/// Just like in Rust, expressions can appear as top-level statements, followed
/// by a semicolon `;`.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// 42;
/// ["hello", "world"];
/// }
/// ```
///
/// When the current block is evaluated in an expression context, the last
/// expression in the block will be used as the result of the overall block if
/// the expression is not followed by a semicolon `;`.
///
/// # Let bindings
///
/// They mirror Rust's own `let` bindings. They allow you bind the result of an
/// [`expression`] to a variable.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// let message = "hello";
/// }
/// ```
///
/// Note that unlike in Rust, you can't implicitly shadow a previous variable
/// with the same name.
///
/// ```compile_fail
/// # use rukt::rukt;
/// rukt! {
/// let message = "hello";
/// let message = "world"; // error: duplicate matcher binding
/// let _ = message;
/// }
/// ```
///
/// There's also no `let mut`, all variables are immutable.
///
/// Using an underscore `_` as the variable name will explicitly discard the
/// result of the expression.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// let _ = "hello";
/// }
/// ```
///
/// The left side can also be a pattern for destructuring the value specified as
/// a delimiter-enclosed
/// [`macro_rules`](https://doc.rust-lang.org/reference/macros-by-example.html)
/// matchers. This is particularly useful for binding [repeated
/// fragments](https://veykril.github.io/tlborm/decl-macros/macros-methodical.html#repetitions).
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// let {$($name:ident: $operator:tt,)*} = {
/// add: +,
/// sub: -,
/// mul: *,
/// div: /,
/// };
/// }
/// ```
///
/// Note that depending on the fragment specifier you might not be able to
/// inspect the tokens further. You can usually stick to `tt` and `ident`. See
/// [forwarding a matched
/// fragment](https://doc.rust-lang.org/stable/reference/macros-by-example.html#forwarding-a-matched-fragment).
///
/// # Expand statements
///
/// The `expand` statement will substitute all variables accessible in the
/// current scope in the given code block, and paste the resulting Rust code as
/// part of the expansion of the [`rukt`](crate::rukt) macro. The expansion
/// doesn't include the braces `{}` used to delimit the code block.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// let message = "Expanded from Rukt!";
/// expand {
/// fn example() -> &'static str {
/// $message
/// }
/// }
/// }
/// # assert_eq!(example(), "Expanded from Rukt!");
/// ```
///
/// Variable substitutions in the code block rely on the standard `$variable`
/// syntax handled by
/// [`macro_rules`](https://doc.rust-lang.org/reference/macros-by-example.html#metavariables).
///
/// # If statements
///
/// They're exactly the same as Rust's own `if` statements. You can use `if`
/// statements to evaluate Rukt code conditionally.
///
/// ```compile_fail
/// # use rukt::rukt;
/// rukt! {
/// let value = 0;
/// if value == 0 {
/// expand {
/// compile_error!("invalid"); // error: invalid
/// }
/// }
/// }
/// ```
///
/// You can also use `if` statements in
/// [expression](crate::eval::expression#if-expressions) contexts as long as
/// they specify an explicit `else` branch.
///
/// # Function definitions
///
/// Just like in regular Rust, you can define functions with the `fn` keyword.
/// Rukt functions use `macro_rules` patterns to match their arguments.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// fn hello($name:literal) {
/// let message = { concat!("hello ", $name) };
/// message
/// }
/// let result = hello("world");
/// expand {
/// assert_eq!($result, "hello world");
/// }
/// }
/// ```
///
/// The last expression in the body will be returned if it's not followed by a
/// semicolon `;`. Otherwise, the function will return the unit token `()` by
/// default.
///
/// Rukt functions can be passed around as values and as arguments to other
/// functions. Recursion is supported.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// fn apply($f:tt $($args:tt)*) {
/// f($($args)*)
/// }
/// fn twice($arg:tt) {
/// [$arg, $arg]
/// }
/// let result = apply($twice 42);
/// expand {
/// assert_eq!($result, [42, 42]);
/// }
/// }
/// ```
///
/// Additionally, functions will capture all the variables currently in scope at
/// their definition.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// let a = 1;
/// fn get_function($b:tt) {
/// let c = 3;
/// fn f() {
/// [$a, $b, $c]
/// }
/// f
/// }
/// let result = get_function(2)();
/// expand {
/// assert_eq!($result, [1, 2, 3]);
/// }
/// }
/// ```
///
/// # Exports
///
/// By default, none of the variables created during the expansion of a
/// [`rukt`](crate::rukt) block will be visible to the outside.
///
/// You can use the `pub` keyword with the `#[macro_export]` attribute to export
/// variables and make them accessible from [`rukt`](crate::rukt) blocks in
/// other crates.
///
/// ```
/// # use rukt::rukt;
/// // my_crate/src/lib.rs
/// rukt! {
/// #[macro_export]
/// pub let values = {
/// A: 1,
/// B: 2,
/// C: 3,
/// };
/// }
/// ```
/// ```
/// # mod my_crate {
/// # use rukt::rukt;
/// # rukt! {
/// # pub(crate) let values = {
/// # A: 1,
/// # B: 2,
/// # C: 3,
/// # };
/// # }
/// # }
/// # use rukt::rukt;
/// rukt! {
/// let {$($name:ident: $value:expr,)*} = my_crate::values;
/// expand {
/// enum MyEnum {
/// $($name = $value,)*
/// }
/// }
/// }
/// assert_eq!(MyEnum::A as u32, 1);
/// assert_eq!(MyEnum::B as u32, 2);
/// assert_eq!(MyEnum::C as u32, 3);
/// ```
///
/// In addition to binding the variable in the current scope, the `let`
/// statement will generate a [`builtin`](crate::builtins) that resolves to the
/// assigned value.
///
/// You can make the variable accessible only to other [`rukt`](crate::rukt)
/// blocks in your own crate with the usual `pub(...)` variants. Of course when
/// the variable is not meant to be visible to other crates there's no need for
/// `#[macro_export]`.
///
/// In regular Rust, `pub(self)` is equivalent to not using `pub` in the first
/// place. In Rukt it can be used to signal that you want to export the variable
/// as a builtin without extending its visibility beyond the current Rust scope.
/// As mentioned earlier, Rukt variables are not exported by default, there's no
/// trace of them in the surrounding Rust code unless you use the `pub` keyword.
///
/// Exported variables can also be used directly as macros in the surrounding
/// Rust code.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// pub(self) let numbers = [1, 2, 3];
/// }
/// assert_eq!(numbers!(), [1, 2, 3]);
/// ```
///
/// # Imports
///
/// Rukt supports `use` statements as an alternative to `let` bindings for
/// bringing exported variables into scope.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// pub(crate) let numbers = [1, 2, 3];
/// }
/// rukt! {
/// use numbers;
/// expand {
/// assert_eq!($numbers, [1, 2, 3]);
/// }
/// }
/// ```
///
/// While you can refer to exported variables by path in
/// [expressions](expression), they must be brought into scope within the
/// [`rukt`](crate::rukt) code block to allow substitution in token trees.
///
/// ```compile_fail
/// # use rukt::rukt;
/// rukt! {
/// pub(crate) let numbers = [1, 2, 3];
/// }
/// rukt! {
/// expand {
/// assert_eq!($numbers, [1, 2, 3]); // error: no rules expected the token `$`
/// }
/// }
/// ```
///
/// Rukt `use` statements also support the `as` keyword for bringing exported
/// variables into scope under a different name.
///
/// ```
/// # mod path {
/// # pub mod to {
/// # use rukt::rukt;
/// # rukt! {
/// # pub(crate) let my_variable = 123;
/// # }
/// # }
/// # }
/// # use rukt::rukt;
/// rukt! {
/// use path::to::my_variable;
/// use path::to::my_variable as alias;
/// }
/// ```
///
/// Note that both variants of the `use` statement are nothing more than a
/// restricted version of `let` which only allow binding exported variables.
/// They're functionally completely equivalent. Rukt `use` statements simply
/// make it easier to identify imports at first glance.
///
/// ```
/// # mod path {
/// # pub mod to {
/// # use rukt::rukt;
/// # rukt! {
/// # pub(crate) let my_variable = 123;
/// # }
/// # }
/// # }
/// # use rukt::rukt;
/// rukt! {
/// let my_variable = path::to::my_variable;
/// let alias = path::to::my_variable;
/// }
/// ```
///
/// # Function exports
///
/// Just like variables, you can export functions with the `pub` keyword.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// pub(self) fn foo() {
/// "hello"
/// }
/// }
/// rukt! {
/// let result = foo();
/// expand {
/// assert_eq!($result, "hello");
/// }
/// }
/// ```
///
/// To make the function accessible outside the crate, make sure to use the
/// `#[macro_export]` attribute.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// #[macro_export]
/// pub fn foo() {
/// "hello"
/// }
/// }
/// ```
///
/// Note that exported functions can also be invoked directly as macros outside
/// of [`rukt`](crate::rukt) blocks.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// pub(crate) fn generate_message($name:literal) {
/// expand {
/// const MESSAGE: &str = concat!("hello ", $name);
/// }
/// }
/// }
/// generate_message!("world");
/// assert_eq!(MESSAGE, "hello world");
/// ```
///
/// However, outside of [`rukt`](crate::rukt) blocks, expanding exported
/// functions in expression contexts is currently not supported.
pub use eval_block as block;
;
=> ;
=> ;
=>
__rukt_transcribe!;
};
=>
__rukt_transcribe!;
};
=>
__rukt_transcribe!;
};
=> ;
=> ;
}
;
=> ;
}
/// Evaluate expression.
///
/// Rukt expressions support the following:
///
/// - [Literals](#literals)
/// - [Variables](#variables)
/// - [Builtins](crate::builtins)
/// - [Operators](operator)
/// - [If expressions](#if-expressions)
///
/// # Literals
///
/// With the exception of identifiers, every Rust token is a literal when used
/// as part of an expression in Rukt.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// let number = 42;
/// let string = "hello";
/// let boolean = true;
/// let operator = +;
/// let separator = ::;
/// let punctuation = .;
/// }
/// ```
///
/// Note that this includes `true` and `false` which are normally tokenized as
/// identifiers by Rust.
///
/// Literals can also be entire token trees enclosed in parenthesis `()`,
/// brackets `[]`, or braces `{}`. Variables accessible in the current scope are
/// expanded inside the token tree.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// let taco = {
/// chat => bouc,
/// cheese => pizza,
/// };
/// let arbitrary = {
/// [1 2 3]
/// $taco
/// };
/// expand {
/// assert_eq!(stringify!($arbitrary), "{ [1 2 3] { chat => bouc, cheese => pizza, } }");
/// }
/// }
/// ```
///
/// Just like in regular
/// [`macro_rules`](https://doc.rust-lang.org/reference/macros-by-example.html),
/// token trees can contain pretty much arbitrary syntax.
///
/// Variable substitutions in delimiter-enclosed token tree literals rely on the
/// standard `$variable` syntax handled by
/// [`macro_rules`](https://doc.rust-lang.org/reference/macros-by-example.html#metavariables).
///
/// # Variables
///
/// Identifiers inside Rukt expressions refer to previously defined variables.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// let value = 123;
/// let number = value;
/// }
/// ```
///
/// If the identifier doesn't match any variable accessible in the current
/// scope, the evaluator will try to fall back to any available
/// [`builtins`](crate::builtins) before failing to compile.
///
/// ```compile_fail
/// # use rukt::rukt;
/// rukt! {
/// let number = value; // error: cannot find macro `value` in this scope
/// }
/// ```
///
/// If you want to store an identifier token in a variable you'll have to
/// extract it from a token tree, for example using `let` destructuring.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// let ($name:ident) = (VALUE);
/// expand {
/// const $name: u32 = 123;
/// }
/// }
/// assert_eq!(VALUE, 123);
/// ```
///
/// # If expressions
///
/// You can use `if` expressions to conditionally evaluate nested blocks.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// let value = "b";
/// let result = if value == "a" {
/// 1
/// } else if value == "b" {
/// 2
/// } else {
/// 3
/// };
/// expand {
/// assert_eq!($result, 2);
/// }
/// }
/// ```
///
/// Note that unlike in regular Rust, the condition of `else if` clauses will
/// always be eagerly evaluated, even when the branch to take has already been
/// decided.
pub use eval_expression as expression;
$S:tt $O:tt $N:tt $P:tt $V:tt $D:tt) => ;
// builtin
=> ;
// ! operator
=> ;
// comparison operators
=> ;
=> ;
=> ;
=> ;
// boolean operators
=> ;
=> ;
=> ;
=> ;
// nothing
=> ;
}
}
$A:tt
$N:tt
$P:tt
$V:tt
$D:tt
) =>
}
;
=> ;
}
;
=> ;
=> ;
=> ;
}
;
=> ;
=> ;
=> ;
}
/// Evaluate operator.
///
/// Rukt supports the following operators:
///
/// - [Comparison operators](#comparison-operators)
/// - [Boolean operators](#boolean-operators)
/// - [Function calls](#function-calls)
/// - [Builtin operators](#builtin-operators)
///
/// # Comparison operators
///
/// You can use `==` and `!=` for comparing tokens.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// let value = 42;
/// let equal = value == 42;
/// let not_equal = equal != false;
/// expand {
/// assert_eq!($equal, true);
/// assert_eq!($not_equal, true);
/// }
/// }
/// ```
///
/// # Boolean operators
///
/// You can use the typical `!`, `&&`, and `||` boolean operators.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// let a = !true;
/// let b = !false;
/// expand {
/// assert_eq!([$a, $b], [false, true]);
/// }
/// }
/// rukt! {
/// let a = true && true;
/// let b = true && false;
/// let c = false && true;
/// let d = false && false;
/// expand {
/// assert_eq!([$a, $b, $c, $d], [true, false, false, false]);
/// }
/// }
/// rukt! {
/// let a = true || true;
/// let b = true || false;
/// let c = false || true;
/// let d = false || false;
/// expand {
/// assert_eq!([$a, $b, $c, $d], [true, true, true, false]);
/// }
/// }
/// ```
///
/// These operators will fail to compile when used with tokens other than `true`
/// and `false`.
///
/// ```compile_fail
/// # use rukt::rukt;
/// rukt! {
/// let value = 42;
/// let _ = true && value; // error: no rules expected the token `42`
/// }
/// ```
///
/// Note that unlike in regular Rust, the right-side of `&&` and `||` is not
/// lazy and will always be evaluated eagerly.
///
/// # Function calls
///
/// You can call Rukt [functions](block#function-definitions) by supplying arguments enclosed in parentheses
/// `()`. Variables defined in the current scope will be substituted before
/// passing the arguments.
///
/// ```
/// # use rukt::rukt;
/// rukt! {
/// fn generate_constants($($name:ident: $type:ty = $value:expr),*) {
/// expand {
/// $(
/// const $name: $type = $value;
/// )*
/// }
/// }
/// let value = { 1 + 2 };
/// generate_constants(FOO: &str = "hello", BAR: u32 = $value);
/// }
/// assert_eq!(FOO, "hello");
/// assert_eq!(BAR, 3);
/// ```
///
/// # Builtin operators
///
/// You can apply [`builtins`](crate::builtins) to any value using Rust's
/// standard field/method syntax.
///
/// ```
/// # use rukt::rukt;
/// use rukt::builtins::starts_with;
/// rukt! {
/// let result = [1 2 3].starts_with(1 2);
/// expand {
/// assert_eq!($result, true);
/// }
/// }
/// ```
pub use eval_operator as operator;
;
}
/// Resume evaluation of the parent block.
pub use eval_parent as parent;
/// End evaluation.
pub use eval_stop as stop;
}
/// Helper accepting tokens for the current subject as first argument.
pub use eval_unwrap as unwrap;