Skip to content

Commit

Permalink
Updated interfaces (getTLBCode, getTLBCodeByAST, add initial expressi…
Browse files Browse the repository at this point in the history
…on to TLBExprMathType and other utils), release v1.1.0
  • Loading branch information
PolyProgrammist committed Apr 1, 2024
1 parent e28897d commit 70260e1
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 10 deletions.
4 changes: 3 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { generateCode } from "./src/main";
export { generateCode, generateCodeByAST, generateCodeWithGenerator } from "./src/main";
export * from "./src/ast";
export { generateCode, getTLBCode, getTLBCodeByAST, generateCodeByAST, generateCodeWithGenerator } from "./src/main";
export { isBigInt as isBigIntForJson, isBigIntExpr as isBigIntExprForJson } from "./src/generators/typescript/utils";
export { CodeGenerator } from "./src/generators/generator";
export { TypescriptGenerator } from "./src/generators/typescript/generator";
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ton-community/tlb-codegen",
"version": "1.0.1",
"version": "1.1.0",
"description": "",
"main": "build/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type TLBNamedType = {
export type TLBMathExprType = {
kind: "TLBExprMathType";
expr: TLBMathExpr;
initialExpr: TLBMathExpr;
};

export type TLBBoolType = {
Expand Down
4 changes: 3 additions & 1 deletion src/astbuilder/handle_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,14 @@ export function getType(
return {
kind: "TLBExprMathType",
expr: getCalculatedExpression(new TLBVarExpr(expr.name), constructor),
initialExpr: new TLBVarExpr(expr.name),
};
} else {
return { kind: "TLBNamedType", name: expr.name, arguments: [] };
}
}
} else if (expr instanceof NumberExpr) {
return { kind: "TLBExprMathType", expr: new TLBNumberExpr(expr.num) };
return { kind: "TLBExprMathType", expr: new TLBNumberExpr(expr.num), initialExpr: new TLBNumberExpr(expr.num) };
} else if (expr instanceof NegateExpr && expr.expr instanceof NameExpr) {
return { kind: "TLBNegatedType", variableName: expr.expr.name };
} else if (expr instanceof CellRefExpr) {
Expand All @@ -309,6 +310,7 @@ export function getType(
return {
kind: "TLBExprMathType",
expr: getCalculatedExpression(convertToMathExpr(expr), constructor),
initialExpr: convertToMathExpr(expr),
};
}
} else if (expr instanceof CondExpr) {
Expand Down
22 changes: 18 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,29 @@ import { CodeGenerator, CommonGenDeclaration } from "./generators/generator";
import { TypescriptGenerator } from "./generators/typescript/generator";
import fs from 'fs'


export function generateCodeByAST(tree: Program, input: string, getGenerator: (tlbCode: TLBCode) => CodeGenerator) {
export function getTLBCodeByAST(tree: Program, input: string) {
let oldTlbCode: TLBCodeBuild = { types: new Map<string, TLBTypeBuild>() };

let splittedInput = input.split("\n");

fillConstructors(tree.declarations, oldTlbCode, splittedInput);
let tlbCode: TLBCode = convertCodeToReadonly(oldTlbCode);

return tlbCode;
}

export function getTLBCode(inputPath: string) {
const input = fs.readFileSync(
inputPath,
'utf-8',
)

const tree = ast(input)

return getTLBCodeByAST(tree, input);
}


export function generateCodeByAST(tree: Program, input: string, getGenerator: (tlbCode: TLBCode) => CodeGenerator) {
let tlbCode = getTLBCodeByAST(tree, input)
let codeGenerator: CodeGenerator = getGenerator(tlbCode);

codeGenerator.addTonCoreClassUsage("Builder");
Expand Down
Loading

0 comments on commit 70260e1

Please sign in to comment.