From fbab9c521d1a8fb275263056fb1a1244f75c1830 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 26 Sep 2026 09:11:01 +0100 Subject: [PATCH] Define Expression type --- src/expression.rs | 135 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 + 2 files changed, 137 insertions(+) create mode 100644 src/expression.rs diff --git a/src/expression.rs b/src/expression.rs new file mode 100644 index 0000000..6694b08 --- /dev/null +++ b/src/expression.rs @@ -0,0 +1,135 @@ +#[derive(Debug, PartialEq)] +pub enum Expression { + Symbol(char), + Integer(i32), + Sum(std::boxed::Box, std::boxed::Box), + Difference(std::boxed::Box, std::boxed::Box), + Product(std::boxed::Box, std::boxed::Box), + Quotient(std::boxed::Box, std::boxed::Box), + Exponent(std::boxed::Box, std::boxed::Box), +} + +impl Expression { + pub fn sum(left: Expression, right: Expression) -> Expression { + Expression::Sum( + std::boxed::Box::new(left), std::boxed::Box::new(right)) + } + + pub fn difference(left: Expression, right: Expression) -> Expression { + Expression::Difference( + std::boxed::Box::new(left), std::boxed::Box::new(right)) + } + + pub fn product(left: Expression, right: Expression) -> Expression { + Expression::Product( + std::boxed::Box::new(left), std::boxed::Box::new(right)) + } + + pub fn quotient(left: Expression, right: Expression) -> Expression { + Expression::Quotient( + std::boxed::Box::new(left), std::boxed::Box::new(right)) + } + + pub fn exponent(left: Expression, right: Expression) -> Expression { + Expression::Exponent( + std::boxed::Box::new(left), std::boxed::Box::new(right)) + } + + pub fn is_constant(&self) -> bool { + match self { + Expression::Integer(_) => true, + Expression::Symbol(_) => false, + Expression::Sum(x, y) => x.is_constant() && y.is_constant(), + Expression::Difference(x, y) => + x.is_constant() && y.is_constant(), + Expression::Product(x, y) => x.is_constant() && y.is_constant(), + Expression::Quotient(x, y) => x.is_constant() && y.is_constant(), + Expression::Exponent(x, y) => x.is_constant() && y.is_constant(), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn integer_is_constant() { + assert!(Expression::Integer(1).is_constant()); + } + + #[test] + fn symbol_is_not_constant() { + assert!(!Expression::Symbol('x').is_constant()); + } + + #[test] + fn sum_of_integers_is_constant() { + assert!(Expression::sum( + Expression::Integer(1), + Expression::Integer(1)).is_constant()); + } + + #[test] + fn sum_of_integer_and_symbol_is_not_constant() { + assert!(!Expression::sum( + Expression::Symbol('x'), + Expression::Integer(1)).is_constant()); + } + + #[test] + fn difference_of_integers_is_constant() { + assert!(Expression::difference( + Expression::Integer(1), + Expression::Integer(1)).is_constant()); + } + + #[test] + fn difference_of_integer_and_symbol_is_not_constant() { + assert!(!Expression::difference( + Expression::Symbol('x'), + Expression::Integer(1)).is_constant()); + } + + #[test] + fn product_of_integers_is_constant() { + assert!(Expression::product( + Expression::Integer(1), + Expression::Integer(1)).is_constant()); + } + + #[test] + fn product_of_integer_and_symbol_is_not_constant() { + assert!(!Expression::product( + Expression::Symbol('x'), + Expression::Integer(1)).is_constant()); + } + + #[test] + fn quotient_of_integers_is_constant() { + assert!(Expression::quotient( + Expression::Integer(1), + Expression::Integer(1)).is_constant()); + } + + #[test] + fn quotient_of_integer_and_symbol_is_not_constant() { + assert!(!Expression::quotient( + Expression::Symbol('x'), + Expression::Integer(1)).is_constant()); + } + + #[test] + fn exponent_of_integers_is_constant() { + assert!(Expression::exponent( + Expression::Integer(1), + Expression::Integer(1)).is_constant()); + } + + #[test] + fn exponent_of_integer_and_symbol_is_not_constant() { + assert!(!Expression::exponent( + Expression::Symbol('x'), + Expression::Integer(1)).is_constant()); + } +} diff --git a/src/lib.rs b/src/lib.rs index f793d1a..6769acd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,4 @@ pub mod token; +pub mod expression; + pub mod lex;