mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
...
This commit is contained in:
parent
864345ccc3
commit
6ee0a169d0
@ -88,12 +88,12 @@ if(x == 1){
|
||||
}
|
||||
```
|
||||
|
||||
## For `shared_ptr<T>`
|
||||
## For `std::shared_ptr<T>`
|
||||
|
||||
Use a `_` suffix to indicate a type is a shared pointer.
|
||||
|
||||
```cpp
|
||||
typedef shared_ptr<CodeObject> CodeObject_;
|
||||
CodeObject_ co = make_shared<CodeObject>();
|
||||
using CodeObject_ = std::shared_ptr<CodeObject>;
|
||||
CodeObject_ co = std::make_shared<CodeObject>();
|
||||
```
|
||||
|
||||
|
@ -48,9 +48,9 @@ struct CodeBlock {
|
||||
};
|
||||
|
||||
struct CodeObject;
|
||||
typedef shared_ptr<CodeObject> CodeObject_;
|
||||
struct FuncDecl;
|
||||
using FuncDecl_ = shared_ptr<FuncDecl>;
|
||||
using CodeObject_ = std::shared_ptr<CodeObject>;
|
||||
using FuncDecl_ = std::shared_ptr<FuncDecl>;
|
||||
|
||||
struct CodeObjectSerializer{
|
||||
std::string buffer;
|
||||
@ -92,7 +92,7 @@ struct CodeObjectSerializer{
|
||||
|
||||
|
||||
struct CodeObject {
|
||||
shared_ptr<SourceData> src;
|
||||
std::shared_ptr<SourceData> src;
|
||||
Str name;
|
||||
bool is_generator = false;
|
||||
|
||||
@ -105,7 +105,7 @@ struct CodeObject {
|
||||
NameDictInt labels;
|
||||
std::vector<FuncDecl_> func_decls;
|
||||
|
||||
CodeObject(shared_ptr<SourceData> src, const Str& name);
|
||||
CodeObject(std::shared_ptr<SourceData> src, const Str& name);
|
||||
void _gc_mark() const;
|
||||
void write(VM* vm, CodeObjectSerializer& ss) const;
|
||||
Str serialize(VM* vm) const;
|
||||
|
@ -103,7 +103,7 @@ enum Precedence {
|
||||
enum StringType { NORMAL_STRING, RAW_STRING, F_STRING, NORMAL_BYTES };
|
||||
|
||||
struct Lexer {
|
||||
shared_ptr<SourceData> src;
|
||||
std::shared_ptr<SourceData> src;
|
||||
const char* token_start;
|
||||
const char* curr_char;
|
||||
int current_line = 1;
|
||||
@ -137,7 +137,7 @@ struct Lexer {
|
||||
void SyntaxError(Str msg){ throw_err("SyntaxError", msg); }
|
||||
void SyntaxError(){ throw_err("SyntaxError", "invalid syntax"); }
|
||||
void IndentationError(Str msg){ throw_err("IndentationError", msg); }
|
||||
Lexer(shared_ptr<SourceData> src);
|
||||
Lexer(std::shared_ptr<SourceData> src);
|
||||
std::vector<Token> run();
|
||||
};
|
||||
|
||||
|
@ -249,68 +249,4 @@ struct MemoryPool{
|
||||
inline MemoryPool<64> pool64;
|
||||
inline MemoryPool<128> pool128;
|
||||
|
||||
template <typename T>
|
||||
struct shared_ptr {
|
||||
int* counter;
|
||||
|
||||
T* _t() const noexcept { return (T*)(counter + 1); }
|
||||
void _inc_counter() { if(counter) ++(*counter); }
|
||||
void _dec_counter() { if(counter && --(*counter) == 0) {((T*)(counter + 1))->~T(); pool128.dealloc(counter);} }
|
||||
|
||||
public:
|
||||
shared_ptr() : counter(nullptr) {}
|
||||
shared_ptr(int* counter) : counter(counter) {}
|
||||
shared_ptr(const shared_ptr& other) : counter(other.counter) {
|
||||
_inc_counter();
|
||||
}
|
||||
shared_ptr(shared_ptr&& other) noexcept : counter(other.counter) {
|
||||
other.counter = nullptr;
|
||||
}
|
||||
~shared_ptr() { _dec_counter(); }
|
||||
|
||||
bool operator==(const shared_ptr& other) const { return counter == other.counter; }
|
||||
bool operator!=(const shared_ptr& other) const { return counter != other.counter; }
|
||||
bool operator<(const shared_ptr& other) const { return counter < other.counter; }
|
||||
bool operator>(const shared_ptr& other) const { return counter > other.counter; }
|
||||
bool operator<=(const shared_ptr& other) const { return counter <= other.counter; }
|
||||
bool operator>=(const shared_ptr& other) const { return counter >= other.counter; }
|
||||
bool operator==(std::nullptr_t) const { return counter == nullptr; }
|
||||
bool operator!=(std::nullptr_t) const { return counter != nullptr; }
|
||||
|
||||
shared_ptr& operator=(const shared_ptr& other) {
|
||||
_dec_counter();
|
||||
counter = other.counter;
|
||||
_inc_counter();
|
||||
return *this;
|
||||
}
|
||||
|
||||
shared_ptr& operator=(shared_ptr&& other) noexcept {
|
||||
_dec_counter();
|
||||
counter = other.counter;
|
||||
other.counter = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
T& operator*() const { return *_t(); }
|
||||
T* operator->() const { return _t(); }
|
||||
T* get() const { return _t(); }
|
||||
|
||||
int use_count() const {
|
||||
return counter ? *counter : 0;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
_dec_counter();
|
||||
counter = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename... Args>
|
||||
shared_ptr<T> make_sp(Args&&... args) {
|
||||
int* p = (int*)pool128.alloc(sizeof(int) + sizeof(T));
|
||||
*p = 1;
|
||||
new(p+1) T(std::forward<Args>(args)...);
|
||||
return shared_ptr<T>(p);
|
||||
}
|
||||
|
||||
}; // namespace pkpy
|
||||
|
@ -198,7 +198,7 @@ while(!_items[i].first.empty()) { \
|
||||
};
|
||||
|
||||
using NameDict = NameDictImpl<PyObject*>;
|
||||
using NameDict_ = shared_ptr<NameDict>;
|
||||
using NameDict_ = std::shared_ptr<NameDict>;
|
||||
using NameDictInt = NameDictImpl<int>;
|
||||
|
||||
} // namespace pkpy
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace pkpy{
|
||||
|
||||
CodeObject::CodeObject(shared_ptr<SourceData> src, const Str& name):
|
||||
CodeObject::CodeObject(std::shared_ptr<SourceData> src, const Str& name):
|
||||
src(src), name(name) {}
|
||||
|
||||
void CodeObject::_gc_mark() const {
|
||||
|
@ -9,14 +9,14 @@ namespace pkpy{
|
||||
}
|
||||
|
||||
CodeObject_ Compiler::push_global_context(){
|
||||
CodeObject_ co = make_sp<CodeObject>(lexer->src, lexer->src->filename);
|
||||
CodeObject_ co = std::make_shared<CodeObject>(lexer->src, lexer->src->filename);
|
||||
contexts.push(CodeEmitContext(vm, co, contexts.size()));
|
||||
return co;
|
||||
}
|
||||
|
||||
FuncDecl_ Compiler::push_f_context(Str name){
|
||||
FuncDecl_ decl = make_sp<FuncDecl>();
|
||||
decl->code = make_sp<CodeObject>(lexer->src, name);
|
||||
FuncDecl_ decl = std::make_shared<FuncDecl>();
|
||||
decl->code = std::make_shared<CodeObject>(lexer->src, name);
|
||||
decl->nested = name_scope() == NAME_LOCAL;
|
||||
contexts.push(CodeEmitContext(vm, decl->code, contexts.size()));
|
||||
return decl;
|
||||
@ -1008,7 +1008,7 @@ __SUBSCR_END:
|
||||
this->used = false;
|
||||
this->unknown_global_scope = unknown_global_scope;
|
||||
this->lexer = std::make_unique<Lexer>(
|
||||
make_sp<SourceData>(source, filename, mode)
|
||||
std::make_shared<SourceData>(source, filename, mode)
|
||||
);
|
||||
init_pratt_rules();
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ namespace pkpy{
|
||||
}
|
||||
|
||||
NameDict_ FastLocals::to_namedict(){
|
||||
NameDict_ dict = make_sp<NameDict>();
|
||||
NameDict_ dict = std::make_shared<NameDict>();
|
||||
varnames_inv->apply([&](StrName name, int index){
|
||||
PyObject* value = a[index];
|
||||
if(value != PY_NULL) dict->set(name, value);
|
||||
|
@ -449,7 +449,7 @@ static bool is_unicode_Lo_char(uint32_t c) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
Lexer::Lexer(shared_ptr<SourceData> src) {
|
||||
Lexer::Lexer(std::shared_ptr<SourceData> src) {
|
||||
this->src = src;
|
||||
this->token_start = src->source.c_str();
|
||||
this->curr_char = src->source.c_str();
|
||||
|
Loading…
x
Reference in New Issue
Block a user