Skip to content

Commit 80b364d

Browse files
authored
Merge pull request #34 from TrueCarry/feat/string-generator
Add non writing to fs generator
2 parents 70260e1 + b3ea92b commit 80b364d

16 files changed

Lines changed: 2170 additions & 699 deletions

File tree

index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { generateCode } from "./src/main";
1+
export { generateCode, getTLBCode, getTLBCodeByAST, generateCodeByAST, generateCodeWithGenerator, generateCodeFromData } from "./src/main";
22
export * from "./src/ast";
3-
export { generateCode, getTLBCode, getTLBCodeByAST, generateCodeByAST, generateCodeWithGenerator } from "./src/main";
43
export { isBigInt as isBigIntForJson, isBigIntExpr as isBigIntExprForJson } from "./src/generators/typescript/utils";
54
export { CodeGenerator } from "./src/generators/generator";
65
export { TypescriptGenerator } from "./src/generators/typescript/generator";

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ton-community/tlb-codegen",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "",
55
"main": "build/index.js",
66
"scripts": {
@@ -27,5 +27,6 @@
2727
"crc-32": "^1.2.2",
2828
"meow": "^9.0.0",
2929
"ts-jest": "^29.1.1"
30-
}
30+
},
31+
"packageManager": "pnpm@9.15.0+sha512.76e2379760a4328ec4415815bcd6628dee727af3779aaa4c914e3944156c4299921a89f976381ee107d41f12cfa4b66681ca9c718f0668fa0831ed4c6d8ba56c"
3132
}

src/ast.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ export type TLBMathExprType = {
7373
initialExpr: TLBMathExpr;
7474
};
7575

76-
export type TLBBoolType = {
77-
kind: "TLBBoolType";
78-
};
79-
8076
export type TLBCoinsType = {
8177
kind: "TLBCoinsType";
8278
}
@@ -97,6 +93,7 @@ export type TLBHashmapType = {
9793
key: TLBMathExprType;
9894
value: TLBFieldType;
9995
extra?: TLBFieldType;
96+
directStore: boolean;
10097
}
10198

10299
export type TLBCellType = {
@@ -127,6 +124,10 @@ export type TLBMultipleType = {
127124
times: TLBMathExpr;
128125
};
129126

127+
export type TLBTupleType = {
128+
kind: "TLBTupleType";
129+
}
130+
130131
export type TLBCondType = {
131132
kind: "TLBCondType";
132133
value: TLBFieldType;
@@ -137,11 +138,16 @@ export type TLBExoticType = {
137138
kind: "TLBExoticType";
138139
};
139140

141+
export type TLBBoolType = {
142+
kind: "TLBBoolType";
143+
value: boolean | undefined;
144+
}
145+
140146
export type TLBFieldType =
141147
| TLBNumberType
148+
| TLBBoolType
142149
| TLBBitsType
143150
| TLBNamedType
144-
| TLBBoolType
145151
| TLBCoinsType
146152
| TLBAddressType
147153
| TLBHashmapType
@@ -151,6 +157,7 @@ export type TLBFieldType =
151157
| TLBNegatedType
152158
| TLBCellInsideType
153159
| TLBMultipleType
160+
| TLBTupleType
154161
| TLBCondType
155162
| TLBExoticType;
156163

src/astbuilder/fill_constructors.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ function checkAndRemovePrimitives(
434434
typesToDelete.set("VarInteger", ["d466ed5"])
435435
typesToDelete.set("HashmapE", ["32bae5cb", "28fa3979"])
436436
typesToDelete.set("HashmapAugE", ["36820dce", "5f71ac75"])
437+
typesToDelete.set("BoolTrue", ["943ebb5"])
438+
typesToDelete.set("BoolFalse", ["1f5e497d"])
437439

438440
typesToDelete.forEach((opCodesExpected: string[], typeName: string) => {
439441
let typeItems = typeDeclarations.get(typeName);

src/astbuilder/handle_field.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function getField(
2525
let result: TLBField = {
2626
name: "",
2727
anonymous: true,
28-
fieldType: { kind: "TLBBoolType" },
28+
fieldType: { kind: "TLBNamedType", name: "Bool", arguments: [] },
2929
subFields: [],
3030
};
3131
let currentFieldIndex = 0;
@@ -80,7 +80,7 @@ function getField(
8080
let result: TLBField = {
8181
name: fieldName,
8282
anonymous: true,
83-
fieldType: { kind: "TLBBoolType" },
83+
fieldType: { kind: "TLBNamedType", name: "Bool", arguments: [] },
8484
subFields: [subField],
8585
};
8686
return result;

src/astbuilder/handle_type.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export function getType(
170170
if (key.kind != 'TLBExprMathType') {
171171
throw new Error('Hashmap key should be number')
172172
}
173-
return { kind: "TLBHashmapType", key: key, value: value };
173+
return { kind: "TLBHashmapType", key: key, value: value, directStore: false };
174174
} else if (expr.name == "HashmapAugE") {
175175
if (expr.args.length != 3) {
176176
throw new Error('Not enough arguments for HashmapAugE')
@@ -181,7 +181,14 @@ export function getType(
181181
if (key.kind != 'TLBExprMathType') {
182182
throw new Error('Hashmap key should be number')
183183
}
184-
return { kind: "TLBHashmapType", key: key, value: value, extra: extra };
184+
return { kind: "TLBHashmapType", key: key, value: value, extra: extra, directStore: false };
185+
} else if (expr.name == "Hashmap" && constructor.tlbType != "HashmapNode") {
186+
let key = getType(expr.args[0], constructor, fieldTypeName)
187+
let value = getType(expr.args[1], constructor, fieldTypeName)
188+
if (key.kind != 'TLBExprMathType') {
189+
throw new Error('Hashmap key should be number')
190+
}
191+
return { kind: "TLBHashmapType", key: key, value: value, directStore: true };
185192
} else if (
186193
expr.name == "VarUInteger" &&
187194
(expr.args[0] instanceof MathExpr ||
@@ -229,6 +236,10 @@ export function getType(
229236
signed: true,
230237
maxBits: 257,
231238
};
239+
} else if (expr.name == "VmStack") {
240+
return {
241+
kind: "TLBTupleType",
242+
}
232243
} else if (expr.name == "Bits") {
233244
return { kind: "TLBBitsType", bits: new TLBNumberExpr(1023) };
234245
} else if (expr.name == "Bit") {
@@ -261,8 +272,6 @@ export function getType(
261272
};
262273
} else if ((theNum = splitForTypeValue(expr.name, "bits")) != undefined) {
263274
return { kind: "TLBBitsType", bits: new TLBNumberExpr(theNum) };
264-
} else if (expr.name == "Bool") {
265-
return { kind: "TLBBoolType" };
266275
} else if (expr.name == "MsgAddressInt") {
267276
return { kind: "TLBAddressType", addrType: "Internal" };
268277
} else if (expr.name == "MsgAddressExt") {
@@ -271,8 +280,14 @@ export function getType(
271280
return { kind: "TLBAddressType", addrType: "Any" };
272281
} else if (expr.name == "Bit") {
273282
return { kind: "TLBBitsType", bits: new TLBNumberExpr(1) };
274-
} else if (expr.name == "Grams") {
283+
} else if (expr.name == "Grams" || expr.name == "Coins") {
275284
return { kind: "TLBCoinsType" };
285+
} else if (expr.name == "Bool") {
286+
return { kind: "TLBBoolType", value: undefined };
287+
} else if (expr.name == "BoolFalse") {
288+
return { kind: "TLBBoolType", value: false };
289+
} else if (expr.name == "BoolTrue") {
290+
return { kind: "TLBBoolType", value: true };
276291
} else {
277292
if (constructor.variablesMap.get(expr.name)?.type == "#") {
278293
return {

src/generators/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface CodeGenerator {
99
tlbCode: TLBCode
1010

1111
addTonCoreClassUsage(name: string): void
12-
addBitLenFunction(): void
12+
addBuiltinCode(): void
1313
addTlbType(tlbType: TLBType): void
1414
toCode(node: TheNode, code: CodeBuilder): CodeBuilder
1515
}

src/generators/typescript/complex_expr.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TLBCode, TLBConstructorTag, TLBField, TLBHashmapType, TLBMathExprType } from "../../ast";
22
import { findNotReservedName, firstLower, getCurrentSlice } from "../../utils";
33
import { ConstructorContext } from "./generator";
4-
import { BinaryExpression, Expression, GenDeclaration, Identifier, ObjectExpression, Statement, TypeExpression, TypeParametersExpression, TypedIdentifier, id, tArrowFunctionExpression, tArrowFunctionType, tBinaryExpression, tDeclareVariable, tExpressionStatement, tForCycle, tFunctionCall, tFunctionDeclaration, tIfStatement, tMemberExpression, tMultiStatement, tNumericLiteral, tObjectExpression, tObjectProperty, tReturnStatement, tStringLiteral, tStructExpression, tTypeParametersExpression, tTypeWithParameters, tTypedIdentifier, tUnaryOpExpression, toCode } from "./tsgen";
4+
import { BinaryExpression, Expression, GenDeclaration, Identifier, ObjectExpression, Statement, TypeExpression, TypeParametersExpression, TypedIdentifier, id, tArrowFunctionExpression, tArrowFunctionType, tBinaryExpression, tDeclareVariable, tExpressionStatement, tForCycle, tFunctionCall, tFunctionDeclaration, tIdentifier, tIfStatement, tMemberExpression, tMultiStatement, tNumericLiteral, tObjectExpression, tObjectProperty, tReturnStatement, tStringLiteral, tStructDeclaration, tStructExpression, tTypeParametersExpression, tTypeWithParameters, tTypedIdentifier, tUnaryOpExpression, toCode } from "./tsgen";
55
import { ExprForParam, convertToAST, getNegationDerivationFunctionBody, isBigIntExpr } from "./utils";
66

77
export function tEqualExpression(left: Expression, right: Expression) {
@@ -40,6 +40,7 @@ export function bitlenFunctionDecl(): GenDeclaration {
4040
[tExpressionStatement(id("return n.toString(2).length"))]
4141
);
4242
}
43+
4344
export function typedSlice() {
4445
return [tTypedIdentifier(id("slice"), id("Slice"))];
4546
}
@@ -331,8 +332,8 @@ export function negationDerivationFuncDecl(
331332
)
332333
);
333334
}
334-
export function dictStoreStmt(currentCell: string, storeParametersInside: Expression[], keyForStore: Expression, valueStore: ObjectExpression): Statement | undefined {
335-
return tExpressionStatement(tFunctionCall(tMemberExpression(id(currentCell), id('storeDict')), storeParametersInside.concat([keyForStore, valueStore])));
335+
export function dictStoreStmt(currentCell: string, storeParametersInside: Expression[], keyForStore: Expression, valueStore: ObjectExpression, direct: boolean): Statement | undefined {
336+
return tExpressionStatement(tFunctionCall(tMemberExpression(id(currentCell), id('storeDict' + (direct ? 'Direct' : ''))), storeParametersInside.concat([keyForStore, valueStore])));
336337
}
337338
export function dictTypeParamExpr(fieldType: TLBHashmapType, typeParamExpr: TypeExpression): TypeExpression | undefined {
338339
return tTypeWithParameters(id('Dictionary'), tTypeParametersExpression([(isBigIntExpr(fieldType.key) ? id('bigint') : id('number')), typeParamExpr]));
@@ -351,8 +352,8 @@ export function dictValueStore(typeParamExpr: TypeExpression, storeFunctionExpr:
351352
)
352353
]);
353354
}
354-
export function dictLoadExpr(keyForLoad: Expression, loadFunctionExpr: Expression, currentSlice: string): Expression | undefined {
355-
return tFunctionCall(tMemberExpression(id('Dictionary'), id('load')), [keyForLoad, dictValueLoad(loadFunctionExpr), id(currentSlice)]);
355+
export function dictLoadExpr(keyForLoad: Expression, loadFunctionExpr: Expression, currentSlice: string, direct: boolean): Expression | undefined {
356+
return tFunctionCall(tMemberExpression(id('Dictionary'), id('load' + (direct ? 'Direct' : ''))), [keyForLoad, dictValueLoad(loadFunctionExpr), id(currentSlice)]);
356357
}
357358
function dictValueLoad(loadFunctionExpr: Expression) {
358359
return tObjectExpression([

0 commit comments

Comments
 (0)