Browse Source

refactor & feat: 应用Inter的out和in

SongZihuan 3 years ago
parent
commit
65bb913132
6 changed files with 91 additions and 25 deletions
  1. 2 0
      include/core/inter.h
  2. 8 0
      include/core/inter.inline.h
  3. 14 8
      include/core/msg.h
  4. 16 2
      include/core/msg.inline.h
  5. 11 15
      src/core/msg.cpp
  6. 40 0
      test/src/run-code.cpp

+ 2 - 0
include/core/inter.h

@@ -97,6 +97,8 @@ namespace aFuncore {
         [[nodiscard]] bool checkLiteral(const std::string &element) const;
         [[nodiscard]] bool checkLiteral(const std::string &element) const;
         [[nodiscard]] bool checkLiteral(const std::string &element, std::string &literaler, bool &in_protect) const;
         [[nodiscard]] bool checkLiteral(const std::string &element, std::string &literaler, bool &in_protect) const;
         [[nodiscard]] inline EnvVarSpace &getEnvVarSpace();
         [[nodiscard]] inline EnvVarSpace &getEnvVarSpace();
+        [[nodiscard]] inline InterMessage &getOutMessageStream();
+        [[nodiscard]] inline InterMessage &getInMessageStream();
 
 
         bool pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect);
         bool pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect);
 
 

+ 8 - 0
include/core/inter.inline.h

@@ -39,6 +39,14 @@ namespace aFuncore {
         return env.envvar;
         return env.envvar;
     }
     }
 
 
+    inline InterMessage &Inter::getOutMessageStream() {
+        return out;
+    }
+
+    inline InterMessage &Inter::getInMessageStream() {
+        return in;
+    }
+
     inline size_t Environment::operator++(){
     inline size_t Environment::operator++(){
         return ++reference;
         return ++reference;
     }
     }

+ 14 - 8
include/core/msg.h

@@ -34,6 +34,7 @@ namespace aFuncore {
     class AFUN_CORE_EXPORT NormalMessage : public TopMessage {
     class AFUN_CORE_EXPORT NormalMessage : public TopMessage {
     public:
     public:
         explicit inline NormalMessage(Object *obj_);
         explicit inline NormalMessage(Object *obj_);
+        inline NormalMessage(NormalMessage &&msg) noexcept;
         ~NormalMessage() override;
         ~NormalMessage() override;
         void topProgress(Inter &inter, Activation &activation) override;
         void topProgress(Inter &inter, Activation &activation) override;
         inline Object *getObject();
         inline Object *getObject();
@@ -44,20 +45,23 @@ namespace aFuncore {
 
 
     class AFUN_CORE_EXPORT ErrorMessage : public TopMessage {
     class AFUN_CORE_EXPORT ErrorMessage : public TopMessage {
     public:
     public:
+        struct TrackBack {
+            const aFuntool::FilePath path;
+            aFuntool::FileLine line;
+        };
+
         explicit ErrorMessage(std::string error_type_, std::string error_info_, Activation *activation);
         explicit ErrorMessage(std::string error_type_, std::string error_info_, Activation *activation);
+        inline ErrorMessage(ErrorMessage &&msg) noexcept;
         void topProgress(Inter &inter_, Activation &activation) override;
         void topProgress(Inter &inter_, Activation &activation) override;
-        inline std::string getErrorType();
-        inline std::string getErrorInfo();
+        [[nodiscard]] inline std::string getErrorType() const;
+        [[nodiscard]] inline std::string getErrorInfo() const;
+        [[nodiscard]] inline const std::list<TrackBack> &getTrackBack() const;
 
 
     private:
     private:
         Inter &inter;
         Inter &inter;
 
 
         std::string error_type;
         std::string error_type;
         std::string error_info;
         std::string error_info;
-        struct TrackBack{
-            const aFuntool::FilePath path;
-            aFuntool::FileLine line;
-        };
         std::list<TrackBack> trackback;
         std::list<TrackBack> trackback;
     };
     };
 
 
@@ -70,7 +74,7 @@ namespace aFuncore {
         template<class T>
         template<class T>
         [[nodiscard]] T *getMessage(const std::string &type) const;
         [[nodiscard]] T *getMessage(const std::string &type) const;
 
 
-        virtual Message *popMessage(const std::string &type);
+        Message *popMessage(const std::string &type);
         void pushMessage(Message *msg);
         void pushMessage(Message *msg);
 
 
         template <typename Callable, typename...T>
         template <typename Callable, typename...T>
@@ -86,7 +90,7 @@ namespace aFuncore {
         explicit UpMessage(const UpMessage *old=nullptr);
         explicit UpMessage(const UpMessage *old=nullptr);
         ~UpMessage() override;
         ~UpMessage() override;
 
 
-        Message *popMessage(const std::string &type) override;
+        Message *popMessage(const std::string &type);
 
 
     protected:
     protected:
         Message *old;
         Message *old;
@@ -99,6 +103,8 @@ namespace aFuncore {
 
 
     class AFUN_CORE_EXPORT InterMessage : public MessageStream {
     class AFUN_CORE_EXPORT InterMessage : public MessageStream {
         std::mutex mutex;
         std::mutex mutex;
+    public:
+        Message *popFrontMessage();
     };
     };
 }
 }
 
 

+ 16 - 2
include/core/msg.inline.h

@@ -11,17 +11,31 @@ namespace aFuncore {
 
 
     }
     }
 
 
+    inline NormalMessage::NormalMessage(NormalMessage &&msg) noexcept : Message("NORMAL"), obj {msg.obj}{
+        msg.obj = nullptr;
+    }
+
     inline Object *NormalMessage::getObject() {
     inline Object *NormalMessage::getObject() {
         return obj;
         return obj;
     }
     }
 
 
-    inline std::string ErrorMessage::getErrorType() {
+    inline ErrorMessage::ErrorMessage(ErrorMessage &&msg) noexcept
+        : Message("ERROR"), error_type{std::move(msg.error_type)}, error_info{std::move(msg.error_info)},
+          trackback{std::move(msg.trackback)}, inter{msg.inter}{
+
+    }
+
+    inline std::string ErrorMessage::getErrorType() const {
         return error_type;
         return error_type;
     }
     }
 
 
-    inline std::string ErrorMessage::getErrorInfo() {
+    inline std::string ErrorMessage::getErrorInfo() const {
         return error_info;
         return error_info;
     }
     }
+
+    inline const std::list<ErrorMessage::TrackBack> &ErrorMessage::getTrackBack() const {
+        return trackback;
+    }
 }
 }
 
 
 #endif //AFUN_MSG_INLINE_H
 #endif //AFUN_MSG_INLINE_H

+ 11 - 15
src/core/msg.cpp

@@ -9,10 +9,10 @@ namespace aFuncore {
     }
     }
 
 
     void NormalMessage::topProgress(Inter &inter, Activation &activation){
     void NormalMessage::topProgress(Inter &inter, Activation &activation){
-        aFuntool::printf_stdout(0, "NORMAL: %p\n", obj);  // TODO-szh 使用 Event
+        inter.getOutMessageStream().pushMessage(new NormalMessage(std::move(*this)));
     }
     }
 
 
-    ErrorMessage::ErrorMessage(std::string error_type_, std::string error_info_, Activation *activation)  // TODO-szh 使用Event
+    ErrorMessage::ErrorMessage(std::string error_type_, std::string error_info_, Activation *activation)
             : Message("ERROR"), error_type{std::move(error_type_)}, error_info{std::move(error_info_)}, inter{activation->inter}{
             : Message("ERROR"), error_type{std::move(error_type_)}, error_info{std::move(error_info_)}, inter{activation->inter}{
         for (NULL; activation != nullptr; activation = activation->toPrev()) {
         for (NULL; activation != nullptr; activation = activation->toPrev()) {
             if (activation->getFileLine() != 0)
             if (activation->getFileLine() != 0)
@@ -21,19 +21,7 @@ namespace aFuncore {
     }
     }
 
 
     void ErrorMessage::topProgress(Inter &inter_, Activation &activation){
     void ErrorMessage::topProgress(Inter &inter_, Activation &activation){
-        int32_t error_std = 0;
-        inter.getEnvVarSpace().findNumber("sys:error_std", error_std);
-        if (error_std == 0) {
-            aFuntool::printf_stderr(0, "Error TrackBack\n");
-            for (auto &begin: trackback)
-                aFuntool::printf_stderr(0, "  File \"%s\", line %d\n", begin.path.c_str(), begin.line);
-            aFuntool::printf_stderr(0, "%s: %s\n", error_type.c_str(), error_info.c_str());
-        } else {
-            aFuntool::printf_stdout(0, "Error TrackBack\n");
-            for (auto &begin: trackback)
-                aFuntool::printf_stdout(0, "  File \"%s\", line %d\n", begin.path.c_str(), begin.line);
-            aFuntool::printf_stdout(0, "%s: %s\n", error_type.c_str(), error_info.c_str());
-        }
+        inter_.getOutMessageStream().pushMessage(new ErrorMessage(std::move(*this)));
     }
     }
 
 
     MessageStream::MessageStream(){
     MessageStream::MessageStream(){
@@ -137,4 +125,12 @@ namespace aFuncore {
         msg.stream = m;
         msg.stream = m;
         stream = nullptr;
         stream = nullptr;
     }
     }
+
+    Message *InterMessage::popFrontMessage() {
+        if (stream == nullptr)
+            return nullptr;
+        Message *ret = stream;
+        stream = ret->next;
+        return ret;
+    }
 }
 }

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

@@ -81,6 +81,39 @@ public:
     }
     }
 };
 };
 
 
+static void printMessage(Message *msg, Inter &inter) {
+    if (msg->type == "NORMAL") {
+        auto *msg_ = dynamic_cast<NormalMessage *>(msg);
+        if (msg_ == nullptr)
+            return;
+        aFuntool::printf_stdout(0, "NORMAL: %p\n", msg_->getObject());
+    } else if (msg->type == "ERROR") {
+        auto *msg_ = dynamic_cast<ErrorMessage *>(msg);
+        if (msg_ == nullptr)
+            return;
+        int32_t error_std = 0;
+        inter.getEnvVarSpace().findNumber("sys:error_std", error_std);
+        if (error_std == 0) {
+            aFuntool::printf_stderr(0, "Error TrackBack\n");
+            for (auto &begin: msg_->getTrackBack())
+                aFuntool::printf_stderr(0, "  File \"%s\", line %d\n", begin.path.c_str(), begin.line);
+            aFuntool::printf_stderr(0, "%s: %s\n", msg_->getErrorType().c_str(), msg_->getErrorInfo().c_str());
+        } else {
+            aFuntool::printf_stdout(0, "Error TrackBack\n");
+            for (auto &begin: msg_->getTrackBack())
+                aFuntool::printf_stdout(0, "  File \"%s\", line %d\n", begin.path.c_str(), begin.line);
+            aFuntool::printf_stdout(0, "%s: %s\n", msg_->getErrorType().c_str(), msg_->getErrorInfo().c_str());
+        }
+    }
+}
+
+void printInterEvent(Inter &inter) {
+    for (auto msg = inter.getOutMessageStream().popFrontMessage(); msg != nullptr; msg = inter.getOutMessageStream().popFrontMessage()) {
+        printMessage(msg, inter);
+        delete msg;
+    }
+}
+
 int main() {
 int main() {
     Environment env {};
     Environment env {};
     Inter inter {env};
     Inter inter {env};
@@ -107,6 +140,7 @@ int main() {
         code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
         code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
                                                        new Code::ByteCode(code, "test-var", 1), 0));
                                                        new Code::ByteCode(code, "test-var", 1), 0));
         inter.runCode(code);
         inter.runCode(code);
+        printInterEvent(inter);
         fputs_stdout("\n");
         fputs_stdout("\n");
     }
     }
 
 
@@ -119,6 +153,7 @@ int main() {
         code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_c, arg, 0));
         code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_c, arg, 0));
 
 
         inter.runCode(code);
         inter.runCode(code);
+        printInterEvent(inter);
         fputs_stdout("\n");
         fputs_stdout("\n");
     }
     }
 
 
@@ -130,6 +165,7 @@ int main() {
 
 
         code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_b, arg, 0));
         code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_b, arg, 0));
         inter.runCode(code);
         inter.runCode(code);
+        printInterEvent(inter);
         fputs_stdout("\n");
         fputs_stdout("\n");
     }
     }
 
 
@@ -138,6 +174,7 @@ int main() {
         auto code = Code("run-code.aun");
         auto code = Code("run-code.aun");
         code.getByteCode()->connect(new Code::ByteCode(code, "data3", 1));
         code.getByteCode()->connect(new Code::ByteCode(code, "data3", 1));
         inter.runCode(code);
         inter.runCode(code);
+        printInterEvent(inter);
         fputs_stdout("\n");
         fputs_stdout("\n");
     }
     }
 
 
@@ -145,6 +182,7 @@ int main() {
         auto code = Code("run-code.aun");
         auto code = Code("run-code.aun");
         code.getByteCode()->connect(new Code::ByteCode(code, "test-cbv", 1));
         code.getByteCode()->connect(new Code::ByteCode(code, "test-cbv", 1));
         inter.runCode(code);
         inter.runCode(code);
+        printInterEvent(inter);
         fputs_stdout("\n");
         fputs_stdout("\n");
     }
     }
 
 
@@ -152,6 +190,7 @@ int main() {
         auto code = Code("run-code.aun");
         auto code = Code("run-code.aun");
         code.getByteCode()->connect(new Code::ByteCode(code, "test-not-var", 1));
         code.getByteCode()->connect(new Code::ByteCode(code, "test-not-var", 1));
         inter.runCode(code);
         inter.runCode(code);
+        printInterEvent(inter);
         fputs_stdout("\n");
         fputs_stdout("\n");
     }
     }
 
 
@@ -160,6 +199,7 @@ int main() {
         auto code = Code("run-code.aun");
         auto code = Code("run-code.aun");
         code.getByteCode()->connect(new Code::ByteCode(code, "test-not-var", 1));
         code.getByteCode()->connect(new Code::ByteCode(code, "test-not-var", 1));
         son.runCode(code);
         son.runCode(code);
+        printInterEvent(son);
         fputs_stdout("\n");
         fputs_stdout("\n");
     }
     }