mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
change source to std::string_view
This commit is contained in:
parent
6e38818496
commit
1a9e9dc752
@ -32,12 +32,17 @@ include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
|
|||||||
|
|
||||||
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src POCKETPY_SRC)
|
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)
|
if(PK_USE_CJSON)
|
||||||
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/3rd/cjson)
|
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/3rd/cjson)
|
||||||
add_definitions(-DPK_USE_CJSON)
|
add_definitions(-DPK_USE_CJSON)
|
||||||
endif()
|
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
|
# 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).
|
# or if it is added as a dependency (through add_subdirectory for example).
|
||||||
if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
|
if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||||
|
@ -7,7 +7,7 @@ if not os.path.exists("build"):
|
|||||||
|
|
||||||
os.chdir("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
|
assert code == 0
|
||||||
code = os.system("cmake --build . --config Release")
|
code = os.system("cmake --build . --config Release")
|
||||||
assert code == 0
|
assert code == 0
|
||||||
|
@ -135,7 +135,7 @@ class Compiler {
|
|||||||
void IndentationError(Str msg){ lexer->throw_err("IndentationError", msg, err().line, err().start); }
|
void IndentationError(Str msg){ lexer->throw_err("IndentationError", msg, err().line, err().start); }
|
||||||
|
|
||||||
public:
|
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();
|
CodeObject_ compile();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ struct SourceData {
|
|||||||
Str source;
|
Str source;
|
||||||
std::vector<const char*> line_starts;
|
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);
|
SourceData(const Str& filename, CompileMode mode);
|
||||||
std::pair<const char*,const char*> _get_line(int lineno) const;
|
std::pair<const char*,const char*> _get_line(int lineno) const;
|
||||||
Str snapshot(int lineno, const char* cursor, std::string_view name) const;
|
Str snapshot(int lineno, const char* cursor, std::string_view name) const;
|
||||||
|
@ -27,6 +27,8 @@ struct Str{
|
|||||||
Str(const Str& other);
|
Str(const Str& other);
|
||||||
Str(Str&& other);
|
Str(Str&& other);
|
||||||
|
|
||||||
|
operator std::string_view() const { return sv(); }
|
||||||
|
|
||||||
const char* begin() const { return data; }
|
const char* begin() const { return data; }
|
||||||
const char* end() const { return data + size; }
|
const char* end() const { return data + size; }
|
||||||
char operator[](int idx) const { return data[idx]; }
|
char operator[](int idx) const { return data[idx]; }
|
||||||
|
@ -186,9 +186,11 @@ public:
|
|||||||
PyObject* find_name_in_mro(Type cls, StrName name);
|
PyObject* find_name_in_mro(Type cls, StrName name);
|
||||||
bool isinstance(PyObject* obj, Type base);
|
bool isinstance(PyObject* obj, Type base);
|
||||||
bool issubclass(Type cls, Type base);
|
bool issubclass(Type cls, Type base);
|
||||||
PyObject* exec(Str source, Str filename, CompileMode mode, PyObject* _module=nullptr);
|
|
||||||
PyObject* exec(Str source);
|
CodeObject_ compile(std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
|
||||||
PyObject* eval(Str source);
|
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>
|
template<typename ...Args>
|
||||||
PyObject* _exec(Args&&... args){
|
PyObject* _exec(Args&&... args){
|
||||||
@ -414,7 +416,6 @@ public:
|
|||||||
void _unpack_as_list(ArgsView args, List& list);
|
void _unpack_as_list(ArgsView args, List& list);
|
||||||
void _unpack_as_dict(ArgsView args, Dict& dict);
|
void _unpack_as_dict(ArgsView args, Dict& dict);
|
||||||
PyObject* vectorcall(int ARGC, int KWARGC=0, bool op_call=false);
|
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);
|
PyObject* py_negate(PyObject* obj);
|
||||||
bool py_bool(PyObject* obj);
|
bool py_bool(PyObject* obj);
|
||||||
i64 py_hash(PyObject* obj);
|
i64 py_hash(PyObject* obj);
|
||||||
|
@ -1161,7 +1161,7 @@ __EAT_DOTS_END:
|
|||||||
return nullptr;
|
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->vm = vm;
|
||||||
this->used = false;
|
this->used = false;
|
||||||
this->unknown_global_scope = unknown_global_scope;
|
this->unknown_global_scope = unknown_global_scope;
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace pkpy{
|
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;
|
int index = 0;
|
||||||
// Skip utf8 BOM if there is any.
|
// Skip utf8 BOM if there is any.
|
||||||
if (strncmp(source.begin(), "\xEF\xBB\xBF", 3) == 0) index += 3;
|
if (strncmp(source.begin(), "\xEF\xBB\xBF", 3) == 0) index += 3;
|
||||||
|
@ -1482,7 +1482,7 @@ void VM::post_init(){
|
|||||||
#endif
|
#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);
|
Compiler compiler(this, source, filename, mode, unknown_global_scope);
|
||||||
try{
|
try{
|
||||||
return compiler.compile();
|
return compiler.compile();
|
||||||
|
@ -155,7 +155,7 @@ namespace pkpy{
|
|||||||
return false;
|
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;
|
if(_module == nullptr) _module = _main;
|
||||||
try {
|
try {
|
||||||
CodeObject_ code = compile(source, filename, mode);
|
CodeObject_ code = compile(source, filename, mode);
|
||||||
@ -186,11 +186,11 @@ namespace pkpy{
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject* VM::exec(Str source){
|
PyObject* VM::exec(std::string_view source){
|
||||||
return exec(source, "main.py", EXEC_MODE);
|
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);
|
return exec(source, "<eval>", EVAL_MODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user