KT-930 upto/downto for numbers

This commit is contained in:
Alex Tkachman
2012-01-07 21:27:20 +02:00
parent d9a4410fb1
commit 33f145a83a
11 changed files with 693 additions and 126 deletions
@@ -29,7 +29,8 @@ public class IntrinsicMethods {
private static final IntrinsicMethod INV = new Inv();
private static final IntrinsicMethod TYPEINFO = new TypeInfo();
private static final IntrinsicMethod VALUE_TYPEINFO = new ValueTypeInfo();
private static final IntrinsicMethod RANGE_TO = new RangeTo();
private static final IntrinsicMethod UP_TO = new UpTo(true);
private static final IntrinsicMethod DOWN_TO = new UpTo(false);
private static final IntrinsicMethod INC = new Increment(1);
private static final IntrinsicMethod DEC = new Increment(-1);
@@ -59,7 +60,9 @@ public class IntrinsicMethods {
declareIntrinsicFunction(type, "plus", 0, UNARY_PLUS);
declareIntrinsicFunction(type, "minus", 0, UNARY_MINUS);
declareIntrinsicFunction(type, "inv", 0, INV);
declareIntrinsicFunction(type, "rangeTo", 1, RANGE_TO);
declareIntrinsicFunction(type, "rangeTo", 1, UP_TO);
declareIntrinsicFunction(type, "upto", 1, UP_TO);
declareIntrinsicFunction(type, "downto", 1, DOWN_TO);
declareIntrinsicFunction(type, "inc", 0, INC);
declareIntrinsicFunction(type, "dec", 0, DEC);
}
@@ -14,8 +14,15 @@ import java.util.List;
/**
* @author yole
* @author alex.tkachman
*/
public class RangeTo implements IntrinsicMethod {
public class UpTo implements IntrinsicMethod {
private boolean forward;
public UpTo(boolean forward) {
this.forward = forward;
}
@Override
public StackValue generate(ExpressionCodegen codegen, InstructionAdapter v, Type expectedType, PsiElement element, List<JetExpression> arguments, StackValue receiver) {
if(arguments.size()==1) {
@@ -23,7 +30,7 @@ public class RangeTo implements IntrinsicMethod {
final Type rightType = codegen.expressionType(arguments.get(0));
receiver.put(Type.INT_TYPE, v);
codegen.gen(arguments.get(0), rightType);
v.invokestatic("jet/runtime/Ranges", "rangeTo", "(" + receiver.type.getDescriptor() + leftType.getDescriptor() + ")" + expectedType.getDescriptor());
v.invokestatic("jet/runtime/Ranges", forward ? "upTo" : "downTo", "(" + receiver.type.getDescriptor() + leftType.getDescriptor() + ")" + expectedType.getDescriptor());
return StackValue.onStack(expectedType);
}
else {
@@ -33,7 +40,7 @@ public class RangeTo implements IntrinsicMethod {
// if (JetTypeMapper.isIntPrimitive(leftType)) {
codegen.gen(expression.getLeft(), leftType);
codegen.gen(expression.getRight(), rightType);
v.invokestatic("jet/runtime/Ranges", "rangeTo", "(" + leftType.getDescriptor() + rightType.getDescriptor() + ")" + expectedType.getDescriptor());
v.invokestatic("jet/runtime/Ranges", forward ? "upTo" : "downTo", "(" + leftType.getDescriptor() + rightType.getDescriptor() + ")" + expectedType.getDescriptor());
return StackValue.onStack(expectedType);
// }
// else {
+115 -3
View File
@@ -73,6 +73,22 @@ class Double : Number, Comparable<Double> {
fun rangeTo(other : Byte) : DoubleRange
fun rangeTo(other : Char) : DoubleRange
fun upto(other : Double) : DoubleRange
fun upto(other : Float) : DoubleRange
fun upto(other : Long) : DoubleRange
fun upto(other : Int) : DoubleRange
fun upto(other : Short) : DoubleRange
fun upto(other : Byte) : DoubleRange
fun upto(other : Char) : DoubleRange
fun downto(other : Double) : DoubleRange
fun downto(other : Float) : DoubleRange
fun downto(other : Long) : DoubleRange
fun downto(other : Int) : DoubleRange
fun downto(other : Short) : DoubleRange
fun downto(other : Byte) : DoubleRange
fun downto(other : Char) : DoubleRange
fun inc() : Double
fun dec() : Double
fun plus() : Double
@@ -131,11 +147,27 @@ class Float : Number, Comparable<Float> {
fun rangeTo(other : Double) : DoubleRange
fun rangeTo(other : Float) : FloatRange
fun rangeTo(other : Long) : DoubleRange
fun rangeTo(other : Int) : DoubleRange
fun rangeTo(other : Int) : FloatRange
fun rangeTo(other : Short) : FloatRange
fun rangeTo(other : Byte) : FloatRange
fun rangeTo(other : Char) : FloatRange
fun upto(other : Double) : DoubleRange
fun upto(other : Float) : FloatRange
fun upto(other : Long) : DoubleRange
fun upto(other : Int) : FloatRange
fun upto(other : Short) : FloatRange
fun upto(other : Byte) : FloatRange
fun upto(other : Char) : FloatRange
fun downto(other : Double) : DoubleRange
fun downto(other : Float) : FloatRange
fun downto(other : Long) : DoubleRange
fun downto(other : Int) : FloatRange
fun downto(other : Short) : FloatRange
fun downto(other : Byte) : FloatRange
fun downto(other : Char) : FloatRange
fun inc() : Float
fun dec() : Float
fun plus() : Float
@@ -199,6 +231,22 @@ class Long : Number, Comparable<Long> {
fun rangeTo(other : Byte) : LongRange
fun rangeTo(other : Char) : LongRange
fun upto(other : Double) : DoubleRange
fun upto(other : Float) : FloatRange
fun upto(other : Long) : LongRange
fun upto(other : Int) : LongRange
fun upto(other : Short) : LongRange
fun upto(other : Byte) : LongRange
fun upto(other : Char) : LongRange
fun downto(other : Double) : DoubleRange
fun downto(other : Float) : FloatRange
fun downto(other : Long) : LongRange
fun downto(other : Int) : LongRange
fun downto(other : Short) : LongRange
fun downto(other : Byte) : LongRange
fun downto(other : Char) : LongRange
fun inc() : Long
fun dec() : Long
fun plus() : Long
@@ -270,6 +318,22 @@ class Int : Number, Comparable<Int> {
fun rangeTo(other : Byte) : IntRange
fun rangeTo(other : Char) : IntRange
fun upto(other : Double) : DoubleRange
fun upto(other : Float) : FloatRange
fun upto(other : Long) : LongRange
fun upto(other : Int) : IntRange
fun upto(other : Short) : IntRange
fun upto(other : Byte) : IntRange
fun upto(other : Char) : IntRange
fun downto(other : Double) : DoubleRange
fun downto(other : Float) : FloatRange
fun downto(other : Long) : LongRange
fun downto(other : Int) : IntRange
fun downto(other : Short) : IntRange
fun downto(other : Byte) : IntRange
fun downto(other : Char) : IntRange
fun inc() : Int
fun dec() : Int
fun plus() : Int
@@ -341,6 +405,22 @@ class Char : Number, Comparable<Char> {
fun rangeTo(other : Byte) : CharRange
fun rangeTo(other : Char) : CharRange
fun upto(other : Double) : DoubleRange
fun upto(other : Float) : FloatRange
fun upto(other : Long) : LongRange
fun upto(other : Int) : IntRange
fun upto(other : Short) : ShortRange
fun upto(other : Byte) : CharRange
fun upto(other : Char) : CharRange
fun downto(other : Double) : DoubleRange
fun downto(other : Float) : FloatRange
fun downto(other : Long) : LongRange
fun downto(other : Int) : IntRange
fun downto(other : Short) : ShortRange
fun downto(other : Byte) : CharRange
fun downto(other : Char) : CharRange
fun inc() : Char
fun dec() : Char
fun plus() : Int
@@ -402,7 +482,23 @@ class Short : Number, Comparable<Short> {
fun rangeTo(other : Int) : IntRange
fun rangeTo(other : Short) : ShortRange
fun rangeTo(other : Byte) : ShortRange
fun rangeTo(other : Char) : IntRange
fun rangeTo(other : Char) : ShortRange
fun upto(other : Double) : DoubleRange
fun upto(other : Float) : FloatRange
fun upto(other : Long) : LongRange
fun upto(other : Int) : IntRange
fun upto(other : Short) : ShortRange
fun upto(other : Byte) : ShortRange
fun upto(other : Char) : ShortRange
fun downto(other : Double) : DoubleRange
fun downto(other : Float) : FloatRange
fun downto(other : Long) : LongRange
fun downto(other : Int) : IntRange
fun downto(other : Short) : ShortRange
fun downto(other : Byte) : ShortRange
fun downto(other : Char) : ShortRange
fun inc() : Short
fun dec() : Short
@@ -465,7 +561,23 @@ class Byte : Number, Comparable<Byte> {
fun rangeTo(other : Int) : IntRange
fun rangeTo(other : Short) : ShortRange
fun rangeTo(other : Byte) : ByteRange
fun rangeTo(other : Char) : IntRange
fun rangeTo(other : Char) : CharRange
fun upto(other : Double) : DoubleRange
fun upto(other : Float) : FloatRange
fun upto(other : Long) : LongRange
fun upto(other : Int) : IntRange
fun upto(other : Short) : ShortRange
fun upto(other : Byte) : ByteRange
fun upto(other : Char) : CharRange
fun downto(other : Double) : DoubleRange
fun downto(other : Float) : FloatRange
fun downto(other : Long) : LongRange
fun downto(other : Int) : IntRange
fun downto(other : Short) : ShortRange
fun downto(other : Byte) : ByteRange
fun downto(other : Char) : CharRange
fun inc() : Byte
fun dec() : Byte
@@ -0,0 +1,100 @@
fun testInt () : String {
val r1 = 1 upto 4
if(r1.end != 4 || r1.isReversed || r1.size != 4) return "int upto fail"
val r2 = 4 upto 1
if(r2.start != 0 || r2.size != 0) return "int negative upto fail"
val r3 = 5 downto 0
if(r3.start != 5 || r3.end != 0 || !r3.isReversed || r3.size != 6) return "int downto fail"
val r4 = 5 downto 6
if(r4.start != 0 || r4.end != 0 || !r3.isReversed || r4.size != 0) return "int negative downto fail"
return "OK"
}
fun testByte () : String {
val r1 = 1.byt upto 4.byt
if(r1.end != 4.byt || r1.isReversed || r1.size != 4) return "byte upto fail"
val r2 = 4.byt upto 1.byt
if(r2.start != 0.byt || r2.size != 0) return "byte negative upto fail"
val r3 = 5.byt downto 0.byt
if(r3.start != 5.byt || r3.end != 0.byt || !r3.isReversed || r3.size != 6) return "byte downto fail"
val r4 = 5.byt downto 6.byt
if(r4.start != 0.byt || r4.end != 0.byt || !r3.isReversed || r4.size != 0) return "byte negative downto fail"
return "OK"
}
fun testShort () : String {
val r1 = 1.sht upto 4.sht
if(r1.end != 4.sht || r1.isReversed || r1.size != 4) return "short upto fail"
val r2 = 4.sht upto 1.sht
if(r2.start != 0.sht || r2.size != 0) return "short negative upto fail"
val r3 = 5.sht downto 0.sht
if(r3.start != 5.sht || r3.end != 0.sht || !r3.isReversed || r3.size != 6) return "short downto fail"
val r4 = 5.sht downto 6.sht
if(r4.start != 0.sht || r4.end != 0.sht || !r3.isReversed || r4.size != 0) return "short negative downto fail"
return "OK"
}
fun testLong () : String {
val r1 = 1.lng upto 4.lng
if(r1.end != 4.lng || r1.isReversed || r1.size != 4.lng) return "long upto fail"
val r2 = 4.lng upto 1.lng
if(r2.start != 0.lng || r2.size != 0.lng) return "short negative long fail"
val r3 = 5.lng downto 0.lng
if(r3.start != 5.lng || r3.end != 0.lng || !r3.isReversed || r3.size != 6.lng) return "long downto fail"
val r4 = 5.lng downto 6.lng
if(r4.start != 0.lng || r4.end != 0.lng || !r3.isReversed || r4.size != 0.lng) return "long negative downto fail"
return "OK"
}
fun testChar () : String {
val r1 = 'a' upto 'd'
if(r1.end != 'd' || r1.isReversed || r1.size != 4) return "char upto fail"
val r2 = 'd' upto 'a'
if(r2.start != 0.chr || r2.size != 0) return "char negative long fail"
val r3 = 'd' downto 'a'
if(r3.start != 'd' || r3.end != 'a' || !r3.isReversed || r3.size != 4) return "char downto fail"
val r4 = 'a' downto 'd'
if(r4.start != 0.chr || r4.end != 0.chr || !r3.isReversed || r4.size != 0) return "char negative downto fail"
return "OK"
}
fun box() : String {
var r : String
r = testInt()
if(r != "OK") return r
r = testByte()
if(r != "OK") return r
r = testShort()
if(r != "OK") return r
r = testLong()
if(r != "OK") return r
return "OK"
}
@@ -375,4 +375,8 @@ public class PrimitiveTypesTest extends CodegenTestCase {
public void testKt765 () {
blackBoxFile("regressions/kt765.kt");
}
public void testKt930 () {
blackBoxFile("regressions/kt930.kt");
}
}