|
|
@@ -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);
|
|
|
}
|