Skip to content

Commit

Permalink
int & other as type parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Nov 16, 2023
1 parent 3d03a2c commit 97930ae
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 11 deletions.
44 changes: 40 additions & 4 deletions generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ export function storeA(a: A): (builder: Builder) => void {
builder.storeRef(cell2);
};
}
export type IntEx = {
export type IntEx<Arg> = {
a: number;
b: BitString;
c: number;
Expand All @@ -402,8 +402,12 @@ export type IntEx = {
i: BitString;
j: number;
k: number;
tc: Slice;
Arg: number;
arg: Arg;
x: Slice;
};
export function loadIntEx(slice: Slice): IntEx {
export function loadIntEx<Arg>(slice: Slice, loadArg: (slice: Slice) => Arg, Arg: number): IntEx<Arg> {
let a = slice.loadUint(257);
let slice1 = slice.loadRef().beginParse();
let b = slice1.loadBits(1023);
Expand All @@ -416,6 +420,8 @@ export function loadIntEx(slice: Slice): IntEx {
let i = slice.loadBits(5 + e);
let j = slice.loadInt(5);
let k = slice.loadUint(e);
let tc = slice;
let x = slice;
return {
a: a,
b: b,
Expand All @@ -427,10 +433,14 @@ export function loadIntEx(slice: Slice): IntEx {
f: f,
i: i,
j: j,
k: k
k: k,
tc: tc,
Arg: Arg,
arg: loadArg(slice),
x: x
};
}
export function storeIntEx(intEx: IntEx): (builder: Builder) => void {
export function storeIntEx<Arg>(intEx: IntEx<Arg>, storeArg: (arg: Arg) => (builder: Builder) => void): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(intEx.a, 257);
let cell1 = beginCell();
Expand All @@ -445,5 +455,31 @@ export function storeIntEx(intEx: IntEx): (builder: Builder) => void {
builder.storeBits(intEx.i);
builder.storeInt(intEx.j, 5);
builder.storeUint(intEx.k, intEx.e);
builder.storeSlice(intEx.tc);
storeArg(intEx.arg)(builder);
builder.storeSlice(intEx.x);
};
}
export type IntexArg = {
x: number;
a: IntEx<number>;
};
export function loadIntexArg(slice: Slice): IntexArg {
let x = slice.loadUint(32);
return {
x: x,
a: loadIntEx<number>(slice, () => {
return slice.loadInt(5 * x);
})
};
}
export function storeIntexArg(intexArg: IntexArg): (builder: Builder) => void {
return (builder: Builder) => {
builder.storeUint(intexArg.x, 32);
storeIntEx<number>(intexArg.a, (arg: number) => {
return (builder: Builder) => {
builder.storeInt(arg, 5 * intexArg.x);
};
})(builder);
};
}
19 changes: 19 additions & 0 deletions maybe.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,23 @@ export function loadIntEx(slice: Slice): IntEx {
g: g,
h: h
};
}




export type IntexArg = {
a: IntEx<number>;
};
export function loadIntexArg(slice: Slice): IntexArg {
return {
a: loadIntEx(slice, (slice: Slice) => {return slice.loadUint(22);})
};
}


export function storeIntexArg(intexArg: IntexArg): (builder: Builder) => void {
return (builder: Builder) => {
storeIntEx(intexArg.a, (arg: number) => {return (builder: Builder) => {builder.storeUint(arg, 22);}})(builder);
};
}
4 changes: 2 additions & 2 deletions tests/fixtures/tlb/my.tlb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ a$_ a:^BitInteger = NFT;

a$_ t:# ^[q:#] ^[ a:(## 32) ^[e:#] ^[ b:(## 32) d:# ^[ c:(## 32) ] ] ] = A;

a$_ a:Int b:^Bits c:Uint d:int73 e:uint89 g:bits10 h:(int (e * e * 8)) f:(uint (7 * e)) i:(bits (5 + e)) j:(int 5) k:(uint e) = IntEx;

a$_ a:Int b:^Bits c:Uint d:int73 e:uint89 g:bits10 h:(int (e * e * 8)) f:(uint (7 * e)) i:(bits (5 + e)) j:(int 5) k:(uint e) tc:Cell {Arg:Type} arg:Arg x:Any = IntEx Arg;
a$_ x:# a:(IntEx (int (5*x))) = IntexArg;
49 changes: 44 additions & 5 deletions tests/my.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,6 @@ describe('parsing into intermediate representation using grammar', () => {
let implicitFields = new Map<string, string>();
let implicitFieldsDerived = new Map<string, Expression>();



value.forEach(declaration => {
let structName: string;
if (value.length > 1) {
Expand Down Expand Up @@ -652,7 +650,6 @@ describe('parsing into intermediate representation using grammar', () => {
let slicePrefix: number[] = [0];

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

Expand Down Expand Up @@ -768,6 +765,7 @@ describe('parsing into intermediate representation using grammar', () => {

if (bitsLoad == undefined) {
field.expr.args.forEach(element => {
console.log(element)
if (element instanceof NameExpr) {
typeParameterArray.push(tIdentifier(element.name))
loadFunctionsArray.push(tIdentifier('load' + element.name))
Expand All @@ -782,6 +780,37 @@ describe('parsing into intermediate representation using grammar', () => {
loadFunctionsArray.push(derivedExpression)
}
}
if (element instanceof CombinatorExpr) {
let theFieldType = 'number'
let theFieldLoadStoreName = 'Uint';
let theBitsLoad: Expression = tIdentifier('');
let theBitsStore: Expression = tIdentifier('');
if (element.args.length > 0 && (element.args[0] instanceof MathExpr || element.args[0] instanceof NumberExpr || element.args[0] instanceof NameExpr)) {
// (slice: Slice) => {return slice.loadUint(22);}
if (element.name == 'int') {
theFieldLoadStoreName = 'Int'
let myMathExpr = convertToMathExpr(element.args[0])
theBitsLoad = convertToAST(myMathExpr);
theBitsStore = convertToAST(myMathExpr, tIdentifier(variableStructName))
}
if (element.name == 'uint') {
let myMathExpr = convertToMathExpr(element.args[0])
theBitsLoad = convertToAST(myMathExpr);
theBitsStore = convertToAST(myMathExpr, tIdentifier(variableStructName))
}
if (element.name == 'bits') {
theFieldType = 'BitString'
theFieldLoadStoreName = 'Bits'
let myMathExpr = convertToMathExpr(element.args[0])
theBitsLoad = convertToAST(myMathExpr);
theBitsStore = convertToAST(myMathExpr, tIdentifier(variableStructName))
}
typeParameterArray.push(tIdentifier(theFieldType))
}
loadFunctionsArray.push(tArrowFunctionExpression([], [tReturnStatement(tFunctionCall(tMemberExpression(tIdentifier(currentSlice), tIdentifier('load' + theFieldLoadStoreName)), [theBitsLoad]))]))
//(arg: number) => {return (builder: Builder) => {builder.storeUint(arg, 22);}}
storeFunctionsArray.push(tArrowFunctionExpression([tTypedIdentifier(tIdentifier('arg'), tIdentifier(theFieldType))], [tReturnStatement(tArrowFunctionExpression([tTypedIdentifier(tIdentifier('builder'), tIdentifier('Builder'))], [tExpressionStatement(tFunctionCall(tMemberExpression(tIdentifier('builder'), tIdentifier('store' + theFieldLoadStoreName)), [tIdentifier('arg'), theBitsStore]))]))]))
}
});

let currentTypeParameters = tTypeParametersExpression(typeParameterArray);
Expand All @@ -808,6 +837,12 @@ describe('parsing into intermediate representation using grammar', () => {
if (expName == 'Uint') {
bitsLoad = bitsStore = tNumericLiteral(256);
}
if (expName == 'Any' || expName == 'Cell') {
fieldType = 'Slice'
fieldLoadStoreName = 'Slice'
bitsLoad = tIdentifier(currentSlice);
bitsStore = tIdentifier(currentSlice);
}
let theNum = splitForTypeValue(expName, 'int')
if (theNum != undefined) {
fieldLoadStoreName = 'Int';
Expand All @@ -831,11 +866,15 @@ describe('parsing into intermediate representation using grammar', () => {
}
}
if (bitsLoad != undefined && bitsStore != undefined) {
loadStatements.push(tExpressionStatement(tDeclareVariable(tIdentifier(field.name), tFunctionCall(tMemberExpression(tIdentifier(currentSlice), tIdentifier('load' + fieldLoadStoreName)), [bitsLoad]))))
let loadSt: Expression = tFunctionCall(tMemberExpression(tIdentifier(currentSlice), tIdentifier('load' + fieldLoadStoreName)), [bitsLoad]);
if (fieldType == 'Slice') {
loadSt = tIdentifier(currentSlice)
}
loadStatements.push(tExpressionStatement(tDeclareVariable(tIdentifier(field.name), loadSt)))
structProperties.push(tTypedIdentifier(tIdentifier(field.name), tIdentifier(fieldType)))
loadProperties.push(tObjectProperty(tIdentifier(field.name), tIdentifier(field.name)))
let storeParams: Expression[] = [tMemberExpression(tIdentifier(variableCombinatorName), tIdentifier(field.name))];
if (fieldType != 'BitString') {
if (fieldType != 'BitString' && fieldType != 'Slice') {
storeParams.push(bitsStore);
}
insideStoreStatements.push(tExpressionStatement(tFunctionCall(tMemberExpression(tIdentifier(currentCell), tIdentifier('store' + fieldLoadStoreName)), storeParams)))
Expand Down

0 comments on commit 97930ae

Please sign in to comment.