소스 검색

refactor: activation提取runCode

SongZihuan 3 년 전
부모
커밋
ea49cdbe6e
2개의 변경된 파일52개의 추가작업 그리고 28개의 파일을 삭제
  1. 6 0
      include/core/activation.h
  2. 46 28
      src/core/activation.cpp

+ 6 - 0
include/core/activation.h

@@ -17,6 +17,12 @@ namespace aFuncore {
 
         StringFilePath path;
         FileLine line;
+
+    protected:
+        virtual void runCodeElement(Code *code);
+        virtual void runCodeBlockP(Code *code);
+        virtual void runCodeBlockC(Code *code);
+        virtual void runCodeBlockB(Code *code);
     public:
         Inter *const inter;
 

+ 46 - 28
src/core/activation.cpp

@@ -51,45 +51,23 @@ Activation::~Activation(){
  * 运行代码
  * @param code
  */
-void Activation::runCode(Code *code){
+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 literaler_name;
-            bool in_protect = false;
-            Object *obj = nullptr;
-            if (inter->checkLiteral(code->getElement(), literaler_name, in_protect)) {
-                if (in_protect)
-                    obj = inter->getProtectVarSpace()->findObject(literaler_name);
-                else
-                    obj = varlist->findObject(literaler_name);
-                auto literaler = dynamic_cast<Literaler *>(obj);
-                if (literaler != nullptr)
-                    literaler->getObject(code->getElement(), code->getPrefix());
-                else
-                    down->pushMessage(new ErrorMessage("TypeError", "Error type of literal.", this));
-            } else {
-                if (varlist != nullptr)
-                    obj = varlist->findObject(code->getElement());
-                if (obj != nullptr) {
-                    auto cbv = dynamic_cast<CallBackVar *>(obj);
-                    if (cbv != nullptr && cbv->isCallBack())
-                        cbv->callBack();
-                    else
-                        down->pushMessage(new NormalMessage(obj));
-                } else
-                    down->pushMessage(new ErrorMessage("NameError", std::string("Variable ") + code->getElement() + " not fount.", this));
-            }
+            runCodeElement(code);
         } else switch (code->getBlockType()) {
             case block_p:  // 顺序执行
-                new ExeActivation(code->getSon(), inter);
+                runCodeBlockP(code);
                 break;
             case block_b:
+                runCodeBlockB(code);
+                break;
             case block_c:
-                new FuncActivation(code, inter);
+                runCodeBlockC(code);
                 break;
             default:
                 errorLog(aFunCoreLogger, "Error block type.");
@@ -98,6 +76,46 @@ void Activation::runCode(Code *code){
     }
 }
 
+void Activation::runCodeElement(Code *code) {
+    std::string literaler_name;
+    bool in_protect = false;
+    Object *obj = nullptr;
+    if (inter->checkLiteral(code->getElement(), literaler_name, in_protect)) {
+        if (in_protect)
+            obj = inter->getProtectVarSpace()->findObject(literaler_name);
+        else
+            obj = varlist->findObject(literaler_name);
+        auto literaler = dynamic_cast<Literaler *>(obj);
+        if (literaler != nullptr)
+            literaler->getObject(code->getElement(), code->getPrefix());
+        else
+            down->pushMessage(new ErrorMessage("TypeError", "Error type of literal.", this));
+    } else {
+        if (varlist != nullptr)
+            obj = varlist->findObject(code->getElement());
+        if (obj != nullptr) {
+            auto cbv = dynamic_cast<CallBackVar *>(obj);
+            if (cbv != nullptr && cbv->isCallBack())
+                cbv->callBack();
+            else
+                down->pushMessage(new NormalMessage(obj));
+        } else
+            down->pushMessage(new ErrorMessage("NameError", std::string("Variable ") + code->getElement() + " not fount.", this));
+    }
+}
+
+void Activation::runCodeBlockP(Code *code) {
+    new ExeActivation(code->getSon(), inter);
+}
+
+void Activation::runCodeBlockC(Code *code) {
+    new FuncActivation(code, inter);
+}
+
+void Activation::runCodeBlockB(Code *code) {
+    new FuncActivation(code, inter);
+}
+
 ActivationStatus ExeActivation::getCode(Code *&code){
     code = next;
     if (code == nullptr)