Browse Source

refactor & feat: 去除exit函数

SongZihuan 3 years ago
parent
commit
931c08ca05

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

@@ -1,33 +0,0 @@
-#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

+ 0 - 16
include/core/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 aFuncore {
-    inline ExitFunction::ExitFunction(Inter &inter_) : Object("Function", inter_) {
-
-    }
-
-    inline ExitFunction::ExitFunction(Environment &env_) : Object("Function", env_) {
-
-    }
-}
-
-#endif //AFUN_FUNC_EXIT_INLINE_H

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

@@ -1,34 +0,0 @@
-#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

+ 0 - 15
include/core/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 aFuncore {
-    inline ImportFunction::ImportFunction(Inter &inter_) : Object("Function", inter_) {
-
-    }
-
-    inline ImportFunction::ImportFunction(Environment &env_) : Object("Function", env_) {
-
-    }
-}
-
-#endif //AFUN_FUNC_IMPORT_INLINE_H

+ 24 - 0
include/core/object-value.h

@@ -121,6 +121,30 @@ namespace aFuncore {
         virtual bool isCallBack(Inter &inter, Activation &activation);
         virtual void callBack(Inter &inter, Activation &activation) = 0;
     };
+
+    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 "object-value.inline.h"

+ 8 - 0
include/core/object-value.inline.h

@@ -49,6 +49,14 @@ namespace aFuncore {
     Object *Function::CallFunction::ArgCodeList::getObject() {
         return ret;
     }
+
+    inline ImportFunction::ImportFunction(Inter &inter_) : Object("Function", inter_) {
+
+    }
+
+    inline ImportFunction::ImportFunction(Environment &env_) : Object("Function", env_) {
+
+    }
 };
 
 #endif //AFUN_OBJECT_VALUE_INLINE_H

+ 0 - 40
src/core/func-exit.cpp

@@ -1,40 +0,0 @@
-#include "func-exit.h"
-
-namespace aFuncore {
-    Function::CallFunction *ExitFunction::getCallFunction(const Code::ByteCode *code, Inter &inter) {
-        return dynamic_cast<CallFunction *>(new CallFunc(code, 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();
-            if (arg_code != nullptr) {
-                ArgCodeList agr1{code_->getSon()->toNext()};
-                acl->push_front(agr1);
-            }
-        }
-    }
-
-    std::list<Function::CallFunction::ArgCodeList> *ExitFunction::CallFunc::getArgCodeList(Inter &inter_,
-                                                                                                      Activation &activation,
-                                                                                                     const Code::ByteCode *call) {
-        return acl;
-    }
-
-    void ExitFunction::CallFunc::runFunction() {
-        inter.setInterExit();
-        auto &stream = inter.getActivation()->getDownStream();
-        if (acl->empty()) {
-            auto none = new Object("None", inter);
-            stream.pushMessage("NORMAL", new NormalMessage(none));
-            none->delReference();
-        } else
-            stream.pushMessage("NORMAL", new NormalMessage(acl->begin()->getObject()));
-
-    }
-
-    ExitFunction::CallFunc::~CallFunc() {
-        delete acl;
-    }
-}

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

@@ -1,35 +0,0 @@
-#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;
-    }
-}

+ 0 - 8
src/core/inter.cpp

@@ -3,8 +3,6 @@
 #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_)
@@ -213,12 +211,6 @@ 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);

+ 34 - 0
src/core/object-value.cpp

@@ -1,6 +1,8 @@
 #include "object-value.h"
 #include "inter.h"
 #include "core-init.h"
+#include "core-exception.h"
+#include "core-activation.h"
 
 namespace aFuncore {
     Var::Var(Object *data_, Inter &inter) : Object("Var", inter), data{data_}, env{inter.getEnvironment()}{
@@ -170,4 +172,36 @@ namespace aFuncore {
     bool CallBackVar::isCallBack(Inter &inter, Activation &activation) {
         return true;
     }
+
+    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;
+    }
 }

+ 0 - 3
src/tool/CMakeLists.txt

@@ -31,9 +31,6 @@ foreach(tgt tool-shared tool-static)
     target_include_directories(${tgt} PUBLIC ${build_include} ${install_include})
     set_target_properties(${tgt} PROPERTIES
                           PUBLIC_HEADER "${public_h_build}")
-    if(_build_mem)
-        target_compile_definitions(${tgt} PUBLIC BUILD_MEM=1)
-    endif()
     define_FILENAME(${tgt})
 endforeach()
 

+ 0 - 1
test/bytecode/test1.aun

@@ -1,2 +1 @@
 {import aFun-1.0}
-{exit}

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

@@ -283,15 +283,7 @@ int Main() {
         fputs_stdout("\n");
     }
 
-    {
-        fputs_stdout("Test-last: {exit}\n");
-        auto code = Code("run-code.aun");
-        code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_c,
-                                                       new Code::ByteCode(code, "exit", 1), 0));
-        inter.runCode(code);
-        progressInterEvent(inter);
-        fputs_stdout("\n");
-    }
+    inter.setInterExit();
 
     {
         auto code = Code("run-code.aun");