Skip to content

Latest commit

 

History

History
77 lines (56 loc) · 2.94 KB

README.md

File metadata and controls

77 lines (56 loc) · 2.94 KB

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

Web.mp4
Android.mp4
iOS.mp4

Documentation

Documentation site