Skip to content

Commit

Permalink
cell ref anonymous
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Nov 15, 2023
1 parent 882e81c commit 18b3159
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 32 deletions.
57 changes: 34 additions & 23 deletions generated.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Builder } from "ton"
import { Slice } from "ton"
import { beginCell } from "ton"
export type X = {
a: number;
b: number;
Expand All @@ -10,7 +11,7 @@ export function loadX(slice: Slice): X {
b: slice.loadUint(32)
};
}
export function storeX(x: X): Builder {
export function storeX(x: X): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(x.a, 32);
builder.storeUint(x.b, 32);
Expand Down Expand Up @@ -39,7 +40,7 @@ export function loadBool(slice: Slice): Bool {
};
};
}
export function storeBool(bool: Bool): Builder {
export function storeBool(bool: Bool): (builder: Builder) => void {
if (bool instanceof Bool_bool_false) {
return (builder: Builder) => {
builder.storeUint(0b0, 1);
Expand All @@ -63,7 +64,7 @@ export function loadY(slice: Slice): Y {
y: slice.loadUint(5)
};
}
export function storeY(y: Y): Builder {
export function storeY(y: Y): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(y.y, 5);
};
Expand All @@ -78,7 +79,7 @@ export function loadC(slice: Slice): C {
c: slice.loadUint(32)
};
}
export function storeC(c: C): Builder {
export function storeC(c: C): (builder: Builder) => void {
return (builder: Builder) => {
storeY(c.y)(builder);
builder.storeUint(c.c, 32);
Expand All @@ -94,7 +95,7 @@ export function loadD(slice: Slice): D {
c: slice.loadUint(32)
};
}
export function storeD(d: D): Builder {
export function storeD(d: D): (builder: Builder) => void {
return (builder: Builder) => {
storeY(d.y)(builder);
builder.storeUint(d.c, 32);
Expand All @@ -121,7 +122,7 @@ export function loadMaybe<TheType>(slice: Slice, loadTheType: (slice: Slice) =>
};
};
}
export function storeMaybe<TheType>(maybe: Maybe<TheType>, storeTheType: (theType: TheType) => (builder: Builder) => void): Builder {
export function storeMaybe<TheType>(maybe: Maybe<TheType>, storeTheType: (theType: TheType) => (builder: Builder) => void): (builder: Builder) => void {
if (maybe instanceof Maybe_nothing) {
return (builder: Builder) => {
builder.storeUint(0b0, 1);
Expand All @@ -142,7 +143,7 @@ export function loadTheJust(slice: Slice): TheJust {
x: loadMaybe<D>(slice, loadD)
};
}
export function storeTheJust(theJust: TheJust): Builder {
export function storeTheJust(theJust: TheJust): (builder: Builder) => void {
return (builder: Builder) => {
storeMaybe<D>(theJust.x, storeD)(builder);
};
Expand Down Expand Up @@ -174,7 +175,7 @@ export function loadEither<X,Y>(slice: Slice, loadX: (slice: Slice) => X, loadY:
};
};
}
export function storeEither<X,Y>(either: Either<X,Y>, storeX: (x: X) => (builder: Builder) => void, storeY: (y: Y) => (builder: Builder) => void): Builder {
export function storeEither<X,Y>(either: Either<X,Y>, storeX: (x: X) => (builder: Builder) => void, storeY: (y: Y) => (builder: Builder) => void): (builder: Builder) => void {
if (either instanceof Either_left) {
return (builder: Builder) => {
builder.storeUint(0b0, 1);
Expand Down Expand Up @@ -202,7 +203,7 @@ export function loadBoth<X,Y>(slice: Slice, loadX: (slice: Slice) => X, loadY: (
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 {
export function storeBoth<X,Y>(both: Both<X,Y>, storeX: (x: X) => (builder: Builder) => void, storeY: (y: Y) => (builder: Builder) => void): (builder: Builder) => void {
return (builder: Builder) => {
storeX(both.first)(builder);
storeY(both.second)(builder);
Expand All @@ -216,7 +217,7 @@ export function loadUnit(slice: Slice): Unit {

};
}
export function storeUnit(unit: Unit): Builder {
export function storeUnit(unit: Unit): (builder: Builder) => void {
return (builder: Builder) => {

};
Expand All @@ -229,7 +230,7 @@ export function loadTrue(slice: Slice): True {

};
}
export function storeTrue(true: True): Builder {
export function storeTrue(true: True): (builder: Builder) => void {

Check failure on line 233 in generated.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Identifier expected. 'true' is a reserved word that cannot be used here.

Check failure on line 233 in generated.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Parameter declaration expected.

Check failure on line 233 in generated.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Identifier expected. 'true' is a reserved word that cannot be used here.

Check failure on line 233 in generated.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Parameter declaration expected.

Check failure on line 233 in generated.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Identifier expected. 'true' is a reserved word that cannot be used here.

Check failure on line 233 in generated.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Parameter declaration expected.

Check failure on line 233 in generated.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Identifier expected. 'true' is a reserved word that cannot be used here.

Check failure on line 233 in generated.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Parameter declaration expected.
return (builder: Builder) => {

};
Expand All @@ -244,7 +245,7 @@ export function loadExample(slice: Slice, x: number): Example {
value: slice.loadUint(x - 2)
};
}
export function storeExample(example: Example): Builder {
export function storeExample(example: Example): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(example.value, example.x);
};
Expand All @@ -257,7 +258,7 @@ export function loadBitInteger(slice: Slice): BitInteger {
t: loadExample(slice, 4)
};
}
export function storeBitInteger(bitInteger: BitInteger): Builder {
export function storeBitInteger(bitInteger: BitInteger): (builder: Builder) => void {
return (builder: Builder) => {
storeExample(bitInteger.t)(builder);
};
Expand All @@ -283,7 +284,7 @@ export function loadUnary(slice: Slice, n: number): Unary {
};
};
}
export function storeUnary(unary: Unary): Builder {
export function storeUnary(unary: Unary): (builder: Builder) => void {
if (unary instanceof Unary_unary_zero) {
return (builder: Builder) => {
builder.storeUint(0b0, 1);
Expand All @@ -304,7 +305,7 @@ export function loadNFG(slice: Slice): NFG {
a: loadBitInteger(slice)
};
}
export function storeNFG(nFG: NFG): Builder {
export function storeNFG(nFG: NFG): (builder: Builder) => void {
return (builder: Builder) => {
storeBitInteger(nFG.a)(builder);
};
Expand All @@ -317,7 +318,7 @@ export function loadNFT(slice: Slice): NFT {

};
}
export function storeNFT(nFT: NFT): Builder {
export function storeNFT(nFT: NFT): (builder: Builder) => void {
return (builder: Builder) => {

};
Expand Down Expand Up @@ -347,14 +348,24 @@ export function loadA(slice: Slice): A {
c: slice221.loadUint(32)
};
}
export function storeA(a: A): Builder {
export function storeA(a: A): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(a.t, 32);
builder.storeUint(a.q, 32);
builder.storeUint(a.a, 32);
builder.storeUint(a.e, 32);
builder.storeUint(a.b, 32);
builder.storeUint(a.d, 32);
builder.storeUint(a.c, 32);
let cell1 = beginCell();
cell1.storeUint(a.q, 32);
builder.storeRef(cell1);
let cell2 = beginCell();
cell2.storeUint(a.a, 32);
let cell21 = beginCell();
cell21.storeUint(a.e, 32);
cell2.storeRef(cell21);
let cell22 = beginCell();
cell22.storeUint(a.b, 32);
cell22.storeUint(a.d, 32);
let cell221 = beginCell();
cell221.storeUint(a.c, 32);
cell22.storeRef(cell221);
cell2.storeRef(cell22);
builder.storeRef(cell2);
};
}
26 changes: 26 additions & 0 deletions maybe.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,30 @@ export function loadA(slice: Slice): A {
parent: [Circular *1]
}
]
}



export function storeA(a: A): (builder: Builder) => void {
return (builder: Builder) => {
let cell1 = beginCell();
let cell2 = beginCell();
let cell21 = beginCell();
let cell22 = beginCell();
let cell221 = beginCell();

builder.storeUint(a.t, 32);
cell1.storeUint(a.q, 32);
cell2.storeUint(a.a, 32);
cell21.storeUint(a.e, 32);
cell22.storeUint(a.b, 32);
cell22.storeUint(a.d, 32);
cell221.storeUint(a.c, 32);

cell22.storeRef(cell221);
cell2.storeRef(cell21);
cell2.storeRef(cell22);
builder.storeRef(cell1);
builder.storeRef(cell2);
};
}
30 changes: 21 additions & 9 deletions tests/my.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ describe('parsing into intermediate representation using grammar', () => {
let jsCodeDeclarations = []
jsCodeDeclarations.push(tImportDeclaration(tIdentifier('Builder'), 'ton')) // importDeclaration([importSpecifier(identifier('Builder'), identifier('Builder'))], stringLiteral('../boc/Builder')))
jsCodeDeclarations.push(tImportDeclaration(tIdentifier('Slice'), 'ton')) // importDeclaration([importSpecifier(identifier('Slice'), identifier('Slice'))], stringLiteral('../boc/Slice')))
jsCodeDeclarations.push(tImportDeclaration(tIdentifier('beginCell'), 'ton'))

let declarationsMap = new Map<string, Declaration[]>();
tree.declarations.forEach(declaration => {
Expand Down Expand Up @@ -617,36 +618,47 @@ describe('parsing into intermediate representation using grammar', () => {
typeParameters = tTypeParametersExpression(typeParameterArray);
}

function getCurrentSlice(slicePrefix: number[]): string {
let result = 'slice';
function getCurrentSlice(slicePrefix: number[], name:string): string {
let result = name;
slicePrefix = slicePrefix.slice(0, slicePrefix.length - 1);
slicePrefix.forEach(element => {
result += element.toString();
});
if (result == 'cell') {
return 'builder';
}
return result;
}

let slicePrefix: number[] = [0];

function handleField(field: FieldDefinition) {
let currentSlice = getCurrentSlice(slicePrefix);
let currentSlice = getCurrentSlice(slicePrefix, 'slice');
let currentCell = getCurrentSlice(slicePrefix, 'cell');

if (field instanceof FieldAnonymousDef) {
slicePrefix[slicePrefix.length - 1]++;
slicePrefix.push(0)

console.log(slicePrefix, currentSlice, getCurrentSlice(slicePrefix), field)
loadStatements.push(
tExpressionStatement(tDeclareVariable(tIdentifier(getCurrentSlice(slicePrefix)),
tExpressionStatement(tDeclareVariable(tIdentifier(getCurrentSlice(slicePrefix, 'slice')),
tFunctionCall(tMemberExpression(
tFunctionCall(tMemberExpression(
tIdentifier(currentSlice), tIdentifier('loadRef')
), []),
tIdentifier('beginParse')
), []), )))

insideStoreStatements.push(tExpressionStatement(tDeclareVariable(tIdentifier(getCurrentSlice(slicePrefix, 'cell')), tFunctionCall(tIdentifier('beginCell'), []))))

field.fields.forEach(element => {
handleField(element);
});

// cell22.storeRef(cell221);

insideStoreStatements.push(tExpressionStatement(tFunctionCall(tMemberExpression(tIdentifier(currentCell), tIdentifier('storeRef')), [tIdentifier(getCurrentSlice(slicePrefix, 'cell'))])))

slicePrefix.pop();
}

Expand Down Expand Up @@ -716,17 +728,17 @@ describe('parsing into intermediate representation using grammar', () => {

structProperties.push(tTypedIdentifier(tIdentifier(field.name), tTypeWithParameters(tIdentifier(field.expr.name), currentTypeParameters)));
loadProperties.push(tObjectProperty(tIdentifier(field.name), tFunctionCall(tIdentifier('load' + field.expr.name), insideLoadParameters.concat(loadFunctionsArray), currentTypeParameters)))
insideStoreStatements.push(tExpressionStatement(tFunctionCall(tFunctionCall(tIdentifier('store' + field.expr.name), insideStoreParameters.concat(storeFunctionsArray), currentTypeParameters), [tIdentifier('builder')])))
insideStoreStatements.push(tExpressionStatement(tFunctionCall(tFunctionCall(tIdentifier('store' + field.expr.name), insideStoreParameters.concat(storeFunctionsArray), currentTypeParameters), [tIdentifier(currentCell)])))
}
if (field.expr instanceof NameExpr) {
structProperties.push(tTypedIdentifier(tIdentifier(field.name), tIdentifier(field.expr.name)));
loadProperties.push(tObjectProperty(tIdentifier(field.name), tFunctionCall(tIdentifier('load' + field.expr.name), [tIdentifier(currentSlice)])))
insideStoreStatements.push(tExpressionStatement(tFunctionCall(tFunctionCall(tIdentifier('store' + field.expr.name), [tMemberExpression(tIdentifier(variableCombinatorName), tIdentifier(field.name))]), [tIdentifier('builder')])))
insideStoreStatements.push(tExpressionStatement(tFunctionCall(tFunctionCall(tIdentifier('store' + field.expr.name), [tMemberExpression(tIdentifier(variableCombinatorName), tIdentifier(field.name))]), [tIdentifier(currentCell)])))
}
if (bitsLoad != undefined && bitsStore != undefined) {
structProperties.push(tTypedIdentifier(tIdentifier(field.name), tIdentifier('number')))
loadProperties.push(tObjectProperty(tIdentifier(field.name), tFunctionCall(tMemberExpression(tIdentifier(currentSlice), tIdentifier('loadUint')), [bitsLoad])))
insideStoreStatements.push(tExpressionStatement(tFunctionCall(tMemberExpression(tIdentifier('builder'), tIdentifier('storeUint')), [tMemberExpression(tIdentifier(variableCombinatorName), tIdentifier(field.name)), bitsStore])))
insideStoreStatements.push(tExpressionStatement(tFunctionCall(tMemberExpression(tIdentifier(currentCell), tIdentifier('storeUint')), [tMemberExpression(tIdentifier(variableCombinatorName), tIdentifier(field.name)), bitsStore])))
}
}
}
Expand Down Expand Up @@ -780,7 +792,7 @@ describe('parsing into intermediate representation using grammar', () => {
[tTypedIdentifier(tIdentifier(firstLower(element.name)), tIdentifier(element.name))],
tArrowFunctionType([tTypedIdentifier(tIdentifier('builder'), tIdentifier('Builder'))], tIdentifier('void')))))
});
let storeFunction = tFunctionDeclaration(tIdentifier('store' + combinatorName), typeParameters, tIdentifier('Builder'), storeFunctionParameters, storeStatements)
let storeFunction = tFunctionDeclaration(tIdentifier('store' + combinatorName), typeParameters, tIdentifier('(builder: Builder) => void'), storeFunctionParameters, storeStatements)
tmpDeclarations.push(storeFunction)

if (value.length > 1) {
Expand Down

0 comments on commit 18b3159

Please sign in to comment.