@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
137 lines (136 loc) • 6.04 kB
JavaScript
"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.markRecursiveHoistsAsForced = 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 IRNative_1 = require("../../IRNodes/IRNative/index.js");
var IRRecursive_1 = require("../../IRNodes/IRRecursive.js");
function markRecursiveHoistsAsForced(_term) {
var stack = [{ term: _term, isInRecursiveTerm: false }];
var _loop_1 = function () {
var _a = stack.pop(), t = _a.term, isInRecursiveTerm = _a.isInRecursiveTerm, isIRAppArg = _a.isIRAppArg;
if (t instanceof IRApp_1.IRApp) {
// must push the arg first and then the fucntion
// so that we can check if the function is the Z combinator before the arg is processed
stack.push({ term: t.arg, isIRAppArg: true, isInRecursiveTerm: isInRecursiveTerm }, { term: t.fn, isInRecursiveTerm: isInRecursiveTerm });
return "continue";
}
if (t instanceof IRConstr_1.IRConstr) {
// must push the arg first and then the fucntion
// so that we can check if the function is the Z combinator before the arg is processed
stack.push.apply(stack, __spreadArray([], __read(Array.from(t.fields)
.map(function (f, i) { return ({
term: f,
isIRAppArg: i > 0,
isInRecursiveTerm: isInRecursiveTerm
}); })), false));
return "continue";
}
if (t instanceof IRCase_1.IRCase) {
// must push the arg first and then the fucntion
// so that we can check if the function is the Z combinator before the arg is processed
stack.push.apply(stack, __spreadArray([{
term: t.constrTerm,
isIRAppArg: false,
isInRecursiveTerm: isInRecursiveTerm
}], __read(Array.from(t.continuations)
.map(function (cont) { return ({
term: cont,
isIRAppArg: true,
isInRecursiveTerm: isInRecursiveTerm
}); })), false));
return "continue";
}
if (t instanceof IRNative_1.IRNative &&
t.tag === -1 /* IRNativeTag.z_comb */ &&
stack.length > 0 && (stack[stack.length - 1].isIRAppArg === true)) {
stack[stack.length - 1].isInRecursiveTerm = true;
return "continue";
}
if (t instanceof IRHoisted_1.IRHoisted ||
t instanceof IRLetted_1.IRLetted) {
// if it is an hoisted/letted
// DIRECTLY applied to something that makes it recursive
if (isIRAppArg &&
t.parent instanceof IRApp_1.IRApp &&
t.parent.fn instanceof IRNative_1.IRNative &&
t.parent.fn.tag === -1 /* IRNativeTag.z_comb */) {
// then check the hoisted/letted value instead of marking as hoisted
if (t instanceof IRLetted_1.IRLetted) {
stack.push({ term: t.value, isInRecursiveTerm: isInRecursiveTerm });
return "continue";
}
else // if( t instanceof IRHoisted )
{
stack.push({ term: t.hoisted, isInRecursiveTerm: isInRecursiveTerm });
return "continue";
}
return "continue";
}
if (isInRecursiveTerm) {
t.meta.forceHoist = true;
return "continue";
}
// otherwhise check in values too
else if (t instanceof IRLetted_1.IRLetted) {
stack.push({ term: t.value, isInRecursiveTerm: isInRecursiveTerm });
return "continue";
}
else if (t instanceof IRHoisted_1.IRHoisted) {
stack.push({ term: t.hoisted, isInRecursiveTerm: isInRecursiveTerm });
return "continue";
}
}
if (t instanceof IRDelayed_1.IRDelayed) {
stack.push({ term: t.delayed, isInRecursiveTerm: isInRecursiveTerm });
return "continue";
}
if (t instanceof IRForced_1.IRForced) {
stack.push({ term: t.forced, isInRecursiveTerm: isInRecursiveTerm });
return "continue";
}
if (t instanceof IRFunc_1.IRFunc) {
stack.push({ term: t.body, isInRecursiveTerm: isInRecursiveTerm });
return "continue";
}
if (t instanceof IRRecursive_1.IRRecursive) {
stack.push({ term: t.body, isInRecursiveTerm: true });
return "continue";
}
};
while (stack.length > 0) {
_loop_1();
}
}
exports.markRecursiveHoistsAsForced = markRecursiveHoistsAsForced;