|
| 1 | +use super::*; |
| 2 | + |
| 3 | +#[derive(Clone, Copy, Debug)] |
| 4 | +pub enum Variable { |
| 5 | + Public(u32), |
| 6 | + Private(u32), |
| 7 | +} |
| 8 | + |
| 9 | +#[derive(Clone, Copy, Debug)] |
| 10 | +pub enum Expression<ExpL, ExpR> { |
| 11 | + Add(ExpL, ExpR), |
| 12 | + Mul(ExpL, ExpR), |
| 13 | +} |
| 14 | + |
| 15 | +impl Add for Variable { |
| 16 | + type Output = Expression<Variable, Variable>; |
| 17 | + |
| 18 | + fn add(self, rhs: Self) -> Self::Output { Expression::Add(self, rhs) } |
| 19 | +} |
| 20 | + |
| 21 | +impl<ExpL, ExpR> Add<Expression<ExpL, ExpR>> for Variable { |
| 22 | + type Output = Expression<Variable, Expression<ExpL, ExpR>>; |
| 23 | + |
| 24 | + fn add(self, rhs: Expression<ExpL, ExpR>) -> Self::Output { Expression::Add(self, rhs) } |
| 25 | +} |
| 26 | + |
| 27 | +impl<ExpL, ExpR> Add<Variable> for Expression<ExpL, ExpR> { |
| 28 | + type Output = Expression<Expression<ExpL, ExpR>, Variable>; |
| 29 | + |
| 30 | + fn add(self, rhs: Variable) -> Self::Output { Expression::Add(self, rhs) } |
| 31 | +} |
| 32 | + |
| 33 | +impl<EXP1, EXP2, EXP3, EXP4> Add<Expression<EXP3, EXP4>> for Expression<EXP1, EXP2> { |
| 34 | + type Output = Expression<Expression<EXP1, EXP2>, Expression<EXP3, EXP4>>; |
| 35 | + |
| 36 | + fn add(self, rhs: Expression<EXP3, EXP4>) -> Self::Output { Expression::Add(self, rhs) } |
| 37 | +} |
| 38 | + |
| 39 | +impl Mul for Variable { |
| 40 | + type Output = Expression<Variable, Variable>; |
| 41 | + |
| 42 | + fn mul(self, rhs: Self) -> Self::Output { Expression::Mul(self, rhs) } |
| 43 | +} |
| 44 | + |
| 45 | +impl<ExpL, ExpR> Mul<Expression<ExpL, ExpR>> for Variable { |
| 46 | + type Output = Expression<Variable, Expression<ExpL, ExpR>>; |
| 47 | + |
| 48 | + fn mul(self, rhs: Expression<ExpL, ExpR>) -> Self::Output { Expression::Mul(self, rhs) } |
| 49 | +} |
| 50 | + |
| 51 | +impl<ExpL, ExpR> Mul<Variable> for Expression<ExpL, ExpR> { |
| 52 | + type Output = Expression<Expression<ExpL, ExpR>, Variable>; |
| 53 | + |
| 54 | + fn mul(self, rhs: Variable) -> Self::Output { Expression::Mul(self, rhs) } |
| 55 | +} |
| 56 | + |
| 57 | +impl<EXP1, EXP2, EXP3, EXP4> Mul<Expression<EXP3, EXP4>> for Expression<EXP1, EXP2> { |
| 58 | + type Output = Expression<Expression<EXP1, EXP2>, Expression<EXP3, EXP4>>; |
| 59 | + |
| 60 | + fn mul(self, rhs: Expression<EXP3, EXP4>) -> Self::Output { Expression::Mul(self, rhs) } |
| 61 | +} |
| 62 | + |
| 63 | +impl Display for Variable { |
| 64 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 65 | + match self { |
| 66 | + Variable::Public(val) => write!(f, "{}", val), |
| 67 | + Variable::Private(val) => write!(f, "{}", val), |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl<EXPL: Display, EXPR: Display> Display for Expression<EXPL, EXPR> { |
| 73 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 74 | + match self { |
| 75 | + Expression::Add(left, right) => write!(f, "({} + {})", left, right), |
| 76 | + Expression::Mul(left, right) => write!(f, "({} * {})", left, right), |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#[cfg(test)] |
| 82 | +mod tests { |
| 83 | + use super::*; |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn writing_a_program() { |
| 87 | + // Create two variables |
| 88 | + let a = Variable::Public(7); |
| 89 | + let b = Variable::Private(3); |
| 90 | + |
| 91 | + // Create basic expressions with these variables |
| 92 | + let add_ab = a + b; |
| 93 | + println!("{}", add_ab); |
| 94 | + assert_eq!(format!("{}", add_ab), "(7 + 3)"); |
| 95 | + |
| 96 | + let mul_ab = a * b; |
| 97 | + println!("{}", mul_ab); |
| 98 | + assert_eq!(format!("{}", mul_ab), "(7 * 3)"); |
| 99 | + |
| 100 | + // Check that we can add a variable to an expression |
| 101 | + println!("{}", a + mul_ab); |
| 102 | + assert_eq!(format!("{}", a + mul_ab), "(7 + (7 * 3))"); |
| 103 | + |
| 104 | + // Check that we can add an expression to a variable |
| 105 | + println!("{}", mul_ab + a); |
| 106 | + assert_eq!(format!("{}", mul_ab + a), "((7 * 3) + 7)"); |
| 107 | + |
| 108 | + // Check that we can add two expressions together |
| 109 | + println!("{}", add_ab + mul_ab); |
| 110 | + assert_eq!(format!("{}", add_ab + mul_ab), "((7 + 3) + (7 * 3))"); |
| 111 | + } |
| 112 | +} |
0 commit comments