소스 검색

refactor & feat: import和exit改为core函数

SongZihuan 3 년 전
부모
커밋
9db97ad622

+ 33 - 0
include/core/func-exit.h

@@ -0,0 +1,33 @@
+#ifndef AFUN_FUNC_EXIT_H
+#define AFUN_FUNC_EXIT_H
+#include "aFunlangExport.h"
+#include "aFuncore.h"
+
+namespace aFuncore {
+    class ExitFunction : public Function {
+        class CallFunc : public CallFunction {
+            const Code::ByteCode *call_code;
+            Inter &inter;
+            std::list<ArgCodeList> *acl;
+        public:
+            CallFunc(const Code::ByteCode *code_, Inter &inter_);
+            std::list<ArgCodeList> *getArgCodeList(Inter &inter_,
+                                                    Activation &activation,
+                                                   const Code::ByteCode *call) override;
+
+            void runFunction() override;
+            ~CallFunc() override;
+        };
+
+    public:
+        inline explicit ExitFunction(Inter &inter_);
+        inline explicit ExitFunction(Environment &env_);
+        ~ExitFunction() override = default;
+
+        CallFunction *getCallFunction(const Code::ByteCode *code, Inter &inter) override;
+    };
+}
+
+#include "func-exit.inline.h"
+
+#endif //AFUN_FUNC_EXIT_H

+ 16 - 0
include/core/func-exit.inline.h

@@ -0,0 +1,16 @@
+#ifndef AFUN_FUNC_EXIT_INLINE_H
+#define AFUN_FUNC_EXIT_INLINE_H
+
+#include "func-exit.h"
+
+namespace aFuncore {
+    inline ExitFunction::ExitFunction(Inter &inter_) : Object("Function", inter_) {
+
+    }
+
+    inline ExitFunction::ExitFunction(Environment &env_) : Object("Function", env_) {
+
+    }
+}
+
+#endif //AFUN_FUNC_EXIT_INLINE_H

+ 34 - 0
include/core/func-import.h

@@ -0,0 +1,34 @@
+#ifndef AFUN_FUNC_IMPORT_H
+#define AFUN_FUNC_IMPORT_H
+#include "aFunlangExport.h"
+#include "aFuncore.h"
+
+namespace aFuncore {
+    class ImportFunction : public Function {
+        class CallFunc : public CallFunction {
+            const Code::ByteCode *call_code;
+            Inter &inter;
+            std::list<ArgCodeList> *acl;
+            std::string import;
+        public:
+            CallFunc(const Code::ByteCode *code_, Inter &inter_);
+            std::list<ArgCodeList> *getArgCodeList(Inter &inter_,
+                                                    Activation &activation,
+                                                   const Code::ByteCode *call) override;
+
+            void runFunction() override;
+            ~CallFunc() override;
+        };
+
+    public:
+        inline explicit ImportFunction(Inter &inter_);
+        inline explicit ImportFunction(Environment &env_);
+        ~ImportFunction() override = default;
+
+        CallFunction *getCallFunction(const Code::ByteCode *code, Inter &inter) override;
+    };
+}
+
+#include "func-import.inline.h"
+
+#endif //AFUN_FUNC_IMPORT_H

+ 15 - 0
include/core/func-import.inline.h

@@ -0,0 +1,15 @@
+#ifndef AFUN_FUNC_IMPORT_INLINE_H
+#define AFUN_FUNC_IMPORT_INLINE_H
+#include "func-import.h"
+
+namespace aFuncore {
+    inline ImportFunction::ImportFunction(Inter &inter_) : Object("Function", inter_) {
+
+    }
+
+    inline ImportFunction::ImportFunction(Environment &env_) : Object("Function", env_) {
+
+    }
+}
+
+#endif //AFUN_FUNC_IMPORT_INLINE_H

+ 0 - 1
include/interface/aFunit.h

@@ -3,7 +3,6 @@
 
 #include "it-init.h"
 #include "it-reader.h"
-#include "it-inter.h"
 #include "it-exception.h"
 
 #endif //AFUN_AFUNIT_H

+ 0 - 33
include/interface/func-exit.h

@@ -1,33 +0,0 @@
-#ifndef AFUN_FUNC_EXIT_H
-#define AFUN_FUNC_EXIT_H
-#include "aFunlangExport.h"
-#include "aFuncore.h"
-
-namespace aFunit {
-    class ExitFunction : public aFuncore::Function {
-        class CallFunc : public CallFunction {
-            const aFuncore::Code::ByteCode *call_code;
-            aFuncore::Inter &inter;
-            std::list<ArgCodeList> *acl;
-        public:
-            CallFunc(const aFuncore::Code::ByteCode *code_, aFuncore::Inter &inter_);
-            std::list<ArgCodeList> *getArgCodeList(aFuncore::Inter &inter_,
-                                                   aFuncore:: Activation &activation,
-                                                   const aFuncore::Code::ByteCode *call) override;
-
-            void runFunction() override;
-            ~CallFunc() override;
-        };
-
-    public:
-        inline explicit ExitFunction(aFuncore::Inter &inter_);
-        inline explicit ExitFunction(aFuncore::Environment &env_);
-        ~ExitFunction() override = default;
-
-        CallFunction *getCallFunction(const aFuncore::Code::ByteCode *code, aFuncore::Inter &inter) override;
-    };
-}
-
-#include "func-exit.inline.h"
-
-#endif //AFUN_FUNC_EXIT_H

+ 0 - 16
include/interface/func-exit.inline.h

@@ -1,16 +0,0 @@
-#ifndef AFUN_FUNC_EXIT_INLINE_H
-#define AFUN_FUNC_EXIT_INLINE_H
-
-#include "func-exit.h"
-
-namespace aFunit {
-    inline ExitFunction::ExitFunction(aFuncore::Inter &inter_) : Object("Function", inter_) {
-
-    }
-
-    inline ExitFunction::ExitFunction(aFuncore::Environment &env_) : Object("Function", env_) {
-
-    }
-}
-
-#endif //AFUN_FUNC_EXIT_INLINE_H

+ 0 - 34
include/interface/func-import.h

@@ -1,34 +0,0 @@
-#ifndef AFUN_FUNC_IMPORT_H
-#define AFUN_FUNC_IMPORT_H
-#include "aFunlangExport.h"
-#include "aFuncore.h"
-
-namespace aFunit {
-    class ImportFunction : public aFuncore::Function {
-        class CallFunc : public CallFunction {
-            const aFuncore::Code::ByteCode *call_code;
-            aFuncore::Inter &inter;
-            std::list<ArgCodeList> *acl;
-            std::string import;
-        public:
-            CallFunc(const aFuncore::Code::ByteCode *code_, aFuncore::Inter &inter_);
-            std::list<ArgCodeList> *getArgCodeList(aFuncore::Inter &inter_,
-                                                   aFuncore:: Activation &activation,
-                                                   const aFuncore::Code::ByteCode *call) override;
-
-            void runFunction() override;
-            ~CallFunc() override;
-        };
-
-    public:
-        inline explicit ImportFunction(aFuncore::Inter &inter_);
-        inline explicit ImportFunction(aFuncore::Environment &env_);
-        ~ImportFunction() override = default;
-
-        CallFunction *getCallFunction(const aFuncore::Code::ByteCode *code, aFuncore::Inter &inter) override;
-    };
-}
-
-#include "func-import.inline.h"
-
-#endif //AFUN_FUNC_IMPORT_H

+ 0 - 15
include/interface/func-import.inline.h

@@ -1,15 +0,0 @@
-#ifndef AFUN_FUNC_IMPORT_INLINE_H
-#define AFUN_FUNC_IMPORT_INLINE_H
-#include "func-import.h"
-
-namespace aFunit {
-    inline ImportFunction::ImportFunction(aFuncore::Inter &inter_) : Object("Function", inter_) {
-
-    }
-
-    inline ImportFunction::ImportFunction(aFuncore::Environment &env_) : Object("Function", env_) {
-
-    }
-}
-
-#endif //AFUN_FUNC_IMPORT_INLINE_H

+ 0 - 14
include/interface/it-inter.h

@@ -1,14 +0,0 @@
-#ifndef AFUN_IT_INTER_H
-#define AFUN_IT_INTER_H
-#include "aFunlangExport.h"
-#include "aFuncore.h"
-
-namespace aFunit {
-    class AFUN_LANG_EXPORT aFunEnvironment : public aFuncore::Environment {
-    public:
-        explicit aFunEnvironment(int argc = 0, char **argv = nullptr);
-    };
-}
-
-
-#endif //AFUN_IT_INTER_H

+ 9 - 9
src/interface/func-exit.cpp → src/core/func-exit.cpp

@@ -1,11 +1,11 @@
 #include "func-exit.h"
 
-namespace aFunit {
-    aFuncore::Function::CallFunction *ExitFunction::getCallFunction(const aFuncore::Code::ByteCode *code, aFuncore::Inter &inter) {
+namespace aFuncore {
+    Function::CallFunction *ExitFunction::getCallFunction(const Code::ByteCode *code, Inter &inter) {
         return dynamic_cast<CallFunction *>(new CallFunc(code, inter));
     }
 
-    ExitFunction::CallFunc::CallFunc(const aFuncore::Code::ByteCode *code_, aFuncore::Inter &inter_) : call_code{code_}, inter{inter_} {
+    ExitFunction::CallFunc::CallFunc(const Code::ByteCode *code_, Inter &inter_) : call_code{code_}, inter{inter_} {
         acl = new std::list<ArgCodeList>;
         if (code_ != nullptr) {
             auto arg_code = code_->getSon()->toNext();
@@ -16,9 +16,9 @@ namespace aFunit {
         }
     }
 
-    std::list<aFuncore::Function::CallFunction::ArgCodeList> *ExitFunction::CallFunc::getArgCodeList(aFuncore::Inter &inter_,
-                                                                                                     aFuncore:: Activation &activation,
-                                                                                                     const aFuncore::Code::ByteCode *call) {
+    std::list<Function::CallFunction::ArgCodeList> *ExitFunction::CallFunc::getArgCodeList(Inter &inter_,
+                                                                                                      Activation &activation,
+                                                                                                     const Code::ByteCode *call) {
         return acl;
     }
 
@@ -26,11 +26,11 @@ namespace aFunit {
         inter.setInterExit();
         auto &stream = inter.getActivation()->getDownStream();
         if (acl->empty()) {
-            auto none = new aFuncore::Object("None", inter);
-            stream.pushMessage("NORMAL", new aFuncore::NormalMessage(none));
+            auto none = new Object("None", inter);
+            stream.pushMessage("NORMAL", new NormalMessage(none));
             none->delReference();
         } else
-            stream.pushMessage("NORMAL", new aFuncore::NormalMessage(acl->begin()->getObject()));
+            stream.pushMessage("NORMAL", new NormalMessage(acl->begin()->getObject()));
 
     }
 

+ 35 - 0
src/core/func-import.cpp

@@ -0,0 +1,35 @@
+#include "func-import.h"
+
+namespace aFuncore {
+    Function::CallFunction *ImportFunction::getCallFunction(const Code::ByteCode *code, Inter &inter) {
+        return dynamic_cast<CallFunction *>(new CallFunc(code, inter));
+    }
+
+    ImportFunction::CallFunc::CallFunc(const Code::ByteCode *code_, Inter &inter_) : call_code{code_}, inter{inter_} {
+        if (code_ == nullptr ||
+            code_->getSon() == nullptr ||
+            code_->getSon()->toNext() == nullptr ||
+            code_->getSon()->toNext()->getType() != Code::ByteCode::code_element)
+            throw ArgumentError();
+        acl = new std::list<ArgCodeList>;
+        import = code_->getSon()->toNext()->getElement();
+    }
+
+    std::list<Function::CallFunction::ArgCodeList> *ImportFunction::CallFunc::getArgCodeList(Inter &inter_,
+                                                                                                        Activation &activation,
+                                                                                                       const Code::ByteCode *call) {
+        return acl;
+    }
+
+    void ImportFunction::CallFunc::runFunction() {
+        auto &stream = inter.getActivation()->getDownStream();
+        auto none = new Object("None", inter);
+        stream.pushMessage("NORMAL", new NormalMessage(none));
+        none->delReference();
+        aFuntool::cout << "Import " << import << "\n";
+    }
+
+    ImportFunction::CallFunc::~CallFunc() {
+        delete acl;
+    }
+}

+ 14 - 0
src/core/inter.cpp

@@ -3,6 +3,8 @@
 #include "core-init.h"
 #include "msg.h"
 #include "core-exception.h"
+#include "func-import.h"
+#include "func-exit.h"
 
 namespace aFuncore {
     Inter::Inter(Environment &env_)
@@ -210,6 +212,18 @@ namespace aFuncore {
         }
 
         gc_thread = std::thread([this](){this->gcThread();});
+
+        {  // 退出函数
+            auto exit = new ExitFunction(*this);
+            protect->defineVar("exit", exit);
+            exit->delReference();
+        }
+
+        {  // 导入函数
+            auto import = new ImportFunction(*this);
+            protect->defineVar("import", import);
+            import->delReference();
+        }
     }
 
     Environment::~Environment() noexcept(false) {

+ 0 - 35
src/interface/func-import.cpp

@@ -1,35 +0,0 @@
-#include "func-import.h"
-
-namespace aFunit {
-    aFuncore::Function::CallFunction *ImportFunction::getCallFunction(const aFuncore::Code::ByteCode *code, aFuncore::Inter &inter) {
-        return dynamic_cast<CallFunction *>(new CallFunc(code, inter));
-    }
-
-    ImportFunction::CallFunc::CallFunc(const aFuncore::Code::ByteCode *code_, aFuncore::Inter &inter_) : call_code{code_}, inter{inter_} {
-        if (code_ == nullptr ||
-            code_->getSon() == nullptr ||
-            code_->getSon()->toNext() == nullptr ||
-            code_->getSon()->toNext()->getType() != aFuncore::Code::ByteCode::code_element)
-            throw aFuncore::ArgumentError();
-        acl = new std::list<ArgCodeList>;
-        import = code_->getSon()->toNext()->getElement();
-    }
-
-    std::list<aFuncore::Function::CallFunction::ArgCodeList> *ImportFunction::CallFunc::getArgCodeList(aFuncore::Inter &inter_,
-                                                                                                       aFuncore:: Activation &activation,
-                                                                                                       const aFuncore::Code::ByteCode *call) {
-        return acl;
-    }
-
-    void ImportFunction::CallFunc::runFunction() {
-        auto &stream = inter.getActivation()->getDownStream();
-        auto none = new aFuncore::Object("None", inter);
-        stream.pushMessage("NORMAL", new aFuncore::NormalMessage(none));
-        none->delReference();
-        aFuntool::cout << "Import " << import << "\n";
-    }
-
-    ImportFunction::CallFunc::~CallFunc() {
-        delete acl;
-    }
-}

+ 0 - 19
src/interface/it-inter.cpp

@@ -1,19 +0,0 @@
-#include "it-inter.h"
-#include "func-exit.h"
-#include "func-import.h"
-
-namespace aFunit {
-    aFunEnvironment::aFunEnvironment(int argc, char **argv) : Environment(argc, argv)  {
-        {  // 退出函数
-            auto exit = new ExitFunction(*this);
-            protect->defineVar("exit", exit);
-            exit->delReference();
-        }
-
-        {  // 导入函数
-            auto import = new ImportFunction(*this);
-            protect->defineVar("import", import);
-            import->delReference();
-        }
-    }
-}

+ 2 - 2
src/main-run.cpp

@@ -13,7 +13,7 @@ static void runCodeThread(aFun::Inter &inter, aFun::Code &code, std::mutex &mute
     is_end = true;
 }
 
-static int runCode(aFun::Code &code, aFun::aFunEnvironment &env, aFun::Inter &inter, int argc, char **argv) {
+static int runCode(aFun::Code &code, aFun::Environment &env, aFun::Inter &inter, int argc, char **argv) {
     std::mutex mutex;
     bool is_end = false;
     auto thread = std::thread(runCodeThread, std::ref(inter), std::ref(code), std::ref(mutex), std::ref(is_end));
@@ -49,7 +49,7 @@ int mainRun(ff_FFlags *ff) {
             printParserError(parser, argv[0]);
             if (!ret)
                 return EXIT_FAILURE;
-            auto env = aFun::aFunEnvironment(argc - 1, argv + 1);
+            auto env = aFun::Environment(argc - 1, argv + 1);
             auto inter = aFun::Inter(env);
             return runCode(code, env, inter, argc, argv);
         } catch (aFun::readerFileOpenError &e) {

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

@@ -142,7 +142,7 @@ static void thread_test(Inter &son) {
 }
 
 int Main() {
-    aFunEnvironment env{};
+    Environment env{};
     Inter inter {env};
 
     auto obj = new Object("Object", inter);