toylang
Version:
A toy programming language built with TypeScript for learning purposes
42 lines (41 loc) • 1.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Environment = void 0;
var RuntimeError_1 = require("./RuntimeError");
var Environment = /** @class */ (function () {
function Environment(enclosing) {
if (enclosing === void 0) { enclosing = null; }
this.values = new Map();
this.enclosing = enclosing;
}
Environment.prototype.add = function (name, value) {
this.values.set(name, value);
};
Environment.prototype.remove = function (name) {
this.values.delete(name);
};
Environment.prototype.get = function (name) {
var _a;
if (this.values.has(name)) {
return this.values.get(name);
}
if (this.enclosing !== null) {
return (_a = this.enclosing) === null || _a === void 0 ? void 0 : _a.get(name);
}
throw new RuntimeError_1.RuntimeError(name, "Undeclared variable \"" + name + "\"");
};
Environment.prototype.assign = function (name, value) {
var _a;
if (this.values.has(name)) {
this.values.set(name, value);
return;
}
if (this.enclosing !== null) {
(_a = this.enclosing) === null || _a === void 0 ? void 0 : _a.assign(name, value);
return;
}
throw new RuntimeError_1.RuntimeError(name, "Undeclared variable \"" + name + "\"");
};
return Environment;
}());
exports.Environment = Environment;