All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.sunnychung.lib.multiplatform.kotlite.model.IterableInterface.kt Maven / Gradle / Ivy

Go to download

A Kotlin Multiplatform library to interpret Kotlite code, which is a subset of Kotlin language, in runtime in a safe way.

The newest version!
package com.sunnychung.lib.multiplatform.kotlite.model

import com.sunnychung.lib.multiplatform.kotlite.lexer.BuiltinFilename

fun IterableValue(value: Iterable, typeArgument: DataType, symbolTable: SymbolTable)
    = DelegatedValue>(value, IterableInterface.clazz, listOf(typeArgument), symbolTable)

object IterableInterface {
    val clazz = ProvidedClassDefinition(
        fullQualifiedName = "Iterable",
        isInterface = true,
        typeParameters = listOf(TypeParameter(name = "T", typeUpperBound = null)),
        isInstanceCreationAllowed = false,
        primaryConstructorParameters = emptyList(),
        constructInstance = { _, _, _ -> throw UnsupportedOperationException() },
        position = SourcePosition(BuiltinFilename.BUILTIN, 1, 1),
    )

    val functions = listOf(
        CustomFunctionDefinition(
            receiverType = "Iterable",
            functionName = "iterator",
            returnType = "Iterator",
            parameterTypes = emptyList(),
            typeParameters = listOf(
                TypeParameter(name = "T", typeUpperBound = null),
            ),
            executable = { interpreter, receiver, args, typeArgs ->
                val delegatedValue = (receiver as DelegatedValue<*>).value as Iterable
                IteratorValue(delegatedValue.iterator(), typeArgs["T"]!!, interpreter.symbolTable())
            },
            position = SourcePosition(BuiltinFilename.BUILTIN, 1, 1),
        ),
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy