blueloveTH 1 年之前
父節點
當前提交
fc84e657e3
共有 5 個文件被更改,包括 27 次插入49 次删除
  1. 14 14
      include/pocketpy/error.h
  2. 1 3
      include/pocketpy/expr.h
  3. 6 5
      include/pocketpy/lexer.h
  4. 5 24
      include/pocketpy/vector.h
  5. 1 3
      src/str.cpp

+ 14 - 14
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<SourceData> src;
-    int lineno;
-    const char* cursor;
-    std::string name;
-
-    Str snapshot() const { return src->snapshot(lineno, cursor, name); }
-
-    ExceptionLine(std::shared_ptr<SourceData> 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<ExceptionLine> stacktrace;
+
+    struct Frame{
+        std::shared_ptr<SourceData> src;
+        int lineno;
+        const char* cursor;
+        std::string name;
+
+        Str snapshot() const { return src->snapshot(lineno, cursor, name); }
+
+        Frame(std::shared_ptr<SourceData> src, int lineno, const char* cursor, std::string_view name):
+            src(src), lineno(lineno), cursor(cursor), name(name) {}
+    };
+
+    stack<Frame> stacktrace;
     Exception(StrName type): type(type), is_re(true), _ip_on_error(-1), _code_on_error(nullptr), _self(nullptr) {}
 
     PyObject* self() const{

+ 1 - 3
include/pocketpy/expr.h

@@ -54,9 +54,7 @@ typedef unique_ptr_128<Expr> Expr_;
 typedef small_vector<Expr_, 4> Expr_vector;
 
 template<>
-struct TriviallyRelocatable<Expr_>{
-    constexpr static bool value = true;
-};
+constexpr inline bool is_trivially_relocatable_v<Expr_> = true;
 
 struct Expr{
     int line = 0;

+ 6 - 5
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<SourceData> src);
     vector<Token> run();
 };

+ 5 - 24
include/pocketpy/vector.h

@@ -9,6 +9,9 @@ struct explicit_copy_t {
     explicit explicit_copy_t() = default;
 };
 
+template<typename T>
+constexpr inline bool is_trivially_relocatable_v = std::is_trivially_copyable_v<T> && std::is_trivially_destructible_v<T>;
+
 template<typename T>
 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<T>){
+        if constexpr(is_trivially_relocatable_v<T>){
             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<typename T>
-    struct TriviallyRelocatable
-    {
-        constexpr static bool value =
-                std::is_trivially_copyable_v<T> && std::is_trivially_destructible_v<T>;
-    };
-
-    template<typename T>
-    constexpr inline bool is_trivially_relocatable_v =
-            TriviallyRelocatable<T>::value;
-
-    template<typename T>
-    struct TriviallyRelocatable<std::shared_ptr<T>>
-    {
-        constexpr static bool value = true;
-    };
-
-
-// the implementation of small_vector
+namespace pkpy {
     template<typename T, std::size_t N>
     class small_vector
     {

+ 1 - 3
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
     }