More document for typescript to C# #675
-
This is a very cool project and I would like to have more details on the description of typescript to C#, so that more guys can contribute to it. Maybe it would be better to separate this part into a standalone project |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
For the future please try to follow the contribution guidelines and not add questions like this as issues but rather as a discussion. The effort on my side adds up with everyone not following this procedure. To your question first maybe a bit history: alphaTab was always a "cross platform" and it was already written in many languages in the past and then transpiled to the platforms/languages I wanted to offer it. I started off with Haxe, then later there came C# as source with 3rd party transpilers followed by C# as source with a custom transpiler outputting Haxe. Now we arrived at TypeScript with a custom transpiler to C# (Kotlin is in progress). I was always a bit into this topic of compilers and transpilers (I guess it started on my Bachelor Thesis many years ago, where I made my first DSL with code generators). When I was still maintaining Phase, my custom C# to Haxe transpiler I came to the conclusion that it is not worth maintaining the transpiler part outside alphaTab. The main reasons were:
Coming to the technical bits how it works. There is not much documentation because it is more and underlying toolchain than an own big product. But there is also not so much special to the TS->C# transpiler if you are a bit familiar with the domain. We use the TypeScript Compiler API to setup a compilation context of the project. Then we simply go through all the source files, and translate the TypeScript AST to a C# AST. Then the AST is written to files. There are quite some utility methods around to do various type checks and lookup some further information about nodes, but it is usually all about: "take a look a the TypeScript AST node, and create a C# AST node that matches it". There is no complex logic like, optimizations, dead code elimination or similar. I use tools like https://ts-ast-viewer.com/# to look at the nodes a certain TS construct results in, then I define the C# AST parts that allow me to define how the code should look like in C#, and then I extend the transformer to do the translation and extend the printer to actually write the C# code. The main work is done in the AST transformer which traverses the parsed source files. The most tricky part is usually the handling of data types. The TS Compiler API is not always so convenient to use, and it can be a bit tricky to lookup the right data types of a variable, especially if they are not explicitly defined in the source. But once the foundation is set (which I would say it is for alphaTab), it is fairly easy to extend and adapt. This would be it about the high level details. If you are interested into some more specific bits I am happy to answer your questions 😉 |
Beta Was this translation helpful? Give feedback.
For the future please try to follow the contribution guidelines and not add questions like this as issues but rather as a discussion. The effort on my side adds up with everyone not following this procedure.
To your question first maybe a bit history: alphaTab was always a "cross platform" and it was already written in many languages in the past and then transpiled to the platforms/languages I wanted to offer it. I started off with Haxe, then later there came C# as source with 3rd party transpilers followed by C# as source with a custom transpiler outputting Haxe. Now we arrived at TypeScript with a custom transpiler to C# (Kotlin is in progress).
I was always a bit into this topic of com…