浏览代码

refactor & feat: 添加顺序执行模块

SongZihuan 3 年之前
父节点
当前提交
76f5b1426b
共有 3 个文件被更改,包括 30 次插入24 次删除
  1. 10 5
      include/core/activation.hpp
  2. 19 18
      src/core/activation.cpp
  3. 1 1
      test/src/run-code.cpp

+ 10 - 5
include/core/activation.hpp

@@ -47,17 +47,22 @@ namespace aFuncore {
         [[nodiscard]] DownMessage *getDownStream() const {return down;}
     };
 
-    class TopActivation : public Activation {
+    class ExeActivation : public Activation {
         Code *start;
         Code *next;
+        bool first=true;
+    public:
+        explicit ExeActivation(Code *code, Inter *inter_) : Activation(inter_), start{code}, next{code} {}
+        ActivationStatus getCode(Code *&code) override;
+        bool onTail() override {return next == nullptr;}
+        [[nodiscard]] Code *getStart() const {return start;}
+    };
+
+    class TopActivation : public ExeActivation {
     public:
         explicit TopActivation(Code *code, Inter *inter_);
         ~TopActivation() override;
-
-        ActivationStatus getCode(Code *&code) override;
         bool onTail() override {return false;}
-
-        [[nodiscard]] Code *getStart() const {return start;}
     };
 }
 

+ 19 - 18
src/core/activation.cpp

@@ -58,6 +58,7 @@ void Activation::runCode(Code *code){
             }
         } else switch (code->getBlockType()) {
             case block_p:  // 顺序执行
+                new ExeActivation(code->getSon(), inter);
                 break;
             case block_b:
                 break;
@@ -70,28 +71,12 @@ void Activation::runCode(Code *code){
     }
 }
 
-TopActivation::TopActivation(Code *code, Inter *inter_)
-    : Activation(inter_), start{code}, next{code} {
-    varlist = inter_->getGlobalVarlist();
-    old_varlist = varlist;
-}
-
-static void ActivationTopProgress(Message *msg, void *) {
-    auto *t = dynamic_cast<TopMessage *>(msg);
-    if (t)
-        t->topProgress();
-};
-
-TopActivation::~TopActivation() {
-    down->forEach<void *>(ActivationTopProgress, nullptr);
-}
-
-ActivationStatus TopActivation::getCode(Code *&code) {
+ActivationStatus ExeActivation::getCode(Code *&code){
     code = next;
     if (code == nullptr)
         return as_end;
 
-    if (code->getType() != code_start) {
+    if (!first) {
         Message *msg = down->getMessage<NormalMessage>("NORMAL");
         if (msg == nullptr) {
             return as_end;
@@ -100,6 +85,22 @@ ActivationStatus TopActivation::getCode(Code *&code) {
         delete msg;
     }
 
+    first = false;
     next = code->toNext();
     return as_run;
 }
+
+TopActivation::TopActivation(Code *code, Inter *inter_) : ExeActivation(code, inter_) {
+    varlist = inter_->getGlobalVarlist();
+    old_varlist = varlist;
+}
+
+static void ActivationTopProgress(Message *msg, void *) {
+    auto *t = dynamic_cast<TopMessage *>(msg);
+    if (t)
+        t->topProgress();
+};
+
+TopActivation::~TopActivation() {
+    down->forEach<void *>(ActivationTopProgress, nullptr);
+}

+ 1 - 1
test/src/run-code.cpp

@@ -9,7 +9,7 @@ int main() {
     inter->getGlobalVarlist()->defineVar("test-var", obj);
 
     auto *code = (new Code(0, "run-code.aun"));
-    code->connect(new Code("test-var", 1));
+    code->connect(new Code(block_p, new Code("test-var", 1), 0));
     inter->runCode(code);
     code->destructAll();
     delete inter;