diff --git a/simplifier/src/features/expressions/simplification.js b/simplifier/src/features/expressions/simplification.js index 4b00d910c46346f22c75c4d800e1943c6aede3a9..bf016f601d2bc6f11288121326da74978ed140d5 100644 --- a/simplifier/src/features/expressions/simplification.js +++ b/simplifier/src/features/expressions/simplification.js @@ -1,15 +1,13 @@ import { Expression } from './expressions.js'; -function booleanAssignments(variables) { - const results = []; +function*booleanAssignments(variables) { for (let bits = 1 << variables.length; bits--;) { const assignments = new Map(); for (let index = variables.length; index--;) { assignments.set(variables[index].name, ((bits >> index) & 1) !== 0); } - results.push(assignments); + yield assignments; } - return results; } function matches(expression, semantics, variables) { @@ -21,16 +19,15 @@ function matches(expression, semantics, variables) { return true; } -function expressionsOfSize(environment, operatorCount) { - const results = []; +function*expressionsOfSize(environment, operatorCount) { if (operatorCount === 0) { for (const variable of environment.variables) { - results.push(variable); + yield variable; } } else { for (const operator of environment.unaryOperators) { for (const subexpression of expressionsOfSize(environment, operatorCount - 1)) { - results.push(new Expression(operator, [subexpression])); + yield new Expression(operator, [subexpression]); } } for (let leftOperatorCount = 0; leftOperatorCount < operatorCount; ++leftOperatorCount) { @@ -38,24 +35,21 @@ function expressionsOfSize(environment, operatorCount) { for (const operator of environment.binaryOperators) { for (const leftSubexpression of expressionsOfSize(environment, leftOperatorCount)) { for (const rightSubexpression of expressionsOfSize(environment, rightOperatorCount)) { - results.push(new Expression(operator, [leftSubexpression, rightSubexpression])); + yield new Expression(operator, [leftSubexpression, rightSubexpression]); } } } } } - return results; } -function expressionsUpToSize(environment, maximumOperatorCount) { - const results = []; +function*expressionsUpToSize(environment, maximumOperatorCount) { for (const operator of environment.nullaryOperators) { - results.push(new Expression(operator, [])); + yield new Expression(operator, []); } for (let operatorCount = 0; operatorCount <= maximumOperatorCount; ++operatorCount) { - results.push(...expressionsOfSize(environment, operatorCount)); + yield* expressionsOfSize(environment, operatorCount); } - return results; } export function simplify(semantics, environment, maximumOperatorCount) {