@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
170 lines (169 loc) • 6.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._irToUplc = void 0;
var termTyToConstTy_1 = require("../../../pluts/type_system/termTyToConstTy.js");
var IRApp_1 = require("../../IRNodes/IRApp.js");
var IRConst_1 = require("../../IRNodes/IRConst.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 IRNativeTag_1 = require("../../IRNodes/IRNative/IRNativeTag.js");
var IRVar_1 = require("../../IRNodes/IRVar.js");
var showIR_1 = require("../../utils/showIR.js");
var IRError_1 = require("../../IRNodes/IRError.js");
var IRForced_1 = require("../../IRNodes/IRForced.js");
var IRDelayed_1 = require("../../IRNodes/IRDelayed.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 IRRecursive_1 = require("../../IRNodes/IRRecursive.js");
var IRSelfCall_1 = require("../../IRNodes/IRSelfCall.js");
function _irToUplc(ir, srcmap, node_index) {
if (srcmap === void 0) { srcmap = undefined; }
if (node_index === void 0) { node_index = 0; }
if (!(ir instanceof IRConst_1.IRConst)) {
// console.log("_irToUplc", ir, node_index, Date.now());
}
else {
// if( ir.parent )
// {
// console.log(ir.parent ? Object.getPrototypeOf( ir.parent ).constructor.name : undefined, Date.now() );
// }
// else
// {
// console.log( Error().stack );
// }
}
if (ir instanceof IRConst_1.IRConst) {
return {
term: new uplc_1.UPLCConst((0, termTyToConstTy_1.termTyToConstTy)(ir.type), ir.value),
max_idx: node_index
};
}
if (ir instanceof IRVar_1.IRVar)
return {
term: new uplc_1.UPLCVar(ir.dbn),
max_idx: node_index
};
if (ir instanceof IRFunc_1.IRFunc) {
var _a = _irToUplc(ir.body, srcmap, node_index), body = _a.term, max_idx = _a.max_idx;
var lam = new uplc_1.Lambda(body);
for (var i = 1; i < ir.arity; i++) {
lam = new uplc_1.Lambda(lam);
}
return {
term: lam,
max_idx: max_idx
};
}
if (ir instanceof IRRecursive_1.IRRecursive) {
throw new Error("Can't convert 'IRRecursive' to valid UPLC;" +
"IRRecursive are expected to be converted before calling '_irToUplc'");
}
if (ir instanceof IRSelfCall_1.IRSelfCall) {
throw new Error("Can't convert 'IRSelfCall' to valid UPLC;" +
"IRSelfCall are expected to be converted before calling '_irToUplc'");
}
if (ir instanceof IRApp_1.IRApp) {
var _b = _irToUplc(ir.fn, srcmap, node_index + 1), fn = _b.term, fn_max_idx = _b.max_idx;
var _c = _irToUplc(ir.arg, srcmap, fn_max_idx + 1), arg = _c.term, arg_max_idx = _c.max_idx;
var src = ir.meta.__src__;
if (srcmap && typeof src === "string") {
srcmap[node_index] = adaptSrcString(src);
}
return {
term: new uplc_1.Application(fn, arg),
max_idx: arg_max_idx
};
}
if (ir instanceof IRNative_1.IRNative) {
if (ir.tag < 0)
throw new Error("Can't translate '" + (0, IRNativeTag_1.nativeTagToString)(ir.tag) + "' 'IRNative' to 'UPLCBuiltin'");
return {
term: new uplc_1.Builtin(ir.tag),
max_idx: node_index
};
}
if (ir instanceof IRLetted_1.IRLetted) {
throw new Error("Can't convert 'IRLetted' to valid UPLC");
}
if (ir instanceof IRHoisted_1.IRHoisted) {
// return this.hoisted.toUPLC();
throw new Error("Can't convert 'IRHoisted' to valid UPLC;" +
"\nhoisted hash was: " + (0, IRHash_1.irHashToHex)(ir.hash) +
"\nhoisted term was: " + (0, showIR_1.showIR)(ir.hoisted).text);
}
if (ir instanceof IRError_1.IRError) {
if (typeof ir.addInfos.__src__ === "string" && srcmap) {
srcmap[node_index] = adaptSrcString(ir.addInfos.__src__);
}
return {
term: new uplc_1.ErrorUPLC(ir.msg, ir.addInfos),
max_idx: node_index
};
}
if (ir instanceof IRForced_1.IRForced) {
var _d = _irToUplc(ir.forced, srcmap, node_index), toForce = _d.term, max_idx = _d.max_idx;
return {
term: new uplc_1.Force(toForce),
max_idx: max_idx
};
}
if (ir instanceof IRDelayed_1.IRDelayed) {
var _e = _irToUplc(ir.delayed, srcmap, node_index), toDelay = _e.term, max_idx = _e.max_idx;
return {
term: new uplc_1.Delay(toDelay),
max_idx: max_idx
};
}
if (ir instanceof IRConstr_1.IRConstr) {
if (ir.fields.length === 0) {
return {
term: new uplc_1.Constr(ir.index, []),
max_idx: node_index + 1
};
}
var fields = Array.from(ir.fields)
.slice(1)
.reduce(function (accum, field, i) {
accum.push(_irToUplc(field, srcmap, accum[i].max_idx + 1));
return accum;
}, [_irToUplc(ir.fields[0], srcmap, node_index)]);
return {
term: new uplc_1.Constr(ir.index, fields.map(function (_a) {
var term = _a.term;
return term;
})),
max_idx: fields[fields.length - 1].max_idx
};
}
if (ir instanceof IRCase_1.IRCase) {
var _f = _irToUplc(ir.constrTerm, srcmap, node_index), constrTerm = _f.term, max_idx = _f.max_idx;
var conts = Array.from(ir.continuations)
.slice(1)
.reduce(function (accum, cont, i) {
accum.push(_irToUplc(cont, srcmap, accum[i].max_idx + 1));
return accum;
}, [_irToUplc(ir.continuations[0], srcmap, max_idx + 1)]);
return {
term: new uplc_1.Case(constrTerm, conts.map(function (_a) {
var term = _a.term;
return term;
})),
max_idx: conts[conts.length - 1].max_idx
};
}
throw new Error("unknown IR term calling '_irToUplc'");
}
exports._irToUplc = _irToUplc;
function adaptSrcString(src) {
// " at $ (some/path/to/src.ts:line:column)" -> "some/path/to/src.ts:line:column"
var idx = src.indexOf("(");
src = idx >= 0 ? src.slice(idx + 1, src.length - 1) : src;
// " at some/path/to/src.ts:line:column" -> "some/path/to/src.ts:line:column"
idx = src.indexOf("at ");
src = idx >= 0 ? src.slice(idx + 3) : src;
return src;
}