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

Recorded work from Friday.

parent d1aadc45
No related branches found
No related tags found
No related merge requests found
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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment