Skip to content

Commit

Permalink
botheither
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Nov 14, 2023
1 parent 81078e5 commit c50bfe2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
49 changes: 48 additions & 1 deletion generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function storeD(d: D): Builder {
builder.storeUint(d.c, 32);
};
}
export type Maybe = Maybe_nothing | Maybe_just;
export type Maybe<TheType> = Maybe_nothing<TheType> | Maybe_just<TheType>;
export type Maybe_nothing<TheType> = {

};
Expand Down Expand Up @@ -142,3 +142,50 @@ export function storeTheJust(theJust: TheJust): Builder {
storeMaybe<D>(theJust.x, storeD)(builder);
};
}
export type Either<X,Y> = Either_left<X,Y> | Either_right<X,Y>;
export type Either_left<X,Y> = {
value: X;
};
export type Either_right<X,Y> = {
value: Y;
};
export function loadEither<X,Y>(slice: Slice, loadX: (slice: Slice) => X, loadY: (slice: Slice) => Y): Either<X,Y> {
if (slice.preloadUint(1) == 0b0) {
return {
value: loadX(slice)
};
};
if (slice.preloadUint(1) == 0b1) {
return {
value: loadY(slice)
};
};
}
export function storeEither<X,Y>(either: Either<X,Y>, storeX: (x: X) => (builder: Builder) => void, storeY: (y: Y) => (builder: Builder) => void): Builder {
if (either instanceof Either_left) {
return (builder: Builder) => {
storeX(either.value)(builder);
};
};
if (either instanceof Either_right) {
return (builder: Builder) => {
storeY(either.value)(builder);
};
};
}
export type Both<X,Y> = {
first: X;
second: Y;
};
export function loadBoth<X,Y>(slice: Slice, loadX: (slice: Slice) => X, loadY: (slice: Slice) => Y): Both<X,Y> {
return {
first: loadX(slice),
second: loadY(slice)
};
}
export function storeBoth<X,Y>(both: Both<X,Y>, storeX: (x: X) => (builder: Builder) => void, storeY: (y: Y) => (builder: Builder) => void): Builder {
return (builder: Builder) => {
storeX(both.first)(builder);
storeY(both.second)(builder);
};
}
3 changes: 3 additions & 0 deletions tests/fixtures/tlb/my.tlb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ nothing$0 {TheType:Type} = Maybe TheType;
just$1 {TheType:Type} value:TheType = Maybe TheType;
thejust$_ x:(Maybe D) = TheJust;

left$0 {X:Type} {Y:Type} value:X = Either X Y;
right$1 {X:Type} {Y:Type} value:Y = Either X Y;
pair$_ {X:Type} {Y:Type} first:X second:Y = Both X Y;


18 changes: 9 additions & 9 deletions tests/my.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ interface StructDeclaration extends ASTNode {

interface UnionTypeDeclaration extends ASTNode {
type: "UnionTypeDeclaration",
name: Identifier,
unionMembers: Array<Identifier>
name: TypeExpression,
unionMembers: Array<TypeExpression>
}

interface ObjectProperty extends ASTNode {
Expand Down Expand Up @@ -212,7 +212,7 @@ function tArrowFunctionExpression(parameters: Array<TypedIdentifier>, body: Arra
return {type: "ArrowFunctionExpression", parameters: parameters, body: body}
}

function tUnionTypeDeclaration(name: Identifier, unionMembers: Array<Identifier>): UnionTypeDeclaration {
function tUnionTypeDeclaration(name: TypeExpression, unionMembers: Array<TypeExpression>): UnionTypeDeclaration {
return {type: "UnionTypeDeclaration", name: name, unionMembers: unionMembers}
}

Expand Down Expand Up @@ -321,9 +321,7 @@ ${currentTabs}}`
if (node.type == "TypeParametersExpression") {
if (node.typeParameters.length > 0) {
result += '<';
node.typeParameters.forEach(element => {
result += toCode(element, printContext);
});
result += toCodeArray(node.typeParameters, ',', '', printContext, '');
result += '>';
}
}
Expand Down Expand Up @@ -418,7 +416,7 @@ describe('parsing into intermediate representation using grammar', () => {
if (combinatorName == undefined) {
return;
}
let unionTypes: Identifier[] = []
let unionTypes: TypeExpression[] = []
let tmpDeclarations: ASTNode[] = []
let loadStatements: Statement[] = []
let storeStatements: Statement[] = []
Expand All @@ -433,7 +431,6 @@ describe('parsing into intermediate representation using grammar', () => {
} else {
structName = declaration.combinator.name;
}
unionTypes.push(tIdentifier(structName));
let variableStructName = firstLower(structName)

let structProperties: TypedIdentifier[] = []
Expand Down Expand Up @@ -513,6 +510,9 @@ describe('parsing into intermediate representation using grammar', () => {

typeParameters = tTypeParametersExpression(typeParameterArray);
}

unionTypes.push(tTypeWithParameters(tIdentifier(structName), typeParameters));


let structX = tStructDeclaration(tIdentifier(structName), structProperties, typeParameters);

Expand Down Expand Up @@ -555,7 +555,7 @@ describe('parsing into intermediate representation using grammar', () => {
tmpDeclarations.push(storeFunction)

if (value.length > 1) {
let unionTypeDecl = tUnionTypeDeclaration(tIdentifier(key), unionTypes)
let unionTypeDecl = tUnionTypeDeclaration(tTypeWithParameters(tIdentifier(key), typeParameters), unionTypes)
jsCodeDeclarations.push(unionTypeDecl)
}
tmpDeclarations.forEach(element => {
Expand Down

0 comments on commit c50bfe2

Please sign in to comment.