Ver Fonte

Added binary literal support

Steve Tautonico há 2 anos atrás
pai
commit
badc8d44d1
2 ficheiros alterados com 8 adições e 3 exclusões
  1. 4 3
      src/lexer.cpp
  2. 4 0
      tests/01_int.py

+ 4 - 3
src/lexer.cpp

@@ -260,7 +260,7 @@ static bool is_unicode_Lo_char(uint32_t c) {
     }
 
     void Lexer::eat_number() {
-        PK_LOCAL_STATIC const std::regex pattern("^(0[xo])?[0-9a-fA-F]+(\\.[0-9]+)?(L)?");
+        PK_LOCAL_STATIC const std::regex pattern("^(0[xob])?[0-9a-fA-F]+(\\.[0-9]+)?(L)?");
         std::smatch m;
 
         const char* i = token_start;
@@ -278,14 +278,15 @@ static bool is_unicode_Lo_char(uint32_t c) {
         }
 
         if(m[1].matched && m[2].matched){
-            SyntaxError("hex/octal literal should not contain a dot");
+            SyntaxError("binary/hex/octal literal should not contain a dot");
         }
 
         try{
             int base = 10;
             size_t size;
             if (m[1].matched) {
-                if (m[1].str() == "0o") base=8;
+                if (m[1].str() == "0b") base = 2;
+                else if (m[1].str() == "0o") base = 8;
                 else base = 16;
             }
             if (m[2].matched) {

+ 4 - 0
tests/01_int.py

@@ -9,6 +9,10 @@ assert 2**60-1 + 546 - 0xfffffffffffff == 1148417904979477026
 assert 0o1234 == 668
 assert 0o17777777777 == 2147483647
 
+# test binary literals
+assert 0b10010 == 18
+assert 0b11111111111111111111111111111111 == 4294967295
+
 # test == != >= <= < >
 assert -1 == -1
 assert -1 != 1