kotlite

Lightweight embedded interpreter executes type-safe scripts with a subset of standard libraries and third-party support. Facilitates script execution across multiple platforms within a host runtime environment.

JVMKotlin/NativeJS
GitHub stars67
Open issues0
LicenseMIT License
Creation dateabout 2 years ago

Last activityover 1 year ago
Latest release1.1.2 (over 1 year ago)

Kotlite & Kotlite Interpreter

A lite embedded Kotlin interpreter

Android JVM js iOS macOS watchOS tvOS Verification Test Status

Kotlite is an open-sourced type-safe programming language that has a rich subset of the script variant of the Kotlin programming language. It comes with standard libraries, which are a subset of Kotlin Multiplatform/Common standard libraries and a few third-party libraries.

Kotlite Interpreter is a lightweight Kotlin Multiplatform library to interpret and execute codes written in Kotlite, and bridge the host runtime environment and the embedded runtime environment.

Kotlite Interpreter Kotlite Interpreter

Kotlite Stdlib Kotlite Stdlib

Kotlite Library Preprocessor Kotlite Library Preprocessor

Release Notes

TL;DR

val env = ExecutionEnvironment().apply {
    install(AllStdLibModules())
}
val interpreter = KotliteInterpreter(
    filename = "UserScript",
    code = """
        fun fib(n: Int): Long {
            if (n > 100) throw Exception("n is too large")
            
            val dp = mutableMapOf<Int, Long>()
            fun f(i: Int): Long {
                if (i < 0) throw Exception("Invalid i: ${'$'}i")
                if (i <= 1) return i.toLong()
                if (i in dp) {
                    return dp[i]!!
                }
                return (f(i - 2) + f(i - 1)).also {
                    dp[i] = it
                }
            }
            
            return f(n)
        }
        val a = fib(19)
    """.trimIndent(),
    executionEnvironment = env,
)
interpreter.eval()
val symbolTable = interpreter.symbolTable()
val a = (symbolTable.findPropertyByDeclaredName("a") as LongValue).value // 4181L

The interpreter is well tested.

Well tested

Demo

Web - Kotlite Interpreter in browser

https://github.com/sunny-chung/kotlite/assets/14835950/464ddb68-5623-463e-89cb-5b34a4bebf41

https://github.com/sunny-chung/kotlite/assets/14835950/417143fa-6240-40cf-ba5f-8e40e97a4243

https://github.com/sunny-chung/kotlite/assets/14835950/f731c9a0-d941-4cd3-a23c-bfc2a60429f4

Documentation

Documentation site

JVMKotlin/NativeJS
GitHub stars67
Open issues0
LicenseMIT License
Creation dateabout 2 years ago

Last activityover 1 year ago
Latest release1.1.2 (over 1 year ago)

Kotlite & Kotlite Interpreter

A lite embedded Kotlin interpreter

Android JVM js iOS macOS watchOS tvOS Verification Test Status

Kotlite is an open-sourced type-safe programming language that has a rich subset of the script variant of the Kotlin programming language. It comes with standard libraries, which are a subset of Kotlin Multiplatform/Common standard libraries and a few third-party libraries.

Kotlite Interpreter is a lightweight Kotlin Multiplatform library to interpret and execute codes written in Kotlite, and bridge the host runtime environment and the embedded runtime environment.

Kotlite Interpreter Kotlite Interpreter

Kotlite Stdlib Kotlite Stdlib

Kotlite Library Preprocessor Kotlite Library Preprocessor

Release Notes

TL;DR

val env = ExecutionEnvironment().apply {
    install(AllStdLibModules())
}
val interpreter = KotliteInterpreter(
    filename = "UserScript",
    code = """
        fun fib(n: Int): Long {
            if (n > 100) throw Exception("n is too large")
            
            val dp = mutableMapOf<Int, Long>()
            fun f(i: Int): Long {
                if (i < 0) throw Exception("Invalid i: ${'$'}i")
                if (i <= 1) return i.toLong()
                if (i in dp) {
                    return dp[i]!!
                }
                return (f(i - 2) + f(i - 1)).also {
                    dp[i] = it
                }
            }
            
            return f(n)
        }
        val a = fib(19)
    """.trimIndent(),
    executionEnvironment = env,
)
interpreter.eval()
val symbolTable = interpreter.symbolTable()
val a = (symbolTable.findPropertyByDeclaredName("a") as LongValue).value // 4181L

The interpreter is well tested.

Well tested

Demo

Web - Kotlite Interpreter in browser

https://github.com/sunny-chung/kotlite/assets/14835950/464ddb68-5623-463e-89cb-5b34a4bebf41

https://github.com/sunny-chung/kotlite/assets/14835950/417143fa-6240-40cf-ba5f-8e40e97a4243

https://github.com/sunny-chung/kotlite/assets/14835950/f731c9a0-d941-4cd3-a23c-bfc2a60429f4

Documentation

Documentation site