Pārlūkot izejas kodu

refactor & feat: 设定Inter退出模式

SongZihuan 3 gadi atpakaļ
vecāks
revīzija
218face716
4 mainītis faili ar 52 papildinājumiem un 41 dzēšanām
  1. 6 19
      include/core/inter.h
  2. 18 1
      include/core/inter.inline.h
  3. 15 21
      src/core/inter.cpp
  4. 13 0
      test/src/run-code.cpp

+ 6 - 19
include/core/inter.h

@@ -59,17 +59,6 @@ namespace aFuncore {
             inter_exit = 4,  // 解释器退出
         } InterStatus;
 
-        typedef enum ExitFlat {
-            ef_activity = 0,  // 主动退出
-            ef_passive = 1,  // 被动退出
-            ef_none = 2,
-        } ExitFlat;
-
-        typedef enum ExitMode {
-            em_activity = ef_activity,  // 主动退出
-            em_passive = ef_passive,  // 被动退出
-        } ExitMode;
-
         typedef enum Prefix {
             prefix_quote = 0,  // 变量引用
             prefix_exec_first = 1,
@@ -80,15 +69,16 @@ namespace aFuncore {
         constexpr static const char *B_PREFIX = "$`'%^&<?>";  /* NOLINT block前缀 */
         constexpr static const char *ALL_PREFIX = "$`'%^&<?>";  /* NOLINT block前缀 */
 
-        explicit Inter(Environment &env_, int argc = 0, char **argv = nullptr, ExitMode em = em_activity);
-        Inter(const Inter &base_inter, ExitMode em = em_activity);
+        explicit Inter(Environment &env_, int argc = 0, char **argv = nullptr);
+        Inter(const Inter &base_inter);
         ~Inter();
         Inter &operator=(const Inter &) = delete;
 
         void enable();
 
         [[nodiscard]] inline InterStatus getStatus() const;
-        [[nodiscard]] inline bool isExit() const;
+        [[nodiscard]] inline bool isInterStop() const;
+        [[nodiscard]] inline bool isInterExit() const;
         [[nodiscard]] inline Environment &getEnvironment();
         [[nodiscard]] inline ProtectVarSpace *getProtectVarSpace() const;
         [[nodiscard]] inline VarSpace *getGlobalVarSpace() const;
@@ -105,6 +95,8 @@ namespace aFuncore {
         bool runCode();
         bool runCode(Code &code);
 
+        inline InterStatus setInterStop();
+        inline InterStatus setInterExit();
     private:
         InterStatus status;
 
@@ -117,11 +109,6 @@ namespace aFuncore {
 
         std::list<LiteralRegex> literal;
 
-        Object *result;  // 线程执行的结果
-
-        ExitFlat exit_flat;  // 外部设置退出
-        ExitMode exit_mode;  // 退出模式
-
         inline void pushActivation(Activation *new_activation);
     };
 

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

@@ -15,10 +15,14 @@ namespace aFuncore {
         return status;
     }
 
-    inline bool Inter::isExit() const {
+    inline bool Inter::isInterStop() const {
         return (status == inter_exit || status == inter_stop);
     }
 
+    inline bool Inter::isInterExit() const {
+        return (status == inter_exit);
+    }
+
     inline ProtectVarSpace *Inter::getProtectVarSpace() const {
         return env.protect;
     }
@@ -62,6 +66,19 @@ namespace aFuncore {
     inline size_t Environment::operator--(int){
         return reference--;
     }
+
+    inline Inter::InterStatus Inter::setInterStop() {
+        InterStatus ret = status;
+        if (status != inter_exit)
+            status = inter_stop;
+        return ret;
+    }
+
+    inline Inter::InterStatus Inter::setInterExit() {
+        InterStatus ret = status;
+        status = inter_exit;
+        return ret;
+    }
 }
 
 #endif //AFUN_INTER_INLINE_H

+ 15 - 21
src/core/inter.cpp

@@ -5,7 +5,7 @@
 #include "core-exception.h"
 
 namespace aFuncore {
-    Inter::Inter(Environment &env_, int argc, char **argv, ExitMode em)
+    Inter::Inter(Environment &env_, int argc, char **argv)
             : out{}, in{}, env{env_}{
         status = inter_creat;
 
@@ -23,16 +23,11 @@ namespace aFuncore {
             env.envvar.setString(buf, argv[i]);
         }
 
-        result = nullptr;
-
-        exit_flat = ef_none;
-        exit_mode = em;
-
         status = inter_init;
         env++;
     }
 
-    Inter::Inter(const Inter &base_inter, ExitMode em)
+    Inter::Inter(const Inter &base_inter)
             : out{}, in{}, env{base_inter.env}{
         status = inter_creat;
 
@@ -41,10 +36,6 @@ namespace aFuncore {
         for (auto &i: base_inter.literal)
             literal.push_back(i);
 
-        result = nullptr;
-        exit_flat = ef_none;
-        exit_mode = em;
-
         status = inter_normal;
         env++;
     }
@@ -68,7 +59,19 @@ namespace aFuncore {
      * @return
      */
     bool Inter::runCode(){
+        if (status == inter_stop)
+            status = inter_normal;
+
         while (activation != nullptr) {
+            if (isInterStop()) {
+                while (activation != nullptr) {
+                    Activation *prev = activation->toPrev();
+                    delete activation;
+                    activation = prev;
+                }
+                return false;
+            }
+
             Code::ByteCode *code = nullptr;
             Activation::ActivationStatus as = activation->getCode(code);
             switch (as) {
@@ -87,18 +90,9 @@ namespace aFuncore {
                 default:
                     errorLog(aFunCoreLogger, "Error activation status.");
                     activation->getDownStream().pushMessage("ERROR",
-                            new ErrorMessage("RuntimeError", "Error activation status.", activation));
+                                                            new ErrorMessage("RuntimeError", "Error activation status.", activation));
                     break;
             }
-
-            if (isExit()) {
-                while (activation != nullptr) {
-                    Activation *prev = activation->toPrev();
-                    delete activation;
-                    activation = prev;
-                }
-                return false;
-            }
         }
         return true;
     }

+ 13 - 0
test/src/run-code.cpp

@@ -187,6 +187,7 @@ int main() {
         fputs_stdout("\n");
     }
 
+    /* 执行错误的代码 */
     {
         auto code = Code("run-code.aun");
         code.getByteCode()->connect(new Code::ByteCode(code, "test-not-var", 1));
@@ -195,6 +196,7 @@ int main() {
         fputs_stdout("\n");
     }
 
+    /* 多线程 */
     {
         Inter son {inter};
         auto code = Code("run-code.aun");
@@ -204,5 +206,16 @@ int main() {
         fputs_stdout("\n");
     }
 
+    /* 不会执行的代码 */
+    inter.setInterExit();
+
+    {
+        auto code = Code("run-code.aun");
+        code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
+                                                       new Code::ByteCode(code, "test-var", 1), 0));
+        inter.runCode(code);
+        printInterEvent(inter);
+    }
+
     return 0;
 }