-
Notifications
You must be signed in to change notification settings - Fork 40
Add hexadecimal literals for 'Byte' #1170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
libraries/common/bytearray.effekt
Outdated
| val unsigned: Int = b.toInt.bitwiseAnd(0xFF.toInt) | ||
|
|
||
| val high = unsigned.bitwiseShr(4).bitwiseAnd(0xFF.toInt) | ||
| val low = unsigned.bitwiseAnd(0x0F.toInt) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have bitwise operators on Bytes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... and equality and stuff :)
b959d97 to
5f45e95
Compare
3cf2587 to
29e26d6
Compare
| case Literal((), _) => "()" | ||
| case Literal(n, Type.TInt) => n.toString | ||
| case Literal(n, Type.TChar) => s"'\\${n.toString}'" | ||
| case Literal(b: Byte, Type.TByte) => UByte.unsafeFromByte(b).toHexString |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be a bit confusing, but we can't match on a UByte as it's opaque, so we have to match on a Byte, just to zero-cost convert it to a Byte, just to print the hexString consistently.
... I really don't know how to do this better without either wrapping it in a custom type and dealing with the overhead, or pulling a newtype library :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ok. You could have toHexString operate directly on Byte and use toUnsignedInt everywhere.
| case Literal((), _) => "()" | ||
| case Literal(n, Type.TInt) => n.toString | ||
| case Literal(n, Type.TChar) => s"'\\${n.toString}'" | ||
| case Literal(b: Byte, Type.TByte) => UByte.unsafeFromByte(b).toHexString |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ok. You could have toHexString operate directly on Byte and use toUnsignedInt everywhere.
| val b = UByte.unsafeFromByte(value) | ||
| // TODO: Literal bytes could now also be just byte-sized in Machine (= use 'UByte'; they are byte-sized in LLVM anyway). | ||
| LiteralByte(unboxed, b.toInt, Coerce(variable, unboxed, k(variable))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer toUnsignedInt here instead of the newtype wrapping, but ok.
| do emit(0x28) | ||
| repeat(n) { | ||
| do emit(40.toByte) | ||
| do emit(41.toByte) | ||
| do emit(0x28) | ||
| do emit(0x29) | ||
| } | ||
| emitTree(n - 1) | ||
| do emit(41.toByte) | ||
| do emit(0x29) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful!
| char str[4]; // Byte values range from 0 to 255, 3 characters + null terminator | ||
| snprintf(str, sizeof(str), "%" PRIu8, n); | ||
| char str[5]; // |"0x" ++ <2 hex digits> ++ '\0'| = 5 | ||
| snprintf(str, sizeof str, "0x%02" PRIX8, (uint8_t)n); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't n already be of type uint8_t? We have typedef uint8_t Byte;. Or is this best practice in C?
Originally by @PhictionalOne, started in #1148 (was on a repo fork).
Resolves #814 by adding hexadecimal literals for bytes:
0xAA,0x00,0xFF,0xA0, etc, exclusively of typeByte.