Define Expression type
This commit is contained in:
1 parent
06f125a486
commit
fbab9c521d
2 files changed
+137
No files matched your search
@@ -0,0 +1,135 @@
|
|||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum Expression {
|
||||||
|
Symbol(char),
|
||||||
|
Integer(i32),
|
||||||
|
Sum(std::boxed::Box<Expression>, std::boxed::Box<Expression>),
|
||||||
|
Difference(std::boxed::Box<Expression>, std::boxed::Box<Expression>),
|
||||||
|
Product(std::boxed::Box<Expression>, std::boxed::Box<Expression>),
|
||||||
|
Quotient(std::boxed::Box<Expression>, std::boxed::Box<Expression>),
|
||||||
|
Exponent(std::boxed::Box<Expression>, std::boxed::Box<Expression>),
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,2 +1,4 @@
|
|||||||
pub mod token;
|
pub mod token;
|
||||||
|
pub mod expression;
|
||||||
|
|
||||||
pub mod lex;
|
pub mod lex;
|
||||||
Reference in new issue
Block a user