blueloveTH 2 tahun lalu
induk
melakukan
b28ea2c35b
2 mengubah file dengan 30 tambahan dan 16 penghapusan
  1. 6 6
      include/pocketpy/common.h
  2. 24 10
      src/expr.cpp

+ 6 - 6
include/pocketpy/common.h

@@ -150,12 +150,12 @@ union BitsCvtImpl<8>{
 	BitsCvtImpl(NumberTraits<8>::float_t val): _float(val) {}
 	BitsCvtImpl(NumberTraits<8>::int_t val): _int(val) {}
 
-	void print(){
-		std::string s = std::bitset<64>(_int).to_string();
-		std::cout << s.substr(0, 1) << '|';
-		std::cout << s.substr(1, 11) << '|';
-		std::cout << s.substr(12) << std::endl;
-	}
+	// void print(){
+	// 	std::string s = std::bitset<64>(_int).to_string();
+	// 	std::cout << s.substr(0, 1) << '|';
+	// 	std::cout << s.substr(1, 11) << '|';
+	// 	std::cout << s.substr(12) << std::endl;
+	// }
 };
 
 using BitsCvt = BitsCvtImpl<sizeof(void*)>;

+ 24 - 10
src/expr.cpp

@@ -6,6 +6,13 @@ namespace pkpy{
         return v >= INT16_MIN && v <= INT16_MAX;
     }
 
+    inline bool is_identifier(std::string_view s){
+        if(s.empty()) return false;
+        if(!isalpha(s[0]) && s[0] != '_') return false;
+        for(char c: s) if(!isalnum(c) && c != '_') return false;
+        return true;
+    }
+
     int CodeEmitContext::get_loop() const {
         int index = curr_block_i;
         while(index >= 0){
@@ -402,21 +409,28 @@ namespace pkpy{
             }
         }
         // name or name.name
-        PK_LOCAL_STATIC const std::regex pattern(R"(^[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*){0,1}$)");
-        if(std::regex_match(expr.str(), pattern)){
+        bool is_fastpath = false;
+        if(is_identifier(expr.sv())){
+            ctx->emit_(OP_LOAD_NAME, StrName(expr.sv()).index, line);
+            is_fastpath = true;
+        }else{
             int dot = expr.index(".");
-            if(dot < 0){
-                ctx->emit_(OP_LOAD_NAME, StrName(expr.sv()).index, line);
-            }else{
-                StrName name(expr.substr(0, dot).sv());
-                StrName attr(expr.substr(dot+1).sv());
-                ctx->emit_(OP_LOAD_NAME, name.index, line);
-                ctx->emit_(OP_LOAD_ATTR, attr.index, line);
+            if(dot > 0){
+                std::string_view a = expr.sv().substr(0, dot);
+                std::string_view b = expr.sv().substr(dot+1);
+                if(is_identifier(a) && is_identifier(b)){
+                    ctx->emit_(OP_LOAD_NAME, StrName(a).index, line);
+                    ctx->emit_(OP_LOAD_ATTR, StrName(b).index, line);
+                    is_fastpath = true;
+                }
             }
-        }else{
+        }
+        
+        if(!is_fastpath){
             int index = ctx->add_const_string(expr.sv());
             ctx->emit_(OP_FSTRING_EVAL, index, line);
         }
+
         if(repr){
             ctx->emit_(OP_REPR, BC_NOARG, line);
         }