@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
56 lines • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.assignments = exports.AssignmentOperators = void 0;
const common_syntax_probability_1 = require("../../common-syntax-probability");
const post_process_1 = require("./post-process");
const type_1 = require("../../../../r-bridge/lang-4.x/ast/model/type");
const operators_1 = require("../../../../r-bridge/lang-4.x/ast/model/operators");
const model_1 = require("../../../../r-bridge/lang-4.x/ast/model/model");
const initialAssignmentInfo = {
// operator to occurrence count
assignmentOperator: {},
assigned: (0, common_syntax_probability_1.emptyCommonSyntaxTypeCounts)(),
// find combinations like `` is most often used for functions?
deepestNesting: 0,
nestedOperatorAssignment: 0
};
exports.AssignmentOperators = new Set(operators_1.Operators.filter(op => operators_1.OperatorDatabase[op].usedAs === 'assignment'));
function visitAssignment(info, input) {
const assignmentStack = [];
model_1.RNode.visitAst(input.normalizedRAst.ast.files.map(r => r.root), node => {
if (node.type !== type_1.RType.BinaryOp || !exports.AssignmentOperators.has(node.operator)) {
return;
}
if (assignmentStack.length > 0) {
info.nestedOperatorAssignment++;
info.deepestNesting = Math.max(info.deepestNesting, assignmentStack.length);
}
assignmentStack.push(node);
info.assignmentOperator[node.operator] = (info.assignmentOperator[node.operator] ?? 0n) + 1n;
switch (node.operator) {
case '->':
case '->>':
info.assigned = (0, common_syntax_probability_1.updateCommonSyntaxTypeCounts)(info.assigned, node.lhs);
break;
default:
info.assigned = (0, common_syntax_probability_1.updateCommonSyntaxTypeCounts)(info.assigned, node.rhs);
break;
}
}, node => {
// drop again :D
if (node.type === type_1.RType.BinaryOp && node.flavor === 'assignment') {
assignmentStack.pop();
}
});
}
exports.assignments = {
name: 'Assignments',
description: 'all ways to assign something in R',
process(existing, input) {
visitAssignment(existing, input);
return existing;
},
initialValue: initialAssignmentInfo,
postProcess: post_process_1.postProcess
};
//# sourceMappingURL=assignments.js.map