Hacking the kotlin compiler

I’m tempted to hack the kotlin compiler to insert some additional operators (especially bitwise ones).

Would it be a lot of work?

Which are the interesting classes/files to modifiy?

How are operator overloading implemented? Do they get generated during the build?

Creating a compiler plugin beyond the scope of printing while compiling would take a bit of work. There are multiple steps involving the gradle build process that you have to abstract and create separately.

I haven’t successfully written one myself, but I have tried and it was definitely a lot of work as far as I got! I was trying to implement my own operator as well

As for operator overloading, they are implemented exactly as you define them out.

Say we have an Int wrapper class

class IntWrapper(val wrappedInt: Int) {
    operator fun plus(other: IntWrapper): IntWrapper = IntWrapper(wrappedInt + other.wrappedInt)
    override fun toString(): String = other.toString()
}

Now we write this

fun main() {
    val wrappedInt = IntWrapper(5)
    println(wrappedInt + IntWrapper(3))
}

The Kotlin compiler will translate the + to a .plus(other) call so you could essentially rewrite it as such

fun main() {
    val wrappedInt = IntWrapper(5)
    println(wrappedInt.plus(IntWrapper(3))
}