diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index 74b46350..7bd5665c 100644 --- a/include/pocketpy/common.h +++ b/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; diff --git a/src/expr.cpp b/src/expr.cpp index 9bd4174a..1e2f4113 100644 --- a/src/expr.cpp +++ b/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)){ - 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); - } + 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){ + 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; + } + } + } + + 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); }