Skip to content

Commit

Permalink
finalize binary
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Jan 16, 2024
1 parent d636070 commit 1c76154
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion generate_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { generateCode } from './src/main'

function genCodeForTest(name: string) {
const fixturesDir = path.resolve(__dirname, 'test')
generateCode(path.resolve(fixturesDir, 'tlb', name + '.tlb'), 'test/generated_files/generated_' + name + '.ts')
generateCode(path.resolve(fixturesDir, 'tlb', name + '.tlb'), 'test/generated_files/generated_' + name + '.ts', 'typescript')
}

genCodeForTest('block')
Expand Down
21 changes: 17 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,34 @@ import { generateCode } from './src/main';

const cli = meow(`
Usage
$ foo <tlbpath>
$ tlbgen <tlbpath>
Options
--output, -o Output file, defult = tlbpath + ".tst"
--output, -o Output file, defult = tlbpath + ".ts"
--language, -l Programming language result. Supported languages: ["typescript"]. Default: "typescript"
`, {
flags: {
output: {
type: 'string',
shortFlag: 'o'
alias: 'o'
},
language: {
type: 'string',
alias: 'l'
}
}
});


let input = cli.input.at(0)
if (input) {
generateCode(input, input + ".ts");
let output = input + '.ts'
if (cli.flags.output) {
output = cli.flags.output;
}
let language = 'typescript'
if (cli.flags.language) {
language = cli.flags.language
}
generateCode(input, output, language);
}
14 changes: 10 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import { ast } from '@igorivaniuk/tlb-parser'
import fs from 'fs'


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

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

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

let codeGenerator: CodeGenerator = new TypescriptGenerator(tlbCode);
let codeGenerator: CodeGenerator = getGenerator(tlbCode);

codeGenerator.addTonCoreClassUsage("Builder");
codeGenerator.addTonCoreClassUsage("Slice");
Expand Down Expand Up @@ -63,13 +63,19 @@ export function generate(tree: Program, input: string) {
return generatedCode;
}

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

const tree = ast(input)

fs.writeFile(outputPath, generate(tree, input), () => { });
fs.writeFile(outputPath, generate(tree, input, (tlbCode: TLBCode) => {
if (resultLanguage == 'typescript') {
return new TypescriptGenerator(tlbCode)
} else {
throw new Error(`Result language ${resultLanguage} is not supported`)
}
}), () => { });
}

0 comments on commit 1c76154

Please sign in to comment.