Skip to content

Commit

Permalink
HashmapAugE
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Jan 15, 2024
1 parent 6169930 commit 4bf7e50
Show file tree
Hide file tree
Showing 10 changed files with 521 additions and 144 deletions.
5 changes: 3 additions & 2 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ export type TLBVarIntegerType = {

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

export type TLBCellType = {
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 @@ -433,6 +433,7 @@ function checkAndRemovePrimitives(
typesToDelete.set("VarUInteger", ["11d56c2e"])
typesToDelete.set("VarInteger", ["d466ed5"])
typesToDelete.set("HashmapE", ["32bae5cb", "28fa3979"])
typesToDelete.set("HashmapAugE", ["36820dce", "5f71ac75"])

typesToDelete.forEach((opCodesExpected: string[], typeName: string) => {
let typeItems = typeDeclarations.get(typeName);
Expand Down
11 changes: 11 additions & 0 deletions src/astbuilder/handle_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ export function getType(
throw new Error('Hashmap key should be number')
}
return { kind: "TLBHashmapType", key: key, value: value };
} else if (expr.name == "HashmapAugE") {
if (expr.args.length != 3) {
throw new Error('Not enough arguments for HashmapAugE')
}
let key = getType(expr.args[0], constructor, fieldTypeName)
let value = getType(expr.args[1], constructor, fieldTypeName)
let extra = getType(expr.args[2], constructor, fieldTypeName)
if (key.kind != 'TLBExprMathType') {
throw new Error('Hashmap key should be number')
}
return { kind: "TLBHashmapType", key: key, value: value, extra: extra };
} else if (
expr.name == "VarUInteger" &&
(expr.args[0] instanceof MathExpr ||
Expand Down
21 changes: 18 additions & 3 deletions src/generators/typescript/complex_expr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TLBCode, TLBConstructorTag, TLBField, TLBHashmapType, TLBMathExprType } from "../../ast";
import { findNotReservedName, firstLower, getCurrentSlice } from "../../utils";
import { ConstructorContext } from "./generator";
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 { 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";
import { ExprForParam, convertToAST, getNegationDerivationFunctionBody, isBigIntExpr } from "./utils";

export function tEqualExpression(left: Expression, right: Expression) {
Expand Down Expand Up @@ -336,10 +336,14 @@ export function dictStoreStmt(currentCell: string, storeParametersInside: Expres
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) {
export function dictValueStore(typeParamExpr: TypeExpression, storeFunctionExpr: Expression, extraStoreFunctionExpr?: Expression) {
let subStatements = [tExpressionStatement(tFunctionCall(tFunctionCall(storeFunctionExpr, [id(extraStoreFunctionExpr? 'arg.value' : 'arg')]), [id('builder')]))]
if (extraStoreFunctionExpr) {
subStatements = [tExpressionStatement(tFunctionCall(tFunctionCall(extraStoreFunctionExpr, [id('arg.extra')]), [id('builder')]))].concat(subStatements)
}
return tObjectExpression([
tObjectProperty(id('serialize'),
tArrowFunctionExpression([tTypedIdentifier(id('arg'), typeParamExpr), tTypedIdentifier(id('builder'), id('Builder'))], [tExpressionStatement(tFunctionCall(tFunctionCall(storeFunctionExpr, [id('arg')]), [id('builder')]))])
tArrowFunctionExpression([tTypedIdentifier(id('arg'), typeParamExpr), tTypedIdentifier(id('builder'), id('Builder'))], subStatements)
),
tObjectProperty(id('parse'),
id("() => { throw new Error('Not implemented') }")
Expand Down Expand Up @@ -367,4 +371,15 @@ export function dictKeyExpr(keyType: TLBMathExprType, ctx: ConstructorContext, o
param = convertToAST(keyType.expr, ctx.constructor);
}
return tFunctionCall(tMemberExpression(id('Dictionary.Keys'), (isBigIntExpr(keyType) ? id('BigUint') : id('Uint'))), [param]);
}export function dictAugParse(extraLoadExpr: Expression, loadExpr: Expression): Expression {
return tArrowFunctionExpression([tTypedIdentifier(id('slice'), id('Slice'))], [
tReturnStatement(tObjectExpression([
tObjectProperty(id('extra'), extraLoadExpr),
tObjectProperty(id('value'), loadExpr),
]))
]);
}
export function dictAugTypeExpr(typeExpr: TypeExpression, extraTypeExpr: TypeExpression): TypeExpression {
return tStructExpression([tTypedIdentifier(id('value'), typeExpr), tTypedIdentifier(id('extra'), extraTypeExpr)]);
}

39 changes: 24 additions & 15 deletions src/generators/typescript/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Identifier } from "typescript";
import {
TLBCode,
TLBConstructor,
Expand All @@ -25,6 +24,12 @@ import {
checkKindStmt,
checkTagExpr,
coverFuncCall,
dictAugParse,
dictAugTypeExpr,
dictKeyExpr, dictLoadExpr,
dictStoreStmt,
dictTypeParamExpr,
dictValueStore,
inSeparateRef,
loadExprForParam,
loadFromNewSlice,
Expand Down Expand Up @@ -70,7 +75,6 @@ import {
tIfStatement,
tImportDeclaration,
tMemberExpression,
tNumericLiteral,
tObjectExpression,
tObjectProperty,
tReturnStatement,
Expand All @@ -82,7 +86,7 @@ import {
tTypedIdentifier,
tUnionTypeDeclaration,
tUnionTypeExpression,
toCode,
toCode
} from "./tsgen";
import {
ExprForParam,
Expand All @@ -94,7 +98,6 @@ import {
getTypeParametersExpression,
isBigInt,
} from "./utils";
import { dictKeyExpr, dictLoadExpr, dictValueStore, dictTypeParamExpr, dictStoreStmt } from "./complex_expr";

/*
Expand Down Expand Up @@ -903,18 +906,25 @@ export class TypescriptGenerator implements CodeGenerator {
} 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
);
let subExprInfo = this.handleType(field, fieldType.value, fieldType.extra != undefined, ctx, slicePrefix, argIndex);


if (subExprInfo.typeParamExpr && subExprInfo.loadFunctionExpr && subExprInfo.storeFunctionExpr) {
result.loadExpr = dictLoadExpr(keyForLoad, subExprInfo.loadFunctionExpr, currentSlice)
let valueStore = dictValueStore(subExprInfo.typeParamExpr, subExprInfo.storeFunctionExpr)
let valueStore: Expression;
if (fieldType.extra && subExprInfo.loadExpr) {
let extraInfo = this.handleType(field, fieldType.extra, true, ctx, slicePrefix, argIndex);
if (extraInfo.typeParamExpr) {
subExprInfo.typeParamExpr = dictAugTypeExpr(subExprInfo.typeParamExpr, extraInfo.typeParamExpr)
}
valueStore = dictValueStore(subExprInfo.typeParamExpr, subExprInfo.storeFunctionExpr, extraInfo.storeFunctionExpr)

if (extraInfo.loadExpr) {
result.loadExpr = dictLoadExpr(keyForLoad, dictAugParse(extraInfo.loadExpr, subExprInfo.loadExpr), currentSlice)
}
} else {
valueStore = dictValueStore(subExprInfo.typeParamExpr, subExprInfo.storeFunctionExpr)
result.loadExpr = dictLoadExpr(keyForLoad, subExprInfo.loadFunctionExpr, currentSlice)
}
result.typeParamExpr = dictTypeParamExpr(fieldType, subExprInfo.typeParamExpr)
result.storeStmtInside = dictStoreStmt(currentCell, storeParametersInside, keyForStore, valueStore)
result.storeStmtOutside = dictStoreStmt(currentCell, storeParametersOutside, keyForStore, valueStore)
Expand Down Expand Up @@ -1038,4 +1048,3 @@ export class TypescriptGenerator implements CodeGenerator {
return result;
}
}

22 changes: 21 additions & 1 deletion src/generators/typescript/tsgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export interface ObjectExpression extends ASTNode {
objectValues: Array<ObjectProperty>;
}

export interface StructExpression extends ASTNode {
type: "StructExpression";
fields: Array<TypedIdentifier>;
}

export interface FunctionCall extends ASTNode {
type: "FunctionCall";
functionId: Expression;
Expand Down Expand Up @@ -176,7 +181,8 @@ export type TypeExpression =
| Identifier
| TypeWithParameters
| ArrowFunctionType
| UnionTypeExpression;
| UnionTypeExpression
| StructExpression;
export type Statement =
| ReturnStatement
| ExpressionStatement
Expand All @@ -189,6 +195,7 @@ export type Expression =
| TypeExpression
| Literal
| ObjectExpression
| StructExpression
| FunctionCall
| MemberExpression
| ArrowFunctionExpression
Expand Down Expand Up @@ -298,6 +305,13 @@ export function tObjectExpression(
return { type: "ObjectExpression", objectValues: objectValues };
}

export function tStructExpression(
fields: Array<TypedIdentifier>
): StructExpression {
return { type: "StructExpression", fields: fields };
}


export function tReturnStatement(returnValue: Expression): ReturnStatement {
return { type: "ReturnStatement", returnValue: returnValue };
}
Expand Down Expand Up @@ -574,6 +588,12 @@ export function toCode(
code.add("}", false);
}

if (node.type == "StructExpression") {
code.add("{", false);
toCodeArray(node.fields, code, ", ");
code.add("}", false);
}

if (node.type == "MultiStatement") {
node.statements.forEach((statement) => {
code.append(toCode(statement));
Expand Down
Loading

0 comments on commit 4bf7e50

Please sign in to comment.