瀏覽代碼

refactor & feat: Inter改造

SongZihuan 3 年之前
父節點
當前提交
c9fbf7f901

+ 5 - 5
include/core/activation.h

@@ -24,12 +24,12 @@ namespace aFuncore {
         virtual void runCodeBlockC(Code *code);
         virtual void runCodeBlockB(Code *code);
     public:
-        Inter *const inter;
+        Inter &inter;
 
         template <typename Callable,typename...T>
         static void forEach(Activation *activation, Callable func, T...arg);
 
-        explicit Activation(Inter *inter_);
+        explicit Activation(Inter &inter_);
         virtual ~Activation();
         Activation &operator=(const Activation &)=delete;
 
@@ -51,14 +51,14 @@ namespace aFuncore {
         Code *next;
         bool first=true;
     public:
-        explicit ExeActivation(Code *code, Inter *inter_);
+        explicit ExeActivation(Code *code, Inter &inter_);
         ActivationStatus getCode(Code *&code) override;
         [[nodiscard]] Code *getStart() const;
     };
 
     class AFUN_CORE_EXPORT TopActivation : public ExeActivation {
     public:
-        explicit TopActivation(Code *code, Inter *inter_);
+        explicit TopActivation(Code *code, Inter &inter_);
         ~TopActivation() override;
     };
 
@@ -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 FuncActivation(Code *code, Inter &inter_);
         ~FuncActivation() override;
         ActivationStatus getCode(Code *&code) override;
         void endRun() override;

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

@@ -32,7 +32,7 @@ namespace aFuncore {
         return path;
     }
 
-    inline ExeActivation::ExeActivation(Code *code, Inter *inter_) : Activation(inter_), start{code}, next{code}{
+    inline ExeActivation::ExeActivation(Code *code, Inter &inter_) : Activation(inter_), start{code}, next{code}{
 
     }
 
@@ -40,7 +40,7 @@ namespace aFuncore {
         return start;
     }
 
-    inline FuncActivation::FuncActivation(Code *code, Inter *inter_) : Activation(inter_), call{code,}{
+    inline FuncActivation::FuncActivation(Code *code, Inter &inter_) : Activation(inter_), call{code,}{
 
     }
 }

+ 2 - 2
include/core/code.h

@@ -26,8 +26,8 @@ namespace aFuncore {
 
     protected:
         explicit Code(FileLine line, ConstFilePath file="");
-        Code (const std::string &element, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
-        Code (BlockType block_type, Code *son, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
+        Code(const std::string &element, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
+        Code(BlockType block_type, Code *son, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
         ~Code();
     public:
         Code(const Code &)=delete;

+ 14 - 14
include/core/inter.h

@@ -20,10 +20,10 @@ namespace aFuncore {
             Var *var;
             VarSpace *varspace;
         } *gc;
-        [[nodiscard]] struct GcRecord *getGcRecord() const;
-        friend Object::Object(const std::string &type_, Inter *inter_);
-        friend Var::Var(Object *data_, Inter *inter_);
-        friend VarSpace::VarSpace(Inter *inter_);
+        [[nodiscard]] inline struct GcRecord *getGcRecord() const;
+        friend Object::Object(const std::string &type_, Inter &inter_);
+        friend Var::Var(Object *data_, Inter &inter_);
+        friend VarSpace::VarSpace(Inter &inter_);
 
         /* 运行相关 */
         ProtectVarSpace *protect;  // 保护变量空间
@@ -33,8 +33,8 @@ namespace aFuncore {
         InterMessage *out;
         InterMessage *in;
 
-        void pushActivation(Activation *new_activation);
-        friend Activation::Activation(Inter *inter_);
+        inline void pushActivation(Activation *new_activation);
+        friend Activation::Activation(Inter &inter_);
 
         struct LiteralRegex {
             Regex rg;
@@ -50,7 +50,7 @@ namespace aFuncore {
         /* 线程信息 */
     public:
         const bool is_derive;  // 是否派生
-        Inter *const base;  // 主线程
+        Inter &base;  // 主线程
     private:
         Object *result;  // 线程执行的结果
         std::list<Inter *> *son_inter;  // 派生线程链表, 由主线程负责管理
@@ -65,16 +65,16 @@ namespace aFuncore {
 
         void enable();
 
-        [[nodiscard]] InterStatus getStatus() const;
-        [[nodiscard]] bool isExit() const;
+        [[nodiscard]] inline InterStatus getStatus() const;
+        [[nodiscard]] inline bool isExit() const;
 
-        [[nodiscard]] ProtectVarSpace *getProtectVarSpace() const;
-        [[nodiscard]] VarSpace *getGlobalVarSpace() const;
-        [[nodiscard]] VarList *getGlobalVarlist() const;
-        [[nodiscard]] Activation *getActivation() 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]] EnvVarSpace *getEnvVarSpace() const;
+        [[nodiscard]] inline EnvVarSpace *getEnvVarSpace() const;
 
         bool pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect);
 

+ 1 - 1
include/core/msg.h

@@ -37,7 +37,7 @@ namespace aFuncore {
     };
 
     class AFUN_CORE_EXPORT ErrorMessage : public TopMessage {
-        Inter *inter;
+        Inter &inter;
 
         std::string error_type;
         std::string error_info;

+ 7 - 7
include/core/value.h

@@ -9,11 +9,11 @@
 namespace aFuncore {
     class AFUN_CORE_EXPORT Object : public GcObject<class Object> {
     public:
-        Inter *const inter;
+        Inter &inter;
         const std::string type;  // 标识 Object 的字符串
 
-        explicit Object(const std::string &type_, Inter *inter_);
-        ~Object() override =default;
+        explicit Object(const std::string &type_, Inter &inter_);
+        ~Object() override = default;
     };
 
     class AFUN_CORE_EXPORT Function : public Object {
@@ -33,20 +33,20 @@ namespace aFuncore {
             virtual void runFunction() = 0;
         };
 
-        Function(const std::string &type_, Inter *inter_);
-        virtual CallFunction *getCallFunction(Code *code, Inter *inter) = 0;
+        Function(const std::string &type_, Inter &inter_);
+        virtual CallFunction *getCallFunction(Code *code, Inter &inter) = 0;
         virtual bool isInfix();
     };
 
     class AFUN_CORE_EXPORT Literaler : public Object {
     public:
-        Literaler(const std::string &type_, Inter *inter_);
+        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_);
+        CallBackVar(const std::string &type_, Inter &inter_);
         virtual bool isCallBack();
         virtual void callBack() = 0;
     };

+ 3 - 3
include/core/value.inline.h

@@ -3,7 +3,7 @@
 #include "value.h"
 
 namespace aFuncore {
-    inline Function::Function(const std::string &type_, Inter *inter_) : Object(type_ + ":Function", inter_) {
+    inline Function::Function(const std::string &type_, Inter &inter_) : Object(type_ + ":Function", inter_) {
 
     }
 
@@ -11,11 +11,11 @@ namespace aFuncore {
         return false;
     }
 
-    inline Literaler::Literaler(const std::string &type_, Inter *inter_) : Object(type_ + ":Literaler", inter_) {
+    inline Literaler::Literaler(const std::string &type_, Inter &inter_) : Object(type_ + ":Literaler", inter_) {
 
     }
 
-    inline CallBackVar::CallBackVar(const std::string &type_, Inter *inter_) : Object(type_ + ":CallBackVar", inter_) {
+    inline CallBackVar::CallBackVar(const std::string &type_, Inter &inter_) : Object(type_ + ":CallBackVar", inter_) {
 
     }
 

+ 6 - 5
include/core/var.h

@@ -10,9 +10,9 @@ namespace aFuncore {
     class AFUN_CORE_EXPORT Var : public GcObject<class Var> {
         Object *data;
     public:
-        Inter *const inter;
+        Inter &inter;
 
-        Var(Object *data_, Inter *inter_);
+        Var(Object *data_, Inter &inter_);
         ~Var() override = default;
 
         [[nodiscard]] virtual Object *getData();
@@ -31,8 +31,9 @@ namespace aFuncore {
         size_t count;
         VarCup *var[VAR_HASH_SIZE];
     public:
-        Inter *const inter;
-        explicit VarSpace(Inter *inter_);
+        Inter &inter;
+
+        explicit VarSpace(Inter &inter_);
         ~VarSpace() override;
 
         template <typename Callable,typename...T>
@@ -51,7 +52,7 @@ namespace aFuncore {
     class AFUN_CORE_EXPORT ProtectVarSpace : public VarSpace {
         bool is_protect;
     public:
-        explicit ProtectVarSpace(Inter *inter_);
+        explicit ProtectVarSpace(Inter &inter_);
 
         [[nodiscard]] bool getProtect() const;
         bool setProtect(bool protect);

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

@@ -18,7 +18,7 @@ namespace aFuncore {
         return ret ? ret->getData() : nullptr;
     }
 
-    inline ProtectVarSpace::ProtectVarSpace(Inter *inter_) : VarSpace(inter_), is_protect{false} {
+    inline ProtectVarSpace::ProtectVarSpace(Inter &inter_) : VarSpace(inter_), is_protect{false} {
 
     }
 

+ 1 - 1
include/tool/regex.h

@@ -18,8 +18,8 @@ namespace aFuntool {
         inline Regex(Regex &&regex) noexcept;
         Regex &operator=(const Regex &regex)=delete;
         Regex &operator=(Regex &&regex)=delete;
+        ~Regex();
 
-        ~Regex ();
         int match(const char *subject);
         inline int match(const std::string &subject);
     };

+ 9 - 9
src/core/activation.cpp

@@ -16,8 +16,8 @@ using namespace aFuntool;
  * 自动压入inter
  * @param inter_
  */
-Activation::Activation(Inter *inter_) : inter{inter_}, line{0} {
-    Activation *prev_ = inter->getActivation();
+Activation::Activation(Inter &inter_) : inter{inter_}, line{0} {
+    Activation *prev_ = inter.getActivation();
     prev = prev_;
     down = new DownMessage();
     if (prev != nullptr) {
@@ -31,7 +31,7 @@ Activation::Activation(Inter *inter_) : inter{inter_}, line{0} {
         line = 0;
         path = "";
     }
-    inter->pushActivation(this);
+    inter.pushActivation(this);
 }
 
 /**
@@ -80,9 +80,9 @@ void Activation::runCodeElement(Code *code) {
     std::string literaler_name;
     bool in_protect = false;
     Object *obj = nullptr;
-    if (inter->checkLiteral(code->getElement(), literaler_name, in_protect)) {
+    if (inter.checkLiteral(code->getElement(), literaler_name, in_protect)) {
         if (in_protect)
-            obj = inter->getProtectVarSpace()->findObject(literaler_name);
+            obj = inter.getProtectVarSpace()->findObject(literaler_name);
         else
             obj = varlist->findObject(literaler_name);
         auto literaler = dynamic_cast<Literaler *>(obj);
@@ -138,8 +138,8 @@ ActivationStatus ExeActivation::getCode(Code *&code){
     return as_run;
 }
 
-TopActivation::TopActivation(Code *code, Inter *inter_) : ExeActivation(code, inter_) {
-    varlist->connect(inter_->getGlobalVarlist());
+TopActivation::TopActivation(Code *code, Inter &inter_) : ExeActivation(code, inter_) {
+    varlist->connect(inter_.getGlobalVarlist());
 }
 
 static void ActivationTopProgress(Message *msg, void *) {
@@ -176,11 +176,11 @@ 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()) {
-                    if (var->getType() != code_element || var->getPrefix() == quote || inter->checkLiteral(var->getElement()))
+                    if (var->getType() != code_element || var->getPrefix() == quote || inter.checkLiteral(var->getElement()))
                         continue;
                     Object *obj = varlist->findObject(var->getElement());
                     if (obj == nullptr || !dynamic_cast<Function *>(obj) || !dynamic_cast<Function *>(obj)->isInfix())

+ 4 - 4
src/core/inter.cpp

@@ -8,7 +8,7 @@
 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} {
     status = inter_creat;
 
     gc = new GcRecord;
@@ -38,8 +38,8 @@ Inter::Inter(int argc, char **argv, ExitMode em) : base{this}, is_derive{false}
     exit_flat = ef_none;
     exit_mode = em;
 
-    protect = new ProtectVarSpace(this);  // 放到最后
-    global = new VarSpace(this);  // 放到最后
+    protect = new ProtectVarSpace(*this);  // 放到最后
+    global = new VarSpace(*this);  // 放到最后
     global_varlist = new VarList(protect);
     global_varlist->push(global);
     out = new InterMessage();
@@ -125,7 +125,7 @@ bool Inter::runCode(Code *code){
         return false;
     }
 
-    new TopActivation(code, this);
+    new TopActivation(code, *this);
     return runCode();
 }
 

+ 1 - 1
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)

+ 3 - 3
src/core/value.cpp

@@ -4,7 +4,7 @@
 using namespace aFuncore;
 using namespace aFuntool;
 
-aFuncore::Object::Object(const std::string &type_, Inter *inter_)
-        : type{type_}, inter{inter_->base} {
-    this->addObject(inter->getGcRecord()->obj);
+aFuncore::Object::Object(const std::string &type_, Inter &inter_)
+        : type{type_}, inter{inter_.base} {
+    this->addObject(inter.getGcRecord()->obj);
 }

+ 4 - 4
src/core/var.cpp

@@ -5,12 +5,12 @@ using namespace aFuncore;
 using namespace aFuntool;
 
 
-aFuncore::Var::Var(Object *data_, Inter *inter_) : data{data_}, inter{inter_->base} {
-    addObject(inter->getGcRecord()->var);
+aFuncore::Var::Var(Object *data_, Inter &inter_) : data{data_}, inter{inter_.base} {
+    addObject(inter.getGcRecord()->var);
 }
 
-aFuncore::VarSpace::VarSpace(Inter *inter_) : count{0}, var{}, inter{inter_->base} {
-    addObject(inter->getGcRecord()->varspace);
+aFuncore::VarSpace::VarSpace(Inter &inter_) : count{0}, var{}, inter{inter_.base} {
+    addObject(inter.getGcRecord()->varspace);
 }
 
 /**

+ 19 - 20
test/src/run-code.cpp

@@ -7,10 +7,10 @@ class Func1 : public Function {
     class CallFunc1 : public CallFunction {
         Code *func_code;
         Code *code;
-        Inter *inter;
+        Inter &inter;
         std::list<ArgCodeList> *acl;
     public:
-        CallFunc1(Code *func_code_, Code *code_, Inter *inter_) : func_code{func_code_}, code{code_}, inter{inter_} {
+        CallFunc1(Code *func_code_, Code *code_, Inter &inter_) : func_code{func_code_}, code{code_}, inter{inter_} {
             acl = new std::list<ArgCodeList>;
             ArgCodeList agr1 = {code_->getSon()->toNext()};
             acl->push_front(agr1);
@@ -32,7 +32,7 @@ class Func1 : public Function {
 
     Code *func_code;
 public:
-    explicit Func1(Inter *inter_) : Function("Function", inter_) {
+    explicit Func1(Inter &inter_) : Function("Function", inter_) {
         func_code = (Code::create(0, "run-code.aun"));
         func_code->connect(Code::create(block_p, Code::create("test-var", 1), 0));
     }
@@ -41,7 +41,7 @@ public:
         Code::destruct(func_code);
     }
 
-    CallFunction *getCallFunction(Code *code, Inter *inter) override {
+    CallFunction *getCallFunction(Code *code, Inter &inter) override {
         return dynamic_cast<CallFunction *>(new CallFunc1(func_code, code, inter));
     }
 
@@ -51,7 +51,7 @@ public:
 class Literaler1 : public Literaler {
     Code *func_code;
 public:
-    explicit Literaler1(Inter *inter_) : Literaler("Data", inter_) {
+    explicit Literaler1(Inter &inter_) : Literaler("Data", inter_) {
         func_code = (Code::create(0, "run-code.aun"));
         func_code->connect(Code::create(block_p, Code::create("test-var", 1), 0));
     }
@@ -69,7 +69,7 @@ public:
 class CBV1 : public CallBackVar {
     Code *func_code;
 public:
-    explicit CBV1(Inter *inter_) : CallBackVar("CBV1", inter_) {
+    explicit CBV1(Inter &inter_) : CallBackVar("CBV1", inter_) {
         func_code = (Code::create(0, "run-code.aun"));
         func_code->connect(Code::create(block_p, Code::create("test-var", 1), 0));
     }
@@ -85,30 +85,30 @@ public:
 };
 
 int main() {
-    auto inter = new Inter();
+    Inter inter {};
 
     auto obj = new Object("Object", inter);
-    inter->getGlobalVarlist()->defineVar("test-var", obj);
+    inter.getGlobalVarlist()->defineVar("test-var", obj);
     printf_stdout(0, "obj: %p\n", obj);
 
     auto func = new Func1(inter);
-    inter->getGlobalVarlist()->defineVar("test-func", func);
+    inter.getGlobalVarlist()->defineVar("test-func", func);
     printf_stdout(0, "func: %p\n", func);
 
     auto literaler = new Literaler1(inter);
-    inter->getGlobalVarlist()->defineVar("test-literaler", literaler);
+    inter.getGlobalVarlist()->defineVar("test-literaler", literaler);
     printf_stdout(0, "literaler: %p\n", literaler);
 
     auto cbv = new CBV1(inter);
-    inter->getGlobalVarlist()->defineVar("test-cbv", cbv);
+    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"));
         code->connect(Code::create(block_p, Code::create("test-var", 1), 0));
-        inter->runCode(code);
+        inter.runCode(code);
         Code::destruct(code);
         fputs_stdout("\n");
     }
@@ -119,7 +119,7 @@ int main() {
 
         auto code = (Code::create(0, "run-code.aun"));
         code->connect(Code::create(block_c, arg, 0));
-        inter->runCode(code);
+        inter.runCode(code);
         Code::destruct(code);
         fputs_stdout("\n");
     }
@@ -130,16 +130,16 @@ int main() {
 
         auto code = (Code::create(0, "run-code.aun"));
         code->connect(Code::create(block_b, arg, 0));
-        inter->runCode(code);
+        inter.runCode(code);
         Code::destruct(code);
         fputs_stdout("\n");
     }
 
     {
-        inter->pushLiteral("data[0-9]", "test-literaler", false);
+        inter.pushLiteral("data[0-9]", "test-literaler", false);
         auto code = (Code::create(0, "run-code.aun"));
         code->connect(Code::create("data3", 1));
-        inter->runCode(code);
+        inter.runCode(code);
         Code::destruct(code);
         fputs_stdout("\n");
     }
@@ -147,7 +147,7 @@ int main() {
     {
         auto code = (Code::create(0, "run-code.aun"));
         code->connect(Code::create("test-cbv", 1));
-        inter->runCode(code);
+        inter.runCode(code);
         Code::destruct(code);
         fputs_stdout("\n");
     }
@@ -155,11 +155,10 @@ int main() {
     {
         auto code = (Code::create(0, "run-code.aun"));
         code->connect(Code::create("test-not-var", 1));
-        inter->runCode(code);
+        inter.runCode(code);
         Code::destruct(code);
         fputs_stdout("\n");
     }
 
-    delete inter;
     return 0;
 }