UNPKG

@eagleoutice/flowr

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

90 lines 3.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Bottom = exports.Top = void 0; exports.typeOfValue = typeOfValue; exports.isTop = isTop; exports.isBottom = isBottom; exports.isValue = isValue; exports.asValue = asValue; exports.stringifyValue = stringifyValue; const assert_1 = require("../../../util/assert"); exports.Top = { type: Symbol('⊤') }; exports.Bottom = { type: Symbol('⊥') }; function typeOfValue(value) { return value.type; } // @ts-expect-error -- this is a save cast function isTop(value) { return value === exports.Top; } // @ts-expect-error -- this is a save cast function isBottom(value) { return value === exports.Bottom; } function isValue(value) { return !isTop(value) && !isBottom(value); } function asValue(value) { (0, assert_1.guard)(isValue(value), 'Expected a value, but got a top or bottom value'); return value; } function tryStringifyBoTop(value, otherwise, onTop = () => '⊤', onBottom = () => '⊥') { if (isTop(value)) { return onTop(); } else if (isBottom(value)) { return onBottom(); } else { return otherwise(value); } } function stringifyRNumberSuffix(value) { let suffix = ''; if (value.markedAsInt) { suffix += 'L'; } if (value.complexNumber) { suffix += 'i'; } // do something about iL even though it is impossible? return suffix; } function renderString(value) { const quote = value.quotes; const raw = value.flag === 'raw'; if (raw) { return `r${quote}(${value.str})${quote}`; } else { return `${quote}${JSON.stringify(value.str).slice(1, -1)}${quote}`; } } function stringifyValue(value) { return tryStringifyBoTop(value, v => { const t = v.type; switch (t) { case 'interval': return `${v.startInclusive ? '[' : '('}${stringifyValue(v.start)}, ${stringifyValue(v.end)}${v.endInclusive ? ']' : ')'}`; case 'vector': return tryStringifyBoTop(v.elements, e => { return `<${stringifyValue(v.elementDomain)}> c(${e.map(stringifyValue).join(',')})`; }, () => `⊤ (vector, ${stringifyValue(v.elementDomain)})`, () => `⊥ (vector, ${stringifyValue(v.elementDomain)})`); case 'set': return tryStringifyBoTop(v.elements, e => { return e.length === 1 ? stringifyValue(e[0]) : `{ ${e.map(stringifyValue).join(',')} }`; }, () => '⊤ (set)', () => '⊥ (set)'); case 'number': return tryStringifyBoTop(v.value, n => `${n.num}${stringifyRNumberSuffix(n)}`, () => '⊤ (number)', () => '⊥ (number)'); case 'string': return tryStringifyBoTop(v.value, renderString, () => '⊤ (string)', () => '⊥ (string)'); case 'logical': return tryStringifyBoTop(v.value, l => l === 'maybe' ? 'maybe' : l ? 'TRUE' : 'FALSE', () => '⊤ (logical)', () => '⊥ (logical)'); case 'missing': return '(missing)'; default: (0, assert_1.assertUnreachable)(t); } }); } //# sourceMappingURL=r-value.js.map