Search code examples
adaliterals

Ada Numeric Literals and Underline


This is from the online Ada reference manual: http://www.adaic.org/resources/add_content/standards/05rm/RM.pdf (section 2.3)

A decimal_literal is a numeric_literal in the conventional decimal notation (that is, the base is ten). Syntax decimal_literal ::= numeral [.numeral] [exponent] numeral ::= digit {[underline] digit} exponent ::= E [+] numeral | E – numeral digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 An exponent for an integer literal shall not have a minus sign. Static Semantics An underline character in a numeric_literal does not affect its meaning. The letter E of an exponent can be written either in lower case or in upper case, with the same meaning.

If I do

my_literal ::= 123_456;

what does the underscore (underline) mean? It says it doesn't affect the meaning. Then what is it for? I am sure there is a simple answer but reading and re-reaidng the passage hasn't helped me.


Solution

  • It's the same reason for, say, commas (,) in currency or [other large] numbers: grouping. Thus:

    Million : Constant:= 1_000_000;
    

    Furthermore, you could use it in conjunction with base-setting as a set-up for masking:

    Type Bit is Range 1..8;
    SubType Byte is Interfaces.Unsigned_8;
    Type Masks is Array(Positive Range <>) of Byte;
    
    Mask_Map : Constant Masks(Bit):=
        (
            2#0000_0001#,
            2#0000_0010#,
            2#0000_0100#,
            2#0000_1000#,
            2#0001_0000#,
            2#0010_0000#,
            2#0100_0000#,
            2#1000_0000#
        );
    

    Then perhaps you would use Mask_Map and bits together with or, and, and xor to do bit-manipulation. The above method may seem a bit more work than the simple definition of a lot of constants and directly manipulating them, but it is more flexible in that you can later change it into a function and not have to change any client-code, that could further be useful if that function's result was a parametrized integer, where bit has the definition 1..PARAMETER'Size.