Browse Source

refactor & feat: msg处理调整为activation的任务

SongZihuan 3 năm trước cách đây
mục cha
commit
a906079508
4 tập tin đã thay đổi với 62 bổ sung48 xóa
  1. 2 1
      .gitignore
  2. 7 2
      include/core/activation.hpp
  3. 15 6
      src/core/activation.cpp
  4. 38 39
      src/core/inter.cpp

+ 2 - 1
.gitignore

@@ -57,4 +57,5 @@ dkms.conf
 .vs
 .vscode
 cmake-*
-out
+out
+CMakePresets.json

+ 7 - 2
include/core/activation.hpp

@@ -6,6 +6,11 @@
 namespace aFuncore {
     class Activation;
     class TopActivation;
+
+    typedef enum ActivationStatus {
+        as_run = 0,
+        as_end = 1,
+    } ActivationStatus;
 }
 
 #include "msg.hpp"
@@ -32,7 +37,7 @@ namespace aFuncore {
         explicit Activation(Inter *inter_);
         virtual ~Activation();
 
-        virtual Code *getCode()=0;
+        virtual ActivationStatus getCode(Code *&code)=0;
         virtual bool onTail()=0;
 
         [[nodiscard]] VarList *getVarlist() const {return varlist;}
@@ -48,7 +53,7 @@ namespace aFuncore {
         explicit TopActivation(Code *code, Inter *inter_);
         ~TopActivation() override;
 
-        Code *getCode() override;
+        ActivationStatus getCode(Code *&code) override;
         bool onTail() override {return false;}
     };
 }

+ 15 - 6
src/core/activation.cpp

@@ -51,11 +51,20 @@ TopActivation::~TopActivation() {
     down->forEach<void *>(ActivationTopProgress, nullptr);
 }
 
-Code *TopActivation::getCode(){
-    Code *ret = next;
-    if (ret == nullptr)
-        return nullptr;
+ActivationStatus TopActivation::getCode(Code *&code) {
+    code = next;
+    if (code == nullptr)
+        return as_end;
 
-    next = ret->toNext();
-    return ret;
+    if (code->getType() != code_start) {
+        Message *msg = down->getMessage<NormalMessage>("NORMAL");
+        if (msg == nullptr) {
+            return as_end;
+        } else
+            msg = down->popMessage("NORMAL");
+        delete msg;
+    }
+
+    next = code->toNext();
+    return as_run;
 }

+ 38 - 39
src/core/inter.cpp

@@ -81,53 +81,52 @@ void Inter::enable(){
 bool Inter::runCode(){
     while (activation != nullptr) {
         if (isExit()) {
-            // TODO-szh 弹出所有activation
+            while (activation != nullptr) {
+                Activation *prev = activation->toPrev();
+                delete activation;
+                activation = prev;
+            }
             return false;
         }
 
-        Code *code = activation->getCode();
-        if (code == nullptr) {  // activation 执行完成
+        Code *code = nullptr;
+        ActivationStatus as = activation->getCode(code);
+        if (as == as_end) {  // activation 执行完成
             Activation *prev = activation->toPrev();
             delete activation;
             activation = prev;
-            continue;
-        }
-
-        auto code_type = code->getType();
-        if (code_type == code_start)
-            continue;
-
-        Message *msg = activation->getDownStream()->popMessage("NORMAL");
-        if (msg == nullptr) {
-            // ... 出现异常
-        }
-
-        delete msg;
-
-        if (code_type == code_element) {
-            std::string func;
-            bool in_protect = false;
-            if (checkLiteral(code->getElement(), func, in_protect)) {
-                // ...
+        } else {
+            auto code_type = code->getType();
+            if (code_type == code_start) {  // start 不处理 msg
+                auto *none = new Object("None", this);
+                activation->getDownStream()->pushMessage(new NormalMessage(none));
             } else {
-                auto varlist = activation->getVarlist();
-                Object *obj = nullptr;
-                if (varlist != nullptr)
-                    obj = varlist->findObject(code->getElement());
-                if (obj != nullptr)
-                    activation->getDownStream()->pushMessage(new NormalMessage(obj));
+                if (code_type == code_element) {
+                    std::string func;
+                    bool in_protect = false;
+                    if (checkLiteral(code->getElement(), func, in_protect)) {
+                        // ...
+                    } else {
+                        auto varlist = activation->getVarlist();
+                        Object *obj = nullptr;
+                        if (varlist != nullptr)
+                            obj = varlist->findObject(code->getElement());
+                        if (obj != nullptr)
+                            activation->getDownStream()->pushMessage(new NormalMessage(obj));
+                    }
+
+                } else switch (code->getBlockType()) {
+                    case block_p:  // 顺序执行
+                        break;
+                    case block_b:
+                        break;
+                    case block_c:
+                        break;
+                    default:
+                        errorLog(aFunCoreLogger, "Error block type.");
+                        break;
+                }
             }
-
-        } else switch (code->getBlockType()) {
-            case block_p:
-                break;
-            case block_b:
-                break;
-            case block_c:
-                break;
-            default:
-                errorLog(aFunCoreLogger, "Error block type.");
-                break;
         }
     }
     return true;