Skip to content
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

scanner: add arbitrary-base number literals #154

Open
DeedleFake opened this issue Nov 7, 2018 · 1 comment
Open

scanner: add arbitrary-base number literals #154

DeedleFake opened this issue Nov 7, 2018 · 1 comment

Comments

@DeedleFake
Copy link
Owner

DeedleFake commented Nov 7, 2018

golang/go#28256 got closed, sadly, but that made me think about possibly implementing something like that in WDTE instead. I'd like to do it slightly differently than it was proposed in there, however.

Currently, WDTE has no support for any base other than decimal. The idea is to allow an optional prefix to a number in the form <digit>x, such as 8x. The digit given would indicated the highest digit of the base being used, so 8x would be octal, 2x would be binary, and Fx would be hex. It's likely that it'll be required to be uppercase for digits above 9.

In addition to this, underscores would be allowed to be inserted in number literals, similar to golang/go#28493, allowing the user to break up, among other things, long binary literals. A number would be required to start with at least one digit, so Fx3_2 would be legal, but Fx_32 would not.

Also, non-decimal bases will probably not be allowed for non-integers, but I'm not entirely sure yet.

@fdwr
Copy link

fdwr commented Feb 6, 2020

For some contrast to existing languages that support arbitrary radix literal prefixes...

ALGOL 68 specifies a radix with 0r (supported a half century ago o_o):

16r2d7 // hex
8r1327 // octal
2r1011010111 // binary

https://rosettacode.org/wiki/Literals/Integer#ALGOL_68

Ada/ERLANG/Bash used '#' instead:

16#2d7 // hex
8#1327 // octal
2#1011010111 // binary

https://rosettacode.org/wiki/Literals/Integer#Ada

For my own language, I ultimately decided on 0r for an arbitrary radix (in addition to the typical well known prefixes b, o ,x used across many languages), because it's easy to remember, no potential confusion with hex literal 0x (seems weird that x can function as an arbitrary base except when 0 which actually means 16, so 0x123 and 16x123 mean the same thing o_O), and because I was already using '#' for other purposes.

Also, octal must be explicit 0o, no leading zero faux pas that C did where 042.0 means 42 but 042 means 66 -_-. Some other languages have realized this mistake and fixed it (https://www.python.org/dev/peps/pep-3127/#removal-of-old-octal-syntax, https://florianjw.de/en/octal_zero_considered_harmfull.html), while others blindly propagated it forward :( (golang/go#151).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants