calcium-lang
Version:
Calcium language interpreter
60 lines • 1.53 kB
JavaScript
/**
* saves variables, functions, and so on in a specific scope
*/
export default class Namespace {
/**
*
* @param parent nesting scope
*/
constructor(parent, isClassScope = false) {
this.parent = parent;
this.isClassScope = isClassScope;
/**
* saves by key value pairs
*/
this.dict = new Map();
}
/**
* searches an attribute in a class scope
* @param key attribute's name
* @returns
*/
get(key) {
return this.dict.get(key);
}
/**
* searches identifier and return its value
* @param key identifier
*/
lookUp(key) {
var _a;
const value = this.dict.get(key);
if (value !== undefined) {
return value;
}
else {
return (_a = this.parent) === null || _a === void 0 ? void 0 : _a.lookUp(key);
}
}
/**
*
* @param key identifier
* @param value right hand side of assignment
*/
register(key, value) {
this.dict.set(key, value);
}
/**
* the parent scope of a function or a method.
* When this namespace is not a class scope, then returns this.
*/
get nestingScope() {
var _a;
let scope = this;
while ((_a = scope === null || scope === void 0 ? void 0 : scope.parent) === null || _a === void 0 ? void 0 : _a.isClassScope) {
scope = scope.parent;
}
return scope;
}
}
//# sourceMappingURL=namespace.js.map