https://github.com/sunny-chung/kotlite
A Kotlin Multiplatform library to interpret Kotlite code, which is a subset of the Kotlin language, during runtime in a safe way.
https://github.com/sunny-chung/kotlite
android interpreter ios javascript kotlin kotlin-multiplatform macos tvos watchos
Last synced: 3 months ago
JSON representation
A Kotlin Multiplatform library to interpret Kotlite code, which is a subset of the Kotlin language, during runtime in a safe way.
- Host: GitHub
- URL: https://github.com/sunny-chung/kotlite
- Owner: sunny-chung
- License: mit
- Created: 2024-04-08T04:29:35.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-18T01:49:44.000Z (about 1 year ago)
- Last Synced: 2024-04-18T03:23:02.233Z (about 1 year ago)
- Topics: android, interpreter, ios, javascript, kotlin, kotlin-multiplatform, macos, tvos, watchos
- Language: Kotlin
- Homepage: https://sunny-chung.github.io/kotlite/
- Size: 2.55 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - sunny-chung/kotlite - A Kotlin Multiplatform library to interpret Kotlite code, which is a subset of the Kotlin language, during runtime in a safe way. (Kotlin)
README
# Kotlite & Kotlite Interpreter
_A lite embedded Kotlin interpreter_







**Kotlite** is an open-sourced type-safe programming language that has a rich subset of the [script](https://kotlinlang.org/docs/custom-script-deps-tutorial.html) variant of the [Kotlin](https://kotlinlang.org/) 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 Stdlib 
Kotlite Library Preprocessor 
[Release Notes](CHANGELOG.md)
## TL;DR
```kotlin
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()
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.

## Demo
[Web - Kotlite Interpreter in browser](https://sunny-chung.github.io/kotlite/demo/)
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](https://sunny-chung.github.io/kotlite/)