|
| 1 | +/* |
| 2 | + * INFIX OPERATORS |
| 3 | + * |
| 4 | + * Demonstrates all infix operators: +, -, *, /, %, &, |, ^, &&, ||, ==, !=, <, <=, >, >= |
| 5 | + * |
| 6 | + * Addition and subtraction panic at runtime on overflow/underflow. |
| 7 | + * Multiplication returns a type twice the width of the operands (no overflow). |
| 8 | + * Division and modulo return the same type as the operands. |
| 9 | + * Bitwise operators return the same type as the operands. |
| 10 | + * Logical operators short-circuit and return bool. |
| 11 | + * Comparison operators return bool. |
| 12 | + * |
| 13 | + * Arithmetic operators require: simc -Z infix_arithmetic_operators |
| 14 | + */ |
| 15 | +fn main() { |
| 16 | + let a: u8 = 20; |
| 17 | + let b: u8 = 6; |
| 18 | + |
| 19 | + // run `simc` with `-Z infix_arithmetic_operators` to allow these. |
| 20 | + // Addition: u8 + u8 → u8, panics on overflow |
| 21 | + // let sum: u8 = a + b; |
| 22 | + // assert!(jet::eq_8(sum, 26)); |
| 23 | + |
| 24 | + // Subtraction: u8 - u8 → u8, panics on underflow |
| 25 | + // let diff: u8 = a - b; |
| 26 | + // assert!(jet::eq_8(diff, 14)); |
| 27 | + |
| 28 | + // Multiplication: u8 * u8 → u16 (full precision, no overflow possible) |
| 29 | + // let product: u16 = a * b; |
| 30 | + // assert!(jet::eq_16(product, 120)); |
| 31 | + |
| 32 | + // Division: u8 / u8 → u8, panics if divisor is zero |
| 33 | + // let quotient: u8 = a / b; |
| 34 | + // assert!(jet::eq_8(quotient, 3)); |
| 35 | + |
| 36 | + // Modulo: u8 % u8 → u8, panics if divisor is zero |
| 37 | + // let remainder: u8 = a % b; |
| 38 | + // assert!(jet::eq_8(remainder, 2)); |
| 39 | + |
| 40 | + // Bitwise AND: u8 & u8 → u8 |
| 41 | + let and: u8 = a & b; |
| 42 | + assert!(jet::eq_8(and, 4)); // 0b00010100 & 0b00000110 = 0b00000100 |
| 43 | + |
| 44 | + // Bitwise OR: u8 | u8 → u8 |
| 45 | + let or: u8 = a | b; |
| 46 | + assert!(jet::eq_8(or, 22)); // 0b00010100 | 0b00000110 = 0b00010110 |
| 47 | + |
| 48 | + // Bitwise XOR: u8 ^ u8 → u8 |
| 49 | + let xor: u8 = a ^ b; |
| 50 | + assert!(jet::eq_8(xor, 18)); // 0b00010100 ^ 0b00000110 = 0b00010010 |
| 51 | + |
| 52 | + // Logical AND: bool && bool → bool, short-circuits (rhs not evaluated if lhs is false) |
| 53 | + let a_eq_a: bool = a == a; // true |
| 54 | + let b_eq_b: bool = b == b; // true |
| 55 | + let a_eq_b: bool = a == b; // false |
| 56 | + assert!(a_eq_a && b_eq_b); // true && true = true |
| 57 | + assert!(true && true); |
| 58 | + // false && _ short-circuits to false: |
| 59 | + let and_false: bool = a_eq_b && b_eq_b; |
| 60 | + assert!(match and_false { false => true, true => false, }); |
| 61 | + |
| 62 | + // Logical OR: bool || bool → bool, short-circuits (rhs not evaluated if lhs is true) |
| 63 | + assert!(a_eq_a || a_eq_b); // true || _ = true (rhs not evaluated) |
| 64 | + assert!(false || true); |
| 65 | + // false || false = false: |
| 66 | + let or_false: bool = a_eq_b || a_eq_b; |
| 67 | + assert!(match or_false { false => true, true => false, }); |
| 68 | + |
| 69 | + // Equality: u8 == u8 → bool |
| 70 | + assert!(a == a); // 20 == 20 is true |
| 71 | + assert!(a != b); // 20 != 6 is true |
| 72 | + |
| 73 | + // Less than: u8 < u8 → bool |
| 74 | + assert!(b < a); // 6 < 20 |
| 75 | + |
| 76 | + // Greater than: u8 > u8 → bool |
| 77 | + assert!(a > b); // 20 > 6 |
| 78 | + |
| 79 | + // Less or equal: u8 <= u8 → bool |
| 80 | + assert!(b <= a); // 6 <= 20 |
| 81 | + assert!(b <= b); // 6 <= 6 |
| 82 | + |
| 83 | + // Greater or equal: u8 >= u8 → bool |
| 84 | + assert!(a >= b); // 20 >= 6 |
| 85 | + assert!(a >= a); // 20 >= 20 |
| 86 | +} |
0 commit comments