Skip to content
Snippets Groups Projects
Commit 49ce4615 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Converted "generate"-step helper functions to generator functions.

parent 4459ee3c
Branches
Tags
No related merge requests found
import { Expression } from './expressions.js'; import { Expression } from './expressions.js';
function booleanAssignments(variables) { function*booleanAssignments(variables) {
const results = [];
for (let bits = 1 << variables.length; bits--;) { for (let bits = 1 << variables.length; bits--;) {
const assignments = new Map(); const assignments = new Map();
for (let index = variables.length; index--;) { for (let index = variables.length; index--;) {
assignments.set(variables[index].name, ((bits >> index) & 1) !== 0); assignments.set(variables[index].name, ((bits >> index) & 1) !== 0);
} }
results.push(assignments); yield assignments;
} }
return results;
} }
function matches(expression, semantics, variables) { function matches(expression, semantics, variables) {
...@@ -21,16 +19,15 @@ function matches(expression, semantics, variables) { ...@@ -21,16 +19,15 @@ function matches(expression, semantics, variables) {
return true; return true;
} }
function expressionsOfSize(environment, operatorCount) { function*expressionsOfSize(environment, operatorCount) {
const results = [];
if (operatorCount === 0) { if (operatorCount === 0) {
for (const variable of environment.variables) { for (const variable of environment.variables) {
results.push(variable); yield variable;
} }
} else { } else {
for (const operator of environment.unaryOperators) { for (const operator of environment.unaryOperators) {
for (const subexpression of expressionsOfSize(environment, operatorCount - 1)) { 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) { for (let leftOperatorCount = 0; leftOperatorCount < operatorCount; ++leftOperatorCount) {
...@@ -38,24 +35,21 @@ function expressionsOfSize(environment, operatorCount) { ...@@ -38,24 +35,21 @@ function expressionsOfSize(environment, operatorCount) {
for (const operator of environment.binaryOperators) { for (const operator of environment.binaryOperators) {
for (const leftSubexpression of expressionsOfSize(environment, leftOperatorCount)) { for (const leftSubexpression of expressionsOfSize(environment, leftOperatorCount)) {
for (const rightSubexpression of expressionsOfSize(environment, rightOperatorCount)) { 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) { function*expressionsUpToSize(environment, maximumOperatorCount) {
const results = [];
for (const operator of environment.nullaryOperators) { for (const operator of environment.nullaryOperators) {
results.push(new Expression(operator, [])); yield new Expression(operator, []);
} }
for (let operatorCount = 0; operatorCount <= maximumOperatorCount; ++operatorCount) { for (let operatorCount = 0; operatorCount <= maximumOperatorCount; ++operatorCount) {
results.push(...expressionsOfSize(environment, operatorCount)); yield* expressionsOfSize(environment, operatorCount);
} }
return results;
} }
export function simplify(semantics, environment, maximumOperatorCount) { export function simplify(semantics, environment, maximumOperatorCount) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment