1
0
Эх сурвалжийг харах

change source to `std::string_view`

blueloveTH 2 жил өмнө
parent
commit
1a9e9dc752

+ 6 - 1
CMakeLists.txt

@@ -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}")

+ 1 - 1
cmake_build.py

@@ -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

+ 1 - 1
include/pocketpy/compiler.h

@@ -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();
 };
 

+ 1 - 1
include/pocketpy/error.h

@@ -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;

+ 2 - 0
include/pocketpy/str.h

@@ -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]; }

+ 5 - 4
include/pocketpy/vm.h

@@ -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);

+ 1 - 1
src/compiler.cpp

@@ -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;

+ 1 - 1
src/error.cpp

@@ -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;

+ 1 - 1
src/pocketpy.cpp

@@ -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();

+ 3 - 3
src/vm.cpp

@@ -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);
     }