js-slang
Version:
Javascript-based implementations of Source, written in Typescript
88 lines • 3.86 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StepperIfStatement = void 0;
const __1 = require("..");
const generator_1 = require("../../generator");
const __2 = require("../..");
const Literal_1 = require("../Expression/Literal");
const BlockStatement_1 = require("./BlockStatement");
const ExpressionStatement_1 = require("./ExpressionStatement");
class StepperIfStatement {
constructor(test, consequent, alternate, leadingComments, trailingComments, loc, range) {
this.type = 'IfStatement';
this.test = test;
this.consequent = consequent;
this.alternate = alternate;
this.leadingComments = leadingComments;
this.trailingComments = trailingComments;
this.loc = loc;
this.range = range;
}
static create(node) {
return new StepperIfStatement((0, generator_1.convert)(node.test), (0, generator_1.convert)(node.consequent), node.alternate ? (0, generator_1.convert)(node.alternate) : null, node.leadingComments, node.trailingComments, node.loc, node.range);
}
isContractible() {
return this.test instanceof Literal_1.StepperLiteral;
}
contract() {
if (!(this.test instanceof Literal_1.StepperLiteral)) {
throw new Error('Cannot contract non-literal test');
}
__2.redex.preRedex = [this];
const result = this.test.value ? this.consequent : this.alternate || __1.undefinedNode;
if (result instanceof BlockStatement_1.StepperBlockStatement) {
__2.redex.postRedex = [result];
return new BlockStatement_1.StepperBlockStatement([
new ExpressionStatement_1.StepperExpressionStatement(__1.undefinedNode, undefined, undefined, this.loc, this.range),
...result.body
], result.innerComments, this.leadingComments, this.trailingComments, this.loc, this.range);
}
else if (result instanceof StepperIfStatement) {
// else if statement
return result;
}
else {
throw new Error('Cannot contract to non-block statement');
}
}
isOneStepPossible() {
return this.isContractible() || this.test.isOneStepPossible();
}
oneStep() {
if (!this.isOneStepPossible()) {
throw new Error('No step possible in test');
}
if (this.isContractible()) {
return this.contract();
}
return new StepperIfStatement(this.test.oneStep(), this.consequent, this.alternate, this.leadingComments, this.trailingComments, this.loc, this.range);
}
substitute(id, value) {
return new StepperIfStatement(this.test.substitute(id, value), this.consequent.substitute(id, value), this.alternate ? this.alternate.substitute(id, value) : null, this.leadingComments, this.trailingComments, this.loc, this.range);
}
contractEmpty() {
__2.redex.preRedex = [this];
__2.redex.postRedex = [];
}
freeNames() {
const names = new Set([
...this.test.freeNames(),
...this.consequent.freeNames(),
...(this.alternate ? this.alternate.freeNames() : [])
]);
return Array.from(names);
}
allNames() {
const names = new Set([
...this.test.allNames(),
...this.consequent.allNames(),
...(this.alternate ? this.alternate.allNames() : [])
]);
return Array.from(names);
}
rename(before, after) {
return new StepperIfStatement(this.test.rename(before, after), this.consequent.rename(before, after), this.alternate ? this.alternate.rename(before, after) : null, this.leadingComments, this.trailingComments, this.loc, this.range);
}
}
exports.StepperIfStatement = StepperIfStatement;
//# sourceMappingURL=IfStatement.js.map