1
0
blueloveTH 1 жил өмнө
parent
commit
2cbda5d920

+ 0 - 2
include/pocketpy/common.h

@@ -11,11 +11,9 @@
 #include <memory>
 #include <iostream>
 #include <map>
-#include <set>
 #include <algorithm>
 #include <variant>
 #include <type_traits>
-#include <deque>
 #include <typeindex>
 #include <initializer_list>
 

+ 1 - 1
include/pocketpy/expr.h

@@ -97,7 +97,7 @@ struct CodeEmitContext{
     // so we use stack_no_copy instead
     stack_no_copy<Expr_> s_expr;
     int level;
-    std::set<Str> global_names;
+    vector<Str> global_names;
     CodeEmitContext(VM* vm, CodeObject_ co, int level): vm(vm), co(co), level(level) {}
 
     int curr_iblock = 0;

+ 1 - 1
include/pocketpy/profiler.h

@@ -24,7 +24,7 @@ struct LineProfiler{
     // filename -> records
     std::map<std::string_view, vector<_LineRecord>> records;
     stack_no_copy<_FrameRecord> frames;
-    std::set<FuncDecl*> functions;
+    vector<FuncDecl*> functions;
 
     void begin();
     void _step(int, Frame*);

+ 7 - 0
include/pocketpy/vector.h

@@ -155,6 +155,13 @@ struct vector{
         new(&_data[_size++]) T(std::move(t));
     }
 
+    bool contains(const T& t) const {
+        for(int i=0; i<_size; i++){
+            if(_data[i] == t) return true;
+        }
+        return false;
+    }
+
     template<typename... Args>
     void emplace_back(Args&&... args){
         if(_size == _capacity) reserve(_capacity * 2);

+ 1 - 1
include/pocketpy/vm.h

@@ -176,7 +176,7 @@ public:
     // typeid -> Type
     std::map<const std::type_index, Type> _cxx_typeid_map;
     // this is for repr() recursion detection (no need to mark)
-    std::set<PyVar> _repr_recursion_set;
+    vector<PyVar> _repr_recursion_set;
 
     ImportContext __import_context;
     PyObject* __last_exception;

+ 3 - 3
src/compiler.cpp

@@ -452,7 +452,7 @@ namespace pkpy{
     void Compiler::exprName(){
         Str name = prev().str();
         NameScope scope = name_scope();
-        if(ctx()->global_names.count(name)){
+        if(ctx()->global_names.contains(name)){
             scope = NAME_GLOBAL;
         }
         ctx()->s_expr.push(make_expr<NameExpr>(name, scope));
@@ -885,7 +885,7 @@ __EAT_DOTS_END:
                 consume(TK("@id"));
                 StrName name(prev().sv());
                 NameScope scope = name_scope();
-                bool is_global = ctx()->global_names.count(name.sv());
+                bool is_global = ctx()->global_names.contains(name.sv());
                 if(is_global) scope = NAME_GLOBAL;
                 switch(scope){
                     case NAME_LOCAL:
@@ -932,7 +932,7 @@ __EAT_DOTS_END:
             case TK("global"):
                 do {
                     consume(TK("@id"));
-                    ctx()->global_names.insert(prev().str());
+                    ctx()->global_names.push_back(prev().sv());
                 } while (match(TK(",")));
                 consume_end_stmt();
                 break;

+ 11 - 4
src/expr.cpp

@@ -450,15 +450,22 @@ namespace pkpy{
         }
     }
 
+    static bool is_fmt_valid_char(char c){
+        switch(c){
+            case '-': case '=': case '*': case '#': case '@': case '!': case '~':
+            case '<': case '>': case '^':
+            case '.': case 'f': case 'd': case 's':
+            case '0'...'9': return true;
+            default: return false;
+        }
+    }
+
     void FStringExpr::emit_(CodeEmitContext* ctx){
         int i = 0;              // left index
         int j = 0;              // right index
         int count = 0;          // how many string parts
         bool flag = false;      // true if we are in a expression
 
-        const char* fmt_valid_chars = "0-=*#@!~" "<>^" ".fds" "0123456789";
-        PK_LOCAL_STATIC const std::set<char> fmt_valid_char_set(fmt_valid_chars, fmt_valid_chars + strlen(fmt_valid_chars));
-
         while(j < src.size){
             if(flag){
                 if(src[j] == '}'){
@@ -470,7 +477,7 @@ namespace pkpy{
                         Str spec = expr.substr(conon+1);
                         // filter some invalid spec
                         bool ok = true;
-                        for(char c: spec) if(!fmt_valid_char_set.count(c)){ ok = false; break; }
+                        for(char c: spec) if(!is_fmt_valid_char(c)){ ok = false; break; }
                         if(ok){
                             _load_simple_expr(ctx, expr.substr(0, conon));
                             ctx->emit_(OP_FORMAT_STRING, ctx->add_const_string(spec.sv()), line);

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
src/lexer.cpp


+ 1 - 1
src/modules.cpp

@@ -294,7 +294,7 @@ struct LineProfilerW{
             LineProfilerW& self = PK_OBJ_GET(LineProfilerW, args[0]);
             vm->check_type(args[1], VM::tp_function);
             auto decl = PK_OBJ_GET(Function, args[1]).decl.get();
-            self.profiler.functions.insert(decl);
+            self.profiler.functions.push_back(decl);
             return vm->None;
         });
 

+ 18 - 13
src/pocketpy.cpp

@@ -312,16 +312,21 @@ void __init_builtins(VM* _vm) {
     });
 
     _vm->bind_func(_vm->builtins, "dir", 1, [](VM* vm, ArgsView args) {
-        std::set<StrName> names;
+        vector<StrName> names;
         if(!is_tagged(args[0]) && args[0]->is_attr_valid()){
             auto keys = args[0]->attr().keys();
-            names.insert(keys.begin(), keys.end());
+            names.extend(keys.begin(), keys.end());
         }
         const NameDict& t_attr = vm->_t(args[0])->attr();
         auto keys = t_attr.keys();
-        names.insert(keys.begin(), keys.end());
+        names.extend(keys.begin(), keys.end());
+        std::sort(names.begin(), names.end());
         List ret;
-        for (StrName name : names) ret.push_back(VAR(name.sv()));
+        for(int i=0; i<names.size(); i++){
+            // remove duplicates
+            if(i>0 && names[i] == names[i-1]) continue;
+            ret.push_back(VAR(names[i].sv()));
+        }
         return VAR(std::move(ret));
     });
 
@@ -803,16 +808,16 @@ void __init_builtins(VM* _vm) {
     });
 
     _vm->bind__repr__(VM::tp_list, [](VM* vm, PyVar _0) -> Str{
-        if(vm->_repr_recursion_set.count(_0)) return "[...]";
+        if(vm->_repr_recursion_set.contains(_0)) return "[...]";
         List& iterable = _CAST(List&, _0);
         SStream ss;
         ss << '[';
-        vm->_repr_recursion_set.insert(_0);
+        vm->_repr_recursion_set.push_back(_0);
         for(int i=0; i<iterable.size(); i++){
             ss << vm->py_repr(iterable[i]);
             if(i != iterable.size()-1) ss << ", ";
         }
-        vm->_repr_recursion_set.erase(_0);
+        vm->_repr_recursion_set.pop_back();
         ss << ']';
         return ss.str();
     });
@@ -1270,19 +1275,19 @@ void __init_builtins(VM* _vm) {
     });
 
     _vm->bind__repr__(VM::tp_mappingproxy, [](VM* vm, PyVar _0) -> Str{
-        if(vm->_repr_recursion_set.count(_0)) return "{...}";
+        if(vm->_repr_recursion_set.contains(_0)) return "{...}";
         MappingProxy& self = _CAST(MappingProxy&, _0);
         SStream ss;
         ss << "mappingproxy({";
         bool first = true;
-        vm->_repr_recursion_set.insert(_0);
+        vm->_repr_recursion_set.push_back(_0);
         for(auto [k, v] : self.attr().items()){
             if(!first) ss << ", ";
             first = false;
             ss << k.escape() << ": ";
             ss << vm->py_repr(v);
         }
-        vm->_repr_recursion_set.erase(_0);
+        vm->_repr_recursion_set.pop_back();
         ss << "})";
         return ss.str();
     });
@@ -1430,18 +1435,18 @@ void __init_builtins(VM* _vm) {
     });
 
     _vm->bind__repr__(VM::tp_dict, [](VM* vm, PyVar _0) -> Str{
-        if(vm->_repr_recursion_set.count(_0)) return "{...}";
+        if(vm->_repr_recursion_set.contains(_0)) return "{...}";
         Dict& self = _CAST(Dict&, _0);
         SStream ss;
         ss << "{";
         bool first = true;
-        vm->_repr_recursion_set.insert(_0);
+        vm->_repr_recursion_set.push_back(_0);
         self.apply([&](PyVar k, PyVar v){
             if(!first) ss << ", ";
             first = false;
             ss << vm->py_repr(k) << ": " << vm->py_repr(v);
         });
-        vm->_repr_recursion_set.erase(_0);
+        vm->_repr_recursion_set.pop_back();
         ss << "}";
         return ss.str();
     });

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно