@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
80 lines (79 loc) • 4.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.plet = void 0;
var IRApp_1 = require("../../../IR/IRNodes/IRApp.js");
var IRFunc_1 = require("../../../IR/IRNodes/IRFunc.js");
var IRLetted_1 = require("../../../IR/IRNodes/IRLetted.js");
var IRVar_1 = require("../../../IR/IRNodes/IRVar.js");
var Term_1 = require("../../Term/index.js");
var types_1 = require("../../type_system/types.js");
var fromData_minimal_1 = require("../std/data/conversion/fromData_minimal.js");
var IRHoisted_1 = require("../../../IR/IRNodes/IRHoisted.js");
var isClosedIRTerm_1 = require("../../../IR/utils/isClosedIRTerm.js");
var addUtilityForType_1 = require("../std/UtilityTerms/addUtilityForType.js");
var makeMockUtilityTerm_1 = require("../std/UtilityTerms/mockUtilityTerms/makeMockUtilityTerm.js");
var getCallStackAt_1 = require("../../../utils/getCallStackAt/index.js");
var IRSelfCall_1 = require("../../../IR/IRNodes/IRSelfCall.js");
function plet(varValue, value_name) {
// unwrap 'asData' if is the case
varValue = (varValue.type[0] === types_1.PrimType.AsData ? (0, fromData_minimal_1._fromData)(varValue.type[1])(varValue) : varValue);
var type = varValue.type;
var valueNameIsPresent = typeof value_name === "string";
value_name = valueNameIsPresent ? value_name : undefined;
var callStackSite = (0, getCallStackAt_1.getCallStackAt)(3, {
tryGetNameAsync: valueNameIsPresent,
onNameInferred: valueNameIsPresent ? void 0 : function (inferred) { return value_name = inferred; }
});
var __src__ = callStackSite === null || callStackSite === void 0 ? void 0 : callStackSite.__line__;
value_name = value_name !== null && value_name !== void 0 ? value_name : callStackSite === null || callStackSite === void 0 ? void 0 : callStackSite.inferredName;
var letted = new Term_1.Term(type, function (cfg, dbn) {
var ir = varValue.toIR(cfg, dbn);
// `compileIRToUPLC` can handle it even if this check is not present
// but why spend useful tree iterations if we can avoid them here?
if (ir instanceof IRLetted_1.IRLetted ||
ir instanceof IRHoisted_1.IRHoisted ||
ir instanceof IRVar_1.IRVar ||
ir instanceof IRSelfCall_1.IRSelfCall) {
return ir;
}
if ((0, isClosedIRTerm_1.isClosedIRTerm)(ir)) {
return new IRHoisted_1.IRHoisted(ir);
}
var res = new IRLetted_1.IRLetted(Number(dbn), ir, { __src__: __src__, name: value_name });
return res;
});
var withUtility = (0, addUtilityForType_1.addUtilityForType)(varValue.type);
var continuation = function (expr) {
// only to extracts the type; never compiled
var outType = expr((0, makeMockUtilityTerm_1.makeMockUtilityTerm)(varValue.type)).type;
// return papp( plam( varValue.type, outType )( expr as any ), varValue as any ) as any;
var term = new Term_1.Term(outType, function (cfg, dbn) {
var arg = varValue.toIR(cfg, dbn);
if (
// inline variables; no need to add an application since already in scope
arg instanceof IRVar_1.IRVar ||
arg instanceof IRSelfCall_1.IRSelfCall) {
return expr(withUtility(varValue)).toIR(cfg, dbn);
}
// if(
// // inline letted terms; no need to add an application since already letted
// arg instanceof IRLetted
// )
// {
// return expr( withUtility( varValue as any ) as any ).toIR( cfg, dbn );
// }
return new IRApp_1.IRApp(new IRFunc_1.IRFunc(1, expr(withUtility(new Term_1.Term(varValue.type, function (cfg, varAccessDbn) { return new IRVar_1.IRVar(varAccessDbn - (dbn + BigInt(1))); } // point to the lambda generated here
))).toIR(cfg, (dbn + BigInt(1)))), arg, { __src__: __src__ });
});
return (0, addUtilityForType_1.addUtilityForType)(outType)(term);
};
var lettedUtility = (0, addUtilityForType_1.addUtilityForType)(type)(letted);
Object.defineProperty(lettedUtility, "in", {
value: continuation,
writable: false,
enumerable: false,
configurable: false
});
return lettedUtility;
}
exports.plet = plet;