commonMain.com.sunnychung.lib.multiplatform.kotlite.model.SpecialFunction.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlite-interpreter-jvm Show documentation
Show all versions of kotlite-interpreter-jvm Show documentation
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())),
}
}