Skip to content

Commit

Permalink
add hashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Jan 13, 2024
1 parent 109c651 commit 47b8496
Show file tree
Hide file tree
Showing 6 changed files with 444 additions and 226 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: TLBFieldType,
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
7 changes: 7 additions & 0 deletions src/astbuilder/handle_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ 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)
return { kind: "TLBHashmapType", key: key, value: value };
} else if (
expr.name == "VarUInteger" &&
(expr.args[0] instanceof MathExpr ||
Expand Down
46 changes: 46 additions & 0 deletions src/generators/typescript/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,52 @@ export class TypescriptGenerator implements CodeGenerator {
} else {
throw new Error("Address has type other than ['Internal', 'External', 'Any']")
}
} else if (fieldType.kind == "TLBHashmapType") {
let key: Expression
if (fieldType.key.kind == 'TLBExprMathType') {
key = tFunctionCall(tMemberExpression(id('Dictionary.Keys'), id('Uint')), [convertToAST(fieldType.key.expr, ctx.constructor)]);
} else {
throw new Error('')
}
let subExprInfo = this.handleType(
field,
fieldType.value,
false,
ctx,
slicePrefix,
argIndex
);
let functionId = id('createTheFunction' + (Math.random() + 1).toString(36).substring(7))
let value = tFunctionCall(functionId, [])
result.loadExpr = tFunctionCall(tMemberExpression(id('Dictionary'), id('load')), [key, value, id(currentSlice)])
if (subExprInfo.typeParamExpr && subExprInfo.loadFunctionExpr && subExprInfo.storeStmtInside) {
this.jsCodeFunctionsDeclarations.push(
tFunctionDeclaration(
functionId,
tTypeParametersExpression([]),
tTypeWithParameters(
id('DictionaryValue'),
tTypeParametersExpression([
subExprInfo.typeParamExpr
])
)
, [], [tReturnStatement(tObjectExpression([
tObjectProperty(id('serialize'),
tArrowFunctionExpression([tTypedIdentifier(id('arg'), subExprInfo.typeParamExpr), tTypedIdentifier(id('builder'), id('Builder'))], [subExprInfo.storeStmtInside])
),
tObjectProperty(id('parse'),
subExprInfo.loadFunctionExpr
)
]))]
))
result.typeParamExpr = tTypeWithParameters(id('Dictionary'), tTypeParametersExpression([id('number'), subExprInfo.typeParamExpr]))
}
storeParametersInside = storeParametersInside.concat([key, value])
storeParametersOutside = storeParametersOutside.concat([key, value])
result.storeStmtInside = tExpressionStatement(tFunctionCall(tMemberExpression(id(currentCell), id('storeDict')), storeParametersInside))
result.storeStmtOutside = tExpressionStatement(tFunctionCall(tMemberExpression(id(currentCell), id('storeDict')), storeParametersOutside))
if (subExprInfo.loadExpr)
console.log(toCode(subExprInfo.loadExpr).code)
} else if (fieldType.kind == "TLBExprMathType") {
result.loadExpr = convertToAST(fieldType.expr, ctx.constructor);
result.storeStmtOutside = tExpressionStatement(result.loadExpr);
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 47b8496

Please sign in to comment.