@eliseev_s/tolk-tlb-transpiler
Version:
Transpile Tolk structs to TLB definitions and generate TypeScript wrappers for TON blockchain smart contracts
74 lines • 2.35 kB
JavaScript
;
// ============================================================================
// TLB Utilities - Shared formatting utilities for TLB generation
// ============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatPrefix = formatPrefix;
exports.formatUnionPrefix = formatUnionPrefix;
exports.formatMaybeType = formatMaybeType;
exports.formatEitherType = formatEitherType;
exports.formatCellRefType = formatCellRefType;
exports.formatCollectionType = formatCollectionType;
exports.buildConstructor = buildConstructor;
const utils_1 = require("./utils");
/**
* Format prefix for TLB constructor (hex or binary)
*/
function formatPrefix(prefix) {
if (prefix.format === 'hex') {
return `#${prefix.value
.toString(16)
.toUpperCase()
.padStart(Math.ceil(prefix.length / 4), '0')}`;
}
else {
return `$${prefix.value.toString(2).padStart(prefix.length, '0')}`;
}
}
/**
* Format union prefix (for union alternatives)
*/
function formatUnionPrefix(tag, explicitPrefix, totalAlternatives) {
if (explicitPrefix) {
return `#${tag
.toString(16)
.toUpperCase()
.padStart(Math.max(2, Math.ceil(explicitPrefix.length / 4)), '0')}`;
}
else if (totalAlternatives !== undefined) {
return `$${tag.toString(2).padStart((0, utils_1.calculateRequiredBits)(totalAlternatives), '0')}`;
}
return '#_';
}
/**
* Format Maybe type in TLB
*/
function formatMaybeType(innerTLB) {
return `(Maybe ${innerTLB})`;
}
/**
* Format Either type in TLB
*/
function formatEitherType(leftTLB, rightTLB) {
return `(Either ${leftTLB} ${rightTLB})`;
}
/**
* Format cell reference in TLB
*/
function formatCellRefType(innerTLB) {
const needsParens = innerTLB.includes(' ') && !(innerTLB.startsWith('(') && innerTLB.endsWith(')'));
return needsParens ? `^(${innerTLB})` : `^${innerTLB}`;
}
/**
* Format collection type (tuple/tensor) in TLB
*/
function formatCollectionType(items) {
return `[${items}]`;
}
/**
* Build TLB constructor string
*/
function buildConstructor(constructorName, prefix, fields, typeName) {
return `${constructorName}${prefix} ${fields} = ${typeName};`;
}
//# sourceMappingURL=tlb-utils.js.map