Numeric Literals (Summary 5)

[email protected] (Michael Lazzaro) Mon, 25 Nov 2002 10:06:13 -0800
Newsgroups perl.perl6.documentation
Message-ID <[email protected]>
With clarifications, and additional syntactic edge cases.

The last remaining known "numeric literals" issue is whether we want to 
allow '.' in explicit radix, e.g. 10#1.234, or whether we want to 
disallow it as being Way Too Creepy.  This version assumes that '.' is 
allowed, but exponential notation is _not_, due to ambiguities with 'E'.


--- Numeric Literals ---

decimal notation:
      123       # int 123
     0123       # int 123   (not octal!)
     123.0      # num 123.0
     -123       # int -123  (but - is operator, not part of num)

    0_1.2_3     # ok
    _01.23      # WRONG
     01.23_     # WRONG
     01_._23    # WRONG
    1__2        # WRONG

    0.1         # ok
    0.1.1       # WRONG, can have only one decimal point
    .1          # WRONG; looks like method call on $_
    -.1         # WRONG, looks like method call on $_

exponential notation:
    -1.23e4     # num
    -1.23E4     # num (identical)
    1.23_e_4    # WRONG (underscore only between digits)

    1e4         # ok
    0e4         # ok, == 0
    0e0         # ok, == 0
    1e0         # ok, == 1
     e4         # WRONG, looks like func
    .e4         # WRONG, looks like method

bin/oct/hex notation:

    0d6789      # decimal
    0b0110      # bin
    0o0123      # oct
    0x00ff      # hex
    0x00fF      # hex, == 0x00ff
    0x00FF      # hex, == 0x00ff

    -0xff       # ok
    -0x00ff     # ok
    0x-ff       # WRONG, - is an operator, not part of the literal

    0xf_f       # ok
    0x_ff       # WRONG, underscore only between digits

    0x0         # ok, == 0
    0x0.0       # ok
    0x.0        # ok
    0d1.1E5     # WRONG, exp not allowed in shorthand radix

    0B0110      # WRONG, should be 0b0110
    0O0123      # WRONG, should be 0o0123
    0X00FF      # WRONG, should be 0x00FF

explicit radix:

    (radix 2-36)

    20#1gj       # base 20
    20#1GJ       # base 20 (identical)
    20#1:G:J     # base 20 (identical)
    20#1_G_J     # base 20 (identical)
    20#1:16:19   # base 20 (identical)
    20#1_16_19   # NOT identical, == 20:11619
    20#1:1_6:19  # WRONG: colon form may not have underlines

    20#0:0:1     # base 20
    20#0:1       # base 20 (identical)
    20#:1        # base 20 (identical) (leading : is ok)
    :1           # WRONG, need radix specifier
    20#          # WRONG, need at least an '0'
    1#0          # WRONG, radix must be >= 2

    20#1_G.J     # ok, radix point (e.g. float) allowed
    20#1:16.19   # ok, radix point (e.g. float) allowed
    10#1.1E5     # WRONG, no exp notation in explicit radix

    -20#1GJ      # base 20 (negative)
    -20#1:16:19  # base 20 (negative)
     20#-1GJ     # WRONG, - is an operator, not part of the literal

   (radix 37-RADIX_MAX)

   256#0:253:254:255   # base 256
   256#:253:254:255    # base 256 (identical)
   256#0_253_254_255   # WRONG, digit 253254255 doesn't
                       # exist in base 256.


Other issues w/ literals:

- radix <= 36, alpha digits may be upper or lowercase
- radix > 36, only colon form is allowed, not alpha digits
- underlines may appear ONLY between digits
- colon form may therefore not have underlines
- radix < 2 throws error
- negative sign goes before radix, e.g. -20#1GJ.
- need to specify RADIX_MAX (platform dependent?)
- explicit radix form may have radix point, '.',
     but cannot use exponential notation ('e')

- can't have runtime radix, e.g. 2**8#10, because # binds tighter.
- can't say (2**8)#10, because not a literal.

MikeL