Skip to content

Commit

Permalink
Handle HashmapE as Dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Jan 14, 2024
1 parent 25df103 commit 6169930
Show file tree
Hide file tree
Showing 11 changed files with 801 additions and 368 deletions.
7 changes: 7 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ export type TLBVarIntegerType = {
n: TLBMathExpr;
}

export type TLBHashmapType = {
kind: "TLBHashmapType";
key: TLBMathExprType,
value: TLBFieldType
}

export type TLBCellType = {
kind: "TLBCellType";
};
Expand Down Expand Up @@ -136,6 +142,7 @@ export type TLBFieldType =
| TLBBoolType
| TLBCoinsType
| TLBAddressType
| TLBHashmapType
| TLBVarIntegerType
| TLBCellType
| TLBMathExprType
Expand Down
1 change: 1 addition & 0 deletions src/astbuilder/fill_constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ function checkAndRemovePrimitives(
typesToDelete.set("MsgAddress", ["606aa05e", "21d0382b"])
typesToDelete.set("VarUInteger", ["11d56c2e"])
typesToDelete.set("VarInteger", ["d466ed5"])
typesToDelete.set("HashmapE", ["32bae5cb", "28fa3979"])

typesToDelete.forEach((opCodesExpected: string[], typeName: string) => {
let typeItems = typeDeclarations.get(typeName);
Expand Down
10 changes: 10 additions & 0 deletions src/astbuilder/handle_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ export function getType(
constructor
),
};
} else if (expr.name == "HashmapE") {
if (expr.args.length != 2) {
throw new Error('')
}
let key = getType(expr.args[0], constructor, fieldTypeName)
let value = getType(expr.args[1], constructor, fieldTypeName)
if (key.kind != 'TLBExprMathType') {
throw new Error('Hashmap key should be number')
}
return { kind: "TLBHashmapType", key: key, value: value };
} else if (
expr.name == "VarUInteger" &&
(expr.args[0] instanceof MathExpr ||
Expand Down
44 changes: 41 additions & 3 deletions src/generators/typescript/complex_expr.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TLBCode, TLBConstructorTag, TLBField } from "../../ast";
import { TLBCode, TLBConstructorTag, TLBField, TLBHashmapType, TLBMathExprType } from "../../ast";
import { findNotReservedName, firstLower, getCurrentSlice } from "../../utils";
import { ConstructorContext } from "./generator";
import { BinaryExpression, Expression, GenDeclaration, Identifier, Statement, TypeExpression, TypeParametersExpression, TypedIdentifier, id, tArrowFunctionExpression, tArrowFunctionType, tBinaryExpression, tDeclareVariable, tExpressionStatement, tForCycle, tFunctionCall, tFunctionDeclaration, tIfStatement, tMemberExpression, tMultiStatement, tNumericLiteral, tReturnStatement, tStringLiteral, tTypeParametersExpression, tTypeWithParameters, tTypedIdentifier, tUnaryOpExpression, toCode } from "./tsgen";
import { ExprForParam, getNegationDerivationFunctionBody } from "./utils";
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, tTypeParametersExpression, tTypeWithParameters, tTypedIdentifier, tUnaryOpExpression, toCode } from "./tsgen";
import { ExprForParam, convertToAST, getNegationDerivationFunctionBody, isBigIntExpr } from "./utils";

export function tEqualExpression(left: Expression, right: Expression) {
return tBinaryExpression(left, '==', right)
Expand Down Expand Up @@ -330,3 +330,41 @@ export function negationDerivationFuncDecl(
)
);
}
export function dictStoreStmt(currentCell: string, storeParametersInside: Expression[], keyForStore: Expression, valueStore: ObjectExpression): Statement | undefined {
return tExpressionStatement(tFunctionCall(tMemberExpression(id(currentCell), id('storeDict')), storeParametersInside.concat([keyForStore, valueStore])));
}
export function dictTypeParamExpr(fieldType: TLBHashmapType, typeParamExpr: TypeExpression): TypeExpression | undefined {
return tTypeWithParameters(id('Dictionary'), tTypeParametersExpression([(isBigIntExpr(fieldType.key) ? id('bigint') : id('number')), typeParamExpr]));
}
export function dictValueStore(typeParamExpr: TypeExpression, storeFunctionExpr: Expression) {
return tObjectExpression([
tObjectProperty(id('serialize'),
tArrowFunctionExpression([tTypedIdentifier(id('arg'), typeParamExpr), tTypedIdentifier(id('builder'), id('Builder'))], [tExpressionStatement(tFunctionCall(tFunctionCall(storeFunctionExpr, [id('arg')]), [id('builder')]))])
),
tObjectProperty(id('parse'),
id("() => { throw new Error('Not implemented') }")
)
]);
}
export function dictLoadExpr(keyForLoad: Expression, loadFunctionExpr: Expression, currentSlice: string): Expression | undefined {
return tFunctionCall(tMemberExpression(id('Dictionary'), id('load')), [keyForLoad, dictValueLoad(loadFunctionExpr), id(currentSlice)]);
}
function dictValueLoad(loadFunctionExpr: Expression) {
return tObjectExpression([
tObjectProperty(id('serialize'),
id("() => { throw new Error('Not implemented') }")
),
tObjectProperty(id('parse'),
loadFunctionExpr
)
]);
}
export function dictKeyExpr(keyType: TLBMathExprType, ctx: ConstructorContext, objectId?: string): Expression {
let param: Expression;
if (objectId) {
param = convertToAST(keyType.expr, ctx.constructor, id(objectId));
} else {
param = convertToAST(keyType.expr, ctx.constructor);
}
return tFunctionCall(tMemberExpression(id('Dictionary.Keys'), (isBigIntExpr(keyType) ? id('BigUint') : id('Uint'))), [param]);
}
26 changes: 22 additions & 4 deletions src/generators/typescript/generator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Identifier } from "typescript";
import {
TLBCode,
TLBConstructor,
Expand Down Expand Up @@ -62,7 +63,6 @@ import {
TypeParametersExpression,
TypedIdentifier,
id,
tArrowFunctionExpression,
tComment,
tExpressionStatement,
tFunctionCall,
Expand Down Expand Up @@ -94,6 +94,7 @@ import {
getTypeParametersExpression,
isBigInt,
} from "./utils";
import { dictKeyExpr, dictLoadExpr, dictValueStore, dictTypeParamExpr, dictStoreStmt } from "./complex_expr";

/*
Expand Down Expand Up @@ -589,9 +590,6 @@ export class TypescriptGenerator implements CodeGenerator {
);
});
} else if (field.subFields.length == 0) {
if (field == undefined) {
throw new Error("");
}
let fieldInfo = this.handleType(
field,
field.fieldType,
Expand Down Expand Up @@ -902,6 +900,25 @@ export class TypescriptGenerator implements CodeGenerator {
if (subExprInfo.storeStmtInside) {
result.storeStmtInside = storeInNewCell(currentCell, subExprInfo.storeStmtInside);
}
} else if (fieldType.kind == "TLBHashmapType") {
let keyForLoad: Expression = dictKeyExpr(fieldType.key, ctx);
let keyForStore: Expression = dictKeyExpr(fieldType.key, ctx, ctx.typeName);
let subExprInfo = this.handleType(
field,
fieldType.value,
false,
ctx,
slicePrefix,
argIndex
);

if (subExprInfo.typeParamExpr && subExprInfo.loadFunctionExpr && subExprInfo.storeFunctionExpr) {
result.loadExpr = dictLoadExpr(keyForLoad, subExprInfo.loadFunctionExpr, currentSlice)
let valueStore = dictValueStore(subExprInfo.typeParamExpr, subExprInfo.storeFunctionExpr)
result.typeParamExpr = dictTypeParamExpr(fieldType, subExprInfo.typeParamExpr)
result.storeStmtInside = dictStoreStmt(currentCell, storeParametersInside, keyForStore, valueStore)
result.storeStmtOutside = dictStoreStmt(currentCell, storeParametersOutside, keyForStore, valueStore)
}
} else if (fieldType.kind == "TLBNamedType" && fieldType.arguments.length) {
let typeName = fieldType.name;

Expand Down Expand Up @@ -1021,3 +1038,4 @@ export class TypescriptGenerator implements CodeGenerator {
return result;
}
}

8 changes: 8 additions & 0 deletions src/generators/typescript/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
TLBCode,
TLBConstructor,
TLBMathExpr,
TLBMathExprType,
TLBNumberExpr,
TLBNumberType,
TLBParameter,
Expand Down Expand Up @@ -215,6 +216,13 @@ export function getCondition(conditions: Array<BinaryExpression>): Expression {
}
}

export function isBigIntExpr(fieldType: TLBMathExprType) {
if (fieldType.expr instanceof TLBNumberExpr && fieldType.expr.n <= 64) {
return false;
}
return true;
}

export function isBigInt(fieldType: TLBNumberType) {
if (fieldType.bits instanceof TLBNumberExpr) {
if (fieldType.bits.n <= 64) {
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export function generate(tree: Program, input: string) {
codeGenerator.addTonCoreClassUsage("Cell");
codeGenerator.addTonCoreClassUsage("Address");
codeGenerator.addTonCoreClassUsage("ExternalAddress");
codeGenerator.addTonCoreClassUsage("Dictionary")
codeGenerator.addTonCoreClassUsage("DictionaryValue")

codeGenerator.addBitLenFunction();

Expand Down
Loading

0 comments on commit 6169930

Please sign in to comment.