UNPKG

expression-evaluation

Version:
40 lines (39 loc) 1.11 kB
export 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; } }