Integer conversions (to* methods).

Add NumberTest, NumberConversionFIF.
This commit is contained in:
Pavel V. Talanov
2012-07-18 13:31:37 +04:00
parent 459802e726
commit 3cd406a155
6 changed files with 131 additions and 1 deletions
@@ -32,7 +32,7 @@ public final class NativeInteropTest extends SingleFileTranslationTest {
private static final String NATIVE = "native/";
public NativeInteropTest() {
super("native/");
super(NATIVE);
}
@NotNull
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2012 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.k2js.test.semantics;
import org.jetbrains.k2js.test.SingleFileTranslationTest;
/**
* @author Pavel Talanov
*/
public final class NumberTest extends SingleFileTranslationTest {
public NumberTest() {
super("number/");
}
public void testIntConversions() throws Exception {
fooBoxTest();
}
}
@@ -67,6 +67,7 @@ public final class FunctionIntrinsics {
register(StringOperationFIF.INSTANCE);
register(ArrayFIF.INSTANCE);
register(TopLevelFIF.INSTANCE);
register(NumberConversionFIF.INSTANCE);
}
private boolean register(@NotNull FunctionIntrinsicFactory instance) {
@@ -0,0 +1,69 @@
/*
* Copyright 2010-2012 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.k2js.translate.intrinsic.functions.factories;
import closurecompiler.internal.com.google.common.collect.Sets;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NameChecker;
import java.util.HashSet;
import java.util.List;
import static org.jetbrains.jet.lang.types.expressions.OperatorConventions.*;
import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
/**
* @author Pavel Talanov
*/
public final class NumberConversionFIF {
@NotNull
private static final NameChecker CONVERSIONS;
static {
HashSet<Name> supportedConversions = Sets.newHashSet(NUMBER_CONVERSIONS);
//TODO: support longs and chars
supportedConversions.remove(CHAR);
supportedConversions.remove(LONG);
CONVERSIONS = new NameChecker(supportedConversions);
}
@NotNull
private static final FunctionIntrinsic RETURN_RECEIVER = new FunctionIntrinsic() {
@NotNull
@Override
public JsExpression apply(@Nullable JsExpression receiver,
@NotNull List<JsExpression> arguments,
@NotNull TranslationContext context) {
assert receiver != null;
assert arguments.isEmpty();
return receiver;
}
};
@NotNull
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
.add(pattern("Int", CONVERSIONS), RETURN_RECEIVER)
.build();
private NumberConversionFIF() {
}
}
@@ -42,6 +42,13 @@ public final class PatternBuilder {
return pattern(checkers);
}
@NotNull
public static Pattern pattern(@NotNull String stringWithPattern, @NotNull NameChecker checker) {
List<NameChecker> checkers = Lists.newArrayList(parseStringAsCheckerList(stringWithPattern));
checkers.add(checker);
return pattern(checkers);
}
@NotNull
public static Pattern pattern(@NotNull String string) {
List<NameChecker> checkers = parseStringAsCheckerList(string);
@@ -0,0 +1,21 @@
package foo
fun box(): Boolean {
val c: Int = 3
if (c.toDouble() != 3.0) {
return false
}
if (c.toFloat() != 3.toFloat()) {
return false
}
if (c.toByte() != 3.toByte()) {
return false
}
if (c.toInt() != 3) {
return false
}
if (c.toShort() != 3.toShort()) {
return false
}
return true
}