@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
95 lines • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Environment = void 0;
exports.makeReferenceMaybe = makeReferenceMaybe;
exports.makeAllMaybe = makeAllMaybe;
exports.isDefaultBuiltInEnvironment = isDefaultBuiltInEnvironment;
exports.initializeCleanEnvironments = initializeCleanEnvironments;
exports.builtInEnvJsonReplacer = builtInEnvJsonReplacer;
const identifier_1 = require("./identifier");
const resolve_by_name_1 = require("./resolve-by-name");
const json_1 = require("../../util/json");
const built_in_config_1 = require("./built-in-config");
/**
* Marks the reference as maybe (i.e., as controlled by a set of {@link IdentifierReference#controlDependencies|control dependencies}).
*/
function makeReferenceMaybe(ref, graph, environments, includeDefs, defaultCd = undefined) {
if (includeDefs) {
const definitions = ref.name ? (0, resolve_by_name_1.resolveByName)(ref.name, environments, ref.type) : undefined;
for (const definition of definitions ?? []) {
if (definition.type !== identifier_1.ReferenceType.BuiltInFunction && definition.type !== identifier_1.ReferenceType.BuiltInConstant) {
if (definition.controlDependencies && defaultCd && !definition.controlDependencies.find(c => c.id === defaultCd.id)) {
definition.controlDependencies.push(defaultCd);
}
else {
definition.controlDependencies = defaultCd ? [defaultCd] : [];
}
}
}
}
const node = graph.get(ref.nodeId, true);
if (node) {
const [fst] = node;
if (fst.cds && defaultCd && !fst.cds.includes(defaultCd)) {
fst.cds.push(defaultCd);
}
else {
fst.cds = defaultCd ? [defaultCd] : [];
}
}
return { ...ref, controlDependencies: [...ref.controlDependencies ?? [], ...(defaultCd ? [defaultCd] : [])] };
}
function makeAllMaybe(references, graph, environments, includeDefs, defaultCd = undefined) {
if (references === undefined) {
return [];
}
return references.map(ref => makeReferenceMaybe(ref, graph, environments, includeDefs, defaultCd));
}
/**
* Please use this function only if you do not know the object type.
* Otherwise, rely on {@link IEnvironment#builtInEnv}
*/
function isDefaultBuiltInEnvironment(obj) {
return typeof obj === 'object' && obj !== null && (obj.builtInEnv === true);
}
let environmentIdCounter = 1; // Zero is reserved for built-in environment
/** @see REnvironmentInformation */
class Environment {
id;
parent;
memory;
builtInEnv;
constructor(parent, isBuiltInDefault = undefined) {
this.id = isBuiltInDefault ? 0 : environmentIdCounter++;
this.parent = parent;
this.memory = new Map();
// do not store if not needed!
if (isBuiltInDefault) {
this.builtInEnv = isBuiltInDefault;
}
}
}
exports.Environment = Environment;
/**
* Initialize a new {@link REnvironmentInformation|environment} with the built-ins.
*/
function initializeCleanEnvironments(memory, fullBuiltIns = true) {
const builtInEnv = new Environment(undefined, true);
builtInEnv.memory = memory ?? (fullBuiltIns ? (0, built_in_config_1.getDefaultBuiltInDefinitions)().builtInMemory : (0, built_in_config_1.getDefaultBuiltInDefinitions)().emptyBuiltInMemory);
return {
current: new Environment(builtInEnv),
level: 0
};
}
/**
* Helps to serialize an environment, but replaces the built-in environment with a placeholder.
*/
function builtInEnvJsonReplacer(k, v) {
if (isDefaultBuiltInEnvironment(v)) {
return '<BuiltInEnvironment>';
}
else {
return (0, json_1.jsonReplacer)(k, v);
}
}
//# sourceMappingURL=environment.js.map