Skip to content

Commit

Permalink
create bool base type
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Nov 2, 2024
1 parent ed262d1 commit 954f1fb
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 352 deletions.
5 changes: 0 additions & 5 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ export type TLBMathExprType = {
initialExpr: TLBMathExpr;
};

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

export type TLBCoinsType = {
kind: "TLBCoinsType";
}
Expand Down Expand Up @@ -141,7 +137,6 @@ export type TLBFieldType =
| TLBNumberType
| TLBBitsType
| TLBNamedType
| TLBBoolType
| TLBCoinsType
| TLBAddressType
| TLBHashmapType
Expand Down
4 changes: 2 additions & 2 deletions src/astbuilder/handle_field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function getField(
let result: TLBField = {
name: "",
anonymous: true,
fieldType: { kind: "TLBBoolType" },
fieldType: { kind: "TLBNamedType", name: "Bool", arguments: [] },
subFields: [],
};
let currentFieldIndex = 0;
Expand Down Expand Up @@ -80,7 +80,7 @@ function getField(
let result: TLBField = {
name: fieldName,
anonymous: true,
fieldType: { kind: "TLBBoolType" },
fieldType: { kind: "TLBNamedType", name: "Bool", arguments: [] },
subFields: [subField],
};
return result;
Expand Down
2 changes: 0 additions & 2 deletions src/astbuilder/handle_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,6 @@ export function getType(
};
} else if ((theNum = splitForTypeValue(expr.name, "bits")) != undefined) {
return { kind: "TLBBitsType", bits: new TLBNumberExpr(theNum) };
} else if (expr.name == "Bool") {
return { kind: "TLBBoolType" };
} else if (expr.name == "MsgAddressInt") {
return { kind: "TLBAddressType", addrType: "Internal" };
} else if (expr.name == "MsgAddressExt") {
Expand Down
1 change: 1 addition & 0 deletions src/generators/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface CodeGenerator {

addTonCoreClassUsage(name: string): void
addBitLenFunction(): void
addEmbeddedTypes(): void
addTlbType(tlbType: TLBType): void
toCode(node: TheNode, code: CodeBuilder): CodeBuilder
}
Expand Down
3 changes: 2 additions & 1 deletion 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, tStructExpression, 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, tIdentifier, tIfStatement, tMemberExpression, tMultiStatement, tNumericLiteral, tObjectExpression, tObjectProperty, tReturnStatement, tStringLiteral, tStructDeclaration, 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 @@ -40,6 +40,7 @@ export function bitlenFunctionDecl(): GenDeclaration {
[tExpressionStatement(id("return n.toString(2).length"))]
);
}

export function typedSlice() {
return [tTypedIdentifier(id("slice"), id("Slice"))];
}
Expand Down
36 changes: 28 additions & 8 deletions src/generators/typescript/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {
TypeParametersExpression,
TypedIdentifier,
id,
tCodeAsIs,
tComment,
tExpressionStatement,
tFunctionCall,
Expand Down Expand Up @@ -237,6 +238,33 @@ export class TypescriptGenerator implements CodeGenerator {
addBitLenFunction() {
this.jsCodeDeclarations.push(bitlenFunctionDecl());
}

addEmbeddedTypes() {
this.jsCodeDeclarations.push(tCodeAsIs(`export interface Bool {
readonly kind: 'Bool';
readonly value: boolean;
}
export function loadBool(slice: Slice): Bool {
if (slice.remainingBits >= 1) {
let value = slice.loadUint(1);
return {
kind: 'Bool',
value: value == 1
}
}
throw new Error('Expected one of "BoolFalse" in loading "BoolFalse", but data does not satisfy any constructor');
}
export function storeBool(bool: Bool): (builder: Builder) => void {
return ((builder: Builder) => {
builder.storeUint(bool.value ? 1: 0, 1);
})
}`))
}

addTlbType(tlbType: TLBType): void {
let typeName = findNotReservedName(
firstLower(tlbType.name),
Expand Down Expand Up @@ -714,14 +742,6 @@ export class TypescriptGenerator implements CodeGenerator {
tMemberExpression(storeParametersInside[0], id("beginParse")),
[id("true")]
)
} else if (fieldType.kind == "TLBBoolType") {
exprForParam = {
argLoadExpr: undefined,
argStoreExpr: undefined,
paramType: "boolean",
fieldLoadSuffix: "Boolean",
fieldStoreSuffix: "Bit",
};
} else if (fieldType.kind == "TLBCoinsType") {
exprForParam = {
argLoadExpr: undefined,
Expand Down
16 changes: 15 additions & 1 deletion src/generators/typescript/tsgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ export interface MultiStatement extends ASTNode {
statements: Array<Statement>;
}

export interface CodeAsIs extends ASTNode {
type: "CodeAsIs";
code: string;
}

export type TypeExpression =
| Identifier
| TypeWithParameters
Expand Down Expand Up @@ -210,7 +215,8 @@ export type GenDeclaration =
| StructDeclaration
| UnionTypeDeclaration
| FunctionDeclaration
| Comment;
| Comment
| CodeAsIs;

export type TheNode =
| Identifier
Expand Down Expand Up @@ -466,6 +472,10 @@ export function tTernaryExpression(
};
}

export function tCodeAsIs(code: string): CodeAsIs {
return { type: "CodeAsIs", code: code };
}

function toCodeArray(
nodeArray: Array<TheNode>,
code: CodeBuilder,
Expand Down Expand Up @@ -724,6 +734,10 @@ export function toCode(
}
}

if (node.type == "CodeAsIs") {
code.add(node.code, false);
}

return code;
}

Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function generateCodeByAST(tree: Program, input: string, getGenerator: (t
codeGenerator.addTonCoreClassUsage("DictionaryValue")

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

let jsCodeDeclarations: CommonGenDeclaration[] = [];
codeGenerator.jsCodeDeclarations.forEach((declaration) => {
Expand Down
Loading

0 comments on commit 954f1fb

Please sign in to comment.