blueloveTH 1 rok temu
rodzic
commit
99457e1797
3 zmienionych plików z 20 dodań i 47 usunięć
  1. 15 42
      include/pocketpy/vm.h
  2. 3 3
      src/random.cpp
  3. 2 2
      src/vm.cpp

+ 15 - 42
include/pocketpy/vm.h

@@ -180,7 +180,6 @@ public:
     static constexpr Type tp_list=6, tp_tuple=7;
     static constexpr Type tp_slice=8, tp_range=9, tp_module=10;
     static constexpr Type tp_function=11, tp_native_func=12, tp_bound_method=13;
-    
     static constexpr Type tp_super=14, tp_exception=15, tp_bytes=16, tp_mappingproxy=17;
     static constexpr Type tp_dict=18, tp_property=19, tp_star_wrapper=20;
     static constexpr Type tp_staticmethod=21, tp_classmethod=22;
@@ -213,7 +212,7 @@ public:
 
     /********** utils **********/
     PyObject* new_module(Str name, Str package="");
-    ArgsView _cast_array_view(PyObject* obj);
+    ArgsView cast_array_view(PyObject* obj);
     void set_main_argv(int argc, char** argv);
     i64 normalized_index(i64 index, int size);
     Str disassemble(CodeObject_ co);
@@ -244,16 +243,14 @@ public:
 
     template<typename... Args>
     PyObject* call(PyObject* callable, Args&&... args){
-        PUSH(callable);
-        PUSH(PY_NULL);
+        PUSH(callable); PUSH(PY_NULL);
         __push_varargs(args...);
         return vectorcall(sizeof...(args));
     }
 
     template<typename... Args>
     PyObject* call_method(PyObject* self, PyObject* callable, Args&&... args){
-        PUSH(callable);
-        PUSH(self);
+        PUSH(callable); PUSH(self);
         __push_varargs(args...);
         return vectorcall(sizeof...(args));
     }
@@ -335,6 +332,7 @@ public:
     void IOError(const Str& msg) { __builtin_error("IOError", msg); }
     void NotImplementedError(){ __builtin_error("NotImplementedError"); }
     void TypeError(const Str& msg){ __builtin_error("TypeError", msg); }
+    void TypeError(Type expected, Type actual) { TypeError("expected " + _type_name(vm, expected).escape() + ", got " + _type_name(vm, actual).escape()); }
     void IndexError(const Str& msg){ __builtin_error("IndexError", msg); }
     void ValueError(const Str& msg){ __builtin_error("ValueError", msg); }
     void RuntimeError(const Str& msg){ __builtin_error("RuntimeError", msg); }
@@ -346,42 +344,27 @@ public:
     void ImportError(const Str& msg){ __builtin_error("ImportError", msg); }
     void AssertionError(const Str& msg){ __builtin_error("AssertionError", msg); }
     void AssertionError(){ __builtin_error("AssertionError"); }
-
     void BinaryOptError(const char* op, PyObject* _0, PyObject* _1);
     void AttributeError(PyObject* obj, StrName name);
     void AttributeError(const Str& msg){ __builtin_error("AttributeError", msg); }
 
     /********** type **********/
     PyObject* new_type_object(PyObject* mod, StrName name, Type base, bool subclass_enabled=true);
-    
     const PyTypeInfo* _inst_type_info(PyObject* obj);
     bool isinstance(PyObject* obj, Type base);
     bool issubclass(Type cls, Type base);
-
-    void check_type(PyObject* obj, Type type){
-        if(is_type(obj, type)) return;
-        TypeError("expected " + _type_name(vm, type).escape() + ", got " + _type_name(vm, _tp(obj)).escape());
-    }
-
-    void check_compatible_type(PyObject* obj, Type type){
-        if(isinstance(obj, type)) return;
-        TypeError("expected " + _type_name(vm, type).escape() + ", got " + _type_name(vm, _tp(obj)).escape());
-    }
-
-    PyObject* _t(Type t){
-        return _all_types[t.index].obj;
-    }
-
-    Type _tp(PyObject* obj){
-        if(!is_tagged(obj)) return obj->type;
-        return tp_int;
-    }
-
-    PyObject* _t(PyObject* obj){
-        return _all_types[_tp(obj).index].obj;
-    }
+    void check_type(PyObject* obj, Type type){ if(!is_type(obj, type)) TypeError(type, _tp(obj)); }
+    void check_compatible_type(PyObject* obj, Type type){ if(!isinstance(obj, type)) TypeError(type, _tp(obj)); }
+    PyObject* _t(PyObject* obj){ return _all_types[_tp(obj)].obj; }
+    PyObject* _t(Type t){ return _all_types[t.index].obj; }
+    Type _tp(PyObject* obj){ return is_small_int(obj) ? tp_int : obj->type; }
 
     /********** user type **********/
+    template<typename T>
+    Type _tp_user(){ return _find_type_in_cxx_typeid_map<T>(); }
+    template<typename T>
+    bool is_user_type(PyObject* obj){ return _tp(obj) == _tp_user<T>(); }
+
     template<typename T>
     PyObject* register_user_class(PyObject* mod, StrName name, bool subclass_enabled=false){
         PyObject* type = new_type_object(mod, name, 0, subclass_enabled);
@@ -396,16 +379,6 @@ public:
         return heap.gcnew<T>(_tp_user<T>(), std::forward<Args>(args)...);
     }
 
-    template<typename T>
-    Type _tp_user(){
-        return _find_type_in_cxx_typeid_map<T>();
-    }
-
-    template<typename T>
-    bool is_user_type(PyObject* obj){
-        return _tp(obj) == _tp_user<T>();
-    }
-
     template<typename T>
     Type _find_type_in_cxx_typeid_map(){
         auto it = _cxx_typeid_map.find(typeid(T));
@@ -421,9 +394,9 @@ public:
         return it->second;
     }
 
+    /********** private **********/
     virtual ~VM();
 
-    /********** private **********/
 #if PK_DEBUG_CEVAL_STEP
     void __log_s_data(const char* title = nullptr);
 #endif

+ 3 - 3
src/random.cpp

@@ -178,7 +178,7 @@ struct Random{
 
         vm->bind_method<1>(type, "choice", [](VM* vm, ArgsView args) {
             Random& self = PK_OBJ_GET(Random, args[0]);
-            ArgsView view = vm->_cast_array_view(args[1]);
+            ArgsView view = vm->cast_array_view(args[1]);
             if(view.empty()) vm->IndexError("cannot choose from an empty sequence");
             int index = self.gen.randint(0, view.size()-1);
             return view[index];
@@ -186,7 +186,7 @@ struct Random{
 
         vm->bind(type, "choices(self, population, weights=None, k=1)", [](VM* vm, ArgsView args) {
             Random& self = PK_OBJ_GET(Random, args[0]);
-            ArgsView view = vm->_cast_array_view(args[1]);
+            ArgsView view = vm->cast_array_view(args[1]);
             PyObject** data = view.begin();
             int size = view.size();
             if(size == 0) vm->IndexError("cannot choose from an empty sequence");
@@ -194,7 +194,7 @@ struct Random{
             if(args[2] == vm->None){
                 for(int i = 0; i < size; i++) cum_weights[i] = i + 1;
             }else{
-                ArgsView weights = vm->_cast_array_view(args[2]);
+                ArgsView weights = vm->cast_array_view(args[2]);
                 if(weights.size() != size) vm->ValueError(_S("len(weights) != ", size));
                 cum_weights[0] = CAST(f64, weights[0]);
                 for(int i = 1; i < size; i++){

+ 2 - 2
src/vm.cpp

@@ -113,7 +113,7 @@ namespace pkpy{
         return nullptr;
     }
 
-    ArgsView VM::_cast_array_view(PyObject* obj){
+    ArgsView VM::cast_array_view(PyObject* obj){
         if(is_type(obj, VM::tp_list)){
             List& list = PK_OBJ_GET(List, obj);
             return ArgsView(list.begin(), list.end());
@@ -284,7 +284,7 @@ namespace pkpy{
         
         ArgsView view(nullptr, nullptr);
         if(args_tuple.size()==1){
-            view = _cast_array_view(args_tuple[0]);
+            view = cast_array_view(args_tuple[0]);
         }else{
             view = ArgsView(args_tuple);
         }