Browse Source

refactor & style: 格式化代码

SongZihuan 3 years ago
parent
commit
349680ba81
8 changed files with 161 additions and 118 deletions
  1. 28 23
      include/core/activation.h
  2. 15 11
      include/core/env-var.h
  3. 12 7
      include/core/gc.h
  4. 40 32
      include/core/inter.h
  5. 20 12
      include/core/msg.h
  6. 19 14
      include/core/value.h
  7. 21 14
      include/core/var.h
  8. 6 5
      include/tool/log.h

+ 28 - 23
include/core/activation.h

@@ -8,21 +8,6 @@
 
 namespace aFuncore {
     class AFUN_CORE_EXPORT Activation {
-    protected:
-        Activation *prev;
-
-        VarList *varlist;
-
-        UpMessage up;
-        DownMessage down;
-
-        StringFilePath path;
-        FileLine line;
-
-        virtual void runCodeElement(Code *code);
-        virtual void runCodeBlockP(Code *code);
-        virtual void runCodeBlockC(Code *code);
-        virtual void runCodeBlockB(Code *code);
     public:
         Inter &inter;
 
@@ -44,16 +29,34 @@ namespace aFuncore {
 
         [[nodiscard]] inline FileLine getFileLine() const;
         [[nodiscard]] inline  const StringFilePath &getFilePath() const;
+
+    protected:
+        Activation *prev;
+
+        VarList *varlist;
+
+        UpMessage up;
+        DownMessage down;
+
+        StringFilePath path;
+        FileLine line;
+
+        virtual void runCodeElement(Code *code);
+        virtual void runCodeBlockP(Code *code);
+        virtual void runCodeBlockC(Code *code);
+        virtual void runCodeBlockB(Code *code);
     };
 
     class AFUN_CORE_EXPORT ExeActivation : public Activation {
-        Code *start;
-        Code *next;
-        bool first=true;
     public:
         explicit inline  ExeActivation(Code *code, Inter &inter_);
         ActivationStatus getCode(Code *&code) override;
         [[nodiscard]] inline  Code *getStart() const;
+
+    private:
+        Code *start;
+        Code *next;
+        bool first=true;
     };
 
     class AFUN_CORE_EXPORT TopActivation : public ExeActivation {
@@ -63,6 +66,13 @@ namespace aFuncore {
     };
 
     class AFUN_CORE_EXPORT FuncActivation : public Activation {
+    public:
+        explicit inline FuncActivation(Code *code, Inter &inter_);
+        ~FuncActivation() override;
+        ActivationStatus getCode(Code *&code) override;
+        void endRun() override;
+
+    private:
         enum {
             func_first = 0,
             func_get_func = 1,
@@ -78,11 +88,6 @@ namespace aFuncore {
         std::list<Function::CallFunction::ArgCodeList> *acl = nullptr;
         std::list<Function::CallFunction::ArgCodeList>::iterator acl_begin;
         std::list<Function::CallFunction::ArgCodeList>::iterator acl_end;
-    public:
-        explicit inline FuncActivation(Code *code, Inter &inter_);
-        ~FuncActivation() override;
-        ActivationStatus getCode(Code *&code) override;
-        void endRun() override;
     };
 }
 

+ 15 - 11
include/core/env-var.h

@@ -6,17 +6,6 @@
 
 namespace aFuncore {
     class AFUN_CORE_EXPORT EnvVarSpace {  // 环境变量
-        static const size_t ENV_VAR_HASH_SIZE = 100;  // 环境变量哈希表大小
-        struct EnvVar {  // 环境变量
-            std::string name;
-            std::string str;
-            int32_t num = 0;  // 可以同时记录字符串和数字
-            struct EnvVar *next = nullptr;
-        };
-
-        size_t count;
-        EnvVar *var[ENV_VAR_HASH_SIZE] {};
-        std::shared_mutex lock;
     public:
         EnvVarSpace();
         ~EnvVarSpace();
@@ -32,6 +21,21 @@ namespace aFuncore {
 
         void addString(const std::string &name, const std::string &str);
         void addNumber(const std::string &name, int32_t num);
+
+    private:
+        static const size_t ENV_VAR_HASH_SIZE = 100;  // 环境变量哈希表大小
+        struct EnvVar;
+
+        size_t count;
+        EnvVar *var[ENV_VAR_HASH_SIZE] {};
+        std::shared_mutex lock;
+    };
+
+    struct EnvVarSpace::EnvVar {  // 环境变量
+        std::string name;
+        std::string str;
+        int32_t num = 0;  // 可以同时记录字符串和数字
+        struct EnvVar *next = nullptr;
     };
 }
 

+ 12 - 7
include/core/gc.h

@@ -8,12 +8,6 @@ namespace aFuncore {
     typedef unsigned GcCount;
 
     class AFUN_CORE_EXPORT GcObjectBase {
-        bool not_clear;  // 不清除
-        bool reachable;  // 可达标记 [同时标识已迭代]
-        GcCount reference;  // 引用计数
-    protected:
-        inline GcObjectBase();
-        virtual ~GcObjectBase() = default;
     public:
         GcObjectBase(const GcObjectBase &) = delete;
         GcObjectBase &operator=(const GcObjectBase &) = delete;
@@ -22,17 +16,28 @@ namespace aFuncore {
         inline void delReference();
         inline void setClear(bool clear=false);
         inline void setReachable(bool is_reference=false);
+
+    protected:
+        inline GcObjectBase();
+        virtual ~GcObjectBase() = default;
+
+    private:
+        bool not_clear;  // 不清除
+        bool reachable;  // 可达标记 [同时标识已迭代]
+        GcCount reference;  // 引用计数
     };
 
 
     class AFUN_CORE_EXPORT GcList {
-        std::queue<GcObjectBase *> queue;
     public :
         size_t add(GcObjectBase *obj);
         GcObjectBase *pop();
 
         [[nodiscard]] inline size_t getSize() const;
         [[nodiscard]] inline size_t isEmpty() const;
+
+    private:
+        std::queue<GcObjectBase *> queue;
     };
 
 };

+ 40 - 32
include/core/inter.h

@@ -11,15 +11,37 @@
 
 namespace aFuncore {
     class AFUN_CORE_EXPORT Inter {
+    public:
+        explicit Inter(int argc=0, char **argv=nullptr, ExitMode em=em_activity);
+        ~Inter();
+        Inter(const Inter &)=delete;
+        Inter &operator=(const Inter &)=delete;
+
+        void enable();
+
+        [[nodiscard]] inline InterStatus getStatus() const;
+        [[nodiscard]] inline bool isExit() const;
+
+        [[nodiscard]] inline ProtectVarSpace *getProtectVarSpace() const;
+        [[nodiscard]] inline VarSpace *getGlobalVarSpace() const;
+        [[nodiscard]] inline VarList *getGlobalVarlist() const;
+        [[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();
+
+        bool pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect);
+
+        bool runCode();
+        bool runCode(Code *code);
+
+    private:
         /* 解释器原信息记录 */
         InterStatus status;
 
         /* GC 记录器 */
-        struct GcRecord {
-            Object *obj;
-            Var *var;
-            VarSpace *varspace;
-        } *gc;
+        struct GcRecord;
+        struct GcRecord *gc;
         [[nodiscard]] inline struct GcRecord *getGcRecord() const;
         friend Object::Object(const std::string &type_, Inter &inter_);
         friend Var::Var(Object *data_, Inter &inter_);
@@ -36,12 +58,7 @@ namespace aFuncore {
         inline void pushActivation(Activation *new_activation);
         friend Activation::Activation(Inter &inter_);
 
-        struct LiteralRegex {
-            Regex rg;
-            std::string pattern;  // 派生 LiteralRegex 时使用
-            std::string literaler;  // 调用的函数
-            bool in_protect;  // 是否在protect空间
-        };
+        struct LiteralRegex;
         std::list<LiteralRegex> *literal;
 
         /* 配置信息记录器 */
@@ -51,35 +68,26 @@ namespace aFuncore {
     public:
         const bool is_derive;  // 是否派生
         Inter &base;  // 主线程
+
     private:
         Object *result;  // 线程执行的结果
         std::list<Inter *> *son_inter;  // 派生线程链表, 由主线程负责管理
 
         ExitFlat exit_flat;  // 外部设置退出
         ExitMode exit_mode;  // 退出模式
-    public:
-        explicit Inter(int argc=0, char **argv=nullptr, ExitMode em=em_activity);
-        ~Inter();
-        Inter(const Inter &)=delete;
-        Inter &operator=(const Inter &)=delete;
-
-        void enable();
-
-        [[nodiscard]] inline InterStatus getStatus() const;
-        [[nodiscard]] inline bool isExit() const;
-
-        [[nodiscard]] inline ProtectVarSpace *getProtectVarSpace() const;
-        [[nodiscard]] inline VarSpace *getGlobalVarSpace() const;
-        [[nodiscard]] inline VarList *getGlobalVarlist() const;
-        [[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();
+    };
 
-        bool pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect);
+    struct Inter::GcRecord {
+        Object *obj;
+        Var *var;
+        VarSpace *varspace;
+    };
 
-        bool runCode();
-        bool runCode(Code *code);
+    struct Inter::LiteralRegex {
+        Regex rg;
+        std::string pattern;  // 派生 LiteralRegex 时使用
+        std::string literaler;  // 调用的函数
+        bool in_protect;  // 是否在protect空间
     };
 }
 

+ 20 - 12
include/core/msg.h

@@ -13,12 +13,14 @@ namespace aFuncore {
         friend class DownMessage;
         friend class InterMessage;
 
-        Message *next;  // 下一条消息
     public:
         const std::string type;  // 消息类型标注
         explicit inline Message(const std::string &type_);
         virtual ~Message() = default;
         Message &operator=(const Message &)=delete;
+
+    private:
+        Message *next;  // 下一条消息
     };
 
     class TopMessage : public Message {
@@ -28,15 +30,24 @@ namespace aFuncore {
     };
 
     class AFUN_CORE_EXPORT NormalMessage : public TopMessage {
-        Object *obj;
     public:
         explicit inline NormalMessage(Object *obj_);
         ~NormalMessage() override;
         void topProgress() override;
         inline Object *getObject();
+
+    private:
+        Object *obj;
     };
 
     class AFUN_CORE_EXPORT ErrorMessage : public TopMessage {
+    public:
+        explicit ErrorMessage(const std::string &error_type_, const std::string &error_info_, Activation *activation);
+        void topProgress() override;
+        inline std::string getErrorType();
+        inline std::string getErrorInfo();
+
+    private:
         Inter &inter;
 
         std::string error_type;
@@ -46,17 +57,9 @@ namespace aFuncore {
             FileLine line;
         };
         std::list<TrackBack> trackback;
-    public:
-        explicit ErrorMessage(const std::string &error_type_, const std::string &error_info_, Activation *activation);
-        void topProgress() override;
-        inline std::string getErrorType();
-        inline std::string getErrorInfo();
     };
 
     class AFUN_CORE_EXPORT MessageStream {
-    protected:
-        Message *stream;
-        [[nodiscard]] virtual Message *_getMessage(const std::string &type) const;
     public:
         MessageStream();
         virtual ~MessageStream();
@@ -70,16 +73,21 @@ namespace aFuncore {
 
         template <typename Callable, typename...T>
         void forEach(Callable func, T...arg);
+
+    protected:
+        Message *stream;
+        [[nodiscard]] virtual Message *_getMessage(const std::string &type) const;
     };
 
     class AFUN_CORE_EXPORT UpMessage : public MessageStream {
-    protected:
-        Message *old;
     public:
         explicit UpMessage(const UpMessage *old=nullptr);
         ~UpMessage() override;
 
         Message *popMessage(const std::string &type) override;
+
+    protected:
+        Message *old;
     };
 
     class AFUN_CORE_EXPORT DownMessage : public MessageStream {

+ 19 - 14
include/core/value.h

@@ -18,26 +18,31 @@ namespace aFuncore {
 
     class AFUN_CORE_EXPORT Function : public Object {
     public:
-        class AFUN_CORE_EXPORT CallFunction {
-        public:
-            struct ArgCodeList {
-                Code *code = nullptr;
-                Object *ret = nullptr;
-            };
-            CallFunction() = default;
-            virtual ~CallFunction() = default;
-            CallFunction(const CallFunction &)=delete;
-            CallFunction &operator=(const CallFunction &)=delete;
-
-            virtual std::list<ArgCodeList> *getArgCodeList() = 0;
-            virtual void runFunction() = 0;
-        };
+        class AFUN_CORE_EXPORT CallFunction;
 
         inline Function(const std::string &type_, Inter &inter_);
         virtual CallFunction *getCallFunction(Code *code, Inter &inter) = 0;
         virtual inline bool isInfix();
     };
 
+    class AFUN_CORE_EXPORT Function::CallFunction {
+    public:
+        struct ArgCodeList;
+
+        CallFunction() = default;
+        virtual ~CallFunction() = default;
+        CallFunction(const CallFunction &)=delete;
+        CallFunction &operator=(const CallFunction &)=delete;
+
+        virtual std::list<ArgCodeList> *getArgCodeList() = 0;
+        virtual void runFunction() = 0;
+    };
+
+    struct Function::CallFunction::ArgCodeList {
+        Code *code = nullptr;
+        Object *ret = nullptr;
+    };
+
     class AFUN_CORE_EXPORT Literaler : public Object {
     public:
         inline Literaler(const std::string &type_, Inter &inter_);

+ 21 - 14
include/core/var.h

@@ -8,7 +8,6 @@
 
 namespace aFuncore {
     class AFUN_CORE_EXPORT Var : public GcObject<class Var> {
-        Object *data;
     public:
         Inter &inter;
 
@@ -17,19 +16,12 @@ namespace aFuncore {
 
         [[nodiscard]] inline virtual Object *getData();
         virtual void inline setData(Object *data_);
+
+    private:
+        Object *data;
     };
 
     class AFUN_CORE_EXPORT VarSpace : public GcObject<class VarSpace> {
-    public:
-        static const size_t VAR_HASH_SIZE = 100;  // 环境变量哈希表大小
-    private:
-        struct VarCup {
-            std::string name;
-            Var *var;
-            VarCup *next=nullptr;
-        };
-        size_t count;
-        VarCup *var[VAR_HASH_SIZE];
     public:
         Inter &inter;
 
@@ -40,17 +32,27 @@ namespace aFuncore {
         void forEach(Callable func, T...arg);
 
         [[nodiscard]] inline size_t getCount() const;
-        [[nodiscard]] inline virtual Var *findVar(const std::string &name);
+        [[nodiscard]] 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);
         virtual VarOperationFlat delVar(const std::string &name);
 
         [[nodiscard]] Object *findObject(const std::string &name);
+
+        static const size_t VAR_HASH_SIZE = 100;  // 环境变量哈希表大小
+
+    private:
+        struct VarCup {
+            std::string name;
+            Var *var;
+            VarCup *next=nullptr;
+        };
+        size_t count;
+        VarCup *var[VAR_HASH_SIZE];
     };
 
     class AFUN_CORE_EXPORT ProtectVarSpace : public VarSpace {
-        bool is_protect;
     public:
         explicit inline ProtectVarSpace(Inter &inter_);
 
@@ -61,10 +63,12 @@ namespace aFuncore {
         VarOperationFlat defineVar(const std::string &name, Var *data) override;
         VarOperationFlat setVar(const std::string &name, Object *data) override;
         VarOperationFlat delVar(const std::string &name) override;
+
+    private:
+        bool is_protect;
     };
 
     class AFUN_CORE_EXPORT VarList {
-        std::list<VarSpace *> varspace;
     public:
         explicit inline VarList() = default;
         explicit VarList(VarList *varlist);
@@ -85,6 +89,9 @@ namespace aFuncore {
         virtual bool setVar(const std::string &name, Object *data);
         virtual bool delVar(const std::string &name);
         [[nodiscard]] inline Object *findObject(const std::string &name);
+
+    private:
+        std::list<VarSpace *> varspace;
     };
 }
 

+ 6 - 5
include/tool/log.h

@@ -76,11 +76,7 @@ namespace aFuntool {
                    const char *format, va_list ap);
         struct LogNode *pop();
 
-        struct ansyData {
-            LogFactory &factor;
-            std::mutex &mutex;
-        };
-
+        struct ansyData;
         void ansyWritrLog(ansyData *data);
     private:
         bool init_;  // 是否已经初始化
@@ -96,6 +92,11 @@ namespace aFuntool {
         struct LogNode *log_buf_;
         struct LogNode **plog_buf_;  // 指向 log_buf的末端
     };
+
+    struct LogFactory::ansyData {
+        LogFactory &factor;
+        std::mutex &mutex;
+    };
 }
 
 #include "log.inline.h"