Add support for literal hexadecimal, binary and octal numbers #69
Labels
enhancement
New feature or request
good first issue
Good for newcomers
help wanted
Extra attention is needed
Milestone
Goals
It should be possible to specify literal hexadecimal, binary and octal integer and long values in Perlang.
Non-goals
Supporting mechanisms like incbin in NASM is out of scope for this issue.
Implementation details
Hexadecimal integers are simple, since there is a uniform notation for them supported by most languages:
0x42
=> literal hexadecimal 42, i.e. the decimal value 66. (We can either be strict and enforce a lowercase0x
here or be relaxed and support0X
prefixes as well; Ruby as C# seem to support both.)Binary numbers are not supported by all languages, but C# seems to support the
0b
(or0B
) prefix for this. We could do the same.Octal literals are interestingly enough not supported in C#. Many other languages like Ruby, Python and good-old C supports octal notation using a single
0
prefix -0755
is treated as an octal number, equivalent to493
in decimal notation.However, the "plain-zero" prefix is problematic in that it is too ambiguous; it's not obvious enough to any new developer unfamiliar with your language from beforehand that
042
is in fact, not the literal number 42 but instead 34. Perhaps to avoid this, the Rust language chose a different approach 1 2 - it uses0o42
for octal notation. (Later edit: I discovered that this syntax is supported in Ruby as well.)While this might look a bit "odd" at first (for people used to simple
0
-prefixing octal numbers), it does make quite a bit of sense in being:0x
and0b
)0755
notation. They can still write Unix file permissions in a natural way, they just have to do some very minor learning.Suggested syntax
0x42
- literal hexadecimal 42 (decimal 66). Supported as of (parser) Support hexadecimal number literals #217.0b10011001
- literal binary (decimal 153). Supported as of (parser) Add support for binary literals #219.0o755
- literal octal (decimal 493). Supported as of (parser) Support octal literals #220.We can either implement all of these in a single PR, or do a separate PR for each notation.
The text was updated successfully, but these errors were encountered: