@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
64 lines • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.reduceDfg = reduceDfg;
const graph_1 = require("../../dataflow/graph/graph");
const vertex_1 = require("../../dataflow/graph/vertex");
const objects_1 = require("../objects");
const assert_1 = require("../assert");
const defaultReduceOptions = {
vertices: {
tags: [...Object.values(vertex_1.VertexType)],
nameRegex: '.*',
blacklistWithName: false,
keepEnv: false,
keepCd: true,
compactFunctions: true
}
};
function makeFilter(options, idMap) {
const nameRegex = new RegExp(options.nameRegex);
return (arg) => {
if (!options.tags.includes(arg.tag)) {
return undefined;
}
const lexeme = idMap?.get(arg.id)?.lexeme;
if (lexeme && (nameRegex.test(lexeme) === options.blacklistWithName)) {
return undefined;
}
return {
...arg,
environment: options.keepEnv ? arg.environment : undefined,
controlDependencies: options.keepCd ? arg.controlDependencies : undefined,
functionInformation: options.compactFunctions ? arg.functionInformation : undefined
};
};
}
function reduceDfg(dfg, options) {
const newDfg = new graph_1.DataflowGraph(dfg.idMap);
const applyOptions = (0, objects_1.deepMergeObject)(defaultReduceOptions, options);
// overwrite the tag set if possible
if (options.vertices?.tags) {
applyOptions.vertices.tags = options.vertices.tags.filter(assert_1.isNotUndefined);
}
const applyFilter = makeFilter(applyOptions.vertices, dfg.idMap);
// go over the vertices
for (const [id, info] of dfg.vertices(!applyOptions)) {
const result = applyFilter(info);
if (result) {
newDfg.addVertex(result, dfg.isRoot(id));
}
}
for (const [from, out] of dfg.edges()) {
if (!newDfg.hasVertex(from)) {
continue;
}
for (const [to, { types }] of out) {
if (!newDfg.hasVertex(to)) {
continue;
}
newDfg.addEdge(from, to, types);
}
}
return newDfg;
}
//# sourceMappingURL=dfg-view.js.map