-
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: master
Are you sure you want to change the base?
Conversation
| 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
| // NOTE: It seems like these cannot really be defined earlier... Oh well. | ||
|
|
||
| def show(b: Byte): String = b.toHexCharBytes.toString | ||
| def println(b: Byte): Unit = println(show(b)) |
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.
Only tested locally, needs a proper test.
| def toHexCharBytes(b: Byte): Unit / emit[Byte] = { | ||
| val unsigned: Int = b.toInt.bitwiseAnd(0xFF.toInt) | ||
|
|
||
| val high = unsigned.bitwiseShr(4).bitwiseAnd(0xFF.toInt) | ||
| val low = unsigned.bitwiseAnd(0x0F.toInt) | ||
| println(b.toInt.show ++ " (U" ++ unsigned.show ++ ") := " ++ high.show ++ "; " ++ low.show) | ||
|
|
||
| def toHexDigit(n: Int): Byte = | ||
| if (n < 10) { | ||
| (n + 48) // '0' = 48 | ||
| } else { | ||
| (n - 10 + 65) // 'A' = 65 | ||
| }.toByte | ||
|
|
||
| do emit(high.toHexDigit) | ||
| do emit(low.toHexDigit) | ||
| } |
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.
The show instance for Bytes is a bit weird since we're trying to write it in Effekt directly.
In order to do anything at all, we need to either convert to characters or use bytearrays -- I've chosen to do the latter here, but as a consequence, the println and show for Byte are defined awkwardly in bytearray...
show(b: Byte) := bytearray::collect { b.toHexCharBytes }
| js "'' + ${value}" | ||
| chez "(string ${value})" | ||
| llvm """ | ||
| %z = call %Pos @c_bytearray_show_Byte(i8 ${value}) |
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.
TODO: now we should be able to deprecate c_bytearray_show_Byte 😌
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.
Maybe I'll revert the custom show instance defined in Effekt directly and provide them via FFI instead.
Then we don't have to change the bytearray module.
b959d97 to
5f45e95
Compare
Originally by @PhictionalOne, started in #1148 (was on a repo fork).
Resolves #814