浏览代码

refactor & feat: 程序运行调整为activation的任务

SongZihuan 3 年之前
父节点
当前提交
e24618bf9e
共有 4 个文件被更改,包括 51 次插入36 次删除
  1. 3 0
      include/core/activation.hpp
  2. 1 1
      include/core/value.hpp
  3. 35 0
      src/core/activation.cpp
  4. 12 35
      src/core/inter.cpp

+ 3 - 0
include/core/activation.hpp

@@ -39,6 +39,7 @@ namespace aFuncore {
 
         virtual ActivationStatus getCode(Code *&code)=0;
         virtual bool onTail()=0;
+        virtual void runCode(Code *code);
 
         [[nodiscard]] VarList *getVarlist() const {return varlist;}
         [[nodiscard]] Activation *toPrev() const {return prev;}
@@ -55,6 +56,8 @@ namespace aFuncore {
 
         ActivationStatus getCode(Code *&code) override;
         bool onTail() override {return false;}
+
+        [[nodiscard]] Code *getStart() const {return start;}
     };
 }
 

+ 1 - 1
include/core/value.hpp

@@ -16,7 +16,7 @@ namespace aFuncore {
         Inter *const inter;
         const std::string type;  // 标识 Object 的字符串
 
-        AFUN_CORE_EXPORT explicit Object(const std::string &type, Inter *inter_);
+        AFUN_CORE_EXPORT explicit Object(const std::string &type_, Inter *inter_);
         AFUN_CORE_EXPORT ~Object() override =default;
     };
 };

+ 35 - 0
src/core/activation.cpp

@@ -1,4 +1,7 @@
 #include "activation.hpp"
+#include "value.hpp"
+#include "init.hpp"
+
 using namespace aFuncore;
 using namespace aFuntool;
 
@@ -35,6 +38,38 @@ Activation::~Activation(){
     delete down;
 }
 
+void Activation::runCode(Code *code){
+    auto code_type = code->getType();
+    if (code_type == code_start) {  // start 不处理 msg
+        auto *none = new Object("None", inter);
+        down->pushMessage(new NormalMessage(none));
+    } else {
+        if (code_type == code_element) {
+            std::string func;
+            bool in_protect = false;
+            if (inter->checkLiteral(code->getElement(), func, in_protect)) {
+                // ...
+            } else {
+                Object *obj = nullptr;
+                if (varlist != nullptr)
+                    obj = varlist->findObject(code->getElement());
+                if (obj != nullptr)
+                    down->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;
+        }
+    }
+}
+
 TopActivation::TopActivation(Code *code, Inter *inter_)
     : Activation(inter_), start{code}, next{code} {
     varlist = inter_->getGlobalVarlist();

+ 12 - 35
src/core/inter.cpp

@@ -91,42 +91,19 @@ bool Inter::runCode(){
 
         Code *code = nullptr;
         ActivationStatus as = activation->getCode(code);
-        if (as == as_end) {  // activation 执行完成
-            Activation *prev = activation->toPrev();
-            delete activation;
-            activation = prev;
-        } 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 {
-                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;
-                }
+        switch (as) {
+            case as_end: {
+                Activation *prev = activation->toPrev();
+                delete activation;
+                activation = prev;
+                break;
             }
+            case as_run:
+                activation->runCode(code);
+                break;
+            default:
+                errorLog(aFunCoreLogger, "Error activation status.");
+                break;
         }
     }
     return true;