UNPKG

@harmoniclabs/plu-ts-onchain

Version:

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

132 lines (131 loc) 5.51 kB
"use strict"; 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.iterTree = 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 IRHoisted_1 = require("../../IRNodes/IRHoisted.js"); var IRLetted_1 = require("../../IRNodes/IRLetted.js"); var IRRecursive_1 = require("../../IRNodes/IRRecursive.js"); function iterTree(_term, fn, // necessary for letted hash calculation (exclude hoisted) shouldSkipNode, shouldExit) { var has_shouldSkipNode = typeof shouldSkipNode === "function"; var has_shouldExit = typeof shouldExit === "function"; var stack = [{ term: _term, dbn: 0 }]; var _loop_1 = function () { var _a = stack.pop(), t = _a.term, dbn = _a.dbn; if (has_shouldSkipNode && shouldSkipNode(t, dbn)) return "continue"; var termParent = t.parent; var negDbn = t instanceof IRFunc_1.IRFunc || t instanceof IRRecursive_1.IRRecursive ? t.arity : 0; var modifiedParent = fn(t, dbn) === true; if (has_shouldExit && shouldExit(t, dbn)) return { value: void 0 }; if (modifiedParent && termParent !== undefined) { while (stack.length > 0 && stack[stack.length - 1].shouldPopIfParentIsModified) { // there is an extra node stack.pop(); } // restart from the parent stack.push({ term: termParent, dbn: dbn - negDbn }); return "continue"; } if (t instanceof IRApp_1.IRApp) { // sanifyTree as we go if (t.fn.parent !== t) t.fn = t.fn.clone(); if (t.arg.parent !== t) t.arg = t.arg.clone(); stack.push({ term: t.fn, dbn: dbn }, { term: t.arg, dbn: dbn, shouldPopIfParentIsModified: true }); return "continue"; } if (t instanceof IRCase_1.IRCase) { // sanifyTree as we go if (t.constrTerm.parent !== t) t.constrTerm = t.constrTerm.clone(); for (var i = 0; i < t.continuations.length; i++) { if (t.continuations[i].parent !== t) t.continuations[i] = t.continuations[i].clone(); } stack.push.apply(stack, __spreadArray([{ term: t.constrTerm, dbn: dbn }], __read(Array.from(t.continuations).map(function (cont) { return ({ term: cont, dbn: dbn, shouldPopIfParentIsModified: true }); })), false)); return "continue"; } if (t instanceof IRConstr_1.IRConstr) { // sanifyTree as we go for (var i = 0; i < t.fields.length; i++) { if (t.fields[i].parent !== t) t.fields[i] = t.fields[i].clone(); } stack.push.apply(stack, __spreadArray([], __read(Array.from(t.fields).map(function (f, i) { return ({ term: f, dbn: dbn, shouldPopIfParentIsModified: i !== 0 }); })), 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"; } if (t instanceof IRHoisted_1.IRHoisted) { // 0 because hoisted are closed // for hoisted we keep track of the depth inside the term stack.push({ term: t.hoisted, dbn: 0 }); return "continue"; } if (t instanceof IRLetted_1.IRLetted) { // same stuff as the hoisted terms // the only difference is that depth is then incremented // once the letted term reaches its final position stack.push({ term: t.value, dbn: dbn }); return "continue"; } }; while (stack.length > 0) { var state_1 = _loop_1(); if (typeof state_1 === "object") return state_1.value; } } exports.iterTree = iterTree;