Git Product home page Git Product logo

kotlite's Introduction

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

kotlite's People

Contributors

sunny-chung avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.