Skip to content

Commit

Permalink
feat(type): Adds (contract and lambda) types and fixes record layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Feb 3, 2022
1 parent 28903e8 commit b873e4a
Show file tree
Hide file tree
Showing 5 changed files with 449 additions and 89 deletions.
29 changes: 16 additions & 13 deletions src/core/type/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { composeRightCombLayout, parenthesis } from '../../misc/utils';
import { ILayout } from '../../typings/literal';
import { IType } from '../../typings/type';
import { Layout } from '../enums/layout';
Expand All @@ -20,20 +21,20 @@ export class ContainerType implements IType {
}

export class Type_Record implements IType {
layout: ILayout | Layout;
private layout: ILayout | Layout;
constructor(public fields: Record<string, IType>, layout?: ILayout | Layout) {
this.layout = layout || Object.keys(fields);
this.layout = layout || composeRightCombLayout(Object.keys(fields));
}

private translateLayout = (layout: ILayout | Layout): string => {
const normalizeTuple = (elements: ILayout): string =>
elements.reduce<string>((pv, cv) => {
if (Array.isArray(cv)) {
return `(${[pv, normalizeTuple(cv)].join(' ')})`;
}
const field = `("${cv}")`;
return !!pv ? `(${pv} ${field})` : field;
}, '');
const normalizeTuple = (layout: string | ILayout): string => {
if (typeof layout === 'string') {
return `("${layout}")`;
}
const [left, right] = layout;
return parenthesis([normalizeTuple(left), normalizeTuple(right)].join(' '));
};

if (Array.isArray(layout)) {
return layout.length ? `(Some ${normalizeTuple(layout)})` : 'None';
}
Expand Down Expand Up @@ -78,7 +79,9 @@ export const TOption = (innerType: IType) => new ContainerType(Prim.option, [inn
export const TMap = (keyType: IType, valueType: IType) => new ContainerType(Prim.map, [keyType, valueType]);
export const TBig_map = (keyType: IType, valueType: IType) =>
new ContainerType(SmartPyAtom.bigmap, [keyType, valueType]);
export const TPair = (...types: IType[]) => new ContainerType(SmartPyAtom.tuple, types);
export const TPair = (left: IType, right: IType) => new ContainerType(SmartPyAtom.tuple, [left, right]);
export const TLambda = (left: IType, right: IType) => new ContainerType(Prim.lambda, [left, right]);
export const TContract = (innerType: IType) => new ContainerType(Prim.contract, [innerType]);
// Artificial Types
export const TRecord = (fields: Record<string, IType>, layout?: ILayout | Layout) => new Type_Record(fields, layout);

Expand Down Expand Up @@ -110,9 +113,9 @@ const Types = {
TPair,
TMap,
TBig_map,
// TLambda,
TLambda,
// TTicket,
// TContract,
TContract,
// TSapling_state,
// TSapling_transaction,
// Artificial Types
Expand Down
17 changes: 17 additions & 0 deletions src/misc/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ILayout } from '../typings/literal';
import { IToString } from '../typings/shared';

export const parenthesis = (str: string) => `(${str})`;
export const capitalizeBoolean = (bool: boolean): string => (bool ? 'True' : 'False');

export class LineInfo implements IToString {
Expand All @@ -21,8 +23,23 @@ export class LineInfo implements IToString {
}
}

/**
* @description Build right aligned nested binary pairs
* @see https://tezos.gitlab.io/active/michelson.html#operations-on-pairs-and-right-combs
* @param fields A sequence of strings
* @returns {ILayout}
*/
export const composeRightCombLayout = (fields: string[]): ILayout => {
if (fields.length > 2) {
return [fields[0], composeRightCombLayout(fields.slice(1))];
}
return fields;
};

const Utils = {
capitalizeBoolean,
composeRightCombLayout,
parenthesis,
};

export default Utils;
2 changes: 0 additions & 2 deletions src/typings/type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { IToString } from './shared';

export interface IType {
toString: () => string;
}
Loading

0 comments on commit b873e4a

Please sign in to comment.