expression-evaluation
Version:
Expression Evaluation
44 lines (43 loc) • 1.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StaticScope = void 0;
class StaticScope {
_superscope;
_subscopes = [];
_variables = new Map();
_locals = new Set();
has(name) {
return this._variables.has(name) || Boolean(this._superscope?.has(name));
}
get(name) {
return this._variables.get(name) ?? this._superscope?.get(name);
}
global(name, variable) {
this._superscope?.global(name, variable) ?? this._variables.set(name, variable);
return this;
}
local(name, variable) {
this._variables.set(name, variable);
this._locals.add(name);
return this;
}
subscope(variables) {
const scope = new StaticScope();
scope._superscope = this;
this._subscopes.push(scope);
for (const [name, variable] of variables) {
scope.local(name, variable);
}
return scope;
}
variables() {
const variables = {};
for (const [name, variable] of this._variables) {
if (!this._locals.has(name)) {
variables[name] = variable;
}
}
return variables;
}
}
exports.StaticScope = StaticScope;