New J2K: add conversion of binary literals & fix conversion of octal ones

#KT-32835 fixed
This commit is contained in:
Ilya Kirillov
2019-07-24 23:39:56 +03:00
parent 72a09ab59b
commit e795e4b73d
6 changed files with 45 additions and 14 deletions
@@ -73,7 +73,8 @@ class LiteralConversion : RecursiveApplicableConversionBase() {
literal
.cleanIntAndLongLiterals()
.convertHexLiteral(isLongLiteral = false)
.convertOctalLiteral(),
.convertBinaryLiteral(isLongLiteral = false)
.convertOctalLiteral(isLongLiteral = false),
JKLiteralExpression.LiteralType.INT
)
@@ -83,7 +84,8 @@ class LiteralConversion : RecursiveApplicableConversionBase() {
literal
.cleanIntAndLongLiterals()
.convertHexLiteral(isLongLiteral = true)
.convertOctalLiteral() + "L",
.convertBinaryLiteral(isLongLiteral = true)
.convertOctalLiteral(isLongLiteral = true) + "L",
JKLiteralExpression.LiteralType.LONG
)
@@ -101,10 +103,16 @@ class LiteralConversion : RecursiveApplicableConversionBase() {
}
}
private fun String.convertOctalLiteral(): String {
private fun String.convertBinaryLiteral(isLongLiteral: Boolean): String {
if (!startsWith("0b", ignoreCase = true)) return this
val value = BigInteger(drop(2), 2)
return if (isLongLiteral) value.toLong().toString(10) else value.toInt().toString()
}
private fun String.convertOctalLiteral(isLongLiteral: Boolean): String {
if (!startsWith("0") || length == 1 || get(1).toLowerCase() == 'x') return this
val value = BigInteger(drop(1), 8)
return value.toString()
return if (isLongLiteral) value.toLong().toString(10) else value.toInt().toString(10)
}
private fun String.cleanFloatAndDoubleLiterals() =
@@ -118,5 +126,4 @@ class LiteralConversion : RecursiveApplicableConversionBase() {
private fun String.cleanIntAndLongLiterals() =
replace("l", "", ignoreCase = true)
.replace("_", "")
}
+8
View File
@@ -0,0 +1,8 @@
public class Binary {
int negativeBinaryInt = 0b10111111111111111111111111111111;
int binaryInt = 0b10111111111;
long negativeBinaryLong = 0b1011111111111111111111111111111111111111111111111111111111111111L;
long binaryLong = 0b1011111111111111111L;
short binaryShort = 0b111111111111111;
byte binaryByte = 0b1010011;
}
+8
View File
@@ -0,0 +1,8 @@
class Binary {
internal var negativeBinaryInt = -1073741825
internal var binaryInt = 1535
internal var negativeBinaryLong = -4611686018427387905L
internal var binaryLong = 393215L
internal var binaryShort: Short = 32767
internal var binaryByte: Byte = 83
}
+6 -5
View File
@@ -1,7 +1,8 @@
//file
class Test {
void test() {
int i1 = 045;
Int i2 = 032;
}
int negativeOctalInt = 023432423432;
int octalInt = 07432423432;
long negativeOctalLong = 01777777777777777777777L;
long octalLong = 0777777777777777777777L;
short octalShort = 047777;
byte octalByte = 077;
}
+6 -4
View File
@@ -1,6 +1,8 @@
internal class Test {
fun test() {
val i1 = 37
val i2 = 26
}
var negativeOctalInt = -1670764774
var octalInt = 1013589786
var negativeOctalLong = -1L
var octalLong = 9223372036854775807L
var octalShort: Short = 20479
var octalByte: Byte = 63
}
@@ -3296,6 +3296,11 @@ public class NewJavaToKotlinConverterSingleFileTestGenerated extends AbstractNew
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("nj2k/testData/newJ2k/literalExpression"), Pattern.compile("^([^\\.]+)\\.java$"), TargetBackend.ANY, true);
}
@TestMetadata("binary.java")
public void testBinary() throws Exception {
runTest("nj2k/testData/newJ2k/literalExpression/binary.java");
}
@TestMetadata("char.java")
public void testChar() throws Exception {
runTest("nj2k/testData/newJ2k/literalExpression/char.java");