@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
85 lines (84 loc) • 3.9 kB
JavaScript
;
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.matchNCtorsIdxs = void 0;
var IR_1 = require("../../../IR/index.js");
var Term_1 = require("../../Term/index.js");
var types_1 = require("../../type_system/types.js");
function matchNCtorsIdxs(_n, returnT) {
if (_n <= 1)
throw new Error("mathcing ill formed struct data");
var n = Math.round(_n);
if (_n !== n)
throw new Error("number of ctors to match must be an integer");
// if( matchNCtorsIdxsCache[n] !== undefined ) return matchNCtorsIdxsCache[n];
var continuationT = (0, types_1.lam)((0, types_1.list)(types_1.data), returnT);
// built immediately; not at compilation
// all this mess just to allow hoisting
// you have got to reason backwards to understand the process
var body = new IR_1.IRError("matchNCtorsIdxs; unmatched");
for (var i = n - 1; i >= 0; i--) {
// pif( continuationT ).$( isCtorIdx.$( pInt( i ) ) )
// .then( continuation_i )
// .else( ... )
body = new IR_1.IRForced(new IR_1.IRApp(new IR_1.IRApp(new IR_1.IRApp(IR_1.IRNative.strictIfThenElse, new IR_1.IRApp(new IR_1.IRVar(0), // isCtorIdx // last variable introduced (see below)
IR_1.IRConst.int(i) // .$( pInt( i ) )
)), new IR_1.IRDelayed(new IR_1.IRVar(2 + // isCtorIdx and structConstrPair are in scope
i // continuation_i
)) // then matching continuation
), new IR_1.IRDelayed(body) // else go check the next index; or error if it was last possible index
));
}
// plet( peqInt.$( pfstPir(...).$( structConstrPair ) ) ).in( isCtorIdx => ... )
body = new IR_1.IRApp(new IR_1.IRFunc(1, // isCtorIdx
body),
// peqInt.$( pfstPir(...).$( structConstrPair ) )
new IR_1.IRApp(IR_1.IRNative.equalsInteger, new IR_1.IRApp(IR_1.IRNative.fstPair, new IR_1.IRVar(0) // structConstrPair // last variable introduced (see below)
)));
// <whatever continuation was matched>.$( psndPair(...).$( structConstrPair ) )
// aka passing the fields to the continuation
body = new IR_1.IRApp(body, new IR_1.IRApp(IR_1.IRNative.sndPair, new IR_1.IRVar(0)));
// plet( punConstrData.$( structData ) ) ).in( structConstrPair => ... )
body = new IR_1.IRApp(new IR_1.IRFunc(1, // structConstrPair
body), new IR_1.IRApp(IR_1.IRNative.unConstrData, new IR_1.IRVar(n) // structData
));
for (var i = n - 1; i >= 0; i--) {
body = new IR_1.IRFunc(1, // continuation n - 1 - i
body);
}
// seriously, all this mess for this IRHoisted
body = new IR_1.IRHoisted(new IR_1.IRFunc(1, // structData
body), { name: "match_" + n + "_ctors" });
body.hash;
var term = new Term_1.Term((0, types_1.fn)(__spreadArray([
types_1.data
], __read((new Array(n).fill(continuationT))), false), returnT), function (_dbn) { return body.clone(); });
// save in cache
// matchNCtorsIdxsCache[n] = term;
return term;
}
exports.matchNCtorsIdxs = matchNCtorsIdxs;