From fc84e657e308c30839b468105cd91dce31088408 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 1 Jun 2024 18:53:44 +0800 Subject: [PATCH] some fix --- include/pocketpy/error.h | 28 ++++++++++++++-------------- include/pocketpy/expr.h | 4 +--- include/pocketpy/lexer.h | 11 ++++++----- include/pocketpy/vector.h | 29 +++++------------------------ src/str.cpp | 4 +--- 5 files changed, 27 insertions(+), 49 deletions(-) diff --git a/include/pocketpy/error.h b/include/pocketpy/error.h index bbc65495..b3b7d817 100644 --- a/include/pocketpy/error.h +++ b/include/pocketpy/error.h @@ -49,18 +49,6 @@ struct SourceData { Str snapshot(int lineno, const char* cursor, std::string_view name) const; }; -struct ExceptionLine{ - std::shared_ptr src; - int lineno; - const char* cursor; - std::string name; - - Str snapshot() const { return src->snapshot(lineno, cursor, name); } - - ExceptionLine(std::shared_ptr src, int lineno, const char* cursor, std::string_view name): - src(src), lineno(lineno), cursor(cursor), name(name) {} -}; - struct Exception { StrName type; Str msg; @@ -70,8 +58,20 @@ struct Exception { void* _code_on_error; PyObject* _self; // weak reference - - stack stacktrace; + + struct Frame{ + std::shared_ptr src; + int lineno; + const char* cursor; + std::string name; + + Str snapshot() const { return src->snapshot(lineno, cursor, name); } + + Frame(std::shared_ptr src, int lineno, const char* cursor, std::string_view name): + src(src), lineno(lineno), cursor(cursor), name(name) {} + }; + + stack stacktrace; Exception(StrName type): type(type), is_re(true), _ip_on_error(-1), _code_on_error(nullptr), _self(nullptr) {} PyObject* self() const{ diff --git a/include/pocketpy/expr.h b/include/pocketpy/expr.h index f5d4c7ac..a5730680 100644 --- a/include/pocketpy/expr.h +++ b/include/pocketpy/expr.h @@ -54,9 +54,7 @@ typedef unique_ptr_128 Expr_; typedef small_vector Expr_vector; template<> -struct TriviallyRelocatable{ - constexpr static bool value = true; -}; +constexpr inline bool is_trivially_relocatable_v = true; struct Expr{ int line = 0; diff --git a/include/pocketpy/lexer.h b/include/pocketpy/lexer.h index 3966409e..e208c2a3 100644 --- a/include/pocketpy/lexer.h +++ b/include/pocketpy/lexer.h @@ -123,11 +123,12 @@ struct Lexer { bool lex_one_token(); /***** Error Reporter *****/ - void throw_err(StrName type, Str msg); - void throw_err(StrName type, Str msg, int lineno, const char* cursor); - void SyntaxError(Str msg){ throw_err("SyntaxError", msg); } - void SyntaxError(){ throw_err("SyntaxError", "invalid syntax"); } - void IndentationError(Str msg){ throw_err("IndentationError", msg); } + [[noreturn]] void throw_err(StrName type, Str msg); + [[noreturn]] void throw_err(StrName type, Str msg, int lineno, const char* cursor); + [[noreturn]] void SyntaxError(Str msg){ throw_err("SyntaxError", msg); } + [[noreturn]] void SyntaxError(){ throw_err("SyntaxError", "invalid syntax"); } + [[noreturn]] void IndentationError(Str msg){ throw_err("IndentationError", msg); } + Lexer(VM* vm, std::shared_ptr src); vector run(); }; diff --git a/include/pocketpy/vector.h b/include/pocketpy/vector.h index 656c35bd..de535768 100644 --- a/include/pocketpy/vector.h +++ b/include/pocketpy/vector.h @@ -9,6 +9,9 @@ struct explicit_copy_t { explicit explicit_copy_t() = default; }; +template +constexpr inline bool is_trivially_relocatable_v = std::is_trivially_copyable_v && std::is_trivially_destructible_v; + template struct array{ T* _data; @@ -124,7 +127,7 @@ struct vector{ if(cap < 4) cap = 4; // minimum capacity if(cap <= capacity()) return; T* new_data = (T*)malloc(sizeof(T) * cap); - if constexpr(std::is_trivially_copyable_v){ + if constexpr(is_trivially_relocatable_v){ memcpy(new_data, _data, sizeof(T) * _size); }else{ for(int i=0; i<_size; i++){ @@ -249,29 +252,7 @@ public: } // namespace pkpy -namespace pkpy -{ - -// explicitly mark a type as trivially relocatable for better performance - template - struct TriviallyRelocatable - { - constexpr static bool value = - std::is_trivially_copyable_v && std::is_trivially_destructible_v; - }; - - template - constexpr inline bool is_trivially_relocatable_v = - TriviallyRelocatable::value; - - template - struct TriviallyRelocatable> - { - constexpr static bool value = true; - }; - - -// the implementation of small_vector +namespace pkpy { template class small_vector { diff --git a/src/str.cpp b/src/str.cpp index c135223f..7672c66c 100644 --- a/src/str.cpp +++ b/src/str.cpp @@ -410,9 +410,7 @@ int utf8len(unsigned char c, bool suppress){ buffer[buffer.size()] = '\0'; // set '\0' return Str(buffer.detach()); #else -#warning "SStream::str() needs to be optimized" - buffer.push_back('\0'); - return Str(buffer.data(), buffer.size()-1); + return Str(buffer.data(), buffer.size()); #endif }