| 1 |
#[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 2 |
enum OpType { |
| 3 |
Operator, |
| 4 |
Function, |
| 5 |
} |
| 6 |
|
| 7 |
use self::OpType::*; |
| 8 |
|
| 9 |
struct Op { |
| 10 |
token: &'static str, |
| 11 |
operator: &'static str, |
| 12 |
example: (&'static str, &'static str), |
| 13 |
precedence: u8, |
| 14 |
n_args: u8, |
| 15 |
op_type: OpType, |
| 16 |
} |
| 17 |
|
| 18 |
pub fn write_op_macro() -> ::std::io::Result<()> { |
| 19 |
let out_dir = ::std::env::var("OUT_DIR").unwrap(); |
| 20 |
let dest = ::std::path::Path::new(&out_dir).join("op.rs"); |
| 21 |
let mut f = ::std::fs::File::create(&dest).unwrap(); |
| 22 |
|
| 23 |
// Operator precedence is taken from |
| 24 |
// https://doc.rust-lang.org/reference.html#operator-precedence |
| 25 |
// |
| 26 |
// We choose 16 as the highest precedence (functions are set to 255 but it doesn't matter |
| 27 |
// for them). We also only use operators that are left associative so we don't have to worry |
| 28 |
// about that. |
| 29 |
let ops = &[ |
| 30 |
Op { |
| 31 |
token: "*", |
| 32 |
operator: "Prod", |
| 33 |
example: ("P2 * P3", "P6"), |
| 34 |
precedence: 16, |
| 35 |
n_args: 2, |
| 36 |
op_type: Operator, |
| 37 |
}, |
| 38 |
Op { |
| 39 |
token: "/", |
| 40 |
operator: "Quot", |
| 41 |
example: ("P6 / P2", "P3"), |
| 42 |
precedence: 16, |
| 43 |
n_args: 2, |
| 44 |
op_type: Operator, |
| 45 |
}, |
| 46 |
Op { |
| 47 |
token: "%", |
| 48 |
operator: "Mod", |
| 49 |
example: ("P5 % P3", "P2"), |
| 50 |
precedence: 16, |
| 51 |
n_args: 2, |
| 52 |
op_type: Operator, |
| 53 |
}, |
| 54 |
Op { |
| 55 |
token: "+", |
| 56 |
operator: "Sum", |
| 57 |
example: ("P2 + P3", "P5"), |
| 58 |
precedence: 15, |
| 59 |
n_args: 2, |
| 60 |
op_type: Operator, |
| 61 |
}, |
| 62 |
Op { |
| 63 |
token: "-", |
| 64 |
operator: "Diff", |
| 65 |
example: ("P2 - P3", "N1"), |
| 66 |
precedence: 15, |
| 67 |
n_args: 2, |
| 68 |
op_type: Operator, |
| 69 |
}, |
| 70 |
Op { |
| 71 |
token: "<<", |
| 72 |
operator: "Shleft", |
| 73 |
example: ("U1 << U5", "U32"), |
| 74 |
precedence: 14, |
| 75 |
n_args: 2, |
| 76 |
op_type: Operator, |
| 77 |
}, |
| 78 |
Op { |
| 79 |
token: ">>", |
| 80 |
operator: "Shright", |
| 81 |
example: ("U32 >> U5", "U1"), |
| 82 |
precedence: 14, |
| 83 |
n_args: 2, |
| 84 |
op_type: Operator, |
| 85 |
}, |
| 86 |
Op { |
| 87 |
token: "&", |
| 88 |
operator: "And", |
| 89 |
example: ("U5 & U3", "U1"), |
| 90 |
precedence: 13, |
| 91 |
n_args: 2, |
| 92 |
op_type: Operator, |
| 93 |
}, |
| 94 |
Op { |
| 95 |
token: "^", |
| 96 |
operator: "Xor", |
| 97 |
example: ("U5 ^ U3", "U6"), |
| 98 |
precedence: 12, |
| 99 |
n_args: 2, |
| 100 |
op_type: Operator, |
| 101 |
}, |
| 102 |
Op { |
| 103 |
token: "|", |
| 104 |
operator: "Or", |
| 105 |
example: ("U5 | U3", "U7"), |
| 106 |
precedence: 11, |
| 107 |
n_args: 2, |
| 108 |
op_type: Operator, |
| 109 |
}, |
| 110 |
Op { |
| 111 |
token: "==", |
| 112 |
operator: "Eq", |
| 113 |
example: ("P5 == P3 + P2", "True"), |
| 114 |
precedence: 10, |
| 115 |
n_args: 2, |
| 116 |
op_type: Operator, |
| 117 |
}, |
| 118 |
Op { |
| 119 |
token: "!=", |
| 120 |
operator: "NotEq", |
| 121 |
example: ("P5 != P3 + P2", "False"), |
| 122 |
precedence: 10, |
| 123 |
n_args: 2, |
| 124 |
op_type: Operator, |
| 125 |
}, |
| 126 |
Op { |
| 127 |
token: "<=", |
| 128 |
operator: "LeEq", |
| 129 |
example: ("P6 <= P3 + P2", "False"), |
| 130 |
precedence: 10, |
| 131 |
n_args: 2, |
| 132 |
op_type: Operator, |
| 133 |
}, |
| 134 |
Op { |
| 135 |
token: ">=", |
| 136 |
operator: "GrEq", |
| 137 |
example: ("P6 >= P3 + P2", "True"), |
| 138 |
precedence: 10, |
| 139 |
n_args: 2, |
| 140 |
op_type: Operator, |
| 141 |
}, |
| 142 |
Op { |
| 143 |
token: "<", |
| 144 |
operator: "Le", |
| 145 |
example: ("P4 < P3 + P2", "True"), |
| 146 |
precedence: 10, |
| 147 |
n_args: 2, |
| 148 |
op_type: Operator, |
| 149 |
}, |
| 150 |
Op { |
| 151 |
token: ">", |
| 152 |
operator: "Gr", |
| 153 |
example: ("P5 < P3 + P2", "False"), |
| 154 |
precedence: 10, |
| 155 |
n_args: 2, |
| 156 |
op_type: Operator, |
| 157 |
}, |
| 158 |
Op { |
| 159 |
token: "cmp", |
| 160 |
operator: "Compare", |
| 161 |
example: ("cmp(P2, P3)", "Less"), |
| 162 |
precedence: !0, |
| 163 |
n_args: 2, |
| 164 |
op_type: Function, |
| 165 |
}, |
| 166 |
Op { |
| 167 |
token: "sqr", |
| 168 |
operator: "Square", |
| 169 |
example: ("sqr(P2)", "P4"), |
| 170 |
precedence: !0, |
| 171 |
n_args: 1, |
| 172 |
op_type: Function, |
| 173 |
}, |
| 174 |
Op { |
| 175 |
token: "sqrt", |
| 176 |
operator: "Sqrt", |
| 177 |
example: ("sqrt(U9)", "U3"), |
| 178 |
precedence: !0, |
| 179 |
n_args: 1, |
| 180 |
op_type: Function, |
| 181 |
}, |
| 182 |
Op { |
| 183 |
token: "abs", |
| 184 |
operator: "AbsVal", |
| 185 |
example: ("abs(N2)", "P2"), |
| 186 |
precedence: !0, |
| 187 |
n_args: 1, |
| 188 |
op_type: Function, |
| 189 |
}, |
| 190 |
Op { |
| 191 |
token: "cube", |
| 192 |
operator: "Cube", |
| 193 |
example: ("cube(P2)", "P8"), |
| 194 |
precedence: !0, |
| 195 |
n_args: 1, |
| 196 |
op_type: Function, |
| 197 |
}, |
| 198 |
Op { |
| 199 |
token: "pow", |
| 200 |
operator: "Exp", |
| 201 |
example: ("pow(P2, P3)", "P8"), |
| 202 |
precedence: !0, |
| 203 |
n_args: 2, |
| 204 |
op_type: Function, |
| 205 |
}, |
| 206 |
Op { |
| 207 |
token: "min", |
| 208 |
operator: "Minimum", |
| 209 |
example: ("min(P2, P3)", "P2"), |
| 210 |
precedence: !0, |
| 211 |
n_args: 2, |
| 212 |
op_type: Function, |
| 213 |
}, |
| 214 |
Op { |
| 215 |
token: "max", |
| 216 |
operator: "Maximum", |
| 217 |
example: ("max(P2, P3)", "P3"), |
| 218 |
precedence: !0, |
| 219 |
n_args: 2, |
| 220 |
op_type: Function, |
| 221 |
}, |
| 222 |
Op { |
| 223 |
token: "log2", |
| 224 |
operator: "Log2", |
| 225 |
example: ("log2(U9)", "U3"), |
| 226 |
precedence: !0, |
| 227 |
n_args: 1, |
| 228 |
op_type: Function, |
| 229 |
}, |
| 230 |
Op { |
| 231 |
token: "gcd", |
| 232 |
operator: "Gcf", |
| 233 |
example: ("gcd(U9, U21)", "U3"), |
| 234 |
precedence: !0, |
| 235 |
n_args: 2, |
| 236 |
op_type: Function, |
| 237 |
}, |
| 238 |
]; |
| 239 |
|
| 240 |
use std::io::Write; |
| 241 |
write!( |
| 242 |
f, |
| 243 |
" |
| 244 |
/** |
| 245 |
Convenient type operations. |
| 246 |
|
| 247 |
Any types representing values must be able to be expressed as `ident`s. That means they need to be |
| 248 |
in scope. |
| 249 |
|
| 250 |
For example, `P5` is okay, but `typenum::P5` is not. |
| 251 |
|
| 252 |
You may combine operators arbitrarily, although doing so excessively may require raising the |
| 253 |
recursion limit. |
| 254 |
|
| 255 |
# Example |
| 256 |
```rust |
| 257 |
#![recursion_limit=\"128\"] |
| 258 |
#[macro_use] extern crate typenum; |
| 259 |
use typenum::consts::*; |
| 260 |
|
| 261 |
fn main() {{ |
| 262 |
assert_type!( |
| 263 |
op!(min((P1 - P2) * (N3 + N7), P5 * (P3 + P4)) == P10) |
| 264 |
); |
| 265 |
}} |
| 266 |
``` |
| 267 |
Operators are evaluated based on the operator precedence outlined |
| 268 |
[here](https://doc.rust-lang.org/reference.html#operator-precedence). |
| 269 |
|
| 270 |
The full list of supported operators and functions is as follows: |
| 271 |
|
| 272 |
{} |
| 273 |
|
| 274 |
They all expand to type aliases defined in the `operator_aliases` module. Here is an expanded list, |
| 275 |
including examples: |
| 276 |
|
| 277 |
", |
| 278 |
ops.iter() |
| 279 |
.map(|op| format!("`{}`", op.token)) |
| 280 |
.collect::<Vec<_>>() |
| 281 |
.join(", ") |
| 282 |
)?; |
| 283 |
|
| 284 |
//write!(f, "Token | Alias | Example\n ===|===|===\n")?; |
| 285 |
|
| 286 |
for op in ops.iter() { |
| 287 |
write!( |
| 288 |
f, |
| 289 |
"---\nOperator `{token}`. Expands to `{operator}`. |
| 290 |
|
| 291 |
```rust |
| 292 |
# #[macro_use] extern crate typenum; |
| 293 |
# use typenum::*; |
| 294 |
# fn main() {{ |
| 295 |
assert_type_eq!(op!({ex0}), {ex1}); |
| 296 |
# }} |
| 297 |
```\n |
| 298 |
", |
| 299 |
token = op.token, |
| 300 |
operator = op.operator, |
| 301 |
ex0 = op.example.0, |
| 302 |
ex1 = op.example.1 |
| 303 |
)?; |
| 304 |
} |
| 305 |
|
| 306 |
write!( |
| 307 |
f, |
| 308 |
"*/ |
| 309 |
#[macro_export(local_inner_macros)] |
| 310 |
macro_rules! op {{ |
| 311 |
($($tail:tt)*) => ( __op_internal__!($($tail)*) ); |
| 312 |
}} |
| 313 |
|
| 314 |
#[doc(hidden)] |
| 315 |
#[macro_export(local_inner_macros)] |
| 316 |
macro_rules! __op_internal__ {{ |
| 317 |
" |
| 318 |
)?; |
| 319 |
|
| 320 |
// We first us the shunting-yard algorithm to produce our tokens in Polish notation. |
| 321 |
// See: https://en.wikipedia.org/wiki/Shunting-yard_algorithm |
| 322 |
|
| 323 |
// Note: Due to macro asymmetry, "the top of the stack" refers to the first element, not the |
| 324 |
// last |
| 325 |
|
| 326 |
// ----------------------------------------------------------------------------------------- |
| 327 |
// Stage 1: There are tokens to be read: |
| 328 |
|
| 329 |
// ------- |
| 330 |
// Case 1: Token is a function => Push it onto the stack: |
| 331 |
for fun in ops.iter().filter(|f| f.op_type == Function) { |
| 332 |
write!( |
| 333 |
f, |
| 334 |
" |
| 335 |
(@stack[$($stack:ident,)*] @queue[$($queue:ident,)*] @tail: {f_token} $($tail:tt)*) => ( |
| 336 |
__op_internal__!(@stack[{f_op}, $($stack,)*] @queue[$($queue,)*] @tail: $($tail)*) |
| 337 |
);", |
| 338 |
f_token = fun.token, |
| 339 |
f_op = fun.operator |
| 340 |
)?; |
| 341 |
} |
| 342 |
|
| 343 |
// ------- |
| 344 |
// Case 2: Token is a comma => Until the top of the stack is a LParen, |
| 345 |
// Pop operators from stack to queue |
| 346 |
|
| 347 |
// Base case: Top of stack is LParen, ditch comma and continue |
| 348 |
write!( |
| 349 |
f, |
| 350 |
" |
| 351 |
(@stack[LParen, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail: , $($tail:tt)*) => ( |
| 352 |
__op_internal__!(@stack[LParen, $($stack,)*] @queue[$($queue,)*] @tail: $($tail)*) |
| 353 |
);" |
| 354 |
)?; |
| 355 |
// Recursive case: Not LParen, pop from stack to queue |
| 356 |
write!( |
| 357 |
f, |
| 358 |
" |
| 359 |
(@stack[$stack_top:ident, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail: , $($tail:tt)*) => ( |
| 360 |
__op_internal__!(@stack[$($stack,)*] @queue[$stack_top, $($queue,)*] @tail: , $($tail)*) |
| 361 |
);" |
| 362 |
)?; |
| 363 |
|
| 364 |
// ------- |
| 365 |
// Case 3: Token is an operator, o1: |
| 366 |
for o1 in ops.iter().filter(|op| op.op_type == Operator) { |
| 367 |
// If top of stack is operator o2 with o1.precedence <= o2.precedence, |
| 368 |
// Then pop o2 off stack onto queue: |
| 369 |
for o2 in ops |
| 370 |
.iter() |
| 371 |
.filter(|op| op.op_type == Operator) |
| 372 |
.filter(|o2| o1.precedence <= o2.precedence) |
| 373 |
{ |
| 374 |
write!( |
| 375 |
f, |
| 376 |
" |
| 377 |
(@stack[{o2_op}, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail: {o1_token} $($tail:tt)*) => ( |
| 378 |
__op_internal__!(@stack[$($stack,)*] @queue[{o2_op}, $($queue,)*] @tail: {o1_token} $($tail)*) |
| 379 |
);", |
| 380 |
o2_op = o2.operator, |
| 381 |
o1_token = o1.token |
| 382 |
)?; |
| 383 |
} |
| 384 |
// Base case: push o1 onto stack |
| 385 |
write!( |
| 386 |
f, |
| 387 |
" |
| 388 |
(@stack[$($stack:ident,)*] @queue[$($queue:ident,)*] @tail: {o1_token} $($tail:tt)*) => ( |
| 389 |
__op_internal__!(@stack[{o1_op}, $($stack,)*] @queue[$($queue,)*] @tail: $($tail)*) |
| 390 |
);", |
| 391 |
o1_op = o1.operator, |
| 392 |
o1_token = o1.token |
| 393 |
)?; |
| 394 |
} |
| 395 |
|
| 396 |
// ------- |
| 397 |
// Case 4: Token is "(": push it onto stack as "LParen". Also convert the ")" to "RParen" to |
| 398 |
// appease the macro gods: |
| 399 |
write!( |
| 400 |
f, |
| 401 |
" |
| 402 |
(@stack[$($stack:ident,)*] @queue[$($queue:ident,)*] @tail: ( $($stuff:tt)* ) $($tail:tt)* ) |
| 403 |
=> ( |
| 404 |
__op_internal__!(@stack[LParen, $($stack,)*] @queue[$($queue,)*] |
| 405 |
@tail: $($stuff)* RParen $($tail)*) |
| 406 |
);" |
| 407 |
)?; |
| 408 |
|
| 409 |
// ------- |
| 410 |
// Case 5: Token is "RParen": |
| 411 |
// 1. Pop from stack to queue until we see an "LParen", |
| 412 |
// 2. Kill the "LParen", |
| 413 |
// 3. If the top of the stack is a function, pop it onto the queue |
| 414 |
// 2. Base case: |
| 415 |
write!( |
| 416 |
f, |
| 417 |
" |
| 418 |
(@stack[LParen, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail: RParen $($tail:tt)*) => ( |
| 419 |
__op_internal__!(@rp3 @stack[$($stack,)*] @queue[$($queue,)*] @tail: $($tail)*) |
| 420 |
);" |
| 421 |
)?; |
| 422 |
// 1. Recursive case: |
| 423 |
write!( |
| 424 |
f, |
| 425 |
" |
| 426 |
(@stack[$stack_top:ident, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail: RParen $($tail:tt)*) |
| 427 |
=> ( |
| 428 |
__op_internal__!(@stack[$($stack,)*] @queue[$stack_top, $($queue,)*] @tail: RParen $($tail)*) |
| 429 |
);" |
| 430 |
)?; |
| 431 |
// 3. Check for function: |
| 432 |
for fun in ops.iter().filter(|f| f.op_type == Function) { |
| 433 |
write!( |
| 434 |
f, |
| 435 |
" |
| 436 |
(@rp3 @stack[{fun_op}, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail: $($tail:tt)*) => ( |
| 437 |
__op_internal__!(@stack[$($stack,)*] @queue[{fun_op}, $($queue,)*] @tail: $($tail)*) |
| 438 |
);", |
| 439 |
fun_op = fun.operator |
| 440 |
)?; |
| 441 |
} |
| 442 |
// 3. If no function found: |
| 443 |
write!( |
| 444 |
f, |
| 445 |
" |
| 446 |
(@rp3 @stack[$($stack:ident,)*] @queue[$($queue:ident,)*] @tail: $($tail:tt)*) => ( |
| 447 |
__op_internal__!(@stack[$($stack,)*] @queue[$($queue,)*] @tail: $($tail)*) |
| 448 |
);" |
| 449 |
)?; |
| 450 |
|
| 451 |
// ------- |
| 452 |
// Case 6: Token is a number: Push it onto the queue |
| 453 |
write!( |
| 454 |
f, |
| 455 |
" |
| 456 |
(@stack[$($stack:ident,)*] @queue[$($queue:ident,)*] @tail: $num:ident $($tail:tt)*) => ( |
| 457 |
__op_internal__!(@stack[$($stack,)*] @queue[$num, $($queue,)*] @tail: $($tail)*) |
| 458 |
);" |
| 459 |
)?; |
| 460 |
|
| 461 |
// ------- |
| 462 |
// Case 7: Out of tokens: |
| 463 |
// Base case: Stack empty: Start evaluating |
| 464 |
write!( |
| 465 |
f, |
| 466 |
" |
| 467 |
(@stack[] @queue[$($queue:ident,)*] @tail: ) => ( |
| 468 |
__op_internal__!(@reverse[] @input: $($queue,)*) |
| 469 |
);" |
| 470 |
)?; |
| 471 |
// Recursive case: Pop stack to queue |
| 472 |
write!( |
| 473 |
f, |
| 474 |
" |
| 475 |
(@stack[$stack_top:ident, $($stack:ident,)*] @queue[$($queue:ident,)*] @tail:) => ( |
| 476 |
__op_internal__!(@stack[$($stack,)*] @queue[$stack_top, $($queue,)*] @tail: ) |
| 477 |
);" |
| 478 |
)?; |
| 479 |
|
| 480 |
// ----------------------------------------------------------------------------------------- |
| 481 |
// Stage 2: Reverse so we have RPN |
| 482 |
write!( |
| 483 |
f, |
| 484 |
" |
| 485 |
(@reverse[$($revved:ident,)*] @input: $head:ident, $($tail:ident,)* ) => ( |
| 486 |
__op_internal__!(@reverse[$head, $($revved,)*] @input: $($tail,)*) |
| 487 |
);" |
| 488 |
)?; |
| 489 |
write!( |
| 490 |
f, |
| 491 |
" |
| 492 |
(@reverse[$($revved:ident,)*] @input: ) => ( |
| 493 |
__op_internal__!(@eval @stack[] @input[$($revved,)*]) |
| 494 |
);" |
| 495 |
)?; |
| 496 |
|
| 497 |
// ----------------------------------------------------------------------------------------- |
| 498 |
// Stage 3: Evaluate in Reverse Polish Notation |
| 499 |
// Operators / Operators with 2 args: |
| 500 |
for op in ops.iter().filter(|op| op.n_args == 2) { |
| 501 |
// Note: We have to switch $a and $b here, otherwise non-commutative functions are backwards |
| 502 |
write!( |
| 503 |
f, |
| 504 |
" |
| 505 |
(@eval @stack[$a:ty, $b:ty, $($stack:ty,)*] @input[{op}, $($tail:ident,)*]) => ( |
| 506 |
__op_internal__!(@eval @stack[$crate::{op}<$b, $a>, $($stack,)*] @input[$($tail,)*]) |
| 507 |
);", |
| 508 |
op = op.operator |
| 509 |
)?; |
| 510 |
} |
| 511 |
// Operators with 1 arg: |
| 512 |
for op in ops.iter().filter(|op| op.n_args == 1) { |
| 513 |
write!( |
| 514 |
f, |
| 515 |
" |
| 516 |
(@eval @stack[$a:ty, $($stack:ty,)*] @input[{op}, $($tail:ident,)*]) => ( |
| 517 |
__op_internal__!(@eval @stack[$crate::{op}<$a>, $($stack,)*] @input[$($tail,)*]) |
| 518 |
);", |
| 519 |
op = op.operator |
| 520 |
)?; |
| 521 |
} |
| 522 |
|
| 523 |
// Wasn't a function or operator, so must be a value => push onto stack |
| 524 |
write!( |
| 525 |
f, |
| 526 |
" |
| 527 |
(@eval @stack[$($stack:ty,)*] @input[$head:ident, $($tail:ident,)*]) => ( |
| 528 |
__op_internal__!(@eval @stack[$head, $($stack,)*] @input[$($tail,)*]) |
| 529 |
);" |
| 530 |
)?; |
| 531 |
|
| 532 |
// No input left: |
| 533 |
write!( |
| 534 |
f, |
| 535 |
" |
| 536 |
(@eval @stack[$stack:ty,] @input[]) => ( |
| 537 |
$stack |
| 538 |
);" |
| 539 |
)?; |
| 540 |
|
| 541 |
// ----------------------------------------------------------------------------------------- |
| 542 |
// Stage 0: Get it started |
| 543 |
write!( |
| 544 |
f, |
| 545 |
" |
| 546 |
($($tail:tt)* ) => ( |
| 547 |
__op_internal__!(@stack[] @queue[] @tail: $($tail)*) |
| 548 |
);" |
| 549 |
)?; |
| 550 |
|
| 551 |
write!( |
| 552 |
f, |
| 553 |
" |
| 554 |
}}" |
| 555 |
)?; |
| 556 |
|
| 557 |
Ok(()) |
| 558 |
} |