diff --git a/src/bin/bitart/expression.rs b/src/bin/bitart/expression.rs index 1cc7740..64085ce 100644 --- a/src/bin/bitart/expression.rs +++ b/src/bin/bitart/expression.rs @@ -23,7 +23,7 @@ impl fmt::Display for Literal { } } -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Variable { X, Y, @@ -93,6 +93,9 @@ impl UnaryOperation { fn eval(&self, env: &Env) -> i64 { self.operator.apply(self.operand.eval(env)) } + fn contains_var(&self, variable: Variable) -> bool { + self.operand.contains_var(variable) + } } impl fmt::Display for UnaryOperation { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -180,6 +183,9 @@ impl BinaryOperation { self.operator .apply(self.operands[0].eval(env), self.operands[1].eval(env)) } + fn contains_var(&self, variable: Variable) -> bool { + self.operands.iter().any(|e| e.contains_var(variable)) + } } impl fmt::Display for BinaryOperation { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -207,6 +213,14 @@ impl InnerExpression { InnerExpression::BinaryOperation(op) => op.eval(env), } } + pub fn contains_var(&self, variable: Variable) -> bool { + match self { + InnerExpression::Literal(_) => false, + InnerExpression::Variable(v) => *v == variable, + InnerExpression::UnaryOperation(op) => op.contains_var(variable), + InnerExpression::BinaryOperation(op) => op.contains_var(variable), + } + } } impl fmt::Display for InnerExpression { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/src/bin/bitart/main.rs b/src/bin/bitart/main.rs index 31a0d82..4a40c0e 100644 --- a/src/bin/bitart/main.rs +++ b/src/bin/bitart/main.rs @@ -18,6 +18,7 @@ use pixelfoo_apps::color::Color; mod expression; use expression::Env; use expression::RandomExpressionBuilder; +use expression::Variable; #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Square { @@ -129,7 +130,10 @@ fn main() -> std::io::Result<()> { let percent = 100 * max_count / bbox.area(); - if (min_percent..=max_percent).contains(&percent) { + if expression.contains_var(Variable::X) + && expression.contains_var(Variable::Y) + && (min_percent..=max_percent).contains(&percent) + { eprintln!("chose expression {expression}"); old_board = new_board;