hello everyone, is there a compiler api example fo...
# compiler
d
hello everyone, is there a compiler api example for kotlin anywhere? im working on an integration tool that allows you to write classes/scripts in groovy and wanted to add kotlin to it withou resorting to the
kotlinc
command.
g
If I understand you correctly, you want compile Kotlin classes on runtime, right? Looks that you need Kotlin Scripting: https://github.com/Kotlin/KEEP/blob/master/proposals/scripting-support.md
Tho it’s a keep, but many parts of it are implemented
d
i actually already have the scripting part mostly working. So this can be used to write full blown kotlin classes too?
g
It also supports JSR-223, so you are able to easily integrate it to any JVM program
I see what you mean
d
i actually tried JSR223 but bypassed it and went directly to the scripting host api
g
hmm, It’s definetely should be possible
but honestly I cannot point you our on particular API
d
is there something i can add to the compiler configuration so i can see at least what classes/class names will be generated as a result of the script?
g
Not sure, probably you could ask in #scripting
d
ok thanks
g
but I think in your case use compiler API directly is the way to do that
there is kotlin-compiler-embeddablen artifact
d
any examples on how to use it? also is this api still experimental?
g
I don’rt think that embeddable compiler is experimental, it used by Gradle and Maven plugins to compile Kotlin\
I think you could check implementation of kotlin-compiler-server Old one: https://github.com/JetBrains/kotlin-web-demo New One https://github.com/AlexanderPrendota/kotlin-compiler-server
d
awesome!
g
tho it also executes code, but it also should cache it and in case of Json you actually receive compiled version, so it probably contains all samples which needed to compile Kotlin sources and access results
It compiles script and returns result of compilation, maybe you are able to just get actual class file from it
d
i'll have a look into it, thanks
i just tried with a very simple class but it's created 3 classes with the same name
g
Makes sense for me
Lamp$Lamp is a nested class
this is how Java (and Kotlin) compiles nested classes
You may get even more class files because of lambdas (because they are also separate classes)
d
my source has no inner classes though
Copy code
class Lamp {

    // property (data member)
    private var isOn: Boolean = false

    // member function
    fun turnOn() {
        isOn = true
    }

    // member function
    fun turnOff() {
        isOn = false
    }

    fun displayLightStatus(lamp: String) {
        if (isOn == true)
            println("\$lamp lamp is on.")
        else
            println("\$lamp lamp is off.")
    }
}
g
I see, it’s some another issue, maybe it how scripting works
not really sure
d
i was hoping the scripting api would be enough to just compile some simple source files cleanly. is the the scripting api what web demo uses?
g
Check the sources, not sure how it implemented
d
ok thanks