@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
161 lines (160 loc) • 6.46 kB
JavaScript
;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports._getMinUnboundDbn = exports.groupByScope = void 0;
var IRApp_1 = require("../../../IRNodes/IRApp.js");
var IRCase_1 = require("../../../IRNodes/IRCase.js");
var IRConstr_1 = require("../../../IRNodes/IRConstr.js");
var IRDelayed_1 = require("../../../IRNodes/IRDelayed.js");
var IRForced_1 = require("../../../IRNodes/IRForced.js");
var IRFunc_1 = require("../../../IRNodes/IRFunc.js");
var IRLetted_1 = require("../../../IRNodes/IRLetted.js");
var IRRecursive_1 = require("../../../IRNodes/IRRecursive.js");
var IRSelfCall_1 = require("../../../IRNodes/IRSelfCall.js");
var IRVar_1 = require("../../../IRNodes/IRVar.js");
var mapArrayLike_1 = require("../../../IRNodes/utils/mapArrayLike.js");
function groupByScope(letteds) {
var e_1, _a;
var scopes = [];
function pushScope(scope, letted) {
var scopeEntry = scopes.find(function (entry) { return entry.maxScope === scope; });
if (scopeEntry === undefined) {
scopes.push({
maxScope: scope,
group: [letted]
});
return;
}
scopeEntry.group.push(letted);
}
try {
for (var letteds_1 = __values(letteds), letteds_1_1 = letteds_1.next(); !letteds_1_1.done; letteds_1_1 = letteds_1.next()) {
var _b = letteds_1_1.value, letted = _b.letted, nReferences = _b.nReferences;
var minUnboundDbn = _getMinUnboundDbn(letted.value);
if (minUnboundDbn === undefined) {
pushScope(undefined, { letted: letted, nReferences: nReferences });
continue;
}
var maxScope = letted.parent;
if (maxScope instanceof IRFunc_1.IRFunc ||
maxScope instanceof IRRecursive_1.IRRecursive) {
minUnboundDbn -= maxScope.arity;
}
while (minUnboundDbn >= 0) {
maxScope = maxScope === null || maxScope === void 0 ? void 0 : maxScope.parent;
if (maxScope instanceof IRFunc_1.IRFunc ||
maxScope instanceof IRRecursive_1.IRRecursive) {
minUnboundDbn -= maxScope.arity;
}
}
pushScope(maxScope, { letted: letted, nReferences: nReferences });
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (letteds_1_1 && !letteds_1_1.done && (_a = letteds_1.return)) _a.call(letteds_1);
}
finally { if (e_1) throw e_1.error; }
}
return scopes;
}
exports.groupByScope = groupByScope;
/**
*
* @param _term
* @returns {number | undefined}
* a `number` if the `_term` param is open;
* represents the debuijn index an `IRVar` would have
* in order to point to the smallest scope that fulfills the term;
*
* `undefined` if the term is closed
*/
function _getMinUnboundDbn(_term) {
var minDbn = undefined;
var stack = [{ term: _term, dbn: 0 }];
var _loop_1 = function () {
var _a = stack.pop(), term = _a.term, dbn = _a.dbn;
if (term instanceof IRVar_1.IRVar ||
term instanceof IRSelfCall_1.IRSelfCall) {
if (term.dbn >= dbn) // some val outside
{
var outsideDbn = term.dbn - dbn;
minDbn = minDbn === undefined ? outsideDbn : Math.min(outsideDbn, minDbn);
}
}
if (term instanceof IRApp_1.IRApp) {
stack.push({ term: term.fn, dbn: dbn }, { term: term.arg, dbn: dbn });
return "continue";
}
if (term instanceof IRCase_1.IRCase) {
stack.push.apply(stack, __spreadArray([{ term: term.constrTerm, dbn: dbn }], __read((0, mapArrayLike_1.mapArrayLike)(term.continuations, function (continuation) { return ({ term: continuation, dbn: dbn }); })), false));
return "continue";
}
if (term instanceof IRConstr_1.IRConstr) {
stack.push.apply(stack, __spreadArray([], __read((0, mapArrayLike_1.mapArrayLike)(term.fields, function (field) { return ({ term: field, dbn: dbn }); })), false));
return "continue";
}
if (term instanceof IRDelayed_1.IRDelayed) {
stack.push({ term: term.delayed, dbn: dbn });
return "continue";
}
if (term instanceof IRForced_1.IRForced) {
stack.push({ term: term.forced, dbn: dbn });
return "continue";
}
if (term instanceof IRFunc_1.IRFunc) {
stack.push({ term: term.body, dbn: dbn + term.arity });
return "continue";
}
if (term instanceof IRRecursive_1.IRRecursive) {
stack.push({ term: term.body, dbn: dbn + term.arity });
return "continue";
}
// letted terms do count too
if (term instanceof IRLetted_1.IRLetted) {
stack.push({ term: term.value, dbn: dbn });
return "continue";
}
};
while (stack.length > 0) {
_loop_1();
}
return minDbn;
}
exports._getMinUnboundDbn = _getMinUnboundDbn;