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.
It would be nice to have bitwise operators on Bytes
There was a problem hiding this comment.
... 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.
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.
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.
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.
I would prefer toUnsignedInt here instead of the newtype wrapping, but ok.
There was a problem hiding this comment.
The biggest issue I have with toUnsignedInt is that you need to remember to use that whenever you see a Byte, which felt like a footgun (that I stumbled into like three times when making this PR in the first place).
The alternative is weird since we can't match on UByte as a type, so it's all quite unsatisfactory :/
| 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) |
|
One more thing that would sometimes be useful is byte character literals (without the need to convert them). |
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.