@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
104 lines (103 loc) • 4.28 kB
JavaScript
;
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.incrementUnboundDbns = void 0;
var IRVar_1 = require("../../../IRNodes/IRVar.js");
var IRLetted_1 = require("../../../IRNodes/IRLetted.js");
var IRApp_1 = require("../../../IRNodes/IRApp.js");
var IRDelayed_1 = require("../../../IRNodes/IRDelayed.js");
var IRForced_1 = require("../../../IRNodes/IRForced.js");
var IRFunc_1 = require("../../../IRNodes/IRFunc.js");
var IRCase_1 = require("../../../IRNodes/IRCase.js");
var mapArrayLike_1 = require("../../../IRNodes/utils/mapArrayLike.js");
var IRConstr_1 = require("../../../IRNodes/IRConstr.js");
var IRRecursive_1 = require("../../../IRNodes/IRRecursive.js");
var IRSelfCall_1 = require("../../../IRNodes/IRSelfCall.js");
/**
* add 1 to every var's DeBruijn that accesses stuff outside the parent node
* not including the `parentNode` node
* since the new function introdcued substituting the letted term
* is added inside the `parentNode` node
**/
function incrementUnboundDbns(theTerm, shouldNotModifyLetted) {
var stack = [{ term: theTerm, dbn: 0 }];
var _loop_1 = function () {
var _a = stack.pop(), t = _a.term, dbn = _a.dbn;
if ((t instanceof IRVar_1.IRVar ||
t instanceof IRSelfCall_1.IRSelfCall) &&
t.dbn >= dbn) {
// there's a new variable in scope
t.dbn++;
return "continue";
}
if (t instanceof IRLetted_1.IRLetted) {
if (shouldNotModifyLetted(t, dbn)) {
return "continue";
}
else // other letted to be handled in one of the next cycles
{
// `IRLambdas` DeBruijn are tracking the level of instantiation
// we add a new variable so the dbn of instantiation increments
t.dbn += 1;
stack.push({ term: t.value, dbn: dbn });
}
return "continue";
}
if (t instanceof IRApp_1.IRApp) {
stack.push({ term: t.arg, dbn: dbn }, { term: t.fn, dbn: dbn });
return "continue";
}
if (t instanceof IRCase_1.IRCase) {
stack.push.apply(stack, __spreadArray([{ term: t.constrTerm, dbn: dbn }], __read((0, mapArrayLike_1.mapArrayLike)(t.continuations, function (continuation) { return ({ term: continuation, dbn: dbn }); })), false));
return "continue";
}
if (t instanceof IRConstr_1.IRConstr) {
stack.push.apply(stack, __spreadArray([], __read((0, mapArrayLike_1.mapArrayLike)(t.fields, function (field) { return ({ term: field, dbn: dbn }); })), false));
return "continue";
}
if (t instanceof IRDelayed_1.IRDelayed) {
stack.push({ term: t.delayed, dbn: dbn });
return "continue";
}
if (t instanceof IRForced_1.IRForced) {
stack.push({ term: t.forced, dbn: dbn });
return "continue";
}
if (t instanceof IRFunc_1.IRFunc) {
stack.push({ term: t.body, dbn: dbn + t.arity });
return "continue";
}
if (t instanceof IRRecursive_1.IRRecursive) {
stack.push({ term: t.body, dbn: dbn + t.arity });
return "continue";
}
};
while (stack.length > 0) {
_loop_1();
}
}
exports.incrementUnboundDbns = incrementUnboundDbns;