فهرست منبع

refactor & feat: 堆栈改用链表实现

SongZihuan 3 سال پیش
والد
کامیت
f6359643ec

+ 0 - 7
include/core/core-activation.h

@@ -20,9 +20,6 @@ namespace aFuncore {
 
         Inter &inter;
 
-        template <typename Callable,typename...T>
-        static void forEach(Activation *activation, Callable func, T...arg);
-
         explicit Activation(Inter &inter_);
         virtual ~Activation();
         Activation &operator=(const Activation &)=delete;
@@ -32,7 +29,6 @@ namespace aFuncore {
         virtual inline void endRun();
 
         [[nodiscard]] inline VarList *getVarlist() const;
-        [[nodiscard]] inline Activation *toPrev() const;
         [[nodiscard]] inline UpMessage &getUpStream();
         [[nodiscard]] inline DownMessage &getDownStream();
 
@@ -40,8 +36,6 @@ namespace aFuncore {
         [[nodiscard]] inline  const aFuntool::FilePath &getFilePath() const;
 
     protected:
-        Activation *prev;
-
         VarList *varlist;
 
         UpMessage up;
@@ -107,6 +101,5 @@ namespace aFuncore {
 }
 
 #include "core-activation.inline.h"
-#include "core-activation.template.h"
 
 #endif //AFUN_CORE_ACTIVATION_H

+ 0 - 4
include/core/core-activation.inline.h

@@ -12,10 +12,6 @@ namespace aFuncore {
         return varlist;
     }
 
-    inline Activation *Activation::toPrev() const{
-        return prev;
-    }
-
     inline UpMessage &Activation::getUpStream() {
         return up;
     }

+ 0 - 13
include/core/core-activation.template.h

@@ -1,13 +0,0 @@
-#ifndef AFUN_CORE_ACTIVATION_TEMPLATE_H
-#define AFUN_CORE_ACTIVATION_TEMPLATE_H
-#include "core-activation.h"
-
-namespace aFuncore {
-    template <typename Callable, typename...T>
-    inline void Activation::forEach(Activation *activation, Callable func, T...arg) {
-        for (NULL; activation != nullptr; activation = activation->prev)
-            func(activation, arg...);
-    }
-}
-
-#endif //AFUN_CORE_ACTIVATION_TEMPLATE_H

+ 3 - 0
include/core/inter.h

@@ -85,6 +85,7 @@ namespace aFuncore {
         [[nodiscard]] inline ProtectVarSpace *getProtectVarSpace() const;
         [[nodiscard]] inline VarSpace *getGlobalVarSpace() const;
         [[nodiscard]] inline VarList *getGlobalVarlist() const;
+        [[nodiscard]] inline const std::list<Activation *> &getStack() const;
         [[nodiscard]] inline Activation *getActivation() const;
         [[nodiscard]] bool checkLiteral(const std::string &element) const;
         [[nodiscard]] bool checkLiteral(const std::string &element, std::string &literaler, bool &in_protect) const;
@@ -105,6 +106,7 @@ namespace aFuncore {
 
         Environment &env;
 
+        std::list<Activation *> stack;
         Activation *activation;  // 活动记录
 
         InterOutMessage out;
@@ -113,6 +115,7 @@ namespace aFuncore {
         std::list<LiteralRegex> literal;
 
         inline void pushActivation(Activation *new_activation);
+        inline Activation *popActivation();
     };
 
     struct Inter::LiteralRegex {

+ 18 - 0
include/core/inter.inline.h

@@ -8,9 +8,23 @@ namespace aFuncore {
     }
 
     inline void Inter::pushActivation(Activation *new_activation) {
+        stack.push_front(new_activation);
         activation = new_activation;
     }
 
+    inline Activation *Inter::popActivation() {
+        if (activation == nullptr)
+            return nullptr;
+
+        Activation *ret = activation;
+        stack.pop_front();
+        if (stack.empty())
+            activation = nullptr;
+        else
+            activation = stack.front();
+        return ret;
+    }
+
     inline Inter::InterStatus Inter::getStatus() const {
         return status;
     }
@@ -35,6 +49,10 @@ namespace aFuncore {
         return env.global_varlist;
     }
 
+    inline const std::list<Activation *> &Inter::getStack() const {
+        return stack;
+    }
+
     inline Activation *Inter::getActivation() const {
         return activation;
     }

+ 1 - 1
include/core/msg.h

@@ -43,7 +43,7 @@ namespace aFuncore {
             aFuntool::FileLine line;
         };
 
-        explicit ErrorMessage(std::string error_type_, std::string error_info_, Activation *activation);
+        explicit ErrorMessage(std::string error_type_, std::string error_info_, Activation *start);
         inline ErrorMessage(ErrorMessage &&msg) noexcept;
         void topProgress(Inter &inter_, Activation &activation) override;
         [[nodiscard]] inline std::string getErrorType() const;

+ 9 - 9
src/core/activation.cpp

@@ -12,12 +12,12 @@ namespace aFuncore {
      * @param inter_
      */
     Activation::Activation(Inter &inter_)
-            : inter{inter_}, prev{inter_.getActivation()}, line{0},
-              up{prev == nullptr ? nullptr : &prev->up}, down{}{
-        if (prev != nullptr) {
-            varlist = new VarList(prev->varlist);
-            line = prev->line;
-            path = prev->path;
+            : inter{inter_}, line{0},
+              up{inter_.activation == nullptr ? nullptr : &inter_.activation->up}, down{}{
+        if (inter_.activation != nullptr) {
+            varlist = new VarList(inter_.activation->varlist);
+            line = inter_.activation->line;
+            path = inter_.activation->path;
         } else {
             varlist = new VarList();
             path = "";
@@ -31,8 +31,8 @@ namespace aFuncore {
      * 释放Varlist并且将DownMessage压入上层
      */
     Activation::~Activation(){
-        if (prev != nullptr)
-            down.joinMsg(prev->down);
+        if (inter.activation != nullptr)
+            down.joinMsg(inter.activation->down);
         delete varlist;
     }
 
@@ -151,7 +151,7 @@ namespace aFuncore {
         on_tail = false;  // 跳过所有阶段
         status = func_get_func;
         func = func_;
-        if (prev == nullptr)
+        if (inter_.getActivation() == nullptr)
             varlist->connect(inter_.getGlobalVarlist());
     }
 

+ 3 - 8
src/core/inter.cpp

@@ -52,11 +52,8 @@ namespace aFuncore {
 
         while (activation != nullptr) {
             if (isInterStop()) {
-                while (activation != nullptr) {
-                    Activation *prev = activation->toPrev();
-                    delete activation;
-                    activation = prev;
-                }
+                while (activation != nullptr)
+                    delete popActivation();
                 return false;
             }
 
@@ -64,9 +61,7 @@ namespace aFuncore {
             Activation::ActivationStatus as = activation->getCode(code);
             switch (as) {
                 case Activation::as_end: {
-                    Activation *prev = activation->toPrev();
-                    delete activation;
-                    activation = prev;
+                    delete popActivation();
                     break;
                 }
                 case Activation::as_run:

+ 3 - 3
src/core/msg.cpp

@@ -12,9 +12,9 @@ namespace aFuncore {
         inter.getOutMessageStream().pushMessage("NORMAL", new NormalMessage(std::move(*this)));
     }
 
-    ErrorMessage::ErrorMessage(std::string error_type_, std::string error_info_, Activation *activation)
-        : error_type{std::move(error_type_)}, error_info{std::move(error_info_)}, inter{activation->inter}{
-        for (NULL; activation != nullptr; activation = activation->toPrev()) {
+    ErrorMessage::ErrorMessage(std::string error_type_, std::string error_info_, Activation *start)
+        : error_type{std::move(error_type_)}, error_info{std::move(error_info_)}, inter{start->inter}{
+        for (const auto activation : inter.getStack()) {
             if (activation->getFileLine() != 0)
                 trackback.push_front({activation->getFilePath(), activation->getFileLine()});
         }