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

commonMain.com.sunnychung.lib.multiplatform.kotlite.model.SpecialFunction.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.Interpreter
import com.sunnychung.lib.multiplatform.kotlite.Parser
import com.sunnychung.lib.multiplatform.kotlite.lexer.BuiltinFilename
import com.sunnychung.lib.multiplatform.kotlite.lexer.Lexer

class SpecialFunction(
    val function: FunctionDeclarationNode
) {
    fun call(interpreter: Interpreter, subject: RuntimeValue, arguments: List): RuntimeValue {
        return with(interpreter) {
            FunctionCallNode(
                function = function,
                arguments = arguments.mapIndexed { index, it ->
                    FunctionCallArgumentNode(
                        position = SourcePosition.NONE,
                        index = index,
                        value = ValueNode(SourcePosition.NONE, it)
                    )
                },
                declaredTypeArguments = emptyList(),
                position = SourcePosition.NONE,
            ).evalClassMemberAnyFunctionCall(subject, function)
        }
    }

    companion object {
        private val parseType = { code: String ->
            Parser(Lexer(BuiltinFilename.BUILTIN, code)).type(
                isTryParenthesizedType = true,
                isParseDottedIdentifiers = true,
                isIncludeLastIdentifierAsTypeName = true,
            )
        }
    }

    /**
     * @param acceptableValueParameterTypes ordered list of acceptable value parameters, which are wrapped by a list
     */
    enum class Name(val functionName: String, val acceptableValueParameterTypes: List>) {
        Equals(functionName = "equals", acceptableValueParameterTypes = listOf(listOf(parseType("Any?")))),
        HashCode(functionName = "hashCode", acceptableValueParameterTypes = listOf(emptyList())),
        ToString(functionName = "toString", acceptableValueParameterTypes = listOf(emptyList())),
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy