@palasimi/ipa-cluster
Version:
Cluster words with similar IPA transcriptions together
54 lines • 1.52 kB
JavaScript
;
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2023 Levi Gruspe
// Variable scopes.
Object.defineProperty(exports, "__esModule", { value: true });
exports.Scope = exports.NameError = void 0;
/**
* Exception thrown by `Scope` when trying to resolve an undefined variable.
*/
class NameError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
}
}
exports.NameError = NameError;
/**
* Represents a scope of variable definitions.
*/
class Scope {
constructor(outer = null) {
this.names = new Map();
this.outer = outer;
}
/**
* Defines a variable in the present scope.
* Redefining a variable name is an error.
* Masking variables defined in outer scopes is not an error.
* Returns `true` if there are no errors.
*/
define(name, value) {
if (this.names.has(name)) {
return false;
}
this.names.set(name, value);
return true;
}
/**
* Tries to resolve a variable name.
* Throws a `NameError` if the variable is not defined.
* Returns the value assigned to the variable.
*/
resolve(name) {
if (this.names.has(name)) {
return this.names.get(name);
}
if (this.outer != null) {
return this.outer.resolve(name);
}
throw new NameError(`variable '${name}' is not defined`);
}
}
exports.Scope = Scope;
//# sourceMappingURL=scopes.js.map