This commit is contained in:
blueloveTH 2022-12-05 02:11:23 +08:00
parent b24b6599ab
commit 5a9448dca1
5 changed files with 25 additions and 11 deletions

View File

@ -2,7 +2,7 @@ with open("src/opcodes.h", "rt", encoding='utf-8') as f:
OPCODES_TEXT = f.read() OPCODES_TEXT = f.read()
pipeline = [ pipeline = [
["__stl__.h", "str.h", "safestl.h", "builtins.h", "error.h"], ["__stl__.h", "shared_ptr.h", "str.h", "safestl.h", "builtins.h", "error.h"],
["obj.h", "iter.h", "parser.h", "pointer.h", "codeobject.h"], ["obj.h", "iter.h", "parser.h", "pointer.h", "codeobject.h"],
["vm.h", "compiler.h", "repl.h"], ["vm.h", "compiler.h", "repl.h"],
["pocketpy.h"] ["pocketpy.h"]

View File

@ -15,11 +15,11 @@
} \ } \
}); });
#define BIND_NUM_LOGICAL_OPT(name, op, fallback) \ #define BIND_NUM_LOGICAL_OPT(name, op, is_eq) \
_vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, const pkpy::ArgList& args){ \ _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, const pkpy::ArgList& args){ \
if(!vm->isIntOrFloat(args[0], args[1])){ \ if(!vm->isIntOrFloat(args[0], args[1])){ \
if constexpr(fallback) return vm->PyBool(args[0] op args[1]); \ if constexpr(is_eq) return vm->PyBool(args[0] == args[1]); \
vm->typeError("unsupported operand type(s) for " #op ); \ vm->typeError("unsupported operand type(s) for " #op ); \
} \ } \
return vm->PyBool(vm->numToFloat(args[0]) op vm->numToFloat(args[1])); \ return vm->PyBool(vm->numToFloat(args[0]) op vm->numToFloat(args[1])); \
}); });

View File

@ -1,10 +1,11 @@
#pragma once #pragma once
#include "__stl__.h" #include "__stl__.h"
#include "shared_ptr.h"
#include "str.h" #include "str.h"
struct PyObject; struct PyObject;
typedef std::shared_ptr<PyObject> PyVar; typedef pkpy::shared_ptr<PyObject> PyVar;
typedef PyVar PyVarOrNull; typedef PyVar PyVarOrNull;
class PyVarList: public std::vector<PyVar> { class PyVarList: public std::vector<PyVar> {

View File

@ -16,6 +16,13 @@ namespace pkpy{
public: public:
shared_ptr() : count(nullptr), ptr(nullptr) {} shared_ptr() : count(nullptr), ptr(nullptr) {}
shared_ptr(T* ptr) : count(new int(1)), ptr(ptr) {} shared_ptr(T* ptr) : count(new int(1)), ptr(ptr) {}
shared_ptr(const shared_ptr& other) : count(other.count), ptr(other.ptr) {
if(count) (*count)++;
}
shared_ptr(shared_ptr&& other) : count(other.count), ptr(other.ptr) {
other.count = nullptr;
other.ptr = nullptr;
}
~shared_ptr() { ~shared_ptr() {
if (count && --(*count) == 0) _delete(); if (count && --(*count) == 0) _delete();
} }
@ -45,7 +52,7 @@ namespace pkpy{
} }
return *this; return *this;
} }
shared_ptr& operator=(shared_ptr&& other) { shared_ptr& operator=(shared_ptr&& other) {
if (this != &other) { if (this != &other) {
if (count && --(*count) == 0) _delete(); if (count && --(*count) == 0) _delete();
@ -69,6 +76,12 @@ namespace pkpy{
int use_count() const { int use_count() const {
return count ? *count : 0; return count ? *count : 0;
} }
void reset(){
if (count && --(*count) == 0) _delete();
count = nullptr;
ptr = nullptr;
}
}; };
template <typename T, typename... Args> template <typename T, typename... Args>

View File

@ -599,7 +599,7 @@ public:
PyVar newClassType(_Str name, PyVar base=nullptr) { PyVar newClassType(_Str name, PyVar base=nullptr) {
if(base == nullptr) base = _tp_object; if(base == nullptr) base = _tp_object;
PyVar obj = std::make_shared<PyObject>((_Int)0); PyVar obj = pkpy::make_shared<PyObject>((_Int)0);
obj->setType(_tp_type); obj->setType(_tp_type);
setAttr(obj, __base__, base); setAttr(obj, __base__, base);
_types[name] = obj; _types[name] = obj;
@ -608,7 +608,7 @@ public:
PyVar newObject(PyVar type, _Value _native) { PyVar newObject(PyVar type, _Value _native) {
__checkType(type, _tp_type); __checkType(type, _tp_type);
PyVar obj = std::make_shared<PyObject>(_native); PyVar obj = pkpy::make_shared<PyObject>(_native);
obj->setType(type); obj->setType(type);
return obj; return obj;
} }
@ -794,8 +794,8 @@ public:
inline const PyVar& PyBool(bool value){return value ? True : False;} inline const PyVar& PyBool(bool value){return value ? True : False;}
void initializeBuiltinClasses(){ void initializeBuiltinClasses(){
_tp_object = std::make_shared<PyObject>((_Int)0); _tp_object = pkpy::make_shared<PyObject>((_Int)0);
_tp_type = std::make_shared<PyObject>((_Int)0); _tp_type = pkpy::make_shared<PyObject>((_Int)0);
_types["object"] = _tp_object; _types["object"] = _tp_object;
_types["type"] = _tp_type; _types["type"] = _tp_type;