How to translate JavaScript or TypeScript to Dart? #124
-
Hi, Could we use this package to translate JS or TS files to Dart? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Sure, that is definitely possible: You would need a JS/TS parser (that you could write with this package) and a visitor that converts the parsed result to Dart. I've written similar tools in the past to convert some of my Smalltalk code to Dart (i.e. the initial versions of this package). Unrelated to this package: While I suspect this will be quite easy for control structures and functions (they are pretty much the same in Dart, TS and JS), it will become pretty challenging to fully automate when it comes to more complicated constructs like types, classes, interfaces, imports, etc. |
Beta Was this translation helpful? Give feedback.
-
FYI: Take a look in the package ApolloVM (that uses PetitParser): https://pub.dev/packages/apollovm ApolloVM can run and also can convert from one language to another. If you implement the JS grammar and also the TS grammar you will be able to make the conversion. One of the TODOs of the project is JS support, so we can help in the development and tests for JS. Here you can see a simple web example using it: https://apollovm.github.io/apollovm_web_example/www/ Note that not all Dart and Java syntax is implemented yet. |
Beta Was this translation helpful? Give feedback.
-
I am very new to this petit/peg/grammar parser. So sorry if any noob/obvious question. I guess there are some JS grammar out there for other parsers, but I have no idea if it could be used here, or give a start or nothing at all. |
Beta Was this translation helpful? Give feedback.
-
You just need to implement a directory like that for javascript: https://github.com/ApolloVM/apollovm_dart/tree/master/lib/src/languages/dart You can use the Java files or Dart files as a start point. |
Beta Was this translation helpful? Give feedback.
-
If you need any help just open an issue at ApolloVM GitHub (in case you want to implement it as another language of ApolloVM). |
Beta Was this translation helpful? Give feedback.
FYI:
Take a look in the package ApolloVM (that uses PetitParser):
https://pub.dev/packages/apollovm
ApolloVM can run and also can convert from one language to another.
If you implement the JS grammar and also the TS grammar you will be able to make the conversion.
One of the TODOs of the project is JS support, so we can help in the development and tests for JS.
Here you can see a simple web example using it:
https://apollovm.github.io/apollovm_web_example/www/
Note that not all Dart and Java syntax is implemented yet.