Generate Kotlin sources of ranges
This commit is contained in:
@@ -20,6 +20,18 @@ import java.io.File
|
||||
import kotlin.properties.Delegates
|
||||
import java.io.PrintWriter
|
||||
|
||||
enum class ProgressionKind {
|
||||
BYTE
|
||||
CHAR
|
||||
SHORT
|
||||
INT
|
||||
LONG
|
||||
FLOAT
|
||||
DOUBLE
|
||||
|
||||
val capitalized = name().toLowerCase().capitalize()
|
||||
}
|
||||
|
||||
val OUTPUT_DIR: File by Delegates.lazy {
|
||||
val result = File("runtime/kt/")
|
||||
if (!result.exists()) {
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.generators.runtime.ranges
|
||||
|
||||
import org.jetbrains.jet.generators.runtime.*
|
||||
import org.jetbrains.jet.generators.runtime.ProgressionKind.*
|
||||
import java.io.PrintWriter
|
||||
|
||||
fun hashLong(v: String) = "($v xor ($v ushr 32))"
|
||||
|
||||
fun floatToIntBits(v: String) = "(if ($v != 0.0f) java.lang.Float.floatToIntBits($v) else 0)"
|
||||
|
||||
fun doubleToLongBits(v: String) = "if ($v != 0.0) java.lang.Double.doubleToLongBits($v) else 0L"
|
||||
|
||||
class GenerateRanges(val out: PrintWriter) {
|
||||
fun generate() {
|
||||
generatedBy(out, javaClass.getName())
|
||||
for (kind in ProgressionKind.values()) {
|
||||
val t = kind.capitalized
|
||||
val range = "${t}Range"
|
||||
|
||||
val incrementType = when (kind) {
|
||||
BYTE, CHAR, SHORT -> "Int"
|
||||
else -> kind.capitalized
|
||||
}
|
||||
|
||||
val increment = when (kind) {
|
||||
FLOAT -> "1.0f"
|
||||
DOUBLE -> "1.0"
|
||||
else -> "1"
|
||||
}
|
||||
|
||||
val emptyBounds = when (kind) {
|
||||
CHAR -> "1.toChar(), 0.toChar()"
|
||||
FLOAT -> "1.0f, 0.0f"
|
||||
DOUBLE -> "1.0, 0.0"
|
||||
else -> "1, 0"
|
||||
}
|
||||
|
||||
fun compare(v: String) = when (kind) {
|
||||
FLOAT, DOUBLE -> "java.lang.${kind.capitalized}.compare($v, other.$v) == 0"
|
||||
else -> "$v == other.$v"
|
||||
}
|
||||
|
||||
val hashCode = when (kind) {
|
||||
BYTE, CHAR, SHORT -> " = 31 * start.toInt() + end"
|
||||
INT -> " = 31 * start + end"
|
||||
LONG -> " = (31 * ${hashLong("start")} + ${hashLong("end")}).toInt()"
|
||||
FLOAT -> " = 31 * ${floatToIntBits("start")} +\n" +
|
||||
" ${floatToIntBits("end")}"
|
||||
DOUBLE -> ": Int {\n" +
|
||||
" var temp = ${doubleToLongBits("start")}\n" +
|
||||
" val result = ${hashLong("temp")}\n" +
|
||||
" temp = ${doubleToLongBits("end")}\n" +
|
||||
" return (31 * result + ${hashLong("temp")}).toInt()\n" +
|
||||
" }"
|
||||
}
|
||||
|
||||
out.println(
|
||||
"""public class $range(public override val start: $t, public override val end: $t) : Range<$t>, Progression<$t> {
|
||||
override val increment: $incrementType
|
||||
get() = $increment
|
||||
|
||||
override fun contains(item: $t) = start <= item && item <= end
|
||||
|
||||
override fun iterator(): ${t}Iterator = ${t}ProgressionIterator(start, end, $increment)
|
||||
|
||||
fun equals(other: Any?): Boolean =
|
||||
other is $range && ${compare("start")} && ${compare("end")}
|
||||
|
||||
fun hashCode()$hashCode
|
||||
|
||||
fun toString() = "${"\$start..\$end"}"
|
||||
|
||||
class object {
|
||||
public val EMPTY: $range = $range($emptyBounds)
|
||||
}
|
||||
}""")
|
||||
out.println()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
generateRuntimeFile("Ranges.kt") {
|
||||
GenerateRanges(it).generate()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user