Parcourir la source

Added binary literal support

Steve Tautonico il y a 2 ans
Parent
commit
badc8d44d1
2 fichiers modifiés avec 8 ajouts et 3 suppressions
  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() {
     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;
         std::smatch m;
 
 
         const char* i = token_start;
         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){
         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{
         try{
             int base = 10;
             int base = 10;
             size_t size;
             size_t size;
             if (m[1].matched) {
             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;
                 else base = 16;
             }
             }
             if (m[2].matched) {
             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 0o1234 == 668
 assert 0o17777777777 == 2147483647
 assert 0o17777777777 == 2147483647
 
 
+# test binary literals
+assert 0b10010 == 18
+assert 0b11111111111111111111111111111111 == 4294967295
+
 # test == != >= <= < >
 # test == != >= <= < >
 assert -1 == -1
 assert -1 == -1
 assert -1 != 1
 assert -1 != 1