Skip to content

Commit

Permalink
vmstack to tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Nov 5, 2024
1 parent 954f1fb commit dfea500
Show file tree
Hide file tree
Showing 9 changed files with 1,210 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export type TLBMultipleType = {
times: TLBMathExpr;
};

export type TLBTupleType = {
kind: "TLBTupleType";
}

export type TLBCondType = {
kind: "TLBCondType";
value: TLBFieldType;
Expand All @@ -146,6 +150,7 @@ export type TLBFieldType =
| TLBNegatedType
| TLBCellInsideType
| TLBMultipleType
| TLBTupleType
| TLBCondType
| TLBExoticType;

Expand Down
4 changes: 4 additions & 0 deletions src/astbuilder/handle_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ export function getType(
signed: true,
maxBits: 257,
};
} else if (expr.name == "VmStack") {
return {
kind: "TLBTupleType",
}
} else if (expr.name == "Bits") {
return { kind: "TLBBitsType", bits: new TLBNumberExpr(1023) };
} else if (expr.name == "Bit") {
Expand Down
3 changes: 1 addition & 2 deletions src/generators/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export interface CodeGenerator {
tlbCode: TLBCode

addTonCoreClassUsage(name: string): void
addBitLenFunction(): void
addEmbeddedTypes(): void
addBuiltinCode(): void
addTlbType(tlbType: TLBType): void
toCode(node: TheNode, code: CodeBuilder): CodeBuilder
}
Expand Down
22 changes: 22 additions & 0 deletions src/generators/typescript/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,27 @@ export class TypescriptGenerator implements CodeGenerator {
tImportDeclaration(id(name), tStringLiteral("@ton/core"))
);
}

addBuiltinCode(): void {
this.addBitLenFunction();
this.addEmbeddedTypes();
this.addCopyCellToBuilder();
}

addBitLenFunction() {
this.jsCodeDeclarations.push(bitlenFunctionDecl());
}

addCopyCellToBuilder() {
this.jsCodeDeclarations.push(tCodeAsIs(`export function copyCellToBuilder(from: Cell, to: Builder): void {
let slice = from.beginParse();
to.storeBits(slice.loadBits(slice.remainingBits));
while (slice.remainingRefs) {
to.storeRef(slice.loadRef());
}
}`))
}

addEmbeddedTypes() {
this.jsCodeDeclarations.push(tCodeAsIs(`export interface Bool {
readonly kind: 'Bool';
Expand Down Expand Up @@ -762,6 +779,11 @@ export function storeBool(bool: Bool): (builder: Builder) => void {
fieldLoadSuffix: fieldType.signed ? "VarIntBig" : "VarUintBig",
fieldStoreSuffix: fieldType.signed ? "VarInt" : "VarUint"
}
} else if (fieldType.kind == "TLBTupleType") {
result.loadExpr = tFunctionCall(id("parseTuple"), [tFunctionCall(tMemberExpression(id("slice"), id('asCell')), [])]);
result.typeParamExpr = id('TupleItem[]');
result.storeStmtInside = tExpressionStatement(tFunctionCall(id('copyCellToBuilder'), [tFunctionCall(id('serializeTuple'), storeParametersInside), id('builder')]));
result.storeStmtOutside = tExpressionStatement(tFunctionCall(id('copyCellToBuilder'), [tFunctionCall(id('serializeTuple'), storeParametersOutside), id('builder')]));
} else if (fieldType.kind == "TLBAddressType") {
if (fieldType.addrType == "Internal") {
exprForParam = {
Expand Down
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ export function generateCodeByAST(tree: Program, input: string, getGenerator: (t
codeGenerator.addTonCoreClassUsage("ExternalAddress");
codeGenerator.addTonCoreClassUsage("Dictionary")
codeGenerator.addTonCoreClassUsage("DictionaryValue")
codeGenerator.addTonCoreClassUsage("TupleItem");
codeGenerator.addTonCoreClassUsage("parseTuple");
codeGenerator.addTonCoreClassUsage("serializeTuple");

codeGenerator.addBitLenFunction();
codeGenerator.addEmbeddedTypes();
codeGenerator.addBuiltinCode();

let jsCodeDeclarations: CommonGenDeclaration[] = [];
codeGenerator.jsCodeDeclarations.forEach((declaration) => {
Expand Down
24 changes: 21 additions & 3 deletions test/generated_files/generated_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { Address } from '@ton/core'
import { ExternalAddress } from '@ton/core'
import { Dictionary } from '@ton/core'
import { DictionaryValue } from '@ton/core'
import { TupleItem } from '@ton/core'
import { parseTuple } from '@ton/core'
import { serializeTuple } from '@ton/core'
export function bitLen(n: number) {
return n.toString(2).length;
}
Expand Down Expand Up @@ -34,6 +37,13 @@ export function storeBool(bool: Bool): (builder: Builder) => void {
})

}
export function copyCellToBuilder(from: Cell, to: Builder): void {
let slice = from.beginParse();
to.storeBits(slice.loadBits(slice.remainingBits));
while (slice.remainingRefs) {
to.storeRef(slice.loadRef());
}
}
// unit$_ = Unit;

export interface Unit {
Expand Down Expand Up @@ -3389,7 +3399,7 @@ cp:(Maybe int16) = VmControlData;
export interface VmControlData {
readonly kind: 'VmControlData';
readonly nargs: Maybe<number>;
readonly stack: Maybe<VmStack>;
readonly stack: Maybe<TupleItem[]>;
readonly save: VmSaveList;
readonly cp: Maybe<number>;
}
Expand Down Expand Up @@ -13524,7 +13534,10 @@ export function loadVmControlData(slice: Slice): VmControlData {
return slice.loadUint(13)

}));
let stack: Maybe<VmStack> = loadMaybe<VmStack>(slice, loadVmStack);
let stack: Maybe<TupleItem[]> = loadMaybe<TupleItem[]>(slice, ((slice: Slice) => {
return parseTuple(slice.asCell())

}));
let save: VmSaveList = loadVmSaveList(slice);
let cp: Maybe<number> = loadMaybe<number>(slice, ((slice: Slice) => {
return slice.loadInt(16)
Expand All @@ -13548,7 +13561,12 @@ export function storeVmControlData(vmControlData: VmControlData): (builder: Buil
})

}))(builder);
storeMaybe<VmStack>(vmControlData.stack, storeVmStack)(builder);
storeMaybe<TupleItem[]>(vmControlData.stack, ((arg: TupleItem[]) => {
return ((builder: Builder) => {
copyCellToBuilder(serializeTuple(arg), builder);
})

}))(builder);
storeVmSaveList(vmControlData.save)(builder);
storeMaybe<number>(vmControlData.cp, ((arg: number) => {
return ((builder: Builder) => {
Expand Down
Loading

0 comments on commit dfea500

Please sign in to comment.