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 {