change source to std::string_view

This commit is contained in:
blueloveTH 2024-02-01 13:28:16 +08:00
parent 6e38818496
commit 1a9e9dc752
10 changed files with 22 additions and 14 deletions

View File

@ -32,12 +32,17 @@ include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src POCKETPY_SRC)
option(PK_USE_CJSON "Use cJSON" OFF)
option(PK_USE_CJSON "" OFF)
if(PK_USE_CJSON)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/3rd/cjson)
add_definitions(-DPK_USE_CJSON)
endif()
option(PK_ENABLE_OS "" OFF)
if(PK_ENABLE_OS)
add_definitions(-DPK_ENABLE_OS=1)
endif()
# PK_IS_MAIN determines whether the project is being used from root
# or if it is added as a dependency (through add_subdirectory for example).
if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")

View File

@ -7,7 +7,7 @@ if not os.path.exists("build"):
os.chdir("build")
code = os.system("cmake .. -DPK_USE_CJSON=ON -DCMAKE_BUILD_TYPE=Release")
code = os.system("cmake .. -DPK_USE_CJSON=ON -DPK_ENABLE_OS=ON -DCMAKE_BUILD_TYPE=Release")
assert code == 0
code = os.system("cmake --build . --config Release")
assert code == 0

View File

@ -135,7 +135,7 @@ class Compiler {
void IndentationError(Str msg){ lexer->throw_err("IndentationError", msg, err().line, err().start); }
public:
Compiler(VM* vm, const Str& source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
Compiler(VM* vm, std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
CodeObject_ compile();
};

View File

@ -32,7 +32,7 @@ struct SourceData {
Str source;
std::vector<const char*> line_starts;
SourceData(const Str& source, const Str& filename, CompileMode mode);
SourceData(std::string_view source, const Str& filename, CompileMode mode);
SourceData(const Str& filename, CompileMode mode);
std::pair<const char*,const char*> _get_line(int lineno) const;
Str snapshot(int lineno, const char* cursor, std::string_view name) const;

View File

@ -27,6 +27,8 @@ struct Str{
Str(const Str& other);
Str(Str&& other);
operator std::string_view() const { return sv(); }
const char* begin() const { return data; }
const char* end() const { return data + size; }
char operator[](int idx) const { return data[idx]; }

View File

@ -186,9 +186,11 @@ public:
PyObject* find_name_in_mro(Type cls, StrName name);
bool isinstance(PyObject* obj, Type base);
bool issubclass(Type cls, Type base);
PyObject* exec(Str source, Str filename, CompileMode mode, PyObject* _module=nullptr);
PyObject* exec(Str source);
PyObject* eval(Str source);
CodeObject_ compile(std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
PyObject* exec(std::string_view source, Str filename, CompileMode mode, PyObject* _module=nullptr);
PyObject* exec(std::string_view source);
PyObject* eval(std::string_view source);
template<typename ...Args>
PyObject* _exec(Args&&... args){
@ -414,7 +416,6 @@ public:
void _unpack_as_list(ArgsView args, List& list);
void _unpack_as_dict(ArgsView args, Dict& dict);
PyObject* vectorcall(int ARGC, int KWARGC=0, bool op_call=false);
CodeObject_ compile(const Str& source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
PyObject* py_negate(PyObject* obj);
bool py_bool(PyObject* obj);
i64 py_hash(PyObject* obj);

View File

@ -1161,7 +1161,7 @@ __EAT_DOTS_END:
return nullptr;
}
Compiler::Compiler(VM* vm, const Str& source, const Str& filename, CompileMode mode, bool unknown_global_scope){
Compiler::Compiler(VM* vm, std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope){
this->vm = vm;
this->used = false;
this->unknown_global_scope = unknown_global_scope;

View File

@ -2,7 +2,7 @@
namespace pkpy{
SourceData::SourceData(const Str& source, const Str& filename, CompileMode mode): filename(filename), mode(mode) {
SourceData::SourceData(std::string_view source, const Str& filename, CompileMode mode): filename(filename), mode(mode) {
int index = 0;
// Skip utf8 BOM if there is any.
if (strncmp(source.begin(), "\xEF\xBB\xBF", 3) == 0) index += 3;

View File

@ -1482,7 +1482,7 @@ void VM::post_init(){
#endif
}
CodeObject_ VM::compile(const Str& source, const Str& filename, CompileMode mode, bool unknown_global_scope) {
CodeObject_ VM::compile(std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope) {
Compiler compiler(this, source, filename, mode, unknown_global_scope);
try{
return compiler.compile();

View File

@ -155,7 +155,7 @@ namespace pkpy{
return false;
}
PyObject* VM::exec(Str source, Str filename, CompileMode mode, PyObject* _module){
PyObject* VM::exec(std::string_view source, Str filename, CompileMode mode, PyObject* _module){
if(_module == nullptr) _module = _main;
try {
CodeObject_ code = compile(source, filename, mode);
@ -186,11 +186,11 @@ namespace pkpy{
return nullptr;
}
PyObject* VM::exec(Str source){
PyObject* VM::exec(std::string_view source){
return exec(source, "main.py", EXEC_MODE);
}
PyObject* VM::eval(Str source){
PyObject* VM::eval(std::string_view source){
return exec(source, "<eval>", EVAL_MODE);
}