You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
960 B
37 lines
960 B
// @flow
|
|
|
|
import type {Expression} from './expression.js';
|
|
|
|
/**
|
|
* Tracks `let` bindings during expression parsing.
|
|
* @private
|
|
*/
|
|
class Scope {
|
|
parent: ?Scope;
|
|
bindings: {[_: string]: Expression};
|
|
constructor(parent?: Scope, bindings: Array<[string, Expression]> = []) {
|
|
this.parent = parent;
|
|
this.bindings = {};
|
|
for (const [name, expression] of bindings) {
|
|
this.bindings[name] = expression;
|
|
}
|
|
}
|
|
|
|
concat(bindings: Array<[string, Expression]>): Scope {
|
|
return new Scope(this, bindings);
|
|
}
|
|
|
|
get(name: string): Expression {
|
|
if (this.bindings[name]) { return this.bindings[name]; }
|
|
if (this.parent) { return this.parent.get(name); }
|
|
throw new Error(`${name} not found in scope.`);
|
|
}
|
|
|
|
has(name: string): boolean {
|
|
if (this.bindings[name]) return true;
|
|
return this.parent ? this.parent.has(name) : false;
|
|
}
|
|
}
|
|
|
|
export default Scope;
|