UNPKG

@harmoniclabs/plu-ts-onchain

Version:

An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript

94 lines (93 loc) 3.92 kB
"use strict"; 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."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.hoistForcedNatives = void 0; var IRNodes_1 = require("../../IRNodes/index.js"); var IRFunc_1 = require("../../IRNodes/IRFunc.js"); var IRNative_1 = require("../../IRNodes/IRNative/index.js"); var isForcedNative_1 = require("../../IRNodes/IRNative/isForcedNative.js"); var IRRecursive_1 = require("../../IRNodes/IRRecursive.js"); var isIRParentTerm_1 = require("../../utils/isIRParentTerm.js"); var _modifyChildFromTo_1 = require("../_internal/_modifyChildFromTo.js"); var iterTree_1 = require("../_internal/iterTree.js"); var sanifyTree_1 = require("./sanifyTree.js"); function hoistForcedNatives(term) { var _a, e_1, _b, e_2, _c; if (!(0, isIRParentTerm_1.isIRParentTerm)(term)) return term; var toHoist = (_a = {}, _a[26 /* IRNativeTag.strictIfThenElse */] = [], _a[27 /* IRNativeTag.chooseUnit */] = [], _a[28 /* IRNativeTag.trace */] = [], _a[32 /* IRNativeTag.mkCons */] = [], _a[33 /* IRNativeTag.headList */] = [], _a[34 /* IRNativeTag.tailList */] = [], _a[35 /* IRNativeTag.nullList */] = [], _a[36 /* IRNativeTag.chooseData */] = [], _a[29 /* IRNativeTag.fstPair */] = [], _a[30 /* IRNativeTag.sndPair */] = [], _a[31 /* IRNativeTag.strictChooseList */] = [], _a); (0, sanifyTree_1.sanifyTree)(term); // console.log( showIRText( term ) ); // collect all forced natives (0, iterTree_1.iterTree)(term, function (node) { if ((0, isForcedNative_1.isForcedNative)(node)) { toHoist[node.tag].push(node); } }); var tags = Object.keys(toHoist); try { for (var tags_1 = __values(tags), tags_1_1 = tags_1.next(); !tags_1_1.done; tags_1_1 = tags_1.next()) { var tagStr = tags_1_1.value; var tag = Number(tagStr); var nodes = toHoist[tag]; if (nodes.length <= 0) continue; // also single use (might be inside loop) try { for (var nodes_1 = (e_2 = void 0, __values(nodes)), nodes_1_1 = nodes_1.next(); !nodes_1_1.done; nodes_1_1 = nodes_1.next()) { var node = nodes_1_1.value; (0, _modifyChildFromTo_1._modifyChildFromTo)(node.parent, node, new IRNodes_1.IRVar(getDbnToRoot(node))); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (nodes_1_1 && !nodes_1_1.done && (_c = nodes_1.return)) _c.call(nodes_1); } finally { if (e_2) throw e_2.error; } } term = new IRNodes_1.IRApp(new IRFunc_1.IRFunc(1, term), new IRNative_1.IRNative(tag)); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (tags_1_1 && !tags_1_1.done && (_b = tags_1.return)) _b.call(tags_1); } finally { if (e_1) throw e_1.error; } } return term; } exports.hoistForcedNatives = hoistForcedNatives; function getDbnToRoot(node) { var dbn = 0; // if no IRFunc is found, the node is at dbn 0 (the new func will be added) var current = node; while (current) { if (current instanceof IRFunc_1.IRFunc || current instanceof IRRecursive_1.IRRecursive) dbn += current.arity; current = current.parent; } return dbn; }