wdte icon indicating copy to clipboard operation
wdte copied to clipboard

scanner: add arbitrary-base number literals

Open DeedleFake opened this issue 7 years ago • 1 comments

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.

DeedleFake avatar Nov 07 '18 23:11 DeedleFake

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 :( (https://github.com/golang/go/issues/151).

fdwr avatar Feb 06 '20 05:02 fdwr