Created
March 5, 2022 07:18
-
-
Save voldikss/feed2b8a69259cd630e019ed5bcc9c3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as ts from 'typescript' | |
function makeFactorialFunction() { | |
const functionName = ts.factory.createIdentifier('factorial') | |
const paramName = ts.factory.createIdentifier('n') | |
const parameter = ts.factory.createParameterDeclaration( | |
undefined, | |
undefined, | |
undefined, | |
paramName, | |
undefined, | |
ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword) | |
) | |
const condition = ts.factory.createBinaryExpression( | |
paramName, | |
ts.SyntaxKind.LessThanEqualsToken, | |
ts.factory.createNumericLiteral(1) | |
) | |
const ifBody = ts.factory.createBlock([ | |
ts.factory.createReturnStatement(ts.factory.createNumericLiteral(1)), | |
]) | |
const decrementedArg = ts.factory.createBinaryExpression( | |
paramName, | |
ts.SyntaxKind.MinusToken, | |
ts.factory.createNumericLiteral(1) | |
) | |
const decrementedFunction = ts.factory.createCallExpression( | |
functionName, | |
undefined, | |
[decrementedArg] | |
) | |
const recurse = ts.factory.createBinaryExpression( | |
paramName, | |
ts.SyntaxKind.AsteriskToken, | |
decrementedFunction | |
) | |
const statements = [ | |
ts.factory.createIfStatement(condition, ifBody), | |
ts.factory.createReturnStatement(recurse), | |
] | |
const func = ts.factory.createFunctionDeclaration( | |
undefined, | |
[ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], | |
undefined, | |
functionName, | |
undefined, | |
[parameter], | |
ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword), | |
ts.factory.createBlock(statements, true) | |
) | |
const funcall = ts.factory.createVariableStatement( | |
undefined, | |
ts.factory.createVariableDeclarationList( | |
[ | |
ts.factory.createVariableDeclaration( | |
ts.factory.createIdentifier('res'), | |
undefined, | |
undefined, | |
ts.factory.createCallExpression(functionName, undefined, [ | |
ts.factory.createNumericLiteral(10), | |
]) | |
), | |
], | |
ts.NodeFlags.Const | |
) | |
) | |
ts.NodeFlags.Const | |
const print = ts.factory.createExpressionStatement( | |
ts.factory.createCallExpression( | |
ts.factory.createIdentifier('console.log'), | |
undefined, | |
[ts.factory.createIdentifier('res')] | |
) | |
) | |
return ts.factory.createSourceFile( | |
[func, funcall, print], | |
ts.factory.createToken(ts.SyntaxKind.EndOfFileToken), | |
ts.NodeFlags.BlockScoped | |
) | |
} | |
const resultFile = ts.createSourceFile('factorial.ts', '', ts.ScriptTarget.Latest) | |
const printer = ts.createPrinter() | |
const result = printer.printNode( | |
ts.EmitHint.Unspecified, | |
makeFactorialFunction(), | |
resultFile | |
) | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment