js-slang
Version:
Javascript-based implementations of Source, written in Typescript
106 lines • 4.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StepperVariableDeclaration = exports.StepperVariableDeclarator = void 0;
const generator_1 = require("../../generator");
const __1 = require("..");
const __2 = require("../..");
class StepperVariableDeclarator {
constructor(id, init, leadingComments, trailingComments, loc, range) {
this.type = 'VariableDeclarator';
this.id = id;
this.init = init;
this.leadingComments = leadingComments;
this.trailingComments = trailingComments;
this.loc = loc;
this.range = range;
}
static create(node) {
return new StepperVariableDeclarator((0, generator_1.convert)(node.id), node.init ? (0, generator_1.convert)(node.init) : node.init, node.leadingComments, node.trailingComments, node.loc, node.range);
}
isContractible() {
return this.init ? this.init.isContractible() : false;
}
isOneStepPossible() {
return this.init ? this.init.isOneStepPossible() : false;
}
contract() {
return new StepperVariableDeclarator(this.id, this.init.oneStep(), this.leadingComments, this.trailingComments, this.loc, this.range);
}
oneStep() {
return new StepperVariableDeclarator(this.id, this.init.oneStep(), this.leadingComments, this.trailingComments, this.loc, this.range);
}
substitute(id, value) {
return new StepperVariableDeclarator(this.id, this.init.substitute(id, value), this.leadingComments, this.trailingComments, this.loc, this.range);
}
freeNames() {
return this.init.freeNames();
}
allNames() {
return this.init.allNames();
}
rename(before, after) {
return new StepperVariableDeclarator(this.id.rename(before, after), this.init.rename(before, after), this.leadingComments, this.trailingComments, this.loc, this.range);
}
}
exports.StepperVariableDeclarator = StepperVariableDeclarator;
// After all variable declarators have been contracted,
// StepperVariableDeclaration::contract triggers substitution
class StepperVariableDeclaration {
constructor(declarations, kind, leadingComments, trailingComments, loc, range) {
this.type = 'VariableDeclaration';
this.declarations = declarations;
this.kind = kind;
this.leadingComments = leadingComments;
this.trailingComments = trailingComments;
this.loc = loc;
this.range = range;
}
static create(node) {
return new StepperVariableDeclaration(node.declarations.map(node => (0, generator_1.convert)(node)), node.kind, node.leadingComments, node.trailingComments, node.loc, node.range);
}
isContractible() {
return false;
}
isOneStepPossible() {
return this.declarations
.map(x => x.isOneStepPossible())
.reduce((acc, next) => acc || next, false);
}
contract() {
__2.redex.preRedex = [this];
__2.redex.postRedex = [];
return __1.undefinedNode;
}
contractEmpty() {
__2.redex.preRedex = [this];
__2.redex.postRedex = [];
}
oneStep() {
// Find the one that is not contractible.
for (let i = 0; i < this.declarations.length; i++) {
const ast = this.declarations[i];
if (ast.isOneStepPossible()) {
return new StepperVariableDeclaration([
this.declarations.slice(0, i),
ast.oneStep(),
this.declarations.slice(i + 1)
].flat(), this.kind, this.leadingComments, this.trailingComments, this.loc, this.range);
}
}
return this;
}
substitute(id, value) {
return new StepperVariableDeclaration(this.declarations.map(declaration => declaration.substitute(id, value)), this.kind, this.leadingComments, this.trailingComments, this.loc, this.range);
}
freeNames() {
return Array.from(new Set(this.declarations.flatMap(ast => ast.freeNames())));
}
allNames() {
return Array.from(new Set(this.declarations.flatMap(ast => ast.allNames())));
}
rename(before, after) {
return new StepperVariableDeclaration(this.declarations.map(declaration => declaration.rename(before, after)), this.kind, this.leadingComments, this.trailingComments, this.loc, this.range);
}
}
exports.StepperVariableDeclaration = StepperVariableDeclaration;
//# sourceMappingURL=VariableDeclaration.js.map