A lite embedded Kotlin interpreter
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.
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.
Web - Kotlite Interpreter in browser