cx-language / cx

C* is a hybrid low-level/high-level systems programming language focused on performance and productivity.
https://cx-language.github.io/
MIT License
130 stars 9 forks source link

Parsing numbers #59

Open PavelVozenilek opened 4 years ago

PavelVozenilek commented 4 years ago

Looking on parsing numbers in lex.cpp, I have few notes:

  1. Octal numbers are relic of the ancient past. I think it was a PDP computer which used them regularly last time.

    Walter Bright, author of the D language recently talked, how having them in his language was mistake (here at 12:14 https://youtu.be/p22MM1wc7xQ?t=734 ). D allows leading zeros, making things more bug prone.

  2. It would improve readability, if it was possible to insert a visual separator between the numerals, e.g. underscore _: 132_780, 0xFF60_88A1_73E9_6620, 0b0110_1110

    The separator could be single quote ', as they do it in India; or minus -, if math operators require space before and after. (This would also allow to use - in names, making them more readable.)

  3. Binary and hex numbers should always have size 1, 2, 4, or 8 bytes (filled with leading zeros, if needed). People are not used to read/write them, it is much easier to make a mistake here. Possible separator inside these numbers should preferably be also on these boundaries. If binary/hex is assigned to a known sized integer, the whole sized number should be written.

  4. A bug. This code accepts integer value out of its range (has to be negative, overflow of positives is checked):

    void main() {
        int8 a = -0xFFFF;
        println(a); 
    }
    
  5. Scientific float notation could be supported.

  6. Hex notation for floats could be supported, to make precise values and NaNs.

  7. In lex.ccp, function Lexer::readNumber(), I was surprised to see the name end to be used as both a local identifier and as a label. It is a bit confusing to see such a (valid by standard) trick.