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
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum OpType {
Operator,
Function,
}
use self::OpType::*;
struct Op {
token: &'static str,
operator: &'static str,
example: (&'static str, &'static str),
precedence: u8,
n_args: u8,
op_type: OpType,
}
pub fn write_op_macro() -> ::std::io::Result<()> {
let out_dir = ::std::env::var("OUT_DIR").unwrap();
let dest = ::std::path::Path::new(&out_dir).join("op.rs");
#[cfg(not(feature = "force_unix_path_separator"))]
println!("cargo:rustc-env=TYPENUM_BUILD_OP={}", dest.display());
let mut f = ::std::fs::File::create(&dest).unwrap();
// Operator precedence is taken from
// https://doc.rust-lang.org/reference.html#operator-precedence
//
// We choose 16 as the highest precedence (functions are set to 255 but it doesn't matter
// for them). We also only use operators that are left associative so we don't have to worry
// about that.
let ops = &[
Op {
token: "*",
operator: "Prod",
example: ("P2 * P3", "P6"),
precedence: 16,
n_args: 2,
op_type: Operator,
},
Op {
token: "/",
operator: "Quot",
example: ("P6 / P2", "P3"),
precedence: 16,
n_args: 2,
op_type: Operator,
},
Op {
token: "%",
operator: "Mod",
example: ("P5 % P3", "P2"),
precedence: 16,
n_args: 2,
op_type: Operator,
},
Op {
token: "+",
operator: "Sum",
example: ("P2 + P3", "P5"),
precedence: 15,
n_args: 2,
op_type: Operator,
},
Op {
token: "-",
operator: "Diff",
example: ("P2 - P3", "N1"),
precedence: 15,
n_args: 2,
op_type: Operator,
},
Op {
token: "<<",
operator: "Shleft",
example: ("U1 << U5", "U32"),
precedence: 14,
n_args: 2,
op_type: Operator,
},
Op {
token: ">>",
operator: "Shright",
example: ("U32 >> U5", "U1"),
precedence: 14,
n_args: 2,
op_type: Operator,
},
Op {
token: "&",
operator: "And",
example: ("U5 & U3", "U1"),
precedence: 13,
n_args: 2,
op_type: Operator,
},
Op {
token: "^",
operator: "Xor",
example: ("U5 ^ U3", "U6"),
precedence: 12,
n_args: 2,
op_type: Operator,
},
Op {
token: "|",
operator: "Or",
example: ("U5 | U3", "U7"),
precedence: 11,
n_args: 2,
op_type: Operator,
},
Op {
token: "==",
operator: "Eq",
example: ("P5 == P3 + P2", "True"),
precedence: 10,
n_args: 2,
op_type: Operator,
},
Op {
token: "!=",
operator: "NotEq",
example: ("P5 != P3 + P2", "False"),
precedence: 10,
n_args: 2,
op_type: Operator,
},
Op {
token: "<=",
operator: "LeEq",
example: ("P6 <= P3 + P2", "False"),
precedence: 10,
n_args: 2,
op_type: Operator,
},
Op {
token: ">=",
operator: "GrEq",
example: ("P6 >= P3 + P2", "True"),
precedence: 10,
n_args: 2,
op_type: Operator,
},
Op {
token: "<",
operator: "Le",
example: ("P4 < P3 + P2", "True"),
precedence: 10,
n_args: 2,
op_type: Operator,
},
Op {
token: ">",
operator: "Gr",
example: ("P5 < P3 + P2", "False"),
precedence: 10,
n_args: 2,
op_type: Operator,
},
Op {
token: "cmp",
operator: "Compare",
example: ("cmp(P2, P3)", "Less"),
precedence: !0,
n_args: 2,
op_type: Function,
},
Op {
token: "sqr",
operator: "Square",
example: ("sqr(P2)", "P4"),
precedence: !0,
n_args: 1,
op_type: Function,
},
Op {
token: "sqrt",
operator: "Sqrt",
example: ("sqrt(U9)", "U3"),
precedence: !0,
n_args: 1,
op_type: Function,
},
Op {
token: "abs",
operator: "AbsVal",
example: ("abs(N2)", "P2"),
precedence: !0,
n_args: 1,
op_type: Function,
},
Op {
token: "cube",
operator: "Cube",
example: ("cube(P2)", "P8"),
precedence: !0,
n_args: 1,
op_type: Function,
},
Op {
token: "pow",
operator: "Exp",
example: ("pow(P2, P3)", "P8"),
precedence: !0,
n_args: 2,
op_type: Function,
},
Op {
token: "min",
operator: "Minimum",
example: ("min(P2, P3)", "P2"),
precedence: !0,
n_args: 2,
op_type: Function,
},
Op {
token: "max",
operator: "Maximum",
example: ("max(P2, P3)", "P3"),
precedence: !0,
n_args: 2,
op_type: Function,
},
Op {
token: "log2",
operator: "Log2",
example: ("log2(U9)", "U3"),
precedence: !0,
n_args: 1,
op_type: Function,
},
Op {
token: "gcd",
operator: "Gcf",
example: ("gcd(U9, U21)", "U3"),
precedence: !0,
n_args: 2,
op_type: Function,
},
];
use std::io::Write;
write!(
f,
"
/**
Convenient type operations.
Any types representing values must be able to be expressed as `ident`s. That means they need to be
in scope.
For example, `P5` is okay, but `typenum::P5` is not.
You may combine operators arbitrarily, although doing so excessively may require raising the
recursion limit.
# Example
```rust
#![recursion_limit=\"128\"]
#[macro_use] extern crate typenum;
use typenum::consts::*;
fn main() {{
assert_type!(
op!(min((P1 - P2) * (N3 + N7), P5 * (P3 + P4)) == P10)
);
}}
```
Operators are evaluated based on the operator precedence outlined
[here](https://doc.rust-lang.org/reference.html#operator-precedence).
The full list of supported operators and functions is as follows:
{}
They all expand to type aliases defined in the `operator_aliases` module. Here is an expanded list,
including examples:
",
ops.iter()
.map(|op| format!("`{}`", op.token))
.collect::<Vec<_>>()
.join(", ")
)?;
//write!(f, "Token | Alias | Example\n ===|===|===\n")?;
for op in ops.iter() {
write!(
f,
"---\nOperator `{token}`. Expands to `{operator}`.
```rust
# #[macro_use] extern crate typenum;
# use typenum::*;
# fn main() {{
assert_type_eq!(op!({ex0}), {ex1});
# }}
```\n
",
token = op.token,
operator = op.operator,
ex0 = op.example.0,
ex1 = op.example.1
)?;
}
write!(
f,
"*/
#[macro_export(local_inner_macros)]
macro_rules! op {{
($($tail:tt)*) => ( __op_internal__!($($tail)*) );
}}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! __op_internal__ {{
"
)?;
// We first us the shunting-yard algorithm to produce our tokens in Polish notation.
// See: https://en.wikipedia.org/wiki/Shunting-yard_algorithm
// Note: Due to macro asymmetry, "the top of the stack" refers to the first element, not the
// last
// -----------------------------------------------------------------------------------------
// Stage 1: There are tokens to be read:
// -------
// Case 1: Token is a function => Push it onto the stack:
for fun in ops.iter().filter(|f| f.op_type == Function) {
write!(
f,
"
(@stack[$($stack:ident,)*] @queue[$($queue:ident,)*] @tail: {f_token} $($tail:tt)*) => (
__op_internal__!(@stack[{f_op}, $($stack,)*] @queue[$($queue,)*] @tail: $($tail)*)
);",
f_token = fun.token,
f_op = fun.operator
)?;
}
// -------
// Case 2: Token is a comma => Until the top of the stack is a LParen,
// Pop operators from stack to queue
// Base case: Top of stack is LParen, ditch comma and continue
write!(
f,
"
(@stack[LParen, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail: , $($tail:tt)*) => (
__op_internal__!(@stack[LParen, $($stack,)*] @queue[$($queue,)*] @tail: $($tail)*)
);"
)?;
// Recursive case: Not LParen, pop from stack to queue
write!(
f,
"
(@stack[$stack_top:ident, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail: , $($tail:tt)*) => (
__op_internal__!(@stack[$($stack,)*] @queue[$stack_top, $($queue,)*] @tail: , $($tail)*)
);"
)?;
// -------
// Case 3: Token is an operator, o1:
for o1 in ops.iter().filter(|op| op.op_type == Operator) {
// If top of stack is operator o2 with o1.precedence <= o2.precedence,
// Then pop o2 off stack onto queue:
for o2 in ops
.iter()
.filter(|op| op.op_type == Operator)
.filter(|o2| o1.precedence <= o2.precedence)
{
write!(
f,
"
(@stack[{o2_op}, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail: {o1_token} $($tail:tt)*) => (
__op_internal__!(@stack[$($stack,)*] @queue[{o2_op}, $($queue,)*] @tail: {o1_token} $($tail)*)
);",
o2_op = o2.operator,
o1_token = o1.token
)?;
}
// Base case: push o1 onto stack
write!(
f,
"
(@stack[$($stack:ident,)*] @queue[$($queue:ident,)*] @tail: {o1_token} $($tail:tt)*) => (
__op_internal__!(@stack[{o1_op}, $($stack,)*] @queue[$($queue,)*] @tail: $($tail)*)
);",
o1_op = o1.operator,
o1_token = o1.token
)?;
}
// -------
// Case 4: Token is "(": push it onto stack as "LParen". Also convert the ")" to "RParen" to
// appease the macro gods:
write!(
f,
"
(@stack[$($stack:ident,)*] @queue[$($queue:ident,)*] @tail: ( $($stuff:tt)* ) $($tail:tt)* )
=> (
__op_internal__!(@stack[LParen, $($stack,)*] @queue[$($queue,)*]
@tail: $($stuff)* RParen $($tail)*)
);"
)?;
// -------
// Case 5: Token is "RParen":
// 1. Pop from stack to queue until we see an "LParen",
// 2. Kill the "LParen",
// 3. If the top of the stack is a function, pop it onto the queue
// 2. Base case:
write!(
f,
"
(@stack[LParen, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail: RParen $($tail:tt)*) => (
__op_internal__!(@rp3 @stack[$($stack,)*] @queue[$($queue,)*] @tail: $($tail)*)
);"
)?;
// 1. Recursive case:
write!(
f,
"
(@stack[$stack_top:ident, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail: RParen $($tail:tt)*)
=> (
__op_internal__!(@stack[$($stack,)*] @queue[$stack_top, $($queue,)*] @tail: RParen $($tail)*)
);"
)?;
// 3. Check for function:
for fun in ops.iter().filter(|f| f.op_type == Function) {
write!(
f,
"
(@rp3 @stack[{fun_op}, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail: $($tail:tt)*) => (
__op_internal__!(@stack[$($stack,)*] @queue[{fun_op}, $($queue,)*] @tail: $($tail)*)
);",
fun_op = fun.operator
)?;
}
// 3. If no function found:
write!(
f,
"
(@rp3 @stack[$($stack:ident,)*] @queue[$($queue:ident,)*] @tail: $($tail:tt)*) => (
__op_internal__!(@stack[$($stack,)*] @queue[$($queue,)*] @tail: $($tail)*)
);"
)?;
// -------
// Case 6: Token is a number: Push it onto the queue
write!(
f,
"
(@stack[$($stack:ident,)*] @queue[$($queue:ident,)*] @tail: $num:ident $($tail:tt)*) => (
__op_internal__!(@stack[$($stack,)*] @queue[$num, $($queue,)*] @tail: $($tail)*)
);"
)?;
// -------
// Case 7: Out of tokens:
// Base case: Stack empty: Start evaluating
write!(
f,
"
(@stack[] @queue[$($queue:ident,)*] @tail: ) => (
__op_internal__!(@reverse[] @input: $($queue,)*)
);"
)?;
// Recursive case: Pop stack to queue
write!(
f,
"
(@stack[$stack_top:ident, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail:) => (
__op_internal__!(@stack[$($stack,)*] @queue[$stack_top, $($queue,)*] @tail: )
);"
)?;
// -----------------------------------------------------------------------------------------
// Stage 2: Reverse so we have RPN
write!(
f,
"
(@reverse[$($revved:ident,)*] @input: $head:ident, $($tail:ident,)* ) => (
__op_internal__!(@reverse[$head, $($revved,)*] @input: $($tail,)*)
);"
)?;
write!(
f,
"
(@reverse[$($revved:ident,)*] @input: ) => (
__op_internal__!(@eval @stack[] @input[$($revved,)*])
);"
)?;
// -----------------------------------------------------------------------------------------
// Stage 3: Evaluate in Reverse Polish Notation
// Operators / Operators with 2 args:
for op in ops.iter().filter(|op| op.n_args == 2) {
// Note: We have to switch $a and $b here, otherwise non-commutative functions are backwards
write!(
f,
"
(@eval @stack[$a:ty, $b:ty, $($stack:ty,)*] @input[{op}, $($tail:ident,)*]) => (
__op_internal__!(@eval @stack[$crate::{op}<$b, $a>, $($stack,)*] @input[$($tail,)*])
);",
op = op.operator
)?;
}
// Operators with 1 arg:
for op in ops.iter().filter(|op| op.n_args == 1) {
write!(
f,
"
(@eval @stack[$a:ty, $($stack:ty,)*] @input[{op}, $($tail:ident,)*]) => (
__op_internal__!(@eval @stack[$crate::{op}<$a>, $($stack,)*] @input[$($tail,)*])
);",
op = op.operator
)?;
}
// Wasn't a function or operator, so must be a value => push onto stack
write!(
f,
"
(@eval @stack[$($stack:ty,)*] @input[$head:ident, $($tail:ident,)*]) => (
__op_internal__!(@eval @stack[$head, $($stack,)*] @input[$($tail,)*])
);"
)?;
// No input left:
write!(
f,
"
(@eval @stack[$stack:ty,] @input[]) => (
$stack
);"
)?;
// -----------------------------------------------------------------------------------------
// Stage 0: Get it started
write!(
f,
"
($($tail:tt)* ) => (
__op_internal__!(@stack[] @queue[] @tail: $($tail)*)
);"
)?;
write!(
f,
"
}}"
)?;
Ok(())
}
|