Implement trim function interpretation in compile time

This commit is contained in:
Ivan Kylchik
2020-06-10 19:10:21 +03:00
parent 178b2a07ae
commit f99829ce5e
7 changed files with 248 additions and 127 deletions
@@ -48,6 +48,7 @@ fun generateMap(): String {
val unaryOperationsMap = mutableMapOf<CallableDescriptor, List<KotlinType>>()
val binaryOperationsMap = mutableMapOf<CallableDescriptor, List<KotlinType>>()
val ternaryOperationsMap = mutableMapOf<CallableDescriptor, List<KotlinType>>()
val builtIns = DefaultBuiltIns.Instance
//save common built ins
@@ -67,6 +68,7 @@ fun generateMap(): String {
when (parametersTypes.size) {
1 -> unaryOperationsMap[function] = parametersTypes
2 -> binaryOperationsMap[function] = parametersTypes
3 -> ternaryOperationsMap[function] = parametersTypes
else -> throw IllegalStateException("Couldn't add following method from builtins to operations map: ${function.name} in class ${descriptor.name}")
}
}
@@ -134,6 +136,19 @@ fun generateMap(): String {
p.println(binaryBody)
p.println(")")
p.println("val ternaryFunctions = mapOf<CompileTimeFunction, Function3<Any?, Any?, Any?, Any>>(")
val ternaryBody = ternaryOperationsMap.entries.joinToString(",\n") { (function, parameters) ->
val receiverType = parameters.last().toString()
val firstParameter = parameters[0].toString()
val secondParameter = parameters[1].toString()
val methodName = "${function.name}"
" ternaryOperation<$receiverType, $firstParameter, $secondParameter>" +
"(\"$methodName\", \"$receiverType\", \"$firstParameter\", \"$secondParameter\") { a, b, c -> a.$methodName(b, c) }"
}
p.println(ternaryBody)
p.println(")")
p.println()
return sb.toString()
}