Skip to content

Add support for literal hexadecimal, binary and octal numbers #69

@perlun

Description

@perlun

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 lowercase 0x here or be relaxed and support 0X 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 (or 0B) 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 to 493 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 uses 0o42 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:

  • more consistent with other prefixes (0x and 0b)
  • less confusing to beginners
  • mildly annoying to people who would have preferred plain 0755 notation. They can still write Unix file permissions in a natural way, they just have to do some very minor learning.

Suggested syntax

We can either implement all of these in a single PR, or do a separate PR for each notation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions