Improved live coding with orx-olive

I recently did a 30 minute session for Github Satellite on live coding in OPENRNDR. If you haven’t seen it, you can see a recording of it here. In the week running up to this session I decided to try out some new ideas for orx-olive.

It all started with an idea on our Slack’s #development channel: what if live coding versions of OPENRNDR programs were very similar (if not the same) to normal OPENRNDR programs. For those unaware of what the process of live coding in OPENRNDR looks like, you create a small host application and use extend(Olive()). After starting that application you will find a .kts file next to the application. While the application runs it will watch for changes to the Kotlin script file and attempts to reload a program function from the script.

This old mode works but it is a bit unwieldy, as there is no good way to transform an existing OPENRNDR program into a Kotlin script based program, and vise versa.

My idea to improve this situation was to introduce oliveProgram {} . This construction replaces program , installs orx-olive and serves as an anchor to indicate code that should be reloaded.

fun main() = application {
    oliveProgram {
        // -- reloadable code goes here

Because Kotlin (.kt) and Kotlin script (.kts) are largely written in the same language we can watch the .kt file for changes, extract the code that is inside the oliveProgram block and generate Kotlin script source for it. To make things work we also need to copy the import list to the script. The generated script is then handed to the mechanism that loads and applies the program function.

That leaves us with one unsolved problem: extracting code from Kotlin sources. In prior projects we wrote tools based on kastree, but that library is no longer maintained. An alternate library is kotlinx/ast, but I had a hard time building it and figuring out what it does. So I turned to the kotlin-spec repo that holds Antlr grammar files. Using the Antlr grammar it is a simple process of generating parsers and a parse tree visitor for the Kotlin language.

So that’s it! If you feel like trying out the new live coding interface you can now use it from the OPENRNDR template.