Explorar o código

refactor & feat: core面向对象改造

SongZihuan %!s(int64=3) %!d(string=hai) anos
pai
achega
241a198d71

+ 13 - 13
include/core/activation.h

@@ -4,6 +4,7 @@
 #include "aFunCoreExport.h"
 #include "core.h"
 #include "value.h"
+#include "msg.h"
 
 namespace aFuncore {
     class AFUN_CORE_EXPORT Activation {
@@ -12,13 +13,12 @@ namespace aFuncore {
 
         VarList *varlist;
 
-        UpMessage *up;
-        DownMessage *down;
+        UpMessage up;
+        DownMessage down;
 
         StringFilePath path;
         FileLine line;
 
-    protected:
         virtual void runCodeElement(Code *code);
         virtual void runCodeBlockP(Code *code);
         virtual void runCodeBlockC(Code *code);
@@ -35,15 +35,15 @@ namespace aFuncore {
 
         virtual ActivationStatus getCode(Code *&code) = 0;
         virtual void runCode(Code *code);
-        virtual void endRun();
+        virtual inline void endRun();
 
-        [[nodiscard]] VarList *getVarlist() const;
-        [[nodiscard]] Activation *toPrev() const;
-        [[nodiscard]] UpMessage *getUpStream() const;
-        [[nodiscard]] DownMessage *getDownStream() const;
+        [[nodiscard]] inline VarList *getVarlist() const;
+        [[nodiscard]] inline Activation *toPrev() const;
+        [[nodiscard]] inline UpMessage &getUpStream();
+        [[nodiscard]] inline DownMessage &getDownStream();
 
-        [[nodiscard]] FileLine getFileLine() const;
-        [[nodiscard]] const StringFilePath &getFilePath() const;
+        [[nodiscard]] inline FileLine getFileLine() const;
+        [[nodiscard]] inline  const StringFilePath &getFilePath() const;
     };
 
     class AFUN_CORE_EXPORT ExeActivation : public Activation {
@@ -51,9 +51,9 @@ namespace aFuncore {
         Code *next;
         bool first=true;
     public:
-        explicit ExeActivation(Code *code, Inter &inter_);
+        explicit inline  ExeActivation(Code *code, Inter &inter_);
         ActivationStatus getCode(Code *&code) override;
-        [[nodiscard]] Code *getStart() const;
+        [[nodiscard]] inline  Code *getStart() const;
     };
 
     class AFUN_CORE_EXPORT TopActivation : public ExeActivation {
@@ -79,7 +79,7 @@ namespace aFuncore {
         std::list<Function::CallFunction::ArgCodeList>::iterator acl_begin;
         std::list<Function::CallFunction::ArgCodeList>::iterator acl_end;
     public:
-        explicit FuncActivation(Code *code, Inter &inter_);
+        explicit inline FuncActivation(Code *code, Inter &inter_);
         ~FuncActivation() override;
         ActivationStatus getCode(Code *&code) override;
         void endRun() override;

+ 2 - 2
include/core/activation.inline.h

@@ -16,11 +16,11 @@ namespace aFuncore {
         return prev;
     }
 
-    inline UpMessage *Activation::getUpStream() const{
+    inline UpMessage &Activation::getUpStream() {
         return up;
     }
 
-    inline DownMessage *Activation::getDownStream() const{
+    inline DownMessage &Activation::getDownStream() {
         return down;
     }
 

+ 1 - 1
include/core/env-var.h

@@ -23,7 +23,7 @@ namespace aFuncore {
         EnvVarSpace(const EnvVarSpace &)=delete;
         EnvVarSpace &operator=(const EnvVarSpace &)=delete;
 
-        [[nodiscard]] size_t getCount() const;
+        [[nodiscard]] inline size_t getCount() const;
         bool findString(const std::string &name, std::string &str) const;
         bool findNumber(const std::string &name, int32_t &num) const;
 

+ 7 - 7
include/core/gc.h

@@ -12,16 +12,16 @@ namespace aFuncore {
         bool reachable;  // 可达标记 [同时标识已迭代]
         GcCount reference;  // 引用计数
     protected:
-        GcObjectBase();
+        inline GcObjectBase();
         virtual ~GcObjectBase() = default;
     public:
         GcObjectBase(const GcObjectBase &) = delete;
         GcObjectBase &operator=(const GcObjectBase &) = delete;
 
-        void addReference();
-        void delReference();
-        void setClear(bool clear=false);
-        void setReachable(bool is_reference=false);
+        inline void addReference();
+        inline void delReference();
+        inline void setClear(bool clear=false);
+        inline void setReachable(bool is_reference=false);
     };
 
 
@@ -31,8 +31,8 @@ namespace aFuncore {
         size_t add(GcObjectBase *obj);
         GcObjectBase *pop();
 
-        [[nodiscard]] size_t getSize() const;
-        [[nodiscard]] size_t isEmpty() const;
+        [[nodiscard]] inline size_t getSize() const;
+        [[nodiscard]] inline size_t isEmpty() const;
     };
 
 };

+ 4 - 4
include/core/inter.h

@@ -30,8 +30,8 @@ namespace aFuncore {
         VarSpace *global;  // 全局变量空间
         VarList *global_varlist;  // global + protect
         Activation *activation;  // 活动记录
-        InterMessage *out;
-        InterMessage *in;
+        InterMessage out;
+        InterMessage in;
 
         inline void pushActivation(Activation *new_activation);
         friend Activation::Activation(Inter &inter_);
@@ -45,7 +45,7 @@ namespace aFuncore {
         std::list<LiteralRegex> *literal;
 
         /* 配置信息记录器 */
-        EnvVarSpace *envvar;
+        EnvVarSpace &envvar;
 
         /* 线程信息 */
     public:
@@ -74,7 +74,7 @@ namespace aFuncore {
         [[nodiscard]] inline Activation *getActivation() const;
         [[nodiscard]] bool checkLiteral(const std::string &element) const;
         [[nodiscard]] bool checkLiteral(const std::string &element, std::string &literaler, bool &in_protect) const;
-        [[nodiscard]] inline EnvVarSpace *getEnvVarSpace() const;
+        [[nodiscard]] inline EnvVarSpace &getEnvVarSpace();
 
         bool pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect);
 

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

@@ -35,7 +35,7 @@ namespace aFuncore {
         return activation;
     }
 
-    inline EnvVarSpace *Inter::getEnvVarSpace() const {
+    inline EnvVarSpace &Inter::getEnvVarSpace() {
         return envvar;
     }
 }

+ 7 - 7
include/core/msg.h

@@ -16,24 +16,24 @@ namespace aFuncore {
         Message *next;  // 下一条消息
     public:
         const std::string type;  // 消息类型标注
-        explicit Message(const std::string &type_);
+        explicit inline Message(const std::string &type_);
         virtual ~Message() = default;
         Message &operator=(const Message &)=delete;
     };
 
     class TopMessage : public Message {
     public:
-        explicit TopMessage(const std::string &type_);
+        explicit inline TopMessage(const std::string &type_);
         virtual void topProgress() = 0;
     };
 
     class AFUN_CORE_EXPORT NormalMessage : public TopMessage {
         Object *obj;
     public:
-        explicit NormalMessage(Object *obj_);
+        explicit inline NormalMessage(Object *obj_);
         ~NormalMessage() override;
         void topProgress() override;
-        Object *getObject();
+        inline Object *getObject();
     };
 
     class AFUN_CORE_EXPORT ErrorMessage : public TopMessage {
@@ -49,8 +49,8 @@ namespace aFuncore {
     public:
         explicit ErrorMessage(const std::string &error_type_, const std::string &error_info_, Activation *activation);
         void topProgress() override;
-        std::string getErrorType();
-        std::string getErrorInfo();
+        inline std::string getErrorType();
+        inline std::string getErrorInfo();
     };
 
     class AFUN_CORE_EXPORT MessageStream {
@@ -84,7 +84,7 @@ namespace aFuncore {
 
     class AFUN_CORE_EXPORT DownMessage : public MessageStream {
     public:
-        void joinMsg(DownMessage *msg);
+        void joinMsg(DownMessage &msg);
     };
 
     class AFUN_CORE_EXPORT InterMessage : public MessageStream {

+ 5 - 5
include/core/value.h

@@ -33,21 +33,21 @@ namespace aFuncore {
             virtual void runFunction() = 0;
         };
 
-        Function(const std::string &type_, Inter &inter_);
+        inline Function(const std::string &type_, Inter &inter_);
         virtual CallFunction *getCallFunction(Code *code, Inter &inter) = 0;
-        virtual bool isInfix();
+        virtual inline bool isInfix();
     };
 
     class AFUN_CORE_EXPORT Literaler : public Object {
     public:
-        Literaler(const std::string &type_, Inter &inter_);
+        inline Literaler(const std::string &type_, Inter &inter_);
         virtual void getObject(const std::string &literal, char prefix) = 0;
     };
 
     class AFUN_CORE_EXPORT CallBackVar : public Object {
     public:
-        CallBackVar(const std::string &type_, Inter &inter_);
-        virtual bool isCallBack();
+        inline CallBackVar(const std::string &type_, Inter &inter_);
+        virtual inline bool isCallBack();
         virtual void callBack() = 0;
     };
 };

+ 10 - 10
include/core/var.h

@@ -15,8 +15,8 @@ namespace aFuncore {
         Var(Object *data_, Inter &inter_);
         ~Var() override = default;
 
-        [[nodiscard]] virtual Object *getData();
-        virtual void setData(Object *data_);
+        [[nodiscard]] inline virtual Object *getData();
+        virtual void inline setData(Object *data_);
     };
 
     class AFUN_CORE_EXPORT VarSpace : public GcObject<class VarSpace> {
@@ -39,8 +39,8 @@ namespace aFuncore {
         template <typename Callable,typename...T>
         void forEach(Callable func, T...arg);
 
-        [[nodiscard]] size_t getCount() const;
-        [[nodiscard]] virtual Var *findVar(const std::string &name);
+        [[nodiscard]] inline size_t getCount() const;
+        [[nodiscard]] inline virtual Var *findVar(const std::string &name);
         virtual VarOperationFlat defineVar(const std::string &name, Object *data);
         virtual VarOperationFlat defineVar(const std::string &name, Var *data);
         virtual VarOperationFlat setVar(const std::string &name, Object *data);
@@ -52,10 +52,10 @@ namespace aFuncore {
     class AFUN_CORE_EXPORT ProtectVarSpace : public VarSpace {
         bool is_protect;
     public:
-        explicit ProtectVarSpace(Inter &inter_);
+        explicit inline ProtectVarSpace(Inter &inter_);
 
-        [[nodiscard]] bool getProtect() const;
-        bool setProtect(bool protect);
+        [[nodiscard]] inline bool getProtect() const;
+        inline bool setProtect(bool protect);
 
         VarOperationFlat defineVar(const std::string &name, Object *data) override;
         VarOperationFlat defineVar(const std::string &name, Var *data) override;
@@ -66,7 +66,7 @@ namespace aFuncore {
     class AFUN_CORE_EXPORT VarList {
         std::list<VarSpace *> varspace;
     public:
-        explicit VarList() = default;
+        explicit inline VarList() = default;
         explicit VarList(VarList *varlist);
         explicit VarList(VarSpace *varspace);
         ~VarList() = default;
@@ -74,7 +74,7 @@ namespace aFuncore {
         VarList &operator=(const VarList &) = delete;
 
         void connect(VarList *varlist);
-        void push(VarSpace *varspace_);
+        inline void push(VarSpace *varspace_);
 
         template <typename Callable,typename...T>
         void forEach(Callable func, T...arg);
@@ -84,7 +84,7 @@ namespace aFuncore {
         virtual bool defineVar(const std::string &name, Var *data);
         virtual bool setVar(const std::string &name, Object *data);
         virtual bool delVar(const std::string &name);
-        [[nodiscard]] Object *findObject(const std::string &name);
+        [[nodiscard]] inline Object *findObject(const std::string &name);
     };
 }
 

+ 3 - 1
include/core/var.inline.h

@@ -11,7 +11,9 @@ namespace aFuncore {
         data = data_;
     }
 
-    inline size_t VarSpace::getCount() const {return count;}
+    inline size_t VarSpace::getCount() const {
+        return count;
+    }
 
     inline Object *VarSpace::findObject(const std::string &name) {
         Var *ret = findVar(name);

+ 21 - 27
src/core/activation.cpp

@@ -16,19 +16,15 @@ using namespace aFuntool;
  * 自动压入inter
  * @param inter_
  */
-Activation::Activation(Inter &inter_) : inter{inter_}, line{0} {
-    Activation *prev_ = inter.getActivation();
-    prev = prev_;
-    down = new DownMessage();
+Activation::Activation(Inter &inter_)
+    : inter{inter_}, prev {inter_.getActivation()}, line{0},
+      up{prev == nullptr ? nullptr : &prev->up}, down {} {
     if (prev != nullptr) {
         varlist = new VarList(prev->varlist);
-        up = new UpMessage(prev->up);
         line = prev->line;
         path = prev->path;
     } else {
         varlist = new VarList();
-        up = new UpMessage();
-        line = 0;
         path = "";
     }
     inter.pushActivation(this);
@@ -40,10 +36,8 @@ Activation::Activation(Inter &inter_) : inter{inter_}, line{0} {
  * 释放Varlist并且将DownMessage压入上层
  */
 Activation::~Activation(){
-    if (prev != nullptr && down != nullptr)
-        down->joinMsg(prev->down);
-    delete up;
-    delete down;
+    if (prev != nullptr)
+        down.joinMsg(prev->down);
     delete varlist;
 }
 
@@ -55,7 +49,7 @@ 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));
+        down.pushMessage(new NormalMessage(none));
     } else {
         if (code_type == code_element) {
             runCodeElement(code);
@@ -89,7 +83,7 @@ void Activation::runCodeElement(Code *code) {
         if (literaler != nullptr)
             literaler->getObject(code->getElement(), code->getPrefix());
         else
-            down->pushMessage(new ErrorMessage("TypeError", "Error type of literal.", this));
+            down.pushMessage(new ErrorMessage("TypeError", "Error type of literal.", this));
     } else {
         if (varlist != nullptr)
             obj = varlist->findObject(code->getElement());
@@ -98,9 +92,9 @@ void Activation::runCodeElement(Code *code) {
             if (cbv != nullptr && cbv->isCallBack())
                 cbv->callBack();
             else
-                down->pushMessage(new NormalMessage(obj));
+                down.pushMessage(new NormalMessage(obj));
         } else
-            down->pushMessage(new ErrorMessage("NameError", std::string("Variable ") + code->getElement() + " not fount.", this));
+            down.pushMessage(new ErrorMessage("NameError", std::string("Variable ") + code->getElement() + " not fount.", this));
     }
 }
 
@@ -122,11 +116,11 @@ ActivationStatus ExeActivation::getCode(Code *&code){
         return as_end;
 
     if (!first) {
-        auto msg = down->getMessage<NormalMessage>("NORMAL");
+        auto msg = down.getMessage<NormalMessage>("NORMAL");
         if (msg == nullptr)
             return as_end;
         else
-            down->popMessage("NORMAL");
+            down.popMessage("NORMAL");
         delete msg;
     }
 
@@ -149,7 +143,7 @@ static void ActivationTopProgress(Message *msg, void *) {
 };
 
 TopActivation::~TopActivation() {
-    down->forEach(ActivationTopProgress, nullptr);
+    down.forEach(ActivationTopProgress, nullptr);
 }
 
 FuncActivation::~FuncActivation(){
@@ -167,7 +161,7 @@ ActivationStatus FuncActivation::getCode(Code *&code){
                 code = call->getSon();
                 if (code == nullptr) {
                     line = 0;
-                    down->pushMessage(new ErrorMessage("SyntaxError", "Callback without code.", this));
+                    down.pushMessage(new ErrorMessage("SyntaxError", "Callback without code.", this));
                     return as_end;
                 }
                 line = code->getFileLine();
@@ -176,7 +170,7 @@ ActivationStatus FuncActivation::getCode(Code *&code){
                 return as_run;
             case block_b: {
                 std::string prefix;
-                if (!inter.getEnvVarSpace()->findString("sys:prefix", prefix) || prefix.size() != PREFIX_COUNT)
+                if (!inter.getEnvVarSpace().findString("sys:prefix", prefix) || prefix.size() != PREFIX_COUNT)
                     prefix = "''";
                 char quote = prefix[prefix_quote];
                 for (Code *var = call->getSon(); var != nullptr; var = var->toNext()) {
@@ -193,7 +187,7 @@ ActivationStatus FuncActivation::getCode(Code *&code){
                 }
                 if (status != func_get_func) {
                     line = 0;
-                    down->pushMessage(new ErrorMessage("SyntaxError", "Callback without code.", this));
+                    down.pushMessage(new ErrorMessage("SyntaxError", "Callback without code.", this));
                     return as_end;
                 }
                 break;
@@ -201,22 +195,22 @@ ActivationStatus FuncActivation::getCode(Code *&code){
             default:
                 errorLog(aFunCoreLogger, "Error FuncActivation block type");
                 line = 0;
-                down->pushMessage(new ErrorMessage("RuntimeError", "Error FuncActivation block type.", this));
+                down.pushMessage(new ErrorMessage("RuntimeError", "Error FuncActivation block type.", this));
                 return as_end;
         }
     }
 
     if (status == func_get_func) {
         if (func == nullptr) {
-            auto *msg = down->getMessage<NormalMessage>("NORMAL");
+            auto *msg = down.getMessage<NormalMessage>("NORMAL");
             if (msg == nullptr)
                 return as_end;
             else
-                down->popMessage("NORMAL");
+                down.popMessage("NORMAL");
             func = dynamic_cast<Function *>(msg->getObject());
             delete msg;
             if (func == nullptr) {
-                down->pushMessage(new ErrorMessage("TypeError", "Callback without function.", this));
+                down.pushMessage(new ErrorMessage("TypeError", "Callback without function.", this));
                 return as_end;
             }
         }
@@ -237,10 +231,10 @@ ActivationStatus FuncActivation::getCode(Code *&code){
     }
 
     if (acl_begin != acl_end) {  // 获取参数计算结果
-        auto *msg = down->getMessage<NormalMessage>("NORMAL");
+        auto *msg = down.getMessage<NormalMessage>("NORMAL");
         if (msg == nullptr)
             return as_end;
-        down->popMessage("NORMAL");
+        down.popMessage("NORMAL");
 
         acl_begin->ret = msg->getObject();
         delete msg;

+ 10 - 14
src/core/inter.cpp

@@ -8,7 +8,8 @@
 using namespace aFuncore;
 using namespace aFuntool;
 
-Inter::Inter(int argc, char **argv, ExitMode em) : base{*this}, is_derive{false} {
+Inter::Inter(int argc, char **argv, ExitMode em)
+    : base{*this}, is_derive{false}, out{}, in{}, envvar{*(new EnvVarSpace())} {
     status = inter_creat;
 
     gc = new GcRecord;
@@ -19,17 +20,16 @@ Inter::Inter(int argc, char **argv, ExitMode em) : base{*this}, is_derive{false}
     activation = nullptr;
     literal = new std::list<LiteralRegex>;
 
-    envvar = new EnvVarSpace();
-    envvar->setNumber("sys:gc-runtime", 2);
-    envvar->setString("sys:prefix", "''");  // 引用,顺序执行
-    envvar->setNumber("sys:exit-code", 0);
-    envvar->setNumber("sys:argc", argc);
-    envvar->setNumber("sys:error_std", 0);
+    envvar.setNumber("sys:gc-runtime", 2);
+    envvar.setString("sys:prefix", "''");  // 引用,顺序执行
+    envvar.setNumber("sys:exit-code", 0);
+    envvar.setNumber("sys:argc", argc);
+    envvar.setNumber("sys:error_std", 0);
 
     for (int i = 0; i < argc; i++) {
         char buf[20];
         snprintf(buf, 10, "sys:arg%d", i);
-        envvar->setString(buf, argv[i]);
+        envvar.setString(buf, argv[i]);
     }
 
     result = nullptr;
@@ -42,8 +42,6 @@ Inter::Inter(int argc, char **argv, ExitMode em) : base{*this}, is_derive{false}
     global = new VarSpace(*this);  // 放到最后
     global_varlist = new VarList(protect);
     global_varlist->push(global);
-    out = new InterMessage();
-    in = new InterMessage();
 
     status = inter_init;
 }
@@ -59,9 +57,7 @@ Inter::~Inter(){
         delete literal;
         delete gc;
         delete son_inter;
-        delete envvar;
-        delete out;
-        delete in;
+        delete &envvar;
     }
 }
 
@@ -98,7 +94,7 @@ bool Inter::runCode(){
                 break;
             default:
                 errorLog(aFunCoreLogger, "Error activation status.");
-                activation->getDownStream()->pushMessage(new ErrorMessage("RuntimeError", "Error activation status.", activation));
+                activation->getDownStream().pushMessage(new ErrorMessage("RuntimeError", "Error activation status.", activation));
                 break;
         }
 

+ 4 - 4
src/core/msg.cpp

@@ -24,7 +24,7 @@ ErrorMessage::ErrorMessage(const std::string &error_type_, const std::string &er
 
 void ErrorMessage::topProgress(){
     int32_t error_std = 0;
-    inter.getEnvVarSpace()->findNumber("sys:error_std", error_std);
+    inter.getEnvVarSpace().findNumber("sys:error_std", error_std);
     if (error_std == 0) {
         printf_stderr(0, "Error TrackBack\n");
         for (auto & begin : trackback)
@@ -129,13 +129,13 @@ Message *UpMessage::popMessage(const std::string &type){
  * 拼接数据流
  * @param msg
  */
-void DownMessage::joinMsg(DownMessage *msg){
+void DownMessage::joinMsg(DownMessage &msg){
     Message *m = stream;
     if (m == nullptr)
         return;
     while (m->next != nullptr)
         m = m->next;
-    m->next = msg->stream;
-    msg->stream = m;
+    m->next = msg.stream;
+    msg.stream = m;
     stream = nullptr;
 }

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

@@ -103,7 +103,7 @@ int main() {
     inter.getGlobalVarlist()->defineVar("test-cbv", cbv);
     printf_stdout(0, "cbv: %p\n", cbv);
     fputs_stdout("\n");
-    inter.getEnvVarSpace()->setNumber("sys:error_std", 1);
+    inter.getEnvVarSpace().setNumber("sys:error_std", 1);
 
     {
         auto code = (Code::create(0, "run-code.aun"));