UNPKG

@eagleoutice/flowr-dev

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

90 lines 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.bottomTopGuard = bottomTopGuard; exports.valueSetGuard = valueSetGuard; exports.soleValue = soleValue; exports.valueFromTsValue = valueFromTsValue; exports.valueFromRNodeConstant = valueFromRNodeConstant; const type_1 = require("../../../r-bridge/lang-4.x/ast/model/type"); const interval_constants_1 = require("./intervals/interval-constants"); const logical_constants_1 = require("./logical/logical-constants"); const r_value_1 = require("./r-value"); const string_constants_1 = require("./string/string-constants"); const vector_constants_1 = require("./vectors/vector-constants"); /** * Takes n potentially lifted ops and returns `Top` or `Bottom` if any is `Top` or `Bottom`. */ function bottomTopGuard(...a) { if (a.some(r_value_1.isBottom)) { return r_value_1.Bottom; } else if (a.some(r_value_1.isTop)) { return r_value_1.Top; } } /** * Returns a value set, if a is not bottom or top, otherwise undefined. * Useful when working with values returned by {@link Resolve.toValue} * @param a - value set to check * @returns value set if a is not top or bottom */ function valueSetGuard(a) { return ((0, r_value_1.isBottom)(a) || (0, r_value_1.isTop)(a)) ? undefined : a; } /** * The one value a set holds, `undefined` unless it holds exactly one, optionally of the given kind. * @param set - the set to take the value from * @param type - the kind the value has to have, any kind if unset * @returns the sole value, `undefined` if the set holds another number of them or another kind */ function soleValue(set, type) { const only = set?.elements.length === 1 ? set.elements[0] : undefined; return only !== undefined && (type === undefined || only.type === type) ? only : undefined; } /** * Constructs an Abstract Value from a normal TS value * @param a - ts value * @returns abstract value */ function valueFromTsValue(a) { if (a === undefined) { return r_value_1.Bottom; } else if (a === null) { return { type: 'null' }; } else if (typeof a === 'string') { return (0, string_constants_1.stringFrom)(a); } else if (typeof a === 'number') { return (0, interval_constants_1.intervalFrom)(a, a); } else if (typeof a === 'boolean') { return a ? logical_constants_1.ValueLogicalTrue : logical_constants_1.ValueLogicalFalse; } else if (Array.isArray(a)) { return (0, vector_constants_1.vectorFrom)(a.map(v => valueFromTsValue(v))); } return r_value_1.Top; } /** * Converts a constant from an RNode into an abstract value * @param a - RNode constant * @returns abstract value */ function valueFromRNodeConstant(a) { if (a.type === type_1.RType.String) { return (0, string_constants_1.stringFrom)(a.content.str); } else if (a.type === type_1.RType.Number) { return (0, interval_constants_1.intervalFrom)(a.content.num, a.content.num); } else if (a.type === type_1.RType.Logical) { return a.content.valueOf() ? logical_constants_1.ValueLogicalTrue : logical_constants_1.ValueLogicalFalse; } else if (a.type === type_1.RType.FunctionDefinition) { return { type: 'function-definition' }; } return r_value_1.Top; } //# sourceMappingURL=general.js.map