From af2f83cbd02558213194b1cf32b10f98ca6be517 Mon Sep 17 00:00:00 2001 From: alexeev509 Date: Fri, 15 Mar 2024 12:25:55 +0300 Subject: [PATCH] Hex, oct and bin for byte, ubyte, short, ushort, ulong, uint were added; --- src/main/antlr/LibSLLexer.g4 | 2 +- .../libsl/visitors/ExpressionVisitor.kt | 88 +++++++++++++++---- .../expected/lsl/hexBinAndOctLiterals.lsl | 61 ++++++++++--- .../testdata/lsl/hexBinAndOctLiterals.lsl | 66 +++++++++++--- 4 files changed, 170 insertions(+), 47 deletions(-) diff --git a/src/main/antlr/LibSLLexer.g4 b/src/main/antlr/LibSLLexer.g4 index db602609..9fa9eb1e 100644 --- a/src/main/antlr/LibSLLexer.g4 +++ b/src/main/antlr/LibSLLexer.g4 @@ -335,7 +335,7 @@ Digit: ('0'..'9'); fragment NonZeroDigit: [1-9]; -fragment Hex: Digit | ('A'..'F'); +fragment Hex: Digit | [a-fA-F]; fragment HexNumeral: '0' [xX] Hex+; diff --git a/src/main/kotlin/org/jetbrains/research/libsl/visitors/ExpressionVisitor.kt b/src/main/kotlin/org/jetbrains/research/libsl/visitors/ExpressionVisitor.kt index bc65f28e..f0828533 100644 --- a/src/main/kotlin/org/jetbrains/research/libsl/visitors/ExpressionVisitor.kt +++ b/src/main/kotlin/org/jetbrains/research/libsl/visitors/ExpressionVisitor.kt @@ -8,7 +8,11 @@ import org.jetbrains.research.libsl.nodes.references.builders.* import org.jetbrains.research.libsl.nodes.references.builders.TypeReferenceBuilder.getReference import org.jetbrains.research.libsl.utils.PositionGetter import java.lang.Integer.parseInt +import java.lang.Integer.parseUnsignedInt import java.lang.Long.parseLong +import java.lang.Long.parseUnsignedLong +import java.lang.Byte.parseByte +import java.lang.Short.parseShort class ExpressionVisitor( override val context: LslContextBase @@ -232,42 +236,41 @@ class ExpressionVisitor( } override fun visitIntegerNumber(ctx: LibSLParser.IntegerNumberContext): Atomic { - var num = ctx.text.lowercase() + val num = ctx.text.lowercase() return when { num.endsWith("ub") -> UnsignedInt8Literal( - ctx.text.dropLast(2).toUByte(), + convertBinHexOctToPrimitives(ctx.text.dropLast(2), "ub").toString().toUByte(), "ub", posGetter.getCtxPosition(context.fileName, ctx) ) num.endsWith("b") -> IntegerLiteral( - ctx.text.dropLast(1).toByte(), + convertBinHexOctToPrimitives(ctx.text.dropLast(1), "b"), "b", posGetter.getCtxPosition(context.fileName, ctx) ) num.endsWith("us") -> UnsignedInt16Literal( - ctx.text.dropLast(2).toUShort(), + convertBinHexOctToPrimitives(ctx.text.dropLast(2), "ub").toString().toUShort(), "us", posGetter.getCtxPosition(context.fileName, ctx) ) num.endsWith("s") -> IntegerLiteral( - ctx.text.dropLast(1).toShort(), + convertBinHexOctToPrimitives(ctx.text.dropLast(1), "s"), "s", posGetter.getCtxPosition(context.fileName, ctx) ) num.endsWith("u") -> UnsignedInt32Literal( - ctx.text.dropLast(1).toUInt(), + convertBinHexOctToPrimitives(ctx.text.dropLast(1), "u").toString().toUInt(), "u", posGetter.getCtxPosition(context.fileName, ctx) ) num.endsWith("ul") -> UnsignedInt64Literal( - ctx.text.dropLast(2).toULong(), + convertBinHexOctToPrimitives(ctx.text.dropLast(2), "ul").toString().toULong(), "uL", posGetter.getCtxPosition(context.fileName, ctx) ) num.endsWith("l") -> { - num = num.dropLast(1) IntegerLiteral( - convertBinHexOctToPrimitives(num, "l"), + convertBinHexOctToPrimitives(num.dropLast(1), "l"), "L", posGetter.getCtxPosition(context.fileName, ctx) ) @@ -281,16 +284,65 @@ class ExpressionVisitor( } private fun convertBinHexOctToPrimitives(num: String, type: String): Number { - val isInt = "i".equals(type) + val isByte = "b" == type + val isUByte = "ub" == type + val isShort = "s" == type + val isUShort = "us" == type + val isInt = "i" == type + val isUInt = "u" == type + val isLong = "l" == type + val isULong = "ul" == type return when { - num.startsWith(HEX_PREFIX) -> if (isInt) parseInt(num.drop(2), 16) else parseLong(num.drop(2), 16) - num.startsWith(BIN_PREFIX) -> if (isInt) parseInt(num.drop(2), 2) else parseLong(num.drop(2), 2) - num.startsWith(OCT_PREFIX) && num.length > 1 -> if (isInt) parseInt(num.drop(1), 8) else parseLong( - num.drop( - 1 - ), 8 - ) - else -> if (isInt) num.toInt() else num.toLong() + num.startsWith(HEX_PREFIX) -> return when { + isByte -> parseByte(num.drop(2), 16) + // This is right idea of conversions ? return Uint and the call toUbyte + isUByte -> parseUnsignedInt(num.drop(2), 16) + isShort -> parseShort(num.drop(2), 16) + // This is right idea of conversions ? return Uint and the call toUshort + isUShort -> parseUnsignedInt(num.drop(2), 16) + isInt -> parseInt(num.drop(2), 16) + isUInt -> parseUnsignedInt(num.drop(2), 16) + isLong -> parseLong(num.drop(2), 16) + isULong -> parseUnsignedLong(num.drop(2), 16) + else -> error("unsupported type of hex integer") + } + num.startsWith(BIN_PREFIX) -> return when { + isByte -> parseByte(num.drop(2), 2) + // This is right idea of conversions ? return Uint and the call toUbyte + isUByte -> parseUnsignedInt(num.drop(2), 2) + isShort -> parseShort(num.drop(2), 2) + // This is right idea of conversions ? return Uint and the call toUshort + isUShort -> parseUnsignedInt(num.drop(2), 2) + isInt -> parseInt(num.drop(2), 2) + isUInt -> parseUnsignedInt(num.drop(2), 2) + isLong -> parseLong(num.drop(2), 2) + isULong -> parseUnsignedLong(num.drop(2), 2) + else -> error("unsupported type of binary integer") + } + num.startsWith(OCT_PREFIX) && num.length > 1 -> return when { + isByte -> parseByte(num.drop(1), 8) + // This is right idea of conversions ? return Uint and the call toUbyte + isUByte -> parseUnsignedInt(num.drop(1), 8) + isShort -> parseShort(num.drop(1), 8) + // This is right idea of conversions ? return Uint and the call toUshort + isUShort -> parseUnsignedInt(num.drop(1), 8) + isInt -> parseInt(num.drop(1), 8) + isUInt -> parseUnsignedInt(num.drop(1), 8) + isLong -> parseLong(num.drop(1), 8) + isULong -> parseUnsignedLong(num.drop(1), 8) + else -> error("unsupported type of octal integer") + } + else -> return when { + isByte -> num.toByte() + isUByte -> num.toInt() + isShort -> num.toShort() + isUShort -> num.toInt() + isInt -> num.toInt() + isUInt -> num.toLong() + isLong -> num.toLong() + isULong -> num.toBigDecimal() + else -> error("unsupported type of octal integer") + } } } diff --git a/src/test/testdata/expected/lsl/hexBinAndOctLiterals.lsl b/src/test/testdata/expected/lsl/hexBinAndOctLiterals.lsl index d2b2cb78..e02839ae 100644 --- a/src/test/testdata/expected/lsl/hexBinAndOctLiterals.lsl +++ b/src/test/testdata/expected/lsl/hexBinAndOctLiterals.lsl @@ -1,17 +1,50 @@ libsl "1.0.0"; library simple; -typealias int = int32; -automaton A : int { - var d1: int = 291; - var d2: int = 291; - var d3: int = 15; - var d4: int = 10; - var d5: int = 495; - var d6: int = 291L; - var d7: int = 291L; - var d8: int = 15L; - var d9: int = 10L; - var d10: int = 495L; - var d11: int = 495L; - var plainZero: int = 0; +typealias Int = int32; +typealias UInt = unsigned32; +typealias Byte = int8; +typealias UByte = unsigned8; +typealias Short = int16; +typealias UShort = unsigned16; +typealias Long = int64; +typealias ULong = unsigned64; +automaton A : Int { + var d1: Int = 291; + var d2: Int = 291; + var d3: Int = 15; + var d4: Int = 10; + var d5: Int = 495; + var d6: Long = 291L; + var d7: Long = 291L; + var d8: Long = 15L; + var d9: Long = 10L; + var d10: Long = 495L; + var d11: Long = 495L; + var plainZero: Int = 0; + var d12: UInt = 255u; + var d13: ULong = 23uL; + var d14: UShort = 23us; + var d15: UByte = 23ub; + var d16: Byte = 23b; + var d17: Short = 23s; + var d18: UInt = 63u; + var d19: ULong = 63uL; + var d20: UShort = 63us; + var d21: UByte = 63ub; + var d22: Byte = 63b; + var d23: Short = 63s; + var d24: UInt = 91u; + var d25: ULong = 91uL; + var d26: UShort = 91us; + var d27: UByte = 91ub; + var d28: Byte = 91b; + var d29: Short = 91s; + var d30: UInt = 4294967295u; + var d31: ULong = 18446744073709551615uL; + var d32: UShort = 65535us; + var d33: UByte = 255ub; + var d34: Byte = -128b; + var d35: Byte = 127b; + var d36: Short = -32768s; + var d37: Short = 32767s; } \ No newline at end of file diff --git a/src/test/testdata/lsl/hexBinAndOctLiterals.lsl b/src/test/testdata/lsl/hexBinAndOctLiterals.lsl index 90a0588f..7a7bd4d0 100644 --- a/src/test/testdata/lsl/hexBinAndOctLiterals.lsl +++ b/src/test/testdata/lsl/hexBinAndOctLiterals.lsl @@ -1,18 +1,56 @@ libsl "1.0.0"; library simple; -typealias int = int32; -automaton A : int { - var d1: int = 0X123; - var d2: int = 0x123; - var d3: int = 0B1111; - var d4: int = 0b1010; - var d5: int = 0757; +typealias Int = int32; +typealias UInt = unsigned32; +typealias Byte = int8; +typealias UByte = unsigned8; +typealias Short = int16; +typealias UShort = unsigned16; +typealias Long = int64; +typealias ULong = unsigned64; +automaton A : Int { + var d1: Int = 0X123; + var d2: Int = 0x123; + var d3: Int = 0B1111; + var d4: Int = 0b1010; + var d5: Int = 0757; - var d6: int = 0X123l; - var d7: int = 0x123L; - var d8: int = 0B1111l; - var d9: int = 0b1010L; - var d10: int = 0757l; - var d11: int = 0757L; - var plainZero: int = 0; + var d6: Long = 0X123l; + var d7: Long = 0x123L; + var d8: Long = 0B1111l; + var d9: Long = 0b1010L; + var d10: Long = 0757l; + var d11: Long = 0757L; + + var plainZero: Int = 0; + + var d12: UInt = 0xFFu; + var d13: ULong = 0x17uL; + var d14: UShort = 0x17us; + var d15: UByte = 0x17ub; + var d16: Byte = 0x17b; + var d17: Short = 0x17s; + + var d18: UInt = 0b111111u; + var d19: ULong = 0b111111uL; + var d20: UShort = 0b111111us; + var d21: UByte = 0b111111ub; + var d22: Byte = 0b111111b; + var d23: Short = 0b111111s; + + var d24: UInt = 0133u; + var d25: ULong = 0133uL; + var d26: UShort = 0133us; + var d27: UByte = 0133ub; + var d28: Byte = 0133b; + var d29: Short = 0133s; + + var d30: UInt = 4294967295u; + var d31: ULong = 18446744073709551615uL; + var d32: UShort = 65535us; + var d33: UByte = 255ub; + var d34: Byte = -128b; + var d35: Byte = 127b; + var d36: Short = -32768s; + var d37: Short = 32767s; } \ No newline at end of file