mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 20:40:18 +00:00
up
This commit is contained in:
parent
27a01e91d3
commit
3b30747fcd
@ -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", "shared_ptr.h", "str.h", "hash_table8.hpp", "safestl.h", "builtins.h", "error.h"],
|
["__stl__.h", "memory.h", "str.h", "hash_table8.hpp", "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"]
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <memory>
|
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
@ -23,7 +23,7 @@ struct Loop {
|
|||||||
|
|
||||||
class Compiler {
|
class Compiler {
|
||||||
public:
|
public:
|
||||||
std::unique_ptr<Parser> parser;
|
pkpy::unique_ptr<Parser> parser;
|
||||||
std::stack<_Code> codes;
|
std::stack<_Code> codes;
|
||||||
std::stack<Loop> loops;
|
std::stack<Loop> loops;
|
||||||
bool isCompilingClass = false;
|
bool isCompilingClass = false;
|
||||||
@ -45,7 +45,7 @@ public:
|
|||||||
|
|
||||||
Compiler(VM* vm, const char* source, _Str filename, CompileMode mode){
|
Compiler(VM* vm, const char* source, _Str filename, CompileMode mode){
|
||||||
this->vm = vm;
|
this->vm = vm;
|
||||||
this->parser = std::make_unique<Parser>(
|
this->parser = pkpy::make_unique<Parser>(
|
||||||
pkpy::make_shared<SourceMetadata>(source, filename, mode)
|
pkpy::make_shared<SourceMetadata>(source, filename, mode)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -88,4 +88,68 @@ namespace pkpy{
|
|||||||
shared_ptr<T> make_shared(Args&&... args) {
|
shared_ptr<T> make_shared(Args&&... args) {
|
||||||
return shared_ptr<T>(new T(std::forward<Args>(args)...));
|
return shared_ptr<T>(new T(std::forward<Args>(args)...));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class unique_ptr {
|
||||||
|
T* ptr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
unique_ptr() : ptr(nullptr) {}
|
||||||
|
unique_ptr(T* ptr) : ptr(ptr) {}
|
||||||
|
unique_ptr(const unique_ptr& other) = delete;
|
||||||
|
unique_ptr(unique_ptr&& other) : ptr(other.ptr) {
|
||||||
|
other.ptr = nullptr;
|
||||||
|
}
|
||||||
|
~unique_ptr() {
|
||||||
|
delete ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const unique_ptr& other) const {
|
||||||
|
return ptr == other.ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const unique_ptr& other) const {
|
||||||
|
return ptr != other.ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(std::nullptr_t) const {
|
||||||
|
return ptr == nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(std::nullptr_t) const {
|
||||||
|
return ptr != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
unique_ptr& operator=(const unique_ptr& other) = delete;
|
||||||
|
|
||||||
|
unique_ptr& operator=(unique_ptr&& other) {
|
||||||
|
if (this != &other) {
|
||||||
|
delete ptr;
|
||||||
|
ptr = other.ptr;
|
||||||
|
other.ptr = nullptr;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
T& operator*() const {
|
||||||
|
return *ptr;
|
||||||
|
}
|
||||||
|
T* operator->() const {
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
T* get() const {
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset(){
|
||||||
|
delete ptr;
|
||||||
|
ptr = nullptr;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename... Args>
|
||||||
|
unique_ptr<T> make_unique(Args&&... args) {
|
||||||
|
return unique_ptr<T>(new T(std::forward<Args>(args)...));
|
||||||
|
}
|
||||||
};
|
};
|
17
src/obj.h
17
src/obj.h
@ -69,7 +69,6 @@ typedef std::variant<PyVar,_Int,_Float,bool,_Str,PyVarList,_CppFunc,_Func,pkpy::
|
|||||||
|
|
||||||
const int VALUE_SIZE = sizeof(_Value);
|
const int VALUE_SIZE = sizeof(_Value);
|
||||||
|
|
||||||
static std::vector<void*> _objPool;
|
|
||||||
|
|
||||||
struct PyObject {
|
struct PyObject {
|
||||||
PyVarDict attribs;
|
PyVarDict attribs;
|
||||||
@ -82,7 +81,7 @@ struct PyObject {
|
|||||||
|
|
||||||
inline void setType(const PyVar& type){
|
inline void setType(const PyVar& type){
|
||||||
this->_type = type;
|
this->_type = type;
|
||||||
this->attribs[__class__] = type;
|
// this->attribs[__class__] = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
// currently __name__ is only used for 'type'
|
// currently __name__ is only used for 'type'
|
||||||
@ -97,18 +96,4 @@ struct PyObject {
|
|||||||
|
|
||||||
PyObject(const _Value& val): _native(val) {}
|
PyObject(const _Value& val): _native(val) {}
|
||||||
PyObject(_Value&& val): _native(std::move(val)) {}
|
PyObject(_Value&& val): _native(std::move(val)) {}
|
||||||
|
|
||||||
void* operator new(size_t size){
|
|
||||||
if(_objPool.empty()){
|
|
||||||
return ::operator new(size);
|
|
||||||
}else{
|
|
||||||
void* ptr = _objPool.back();
|
|
||||||
_objPool.pop_back();
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void operator delete(void* ptr){
|
|
||||||
_objPool.push_back(ptr);
|
|
||||||
}
|
|
||||||
};
|
};
|
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "__stl__.h"
|
#include "__stl__.h"
|
||||||
#include "shared_ptr.h"
|
#include "memory.h"
|
||||||
#include "str.h"
|
#include "str.h"
|
||||||
|
|
||||||
struct PyObject;
|
struct PyObject;
|
||||||
|
@ -243,7 +243,7 @@ namespace std {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const _Str& __class__ = _Str("__class__"_c);
|
// const _Str& __class__ = _Str("__class__"_c);
|
||||||
const _Str& __base__ = _Str("__base__"_c);
|
const _Str& __base__ = _Str("__base__"_c);
|
||||||
const _Str& __new__ = _Str("__new__"_c);
|
const _Str& __new__ = _Str("__new__"_c);
|
||||||
const _Str& __iter__ = _Str("__iter__"_c);
|
const _Str& __iter__ = _Str("__iter__"_c);
|
||||||
|
6
src/vm.h
6
src/vm.h
@ -25,7 +25,7 @@ class VM {
|
|||||||
std::atomic<bool> _stopFlag = false;
|
std::atomic<bool> _stopFlag = false;
|
||||||
std::vector<PyVar> _smallIntegers; // [-5, 256]
|
std::vector<PyVar> _smallIntegers; // [-5, 256]
|
||||||
protected:
|
protected:
|
||||||
std::deque< std::unique_ptr<Frame> > callstack;
|
std::deque< pkpy::unique_ptr<Frame> > callstack;
|
||||||
PyVarDict _modules; // loaded modules
|
PyVarDict _modules; // loaded modules
|
||||||
std::map<_Str, _Code> _lazyModules; // lazy loaded modules
|
std::map<_Str, _Code> _lazyModules; // lazy loaded modules
|
||||||
PyVar __py2py_call_signal;
|
PyVar __py2py_call_signal;
|
||||||
@ -563,7 +563,7 @@ public:
|
|||||||
throw RuntimeError("RecursionError", "maximum recursion depth exceeded", _cleanErrorAndGetSnapshots());
|
throw RuntimeError("RecursionError", "maximum recursion depth exceeded", _cleanErrorAndGetSnapshots());
|
||||||
}
|
}
|
||||||
Frame* frame = new Frame(code.get(), _module, locals);
|
Frame* frame = new Frame(code.get(), _module, locals);
|
||||||
callstack.emplace_back(std::unique_ptr<Frame>(frame));
|
callstack.emplace_back(pkpy::unique_ptr<Frame>(frame));
|
||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -608,7 +608,7 @@ public:
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyVar newObject(PyVar type, _Value _native) {
|
PyVar newObject(PyVar type, const _Value& _native) {
|
||||||
__checkType(type, _tp_type);
|
__checkType(type, _tp_type);
|
||||||
PyVar obj = pkpy::make_shared<PyObject>(_native);
|
PyVar obj = pkpy::make_shared<PyObject>(_native);
|
||||||
obj->setType(type);
|
obj->setType(type);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user