Generate Kotlin sources of ranges

This commit is contained in:
Alexander Udalov
2013-12-30 21:19:50 +04:00
parent f9f1065294
commit 87a6ca5910
10 changed files with 264 additions and 608 deletions
@@ -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()
}
}
+150
View File
@@ -0,0 +1,150 @@
// Generated by org.jetbrains.jet.generators.runtime.ranges.GenerateRanges
package jet
public class ByteRange(public override val start: Byte, public override val end: Byte) : Range<Byte>, Progression<Byte> {
override val increment: Int
get() = 1
override fun contains(item: Byte) = start <= item && item <= end
override fun iterator(): ByteIterator = ByteProgressionIterator(start, end, 1)
fun equals(other: Any?): Boolean =
other is ByteRange && start == other.start && end == other.end
fun hashCode() = 31 * start.toInt() + end
fun toString() = "$start..$end"
class object {
public val EMPTY: ByteRange = ByteRange(1, 0)
}
}
public class CharRange(public override val start: Char, public override val end: Char) : Range<Char>, Progression<Char> {
override val increment: Int
get() = 1
override fun contains(item: Char) = start <= item && item <= end
override fun iterator(): CharIterator = CharProgressionIterator(start, end, 1)
fun equals(other: Any?): Boolean =
other is CharRange && start == other.start && end == other.end
fun hashCode() = 31 * start.toInt() + end
fun toString() = "$start..$end"
class object {
public val EMPTY: CharRange = CharRange(1.toChar(), 0.toChar())
}
}
public class ShortRange(public override val start: Short, public override val end: Short) : Range<Short>, Progression<Short> {
override val increment: Int
get() = 1
override fun contains(item: Short) = start <= item && item <= end
override fun iterator(): ShortIterator = ShortProgressionIterator(start, end, 1)
fun equals(other: Any?): Boolean =
other is ShortRange && start == other.start && end == other.end
fun hashCode() = 31 * start.toInt() + end
fun toString() = "$start..$end"
class object {
public val EMPTY: ShortRange = ShortRange(1, 0)
}
}
public class IntRange(public override val start: Int, public override val end: Int) : Range<Int>, Progression<Int> {
override val increment: Int
get() = 1
override fun contains(item: Int) = start <= item && item <= end
override fun iterator(): IntIterator = IntProgressionIterator(start, end, 1)
fun equals(other: Any?): Boolean =
other is IntRange && start == other.start && end == other.end
fun hashCode() = 31 * start + end
fun toString() = "$start..$end"
class object {
public val EMPTY: IntRange = IntRange(1, 0)
}
}
public class LongRange(public override val start: Long, public override val end: Long) : Range<Long>, Progression<Long> {
override val increment: Long
get() = 1
override fun contains(item: Long) = start <= item && item <= end
override fun iterator(): LongIterator = LongProgressionIterator(start, end, 1)
fun equals(other: Any?): Boolean =
other is LongRange && start == other.start && end == other.end
fun hashCode() = (31 * (start xor (start ushr 32)) + (end xor (end ushr 32))).toInt()
fun toString() = "$start..$end"
class object {
public val EMPTY: LongRange = LongRange(1, 0)
}
}
public class FloatRange(public override val start: Float, public override val end: Float) : Range<Float>, Progression<Float> {
override val increment: Float
get() = 1.0f
override fun contains(item: Float) = start <= item && item <= end
override fun iterator(): FloatIterator = FloatProgressionIterator(start, end, 1.0f)
fun equals(other: Any?): Boolean =
other is FloatRange && java.lang.Float.compare(start, other.start) == 0 && java.lang.Float.compare(end, other.end) == 0
fun hashCode() = 31 * (if (start != 0.0f) java.lang.Float.floatToIntBits(start) else 0) +
(if (end != 0.0f) java.lang.Float.floatToIntBits(end) else 0)
fun toString() = "$start..$end"
class object {
public val EMPTY: FloatRange = FloatRange(1.0f, 0.0f)
}
}
public class DoubleRange(public override val start: Double, public override val end: Double) : Range<Double>, Progression<Double> {
override val increment: Double
get() = 1.0
override fun contains(item: Double) = start <= item && item <= end
override fun iterator(): DoubleIterator = DoubleProgressionIterator(start, end, 1.0)
fun equals(other: Any?): Boolean =
other is DoubleRange && java.lang.Double.compare(start, other.start) == 0 && java.lang.Double.compare(end, other.end) == 0
fun hashCode(): Int {
var temp = if (start != 0.0) java.lang.Double.doubleToLongBits(start) else 0L
val result = (temp xor (temp ushr 32))
temp = if (end != 0.0) java.lang.Double.doubleToLongBits(end) else 0L
return (31 * result + (temp xor (temp ushr 32))).toInt()
}
fun toString() = "$start..$end"
class object {
public val EMPTY: DoubleRange = DoubleRange(1.0, 0.0)
}
}
-86
View File
@@ -1,86 +0,0 @@
/*
* 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 jet;
import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver;
@AssertInvisibleInResolver
public final class ByteRange implements Range<Byte>, Progression<Byte> {
public static final ByteRange EMPTY = new ByteRange((byte) 1, (byte) 0);
private final byte start;
private final byte end;
public ByteRange(byte start, byte end) {
this.start = start;
this.end = end;
}
@Override
public boolean contains(Byte item) {
return start <= item && item <= end;
}
public boolean contains(byte item) {
return start <= item && item <= end;
}
@Override
public Byte getStart() {
return start;
}
@Override
public Byte getEnd() {
return end;
}
@Override
public Integer getIncrement() {
return 1;
}
@Override
public ByteIterator iterator() {
return new ByteProgressionIterator(start, end, 1);
}
@Override
public String toString() {
return start + ".." + end;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ByteRange range = (ByteRange) o;
return end == range.end && start == range.start;
}
@Override
public int hashCode() {
int result = (int) start;
result = 31 * result + end;
return result;
}
}
-86
View File
@@ -1,86 +0,0 @@
/*
* 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 jet;
import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver;
@AssertInvisibleInResolver
public final class CharRange implements Range<Character>, Progression<Character> {
public static final CharRange EMPTY = new CharRange((char) 1, (char) 0);
private final char start;
private final char end;
public CharRange(char start, char end) {
this.start = start;
this.end = end;
}
@Override
public boolean contains(Character item) {
return start <= item && item <= end;
}
public boolean contains(char item) {
return start <= item && item <= end;
}
@Override
public Character getStart() {
return start;
}
@Override
public Character getEnd() {
return end;
}
@Override
public Integer getIncrement() {
return 1;
}
@Override
public CharIterator iterator() {
return new CharProgressionIterator(start, end, 1);
}
@Override
public String toString() {
return start + ".." + end;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CharRange range = (CharRange) o;
return end == range.end && start == range.start;
}
@Override
public int hashCode() {
int result = (int) start;
result = 31 * result + end;
return result;
}
}
-91
View File
@@ -1,91 +0,0 @@
/*
* 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 jet;
import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver;
@AssertInvisibleInResolver
public final class DoubleRange implements Range<Double>, Progression<Double> {
public static final DoubleRange EMPTY = new DoubleRange(1, 0);
private final double start;
private final double end;
public DoubleRange(double start, double end) {
this.start = start;
this.end = end;
}
@Override
public boolean contains(Double item) {
return start <= item && item <= end;
}
public boolean contains(double item) {
return start <= item && item <= end;
}
@Override
public Double getStart() {
return start;
}
@Override
public Double getEnd() {
return end;
}
@Override
public Double getIncrement() {
return 1.0;
}
@Override
public DoubleIterator iterator() {
return new DoubleProgressionIterator(start, end, 1.0);
}
@Override
public String toString() {
return start + ".." + end;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DoubleRange range = (DoubleRange) o;
return Double.compare(range.end, end) == 0 && Double.compare(range.start, start) == 0;
}
@Override
public int hashCode() {
int result;
long temp;
temp = start != +0.0d ? Double.doubleToLongBits(start) : 0L;
result = (int) (temp ^ (temp >>> 32));
temp = end != +0.0d ? Double.doubleToLongBits(end) : 0L;
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}
-87
View File
@@ -1,87 +0,0 @@
/*
* 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 jet;
import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver;
@AssertInvisibleInResolver
public final class FloatRange implements Range<Float>, Progression<Float> {
public static final FloatRange EMPTY = new FloatRange(1, 0);
private final float start;
private final float end;
public FloatRange(float start, float end) {
this.start = start;
this.end = end;
}
@Override
public boolean contains(Float item) {
return start <= item && item <= end;
}
public boolean contains(float item) {
return start <= item && item <= end;
}
@Override
public Float getStart() {
return start;
}
@Override
public Float getEnd() {
return end;
}
@Override
public Float getIncrement() {
return 1.0f;
}
@Override
public FloatIterator iterator() {
return new FloatProgressionIterator(start, end, 1);
}
@Override
public String toString() {
return start + ".." + end;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FloatRange range = (FloatRange) o;
return Float.compare(range.end, end) == 0 && Float.compare(range.start, start) == 0;
}
@Override
public int hashCode() {
int result = (start != +0.0f ? Float.floatToIntBits(start) : 0);
result = 31 * result + (end != +0.0f ? Float.floatToIntBits(end) : 0);
return result;
}
}
-86
View File
@@ -1,86 +0,0 @@
/*
* 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 jet;
import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver;
@AssertInvisibleInResolver
public final class IntRange implements Range<Integer>, Progression<Integer> {
public static final IntRange EMPTY = new IntRange(1, 0);
private final int start;
private final int end;
public IntRange(int start, int end) {
this.start = start;
this.end = end;
}
@Override
public boolean contains(Integer item) {
return start <= item && item <= end;
}
public boolean contains(int item) {
return start <= item && item <= end;
}
@Override
public Integer getStart() {
return start;
}
@Override
public Integer getEnd() {
return end;
}
@Override
public Integer getIncrement() {
return 1;
}
@Override
public IntIterator iterator() {
return new IntProgressionIterator(start, end, 1);
}
@Override
public String toString() {
return start + ".." + end;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
IntRange range = (IntRange) o;
return end == range.end && start == range.start;
}
@Override
public int hashCode() {
int result = start;
result = 31 * result + end;
return result;
}
}
-86
View File
@@ -1,86 +0,0 @@
/*
* 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 jet;
import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver;
@AssertInvisibleInResolver
public final class LongRange implements Range<Long>, Progression<Long> {
public static final LongRange EMPTY = new LongRange(1, 0);
private final long start;
private final long end;
public LongRange(long start, long end) {
this.start = start;
this.end = end;
}
@Override
public boolean contains(Long item) {
return start <= item && item <= end;
}
public boolean contains(long item) {
return start <= item && item <= end;
}
@Override
public Long getStart() {
return start;
}
@Override
public Long getEnd() {
return end;
}
@Override
public Long getIncrement() {
return 1L;
}
@Override
public LongIterator iterator() {
return new LongProgressionIterator(start, end, 1);
}
@Override
public String toString() {
return start + ".." + end;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LongRange range = (LongRange) o;
return end == range.end && start == range.start;
}
@Override
public int hashCode() {
int result = (int) (start ^ (start >>> 32));
result = 31 * result + (int) (end ^ (end >>> 32));
return result;
}
}
-86
View File
@@ -1,86 +0,0 @@
/*
* 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 jet;
import org.jetbrains.jet.rt.annotation.AssertInvisibleInResolver;
@AssertInvisibleInResolver
public final class ShortRange implements Range<Short>, Progression<Short> {
public static final ShortRange EMPTY = new ShortRange((short) 1, (short) 0);
private final short start;
private final short end;
public ShortRange(short start, short end) {
this.start = start;
this.end = end;
}
@Override
public boolean contains(Short item) {
return start <= item && item <= end;
}
public boolean contains(short item) {
return start <= item && item <= end;
}
@Override
public Short getStart() {
return start;
}
@Override
public Short getEnd() {
return end;
}
@Override
public Integer getIncrement() {
return 1;
}
@Override
public ShortIterator iterator() {
return new ShortProgressionIterator(start, end, 1);
}
@Override
public String toString() {
return start + ".." + end;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ShortRange range = (ShortRange) o;
return end == range.end && start == range.start;
}
@Override
public int hashCode() {
int result = (int) start;
result = 31 * result + end;
return result;
}
}