@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
147 lines (146 loc) • 5.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._modifyChildFromTo = void 0;
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 IRHoisted_1 = require("../../IRNodes/IRHoisted.js");
var IRLetted_1 = require("../../IRNodes/IRLetted.js");
var utils_1 = require("../../utils/index.js");
var IRConstr_1 = require("../../IRNodes/IRConstr.js");
var IRCase_1 = require("../../IRNodes/IRCase.js");
var IRHash_1 = require("../../IRHash.js");
var IRRecursive_1 = require("../../IRNodes/IRRecursive.js");
/**
*
* @param parent node to modify the child of
* @param currentChild mainly passed to distinguish in case of `IRApp`
* @param newChild new node's child
*/
function _modifyChildFromTo(parent, currentChild, newChild) {
if (parent === undefined) {
throw new Error("'_modifyChildFromTo' received an undefined parent, possibly root");
}
if (
// currentChild has parent property
(0, utils_1.isIRTerm)(currentChild) &&
// and is not (already) undefined
currentChild.parent !== undefined &&
// and the `parent` passed to the function is the parent of the `currentChild`
currentChild.parent === parent &&
// and child to be modified is not the same object
currentChild !== newChild) {
// we are modifying the child
// so we remove parent the reference
currentChild.parent = undefined;
}
// DO NOT USE **ONLY** SHALLOW EQUALITY
// child might be cloned
// however delay hash computation until needed
var _childHash;
var currChildHash = function () {
if ((0, IRHash_1.isIRHash)(_childHash))
return _childHash;
_childHash = (0, IRHash_1.isIRHash)(currentChild) ? currentChild : currentChild.hash;
return _childHash;
};
if (parent instanceof IRApp_1.IRApp) {
// check the argument first as it is more likely to have a smaller tree
if (currentChild === parent.arg) {
parent.arg = newChild;
}
else if (currentChild === parent.fn) {
parent.fn = newChild;
}
else if ((0, IRHash_1.equalIrHash)(parent.arg.hash, currChildHash())) {
parent.arg = newChild;
}
else if ((0, IRHash_1.equalIrHash)(parent.fn.hash, currChildHash())) {
parent.fn = newChild;
}
else {
console.log("currentChild:", (0, utils_1.prettyIRJsonStr)(currentChild, 2, { hoisted: false }), "\nfn :", (0, utils_1.prettyIRJsonStr)(parent.fn, 2, { hoisted: false }), "\narg:", (0, utils_1.prettyIRJsonStr)(parent.arg, 2, { hoisted: false }));
throw new Error("unknown 'IRApp' child to modify; given child to modify hash: " +
(0, IRHash_1.irHashToHex)(currChildHash()) +
"; function child hash: " + (0, IRHash_1.irHashToHex)(parent.fn.hash) +
"; argument child hash: " + (0, IRHash_1.irHashToHex)(parent.arg.hash));
}
return;
}
else if (parent instanceof IRConstr_1.IRConstr) {
var foundChild = false;
for (var i = 0; i < parent.fields.length; i++) {
var field = parent.fields[i];
if (field === currentChild) {
parent.fields[i] = newChild;
foundChild = true;
break;
}
}
if (foundChild)
return;
for (var i = 0; i < parent.fields.length; i++) {
var field = parent.fields[i];
if ((0, IRHash_1.equalIrHash)(field.hash, currChildHash())) {
parent.fields[i] = newChild;
break;
}
}
return;
}
else if (parent instanceof IRCase_1.IRCase) {
if (parent.constrTerm === currentChild) {
parent.constrTerm = newChild;
return;
}
var foundChild = false;
for (var i = 0; i < parent.continuations.length; i++) {
var field = parent.continuations[i];
if (field === currentChild) {
parent.continuations[i] = newChild;
foundChild = true;
break;
}
}
if (foundChild)
return;
if ((0, IRHash_1.equalIrHash)(currChildHash(), parent.constrTerm.hash)) {
parent.constrTerm = newChild;
return;
}
for (var i = 0; i < parent.continuations.length; i++) {
var field = parent.continuations[i];
if ((0, IRHash_1.equalIrHash)(field.hash, currChildHash())) {
parent.continuations[i] = newChild;
break;
}
}
return;
}
if (parent instanceof IRDelayed_1.IRDelayed) {
parent.delayed = newChild;
return;
}
if (parent instanceof IRForced_1.IRForced) {
parent.forced = newChild;
return;
}
if (parent instanceof IRFunc_1.IRFunc) {
parent.body = newChild;
return;
}
if (parent instanceof IRRecursive_1.IRRecursive) {
parent.body = newChild;
return;
}
if (parent instanceof IRHoisted_1.IRHoisted) {
parent.hoisted = newChild;
return;
}
if (parent instanceof IRLetted_1.IRLetted) {
parent.value = newChild;
return;
}
}
exports._modifyChildFromTo = _modifyChildFromTo;