@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
59 lines (54 loc) • 3.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCfg = getCfg;
exports.printCfg = printCfg;
exports.printCfgCode = printCfgCode;
const extract_cfg_1 = require("../../control-flow/extract-cfg");
const default_pipelines_1 = require("../../core/steps/pipeline/default-pipelines");
const time_1 = require("../../util/text/time");
const doc_files_1 = require("./doc-files");
const cfg_1 = require("../../util/mermaid/cfg");
const doc_code_1 = require("./doc-code");
const cfg_simplification_1 = require("../../control-flow/cfg-simplification");
const flowr_analyzer_context_1 = require("../../project/context/flowr-analyzer-context");
/**
* Returns the control flow graph for the given code.
*/
async function getCfg(parser, code, simplifications = [], useDfg = true) {
const context = (0, flowr_analyzer_context_1.contextFromInput)(code);
const result = await (useDfg ? (0, default_pipelines_1.createDataflowPipeline)(parser, { context })
: (0, default_pipelines_1.createNormalizePipeline)(parser, { context })).allRemainingSteps();
const cfg = (0, extract_cfg_1.extractCfg)(result.normalize, context, useDfg ? result.dataflow.graph : undefined, [...cfg_simplification_1.DefaultCfgSimplificationOrder, ...simplifications]);
return {
info: cfg,
ast: result.normalize,
dataflow: 'dataflow' in result ? result.dataflow : undefined
};
}
/**
* Serializes the given control flow graph to a mermaid diagram.
*/
function printCfg(cfg, ast, prefix = 'flowchart BT\n', simplify = false) {
return `
${(0, doc_code_1.codeBlock)('mermaid', (0, cfg_1.cfgToMermaid)(cfg, ast, { prefix, simplify }))}
`;
}
/**
* Generates and prints the control flow graph for the given code, along with optional metadata and the original code.
*/
async function printCfgCode(parser, code, { showCode = true, openCode = false, prefix = 'flowchart BT\n', simplifications = [], simplify = false, useDfg = true } = {}) {
const now = performance.now();
const res = await getCfg(parser, code, simplifications, useDfg);
const duration = performance.now() - now;
const metaInfo = `The analysis required _${(0, time_1.printAsMs)(duration)}_ (including the ${useDfg ? 'dataflow analysis, ' : ''} normalization${useDfg ? ', ' : ''} and parsing with the [${parser.name}](${doc_files_1.FlowrWikiBaseRef}/Engines) engine) within the generation environment.
We used the following simplification${(simplifications?.length ?? 0) + cfg_simplification_1.DefaultCfgSimplificationOrder.length != 1 ? 's' : ''}: ${[...cfg_simplification_1.DefaultCfgSimplificationOrder, ...simplifications].map(s => '`' + s + '`').join(', ')} ${simplify ? ' and render a simplified/compacted version' : ''}.
`;
return '\n\n' + printCfg(res.info, res.ast, prefix, simplify) + (showCode ? `
<details${openCode ? ' open' : ''}>
<summary style="color:gray">R Code of the CFG</summary>
${metaInfo}
${(0, doc_code_1.codeBlock)('r', code)}
</details>
` : '\n_(' + metaInfo + ')_\n\n');
}
//# sourceMappingURL=doc-cfg.js.map