TLDR: Is there a *.kt parser written in Kotlin that I can use?
Long version:
I have some numerical algorithms written in a restricted subset of Kotlin.
I want to automatically convert these *.kt files to *.c (to compile to wasm) and to *.cuda (the restricted subset of kotlin I am using makes the parallelism explicit).
My main problem right now is – how do I parse *.kt files (in Kotlin) in the first place ?
====
Question: what kotlin lirary will help me do *.kt → abstract syntax tree
====
Please ignore the following off-topic discussions:
Q: Kotlin is not parallel. You can not output parallel cuda.
A: I’m using a restricted subset, which expresses individual threads.
Q: Kotlin is not low level, You can not output C.
A: Not a problem.
Q: Why not write in C/cuda directly ?
A: 1. I want to have three version: JVM, js (via Cwasm), Cuda
2. I prefer to just write in a restricted subset of kotlin.i
I don’t think there is a kotlin parser outside of the compiler. So I’d start there. As far as I know it is based on intellij’s PSI system, but the parts specific to kotlin are part of the compiler.
I’ve been thinking about tackling a similar project. I am thinking a lightweight set of data classes that represent the AST (can’t find a good one, the compiler lib is too is heavy for me and the psi package is not very ergonomic to use). I could see a separate piece compiling and then transforming into this model. My use case is for a more powerful code writer (kotlinpoet has many problems).
Also note, Kotlin Native can compile to WASM (wasn’t one of your off-topic parts to avoid specifically, also not sure about cuda files, but if there is something that will translate llvm IR, KN can write llvm bitcode too).
@cretz: lol, good catch; problem is: from what I have read so far, Kotlin native is currently not focused on performance, and thus is not generating fast code
I can’t say because I’m not on the project, but I can say that anything targetting LLVM gets many optimizations included (if they enable them) and from my uses of KN, I have seen quality performance.
And now I realize I could have just used PsiFileFactory.createFileFromText. Also note, if you’re parsing a bunch of files, I am unsure if the env should be held across parsings.
It’s a PSI object. The PSI(Program structure interface) is basically intellij’s version of an AST with some additional features. As far as I know the KtFile is the top element representing a file of kotlin source code.
PSIElement may or may not be a Kotlin compiler internals
We can attach colors / attributes to PSIElement – it’s useful for syntax coloring, tool tips over keywords, etc …
PSIElement applies to languages besides Kotlin
Assuming most of the above is true – why is it, for the task of parsing Kotlin files, we want PSIElement (IDE specific class) instead of some Kotlin-compiler specific class?
[ One possible solution is that Kotlin Compiler also happens to use PSIElement as internal AST representation – but I have no way how to verify that, and it would be very surprising to me]
Not surprising to me. I think that is the case and I think it’s the one and only true parser. (well, the compiler has an IR and there are some other details, but yes, that PSI stuff is the only place that has the proper AST that I’m aware of.) I’m working with the stuff in the org.jetbrains.kotlin.psi package now and it’s not that bad at all, just a lot of helpers and utils to clutter it. I am transforming it into my own AST that will be easier to use if you want to wait a few days.
Ok, the code I put at GitHub - cretz/kastree: Simple Kotlin Source AST and Syntax Parsing, Editing, and Writing will let you parse a string of code into a clean AST. While there are some integration tests against the PSI corpus, I don’t have any tests on the validity just yet. It’s not quite a library yet (my needs require that I make a mutating Visitor and Writer), the AST and the PSI-to-AST converter are sound.
Looks great. I don’t really know why I would ever need this myself but you never know
Maybe just add a short readme and a license to your repository. That way it should be easier (well or leagal) to use for other people
Yup, I usually do (see my other repos), but I just hacked this out and posted here for that user. I have now added the license and will add a README when the lib is complete.