@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
405 lines (404 loc) • 19.4 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.prettyIRInline = exports.constToString = exports.lettedToStr = exports.hoistedToStr = exports.prettyIRJsonStr = exports.prettyIR = exports.prettyIRText = exports.showIR = exports.showIRText = void 0;
var IRApp_1 = require("../IRNodes/IRApp.js");
var IRLetted_1 = require("../IRNodes/IRLetted.js");
var IRNative_1 = require("../IRNodes/IRNative/index.js");
var IRNativeTag_1 = require("../IRNodes/IRNative/IRNativeTag.js");
var IRHoisted_1 = require("../IRNodes/IRHoisted.js");
var IRVar_1 = require("../IRNodes/IRVar.js");
var IRConst_1 = require("../IRNodes/IRConst.js");
var IRDelayed_1 = require("../IRNodes/IRDelayed.js");
var IRForced_1 = require("../IRNodes/IRForced.js");
var IRError_1 = require("../IRNodes/IRError.js");
var IRFunc_1 = require("../IRNodes/IRFunc.js");
var utils_1 = require("../../pluts/type_system/utils.js");
var uplc_1 = require("@harmoniclabs/uplc");
var IRConstr_1 = require("../IRNodes/IRConstr.js");
var IRCase_1 = require("../IRNodes/IRCase.js");
var IRHash_1 = require("../IRHash.js");
var stringify_1 = require("../../utils/stringify.js");
var IRRecursive_1 = require("../IRNodes/IRRecursive.js");
var IRSelfCall_1 = require("../IRNodes/IRSelfCall.js");
var vars = "abcdefghilmopqrstuvzwxyjkABCDEFGHILJMNOPQRSTUVZWXYJK".split('');
function getVarNameForDbn(dbn) {
if (dbn < 0)
return "(".concat(dbn, ")");
if (dbn < vars.length)
return vars[dbn];
return vars[Math.floor(dbn / vars.length)] + getVarNameForDbn(dbn - vars.length);
}
function showIRText(_ir) {
function _loop(ir, dbn) {
if (ir instanceof IRApp_1.IRApp)
return "[".concat(_loop(ir.fn, dbn), " ").concat(_loop(ir.arg, dbn), "]");
if (ir instanceof IRCase_1.IRCase)
return "(case ".concat(_loop(ir.constrTerm, dbn), " [").concat(Array.from(ir.continuations).map(function (f) { return _loop(f, dbn); }).join(" "), "])");
if (ir instanceof IRConstr_1.IRConstr)
return "(constr ".concat(ir.index.toString(), " [").concat(Array.from(ir.fields).map(function (f) { return _loop(f, dbn); }).join(" "), "])");
if (ir instanceof IRNative_1.IRNative)
return "(native ".concat((0, IRNativeTag_1.nativeTagToString)(ir.tag), ")");
if (ir instanceof IRLetted_1.IRLetted) {
// addLetted( ir );
return lettedToStr(ir);
}
if (ir instanceof IRHoisted_1.IRHoisted) {
return hoistedToStr(ir);
}
if (ir instanceof IRVar_1.IRVar)
return getVarNameForDbn(dbn - 1 - ir.dbn);
if (ir instanceof IRSelfCall_1.IRSelfCall)
return "self_" + getVarNameForDbn(dbn - 1 - ir.dbn);
if (ir instanceof IRConst_1.IRConst)
return constToString(ir);
if (ir instanceof IRDelayed_1.IRDelayed)
return "(delay ".concat(_loop(ir.delayed, dbn), ")");
if (ir instanceof IRForced_1.IRForced)
return "(force ".concat(_loop(ir.forced, dbn), ")");
if (ir instanceof IRError_1.IRError)
return "(error)";
if (ir instanceof IRFunc_1.IRFunc) {
var vars_1 = new Array(ir.arity);
for (var i = 0; i < ir.arity; i++) {
vars_1[i] = getVarNameForDbn(dbn++);
}
return "(func ".concat(typeof ir.name === "string" ? "{" + ir.name + "}" : "", " ").concat(vars_1.join(" "), " ").concat(_loop(ir.body, dbn), ")");
}
if (ir instanceof IRRecursive_1.IRRecursive) {
var varName = "self_" + getVarNameForDbn(dbn++);
return "(recursive ".concat(varName, " ").concat(_loop(ir.body, dbn), ")");
}
return "";
}
return _loop(_ir, 0);
}
exports.showIRText = showIRText;
function showIR(_ir) {
var hoistedHashes = [];
var hoisted = {};
function addHoisted(h) {
var hash = h.hash;
if (!hoistedHashes.some(function (hoistedHash) { return (0, IRHash_1.equalIrHash)(hoistedHash, hash); })) {
var deps = h.dependencies;
for (var i = 0; i < deps.length; i++) {
addHoisted(deps[i].hoisted);
}
hoistedHashes.push(hash.slice());
Object.defineProperty(hoisted, (0, IRHash_1.irHashToHex)(hash), {
value: showIRText(h.hoisted),
writable: false,
enumerable: true,
configurable: false
});
}
}
var lettedHashes = [];
var letted = {};
function addLetted(l) {
var hash = l.hash;
if (!lettedHashes.some(function (lettedHash) { return (0, IRHash_1.equalIrHash)(lettedHash, hash); })) {
var deps = l.dependencies;
var nDeps = deps.length;
for (var i = 0; i < nDeps; i++) {
addLetted(deps[i].letted);
}
lettedHashes.push(hash.slice());
(0, IRHoisted_1.getHoistedTerms)(l.value).forEach(function (_a) {
var hoisted = _a.hoisted;
return addHoisted(hoisted);
});
Object.defineProperty(letted, (0, IRHash_1.irHashToHex)(hash), {
value: showIRText(l.value),
writable: false,
enumerable: true,
configurable: false
});
}
}
function _loop(ir, dbn) {
if (ir instanceof IRApp_1.IRApp)
return "[".concat(_loop(ir.fn, dbn), " ").concat(_loop(ir.arg, dbn), "]");
if (ir instanceof IRConstr_1.IRConstr)
return "(constr ".concat(ir.index, " ").concat(Array.from(ir.fields).map(function (f) { return _loop(f, dbn); }).join(" "), ")");
if (ir instanceof IRCase_1.IRCase)
return "(case ".concat(_loop(ir.constrTerm, dbn), " ").concat(Array.from(ir.continuations).map(function (f) { return _loop(f, dbn); }).join(" "), ")");
if (ir instanceof IRNative_1.IRNative)
return "(native ".concat((0, IRNativeTag_1.nativeTagToString)(ir.tag), ")");
if (ir instanceof IRLetted_1.IRLetted) {
addLetted(ir);
return lettedToStr(ir);
}
if (ir instanceof IRHoisted_1.IRHoisted) {
addHoisted(ir);
return hoistedToStr(ir);
}
if (ir instanceof IRVar_1.IRVar)
return getVarNameForDbn(dbn - 1 - ir.dbn);
if (ir instanceof IRSelfCall_1.IRSelfCall)
return "self_" + getVarNameForDbn(dbn - 1 - ir.dbn);
if (ir instanceof IRConst_1.IRConst)
return constToString(ir);
if (ir instanceof IRDelayed_1.IRDelayed)
return "(delay ".concat(_loop(ir.delayed, dbn), ")");
if (ir instanceof IRForced_1.IRForced)
return "(force ".concat(_loop(ir.forced, dbn), ")");
if (ir instanceof IRError_1.IRError)
return "(error)";
if (ir instanceof IRFunc_1.IRFunc) {
var vars_2 = new Array(ir.arity);
for (var i = 0; i < ir.arity; i++) {
vars_2[i] = getVarNameForDbn(dbn++);
}
return "(func ".concat(typeof ir.name === "string" ? "{" + ir.name + "}" : "", " ").concat(vars_2.join(" "), " ").concat(_loop(ir.body, dbn), ")");
}
if (ir instanceof IRRecursive_1.IRRecursive) {
var varName = "self_" + getVarNameForDbn(dbn++);
return "(recursive ".concat(varName, " ").concat(_loop(ir.body, dbn), ")");
}
return "";
}
var text = _loop(_ir, 0);
return {
text: text,
letted: letted,
hoisted: hoisted
};
}
exports.showIR = showIR;
function prettyIRText(_ir, _indent) {
if (_indent === void 0) { _indent = 2; }
if (!Number.isSafeInteger(_indent) || _indent < 1)
return showIR(_ir);
var indentStr = " ".repeat(_indent);
function _loop(ir, dbn, depth) {
var indent = "\n".concat(indentStr.repeat(depth));
if (ir instanceof IRApp_1.IRApp)
return "".concat(indent, "[").concat(_loop(ir.fn, dbn, depth + 1), " ").concat(_loop(ir.arg, dbn, depth + 1)).concat(indent, "]");
if (ir instanceof IRCase_1.IRCase)
return "".concat(indent, "(case ").concat(indent).concat(_loop(ir.constrTerm, dbn, depth + 1), " ").concat(indent, "[").concat(Array.from(ir.continuations).map(function (f) { return _loop(f, dbn, depth + 1); }).join(" ")).concat(indent, "])");
if (ir instanceof IRConstr_1.IRConstr)
return "".concat(indent, "(constr ").concat(ir.index.toString(), " ").concat(indent, "[").concat(Array.from(ir.fields).map(function (f) { return _loop(f, dbn, depth + 1); }).join(" ")).concat(indent, "])");
if (ir instanceof IRNative_1.IRNative)
return "".concat(indent, "(native ").concat((0, IRNativeTag_1.nativeTagToString)(ir.tag), ")");
if (ir instanceof IRLetted_1.IRLetted) {
// addLetted( ir );
return "".concat(indent).concat(lettedToStr(ir));
}
if (ir instanceof IRHoisted_1.IRHoisted) {
return "".concat(indent).concat(hoistedToStr(ir));
}
if (ir instanceof IRVar_1.IRVar)
return indent + getVarNameForDbn(dbn - 1 - ir.dbn);
if (ir instanceof IRSelfCall_1.IRSelfCall)
return indent + "self_" + getVarNameForDbn(dbn - 1 - ir.dbn);
if (ir instanceof IRConst_1.IRConst)
return "".concat(indent).concat(constToString(ir));
if (ir instanceof IRDelayed_1.IRDelayed)
return "".concat(indent, "(delay ").concat(_loop(ir.delayed, dbn, depth + 1)).concat(indent, ")");
if (ir instanceof IRForced_1.IRForced)
return "".concat(indent, "(force ").concat(_loop(ir.forced, dbn, depth + 1)).concat(indent, ")");
if (ir instanceof IRError_1.IRError)
return "(error)";
if (ir instanceof IRFunc_1.IRFunc) {
var vars_3 = new Array(ir.arity);
for (var i = 0; i < ir.arity; i++) {
vars_3[i] = getVarNameForDbn(dbn++);
}
return "".concat(indent, "(func ").concat(typeof ir.name === "string" ? "{" + ir.name + "}" : "", " ").concat(vars_3.join(" "), " ").concat(_loop(ir.body, dbn, depth + 1)).concat(indent, ")");
}
if (ir instanceof IRRecursive_1.IRRecursive) {
var varName = "self_" + getVarNameForDbn(dbn++);
return "".concat(indent, "(recursive ").concat(varName, " ").concat(_loop(ir.body, dbn, depth + 1)).concat(indent, ")");
}
return "";
}
return _loop(_ir, 0, 0);
}
exports.prettyIRText = prettyIRText;
function prettyIR(_ir, _indent) {
if (_indent === void 0) { _indent = 2; }
if (!Number.isSafeInteger(_indent) || _indent < 1)
return showIR(_ir);
var indentStr = " ".repeat(_indent);
var hoistedHashes = [];
var hoisted = {};
function addHoisted(h) {
var hash = h.hash;
if (!hoistedHashes.some(function (hoistedHash) { return (0, IRHash_1.equalIrHash)(hoistedHash, hash); })) {
var deps = h.dependencies;
for (var i = 0; i < deps.length; i++) {
addHoisted(deps[i].hoisted);
}
hoistedHashes.push(hash.slice());
var hashStr = (0, IRHash_1.irHashToHex)(hash);
Object.defineProperty(hoisted, hashStr, { value: prettyIRText(h.hoisted, _indent), writable: true, enumerable: true });
}
}
var lettedHashes = [];
var letted = {};
function addLetted(l) {
var hash = l.hash;
if (!lettedHashes.some(function (lettedHash) { return (0, IRHash_1.equalIrHash)(lettedHash, hash); })) {
var deps = l.dependencies;
var nDeps = deps.length;
for (var i = 0; i < nDeps; i++) {
addLetted(deps[i].letted);
}
lettedHashes.push(hash.slice());
(0, IRHoisted_1.getHoistedTerms)(l.value).forEach(function (_a) {
var hoisted = _a.hoisted;
return addHoisted(hoisted);
});
var hashStr = (0, IRHash_1.irHashToHex)(hash);
Object.defineProperty(letted, hashStr, {
value: prettyIRText(l.value, _indent),
writable: true,
enumerable: true
});
}
}
function _loop(ir, dbn, depth) {
var indent = "\n".concat(indentStr.repeat(depth));
if (ir instanceof IRApp_1.IRApp)
return "".concat(indent, "[").concat(_loop(ir.fn, dbn, depth + 1), " ").concat(_loop(ir.arg, dbn, depth + 1)).concat(indent, "]");
if (ir instanceof IRNative_1.IRNative)
return "".concat(indent, "(native ").concat((0, IRNativeTag_1.nativeTagToString)(ir.tag), ")");
if (ir instanceof IRLetted_1.IRLetted) {
addLetted(ir);
return "".concat(indent).concat(lettedToStr(ir));
}
if (ir instanceof IRHoisted_1.IRHoisted) {
addHoisted(ir);
return "".concat(indent).concat(hoistedToStr(ir));
}
if (ir instanceof IRVar_1.IRVar)
return indent + getVarNameForDbn(dbn - 1 - ir.dbn);
if (ir instanceof IRSelfCall_1.IRSelfCall)
return indent + "self_" + getVarNameForDbn(dbn - 1 - ir.dbn);
if (ir instanceof IRConst_1.IRConst)
return "".concat(indent).concat(constToString(ir));
if (ir instanceof IRDelayed_1.IRDelayed)
return "".concat(indent, "(delay ").concat(_loop(ir.delayed, dbn, depth + 1)).concat(indent, ")");
if (ir instanceof IRForced_1.IRForced)
return "".concat(indent, "(force ").concat(_loop(ir.forced, dbn, depth + 1)).concat(indent, ")");
if (ir instanceof IRError_1.IRError)
return "(error)";
if (ir instanceof IRFunc_1.IRFunc) {
var vars_4 = new Array(ir.arity);
for (var i = 0; i < ir.arity; i++) {
vars_4[i] = getVarNameForDbn(dbn++);
}
return "".concat(indent, "(func ").concat(typeof ir.name === "string" ? "{" + ir.name + "}" : "", " ").concat(vars_4.join(" "), " ").concat(_loop(ir.body, dbn, depth + 1)).concat(indent, ")");
}
if (ir instanceof IRRecursive_1.IRRecursive) {
var varName = "self_" + getVarNameForDbn(dbn++);
return "".concat(indent, "(recursive ").concat(varName, " ").concat(_loop(ir.body, dbn, depth + 1)).concat(indent, ")");
}
return "";
}
var text = _loop(_ir, 0, 0);
return {
text: text,
letted: letted,
hoisted: hoisted
};
}
exports.prettyIR = prettyIR;
var defaultPrettyIRJsonStrOpts = {
text: true,
letted: true,
hoisted: true
};
function prettyIRJsonStr(ir, indent, opts) {
if (indent === void 0) { indent = 2; }
if (opts === void 0) { opts = {}; }
var _opts = __assign(__assign({}, defaultPrettyIRJsonStrOpts), opts);
var toJson = prettyIR(ir, indent);
if (!_opts.text)
toJson.text = undefined;
if (!_opts.letted)
toJson.letted = undefined;
if (!_opts.hoisted)
toJson.hoisted = undefined;
return (0, stringify_1.stringify)(toJson, function (k, v) {
if ((k === "text" || (typeof k === "string" && k.length === 32)) && typeof v === "string") {
return v.split("\n");
}
return v;
}, indent);
}
exports.prettyIRJsonStr = prettyIRJsonStr;
function hoistedToStr(ir) {
return "(hoisted".concat((ir.meta.name ? " {" + ir.meta.name + "}" : ""), " ").concat((0, IRHash_1.irHashToHex)(ir.hash), ")");
}
exports.hoistedToStr = hoistedToStr;
function lettedToStr(ir) {
return "(letted".concat((ir.meta.name ? " {" + ir.meta.name + "}" : ""), " ").concat(ir.dbn, " ").concat((0, IRHash_1.irHashToHex)(ir.hash), ")");
}
exports.lettedToStr = lettedToStr;
function constToString(ir) {
return "(const ".concat((0, utils_1.termTypeToString)(ir.type, 2), " ").concat((0, uplc_1.showUPLCConstValue)(ir.value), ")");
}
exports.constToString = constToString;
function prettyIRInline(_ir, _indent) {
if (_indent === void 0) { _indent = 2; }
var indentStr = " ".repeat(_indent);
function _loop(ir, dbn, depth) {
var indent = "\n".concat(indentStr.repeat(depth));
if (ir instanceof IRApp_1.IRApp)
return "".concat(indent, "[").concat(_loop(ir.fn, dbn, depth + 1), " ").concat(_loop(ir.arg, dbn, depth + 1)).concat(indent, "]");
if (ir instanceof IRNative_1.IRNative)
return "".concat(indent, "(native ").concat((0, IRNativeTag_1.nativeTagToString)(ir.tag), ")");
if (ir instanceof IRLetted_1.IRLetted) {
// addLetted( ir );
return ("".concat(indent, "(letted").concat((ir.meta.name ? " {" + ir.meta.name + "}" : ""), " ").concat(ir.dbn, " ").concat((0, IRHash_1.irHashToHex)(ir.hash)) +
"".concat(_loop(ir.value, dbn, depth + 1)) +
"".concat(indent, ")"));
}
if (ir instanceof IRHoisted_1.IRHoisted) {
// addHoisted( ir )
// return `${indent}${hoistedToStr(ir)}`;
return ("".concat(indent, "(hoisted").concat((ir.meta.name ? " {" + ir.meta.name + "}" : ""), " ").concat((0, IRHash_1.irHashToHex)(ir.hash)) +
"".concat(_loop(ir.hoisted, dbn, depth + 1)) +
"".concat(indent, ")"));
}
if (ir instanceof IRVar_1.IRVar)
return indent + getVarNameForDbn(dbn - 1 - ir.dbn);
if (ir instanceof IRSelfCall_1.IRSelfCall)
return indent + "self_" + getVarNameForDbn(dbn - 1 - ir.dbn);
if (ir instanceof IRConst_1.IRConst)
return "".concat(indent).concat(constToString(ir));
if (ir instanceof IRDelayed_1.IRDelayed)
return "".concat(indent, "(delay ").concat(_loop(ir.delayed, dbn, depth + 1)).concat(indent, ")");
if (ir instanceof IRForced_1.IRForced)
return "".concat(indent, "(force ").concat(_loop(ir.forced, dbn, depth + 1)).concat(indent, ")");
if (ir instanceof IRError_1.IRError)
return "(error)";
if (ir instanceof IRFunc_1.IRFunc) {
var vars_5 = new Array(ir.arity);
for (var i = 0; i < ir.arity; i++) {
vars_5[i] = getVarNameForDbn(dbn++);
}
return "".concat(indent, "(func ").concat(typeof ir.name === "string" ? "{" + ir.name + "}" : "", " ").concat(vars_5.join(" "), " ").concat(_loop(ir.body, dbn, depth + 1)).concat(indent, ")");
}
if (ir instanceof IRRecursive_1.IRRecursive) {
var varName = "self_" + getVarNameForDbn(dbn++);
return "".concat(indent, "(recursive ").concat(varName, " ").concat(_loop(ir.body, dbn, depth + 1)).concat(indent, ")");
}
return "";
}
var text = _loop(_ir, 0, 0);
return text;
}
exports.prettyIRInline = prettyIRInline;