Hey folks, Given that Kotlin/JS compilation follo...
# compiler
t
Hey folks, Given that Kotlin/JS compilation follows the pipeline:
Kotlin Code → Kotlin IR → Kotlin/JS Compiler → JavaScript
does this mean I could theoretically create a Kotlin/C++ compiler (or is it called transpiler?) by building a new backend that transforms Kotlin IR into C++ code, similar to how the existing Kotlin/JS backend transforms IR into JavaScript? 🤔 thanks in advance 🙌
🍀 1
just to be on the same page: am sure the effort will be very high due to Kotlin’s type system, coroutines, null safety, and object model, etc… but my question is more around tech-feasibility
y
A similar effort has been worked on for #C01EY9ZF9B5
👀 1
d
Yes, technically it's possible.
👍 1
t
tiny baby step… (kotlin to c++) 🙂
👀 2
interesting 1
y
Is this based on the Python undertaking? I'm assuming not cuz IIRC The Python backend forked the whole compiler, while it looks like you're making a plugin (which is a pretty neat idea to get around it). Great work!
thank you color 1
t
i haven’t looked at the python part.. am using the “compiler-embeddable” to get access to the “org.jetbrains.kotlin.backend” classes 🙌
K 1
t
I have to ask. Why this vs just compiling with the kt/native tooling?
t
@trevjones my goal is to write programs for microcontrollers (like ESP32/ESP8266) in kotlin… (because unfortunately kotlin/native can’t directly run on them due to memory limitations)
so.. the north star is .. me writing kotlin code like this and it gets transformed to something like this (right side)
K 2
t
that makes perfect sense actually. I hope you succeed. I love the idea of doing a pure kotlin implementation of the klipper 3d print firmware just because it would be epic
❤️ 2
(but also because I despise python, unserious ass language)
😄 2
1
would have to be either this or some insanely good minification reduction of the code when targeting kt/n
t
yeah - but i don’t think the optimised kt/n is going to happen any time soon.. there have been discussions from very early days (when kotlin native introduced) but dropped due to the stdlib size not fitting in tiny MCUs like ESPs… i guess
y
Something I've done before is use Kotlin as a metaprogramming language to generate other languages, C++ in this case. It might work reasonably well and give you the right amount of control.
t
yeah - that’s what i hope 😄 i still don’t know where this project is heading.. am just exploring at this point and learning the APIs available..
y
What I mean is developing a DSL in Kotlin that prints out C++ sources lol. Or more likely an AST type of thing
t
ehh transpile via a compiler plugin or backend written in kotlin meets the "meta" in my mind
y
I made this a while back, but it's very underfeatured. It generates Arm code, and has really nice spiffy syntax
👍 1
t
you should journal this journey either written or in voice. this is some big brain shit worth documenting if you can pull it off
1
t
noted 👍
What I mean is developing a DSL…AST…
yes, the idea will work with any parsing approach..
now, once the C++ code is generated.. the plan is to use the platformIo or arduino CLI for building/uploading/flashing
this whole op will be behind a gradle task - probably something
./gradlew run
(just thinking out loud 😄 ) …
(again.. i may hit a wall tomorrow and endup doing something else.. but let’s see) .. i’ll keep this thread updated 🙌
@Youssef Shoaib [MOD] u aware of any documentation on kotlin compiler plugin construct.. mainly the IR APIs ?
UPDATE: end-to-end development flow K POC appears successful….
runKorduino
does 3 things : 1. Transpile the Kotlin code to C++ code 2. Then build and flash the code to the microcontroller 3. Finally, opens the serial monitor 🙌🏼
K 13
🙀 5
note: currently, korduino supports only a few arduino functions and println. next, planning to work on conditional statements and loops 🙌
UPDATE: i’ve no idea what am doing 😄 but… this is fun
UPDATE: primitive data types and math operators are added now… its definitely a long road, but enjoying building every construct 🌸
❤️ 1
i
Maybe technically it's possible, but I wouldn't recommend doing that for anything mature and big. The languages have different semantics and it's unclear how to express some things in Kotlin. Also, the sequence of intermediate languages is too long, the each next step increases the probability that something will break.
t
i totally agree. at this point, i’m just doing it for fun. enjoying the journey and learning new things. discovering each construct at IR level like gives me an “aha” moment and a bit of a kick 😄 (for some reason… hard to explain 😄 ) this is currently a fun and long-term exercise to satisfy my curiosity needs 😄
👍 1
p
cool idea! co-author of kotlin-python here (from #C01EY9ZF9B5), happy to see a different take on the general idea of producing something out of IR. This project is a good inspiration for me to take another look at producing Python from Kotlin, and I know I won't continue the path of forking Kotlin, but rather creating a compiler plugin or some kind of external IR processor that produces Python
🔥 1
blob ty sign 1