Sfoglia il codice sorgente

refactor: varlist使用<list>管理

SongZihuan 3 anni fa
parent
commit
76226ab651

+ 1 - 2
include/core/activation.hpp

@@ -11,7 +11,6 @@ namespace aFuncore {
         Activation *prev;
 
         VarList *varlist;
-        VarList *old_varlist;
 
         UpMessage *up;
         DownMessage *down;
@@ -25,7 +24,7 @@ namespace aFuncore {
         virtual ~Activation();
         Activation &operator=(const Activation &)=delete;
 
-        virtual ActivationStatus getCode(Code *&code)=0;
+        virtual ActivationStatus getCode(Code *&code) = 0;
         virtual void runCode(Code *code);
         virtual void endRun() {}
 

+ 1 - 1
include/core/gc.hpp

@@ -13,7 +13,7 @@ namespace aFuncore {
         GcCount reference;  // 引用计数
     protected:
         GcObjectBase() : not_clear{false}, reference{0}, reachable{false} {}
-        virtual ~GcObjectBase()=default;
+        virtual ~GcObjectBase() = default;
         GcObjectBase(const GcObjectBase &)=delete;
         GcObjectBase &operator=(const GcObjectBase &)=delete;
     public:

+ 1 - 1
include/core/msg.hpp

@@ -22,7 +22,7 @@ namespace aFuncore {
     class TopMessage : public Message {
     public:
         explicit TopMessage(const std::string &type_) : Message(type_) {}
-        virtual void topProgress()=0;
+        virtual void topProgress() = 0;
     };
 
     AFUN_CORE_EXPORT class NormalMessage : public TopMessage {

+ 7 - 7
include/core/value.hpp

@@ -26,15 +26,15 @@ namespace aFuncore {
                 Code *code = nullptr;
                 Object *ret = nullptr;
             };
-            CallFunction()=default;
-            virtual ~CallFunction()=default;
+            CallFunction() = default;
+            virtual ~CallFunction() = default;
             CallFunction(const CallFunction &)=delete;
             CallFunction &operator=(const CallFunction &)=delete;
 
-            virtual std::list<ArgCodeList> *getArgCodeList()=0;
-            virtual void runFunction()=0;
+            virtual std::list<ArgCodeList> *getArgCodeList() = 0;
+            virtual void runFunction() = 0;
         };
-        virtual CallFunction *getCallFunction(Code *code, Inter *inter)=0;
+        virtual CallFunction *getCallFunction(Code *code, Inter *inter) = 0;
         virtual bool isInfix() {return false;}
     };
 
@@ -42,7 +42,7 @@ namespace aFuncore {
     public:
         Literaler(const std::string &type_, Inter *inter_) : Object(type_ + ":Literaler", inter_) {}
 
-        virtual void getObject(const std::string &literal, char prefix)=0;
+        virtual void getObject(const std::string &literal, char prefix) = 0;
     };
 
     AFUN_CORE_EXPORT class CallBackVar : public Object {
@@ -50,7 +50,7 @@ namespace aFuncore {
         CallBackVar(const std::string &type_, Inter *inter_) : Object(type_ + ":CallBackVar", inter_) {}
 
         virtual bool isCallBack() {return true;}
-        virtual void callBack()=0;
+        virtual void callBack() = 0;
     };
 };
 

+ 9 - 16
include/core/var.hpp

@@ -4,6 +4,7 @@
 #include "aFunCoreExport.h"
 #include "core.hpp"
 #include "gc.hpp"
+#include <list>
 
 namespace aFuncore {
     AFUN_CORE_EXPORT class Var : public GcObject<class Var> {
@@ -62,20 +63,17 @@ namespace aFuncore {
     };
 
     AFUN_CORE_EXPORT class VarList {
-        VarList *next;
-
-        explicit VarList(VarSpace *vs) : varspace {vs}, next {nullptr} {};
-        ~VarList()=default;
+        std::list<VarSpace *> varspace;
+    public:
+        explicit VarList() = default;
+        explicit VarList(VarList *varlist);
+        explicit VarList(VarSpace *varspace) {this->varspace.push_front(varspace);}
+        ~VarList() = default;
         VarList(const VarList &)=delete;
         VarList &operator=(const VarList &)=delete;
-    public:
-        VarSpace *const varspace;
 
-        static VarList *create(VarSpace *vs) {
-            return new VarList(vs);
-        }
-
-        static void destruct(VarList *varlist);
+        void connect(VarList *varlist);
+        void push(VarSpace *varspace_) {varspace.push_front(varspace_);}
 
         [[nodiscard]] virtual Var *findVar(const std::string &name);
         virtual bool defineVar(const std::string &name, Object *data);
@@ -86,11 +84,6 @@ namespace aFuncore {
             Var *var = findVar(name);
             return var ? var->getData() : nullptr;
         }
-
-        [[nodiscard]] VarList *toNext() const {return next;}
-        VarList *connect(VarList *varlist) {next = varlist; return this;}
-        void disconnect(VarList *varlist);
-        void disconnectNext() {next = nullptr;}
     };
 }
 

+ 10 - 8
src/core/activation.cpp

@@ -19,13 +19,17 @@ using namespace aFuntool;
 Activation::Activation(Inter *inter_) : inter{inter_}, line{0} {
     Activation *prev_ = inter->getActivation();
     prev = prev_;
-    old_varlist = prev ? prev->varlist : nullptr;
-    varlist = old_varlist;
     down = new DownMessage();
-    up = new UpMessage(prev ? prev->up : nullptr);
     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);
 }
@@ -36,12 +40,11 @@ Activation::Activation(Inter *inter_) : inter{inter_}, line{0} {
  * 释放Varlist并且将DownMessage压入上层
  */
 Activation::~Activation(){
-    if (varlist != nullptr && old_varlist != nullptr)
-        varlist->disconnect(old_varlist);
-    if (prev && down != nullptr)
+    if (prev != nullptr && down != nullptr)
         down->joinMsg(prev->down);
     delete up;
     delete down;
+    delete varlist;
 }
 
 /**
@@ -118,8 +121,7 @@ ActivationStatus ExeActivation::getCode(Code *&code){
 }
 
 TopActivation::TopActivation(Code *code, Inter *inter_) : ExeActivation(code, inter_) {
-    varlist = inter_->getGlobalVarlist();
-    old_varlist = varlist;
+    varlist->connect(inter_->getGlobalVarlist());
 }
 
 static void ActivationTopProgress(Message *msg, void *) {

+ 4 - 4
src/core/inter.cpp

@@ -45,7 +45,9 @@ Inter::Inter(int argc, char **argv, ExitMode em)
 
     protect = new ProtectVarSpace(this);  // 放到最后
     global = new VarSpace(this);  // 放到最后
-    global_varlist = (VarList::create(global))->connect(VarList::create(protect));
+    global_varlist = new VarList(protect);
+    global_varlist->push(global);
+
     status = inter_init;
 }
 
@@ -55,12 +57,10 @@ Inter::~Inter(){
     pthread_cond_destroy(&monitor_cond);
 
     if (!is_derive) {
-        VarList::destruct(global_varlist);
-
+        delete global_varlist;
         Object::destruct(gc->obj);
         Var::destruct(gc->var);
         VarSpace::destruct(gc->varspace);
-
         for (auto &it : *literal)
             delete it.rg;
         delete literal;

+ 18 - 31
src/core/var.cpp

@@ -112,6 +112,11 @@ aFuncore::VarSpace::~VarSpace(){
     }
 }
 
+aFuncore::VarList::VarList(VarList *varlist) {
+    for (auto &t : varlist->varspace)
+        this->varspace.push_back(t);
+}
+
 /**
  * 定义变量
  * 若启用保护且变量名存在,则返回错误redefine
@@ -175,8 +180,8 @@ VarOperationFlat aFuncore::ProtectVarSpace::delVar(const std::string &name){
  */
 Var *aFuncore::VarList::findVar(const std::string &name){
     Var *ret = nullptr;
-    for (auto tmp = this; tmp != nullptr && ret == nullptr; tmp = tmp->next)
-        ret = tmp->varspace->findVar(name);
+    for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == nullptr; tmp++)
+        ret = (*tmp)->findVar(name);
     return ret;
 }
 
@@ -190,8 +195,8 @@ Var *aFuncore::VarList::findVar(const std::string &name){
  */
 bool aFuncore::VarList::defineVar(const std::string &name, Object *data){
     VarOperationFlat ret = vof_fail;
-    for (auto tmp = this; tmp != nullptr && ret == vof_fail; tmp = tmp->next)
-        ret = tmp->varspace->defineVar(name, data);
+    for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == vof_fail; tmp++)
+        ret = (*tmp)->defineVar(name, data);
     return ret == vof_success;
 }
 
@@ -205,8 +210,8 @@ bool aFuncore::VarList::defineVar(const std::string &name, Object *data){
  */
 bool aFuncore::VarList::defineVar(const std::string &name, Var *data){
     VarOperationFlat ret = vof_fail;
-    for (auto tmp = this; tmp != nullptr && ret == vof_fail; tmp = tmp->next)
-        ret = tmp->varspace->defineVar(name, data);
+    for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == vof_fail; tmp++)
+        ret = (*tmp)->defineVar(name, data);
     return ret == vof_success;
 }
 
@@ -220,8 +225,8 @@ bool aFuncore::VarList::defineVar(const std::string &name, Var *data){
  */
 bool aFuncore::VarList::setVar(const std::string &name, Object *data){
     VarOperationFlat ret = vof_not_var;
-    for (auto tmp = this; tmp != nullptr && ret == vof_not_var; tmp = tmp->next)
-        ret = tmp->varspace->setVar(name, data);
+    for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == vof_not_var; tmp++)
+        ret = (*tmp)->setVar(name, data);
     return ret == vof_success;
 }
 
@@ -234,30 +239,12 @@ bool aFuncore::VarList::setVar(const std::string &name, Object *data){
  */
 bool aFuncore::VarList::delVar(const std::string &name){
     VarOperationFlat ret = vof_not_var;
-    for (auto tmp = this; tmp != nullptr && ret == vof_not_var; tmp = tmp->next)
-        ret = tmp->varspace->delVar(name);
+    for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == vof_not_var; tmp++)
+        ret = (*tmp)->delVar(name);
     return ret == vof_success;
 }
 
-/**
- * 在指定位置断开varlist
- * @param varlist
- */
-void aFuncore::VarList::disconnect(VarList *varlist){
-    for (VarList *tmp = this; tmp != nullptr; tmp = tmp->next) {
-        if (tmp->next == varlist) {
-            tmp->next = nullptr;
-            return;
-        }
-    }
-}
-
-/**
- * 删除所有varlist
- */
-void VarList::destruct(VarList *varlist){
-    for (VarList *n; varlist != nullptr; varlist = n) {
-        n = varlist->next;
-        delete varlist;
-    }
+void VarList::connect(VarList *varlist){
+    for (auto &t : varlist->varspace)
+        this->varspace.push_back(t);
 }

+ 2 - 0
test/src/run-code.cpp

@@ -4,6 +4,7 @@
 #include "code.hpp"
 #include "msg.hpp"
 #include "activation.hpp"
+#include "env-var.hpp"
 
 using namespace aFuncore;
 using namespace aFuntool;
@@ -108,6 +109,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);
 
     {
         auto code = (Code::create(0, "run-code.aun"));