diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e154f41..e3a91186 100644 --- a/CMakeLists.txt +++ b/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}") diff --git a/cmake_build.py b/cmake_build.py index 8ca65680..dcb82fce 100644 --- a/cmake_build.py +++ b/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 diff --git a/include/pocketpy/compiler.h b/include/pocketpy/compiler.h index 7211ae30..133171a0 100644 --- a/include/pocketpy/compiler.h +++ b/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(); }; diff --git a/include/pocketpy/error.h b/include/pocketpy/error.h index a2dbceba..ca284a00 100644 --- a/include/pocketpy/error.h +++ b/include/pocketpy/error.h @@ -32,7 +32,7 @@ struct SourceData { Str source; std::vector 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 _get_line(int lineno) const; Str snapshot(int lineno, const char* cursor, std::string_view name) const; diff --git a/include/pocketpy/str.h b/include/pocketpy/str.h index 78964332..8cf8418f 100644 --- a/include/pocketpy/str.h +++ b/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]; } diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 7fb97bf5..60a377fd 100644 --- a/include/pocketpy/vm.h +++ b/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 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); diff --git a/src/compiler.cpp b/src/compiler.cpp index 6d3251d9..39bc1706 100644 --- a/src/compiler.cpp +++ b/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; diff --git a/src/error.cpp b/src/error.cpp index d280d894..b6492209 100644 --- a/src/error.cpp +++ b/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; diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 21cd65b6..0e5fee46 100644 --- a/src/pocketpy.cpp +++ b/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(); diff --git a/src/vm.cpp b/src/vm.cpp index 9168139e..7e2d2b8d 100644 --- a/src/vm.cpp +++ b/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_MODE); }