js-slang
Version:
Javascript-based implementations of Source, written in Typescript
70 lines • 3.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StepperConditionalExpression = void 0;
const __1 = require("../..");
const generator_1 = require("../../generator");
class StepperConditionalExpression {
constructor(test, consequent, alternate, leadingComments, trailingComments, loc, range) {
this.type = 'ConditionalExpression';
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 StepperConditionalExpression((0, generator_1.convert)(node.test), (0, generator_1.convert)(node.consequent), (0, generator_1.convert)(node.alternate), node.leadingComments, node.trailingComments, node.loc, node.range);
}
isContractible() {
if (this.test.type !== 'Literal')
return false;
const test_value = this.test.value;
if (typeof test_value !== 'boolean') {
throw new Error(`Line ${this.loc?.start.line || 0}: Expected boolean as condition, got ${typeof test_value}.`);
}
__1.redex.preRedex = [this];
return true;
}
isOneStepPossible() {
return this.isContractible() || this.test.isOneStepPossible();
}
contract() {
__1.redex.preRedex = [this];
if (this.test.type !== 'Literal' || typeof this.test.value !== 'boolean') {
throw new Error('Cannot contract non-boolean literal test');
}
const result = this.test.value ? this.consequent : this.alternate;
__1.redex.postRedex = [result];
return result;
}
oneStep() {
if (this.isContractible()) {
return this.contract();
}
return new StepperConditionalExpression(this.test.oneStep(), this.consequent, this.alternate, this.leadingComments, this.trailingComments, this.loc, this.range);
}
substitute(id, value) {
return new StepperConditionalExpression(this.test.substitute(id, value), this.consequent.substitute(id, value), this.alternate.substitute(id, value), this.leadingComments, this.trailingComments, this.loc, this.range);
}
freeNames() {
return Array.from(new Set([
...this.test.freeNames(),
...this.consequent.freeNames(),
...this.alternate.freeNames()
]));
}
allNames() {
return Array.from(new Set([
...this.test.allNames(),
...this.consequent.allNames(),
...this.alternate.allNames()
]));
}
rename(before, after) {
return new StepperConditionalExpression(this.test.rename(before, after), this.consequent.rename(before, after), this.alternate.rename(before, after), this.leadingComments, this.trailingComments, this.loc, this.range);
}
}
exports.StepperConditionalExpression = StepperConditionalExpression;
//# sourceMappingURL=ConditionalExpression.js.map