浏览代码

refactor & feat: Code使用const限定符

SongZihuan 3 年之前
父节点
当前提交
ea455d7ffb

+ 17 - 17
include/core/core-activation.h

@@ -27,8 +27,8 @@ namespace aFuncore {
         virtual ~Activation();
         Activation &operator=(const Activation &)=delete;
 
-        virtual ActivationStatus getCode(Code::ByteCode *&code) = 0;
-        virtual void runCode(Code::ByteCode *code);
+        virtual ActivationStatus getCode(const Code::ByteCode *&code) = 0;
+        virtual void runCode(const Code::ByteCode *code);
         virtual inline void endRun();
 
         [[nodiscard]] inline VarList *getVarlist() const;
@@ -50,40 +50,40 @@ namespace aFuncore {
         aFuntool::FilePath path;
         aFuntool::FileLine line;
 
-        virtual void runCodeElement(Code::ByteCode *code);
-        virtual void runCodeBlockP(Code::ByteCode *code);
-        virtual void runCodeBlockC(Code::ByteCode *code);
-        virtual void runCodeBlockB(Code::ByteCode *code);
+        virtual void runCodeElement(const Code::ByteCode *code);
+        virtual void runCodeBlockP(const Code::ByteCode *code);
+        virtual void runCodeBlockC(const Code::ByteCode *code);
+        virtual void runCodeBlockB(const Code::ByteCode *code);
     };
 
     class AFUN_CORE_EXPORT ExeActivation : public Activation {
     public:
-        inline  ExeActivation(Code &code, Inter &inter_);
-        inline  ExeActivation(Code::ByteCode *code, Inter &inter_);
-        ActivationStatus getCode(Code::ByteCode *&code) override;
-        [[nodiscard]] inline Code::ByteCode *getStart() const;
+        inline ExeActivation(const Code &code, Inter &inter_);
+        inline ExeActivation(const Code::ByteCode *code, Inter &inter_);
+        ActivationStatus getCode(const Code::ByteCode *&code) override;
+        [[nodiscard]] inline const Code::ByteCode *getStart() const;
 
     private:
-        Code::ByteCode *start;
-        Code::ByteCode *next;
+        const Code::ByteCode *start;
+        const Code::ByteCode *next;
         bool first=true;
     };
 
     class AFUN_CORE_EXPORT TopActivation : public ExeActivation {
     public:
-        explicit TopActivation(Code &code, Inter &inter_);
+        explicit TopActivation(const Code &code, Inter &inter_);
         ~TopActivation() override;
         [[nodiscard]] inline const Code &getBase() const;
 
     private:
-        Code &base;
+        const Code &base;
     };
 
     class AFUN_CORE_EXPORT FuncActivation : public Activation {
     public:
-        explicit inline FuncActivation(Code::ByteCode *code, Inter &inter_);
+        explicit inline FuncActivation(const Code::ByteCode *code, Inter &inter_);
         ~FuncActivation() override;
-        ActivationStatus getCode(Code::ByteCode *&code) override;
+        ActivationStatus getCode(const Code::ByteCode *&code) override;
         void endRun() override;
 
     private:
@@ -94,7 +94,7 @@ namespace aFuncore {
         } status = func_first;
 
         bool on_tail = false;
-        Code::ByteCode *call;
+        const Code::ByteCode *call;
 
         Function *func = nullptr;
         Function::CallFunction *call_func = nullptr;

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

@@ -32,15 +32,15 @@ namespace aFuncore {
         return path;
     }
 
-    inline ExeActivation::ExeActivation(Code &code, Inter &inter_) : Activation(inter_), start{code.getByteCode()}, next{code.getByteCode()} {
+    inline ExeActivation::ExeActivation(const Code &code, Inter &inter_) : Activation(inter_), start{code.getByteCode()}, next{code.getByteCode()} {
 
     }
 
-    inline ExeActivation::ExeActivation(Code::ByteCode *code, Inter &inter_) : Activation(inter_), start{code}, next{code} {
+    inline ExeActivation::ExeActivation(const Code::ByteCode *code, Inter &inter_) : Activation(inter_), start{code}, next{code} {
 
     }
 
-    inline Code::ByteCode *ExeActivation::getStart() const{
+    inline const Code::ByteCode *ExeActivation::getStart() const{
         return start;
     }
 
@@ -48,7 +48,7 @@ namespace aFuncore {
         return base;
     }
 
-    inline FuncActivation::FuncActivation(Code::ByteCode *code, Inter &inter_) : Activation(inter_), call{code} {
+    inline FuncActivation::FuncActivation(const Code::ByteCode *code, Inter &inter_) : Activation(inter_), call{code} {
 
     }
 }

+ 1 - 1
include/core/inter.h

@@ -95,7 +95,7 @@ namespace aFuncore {
         bool pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect);
 
         bool runCode();
-        bool runCode(Code &code);
+        bool runCode(const Code &code);
 
         inline InterStatus setInterStop();
         inline InterStatus setInterExit();

+ 3 - 3
include/core/value.h

@@ -22,7 +22,7 @@ namespace aFuncore {
     public:
         class AFUN_CORE_EXPORT CallFunction;
 
-        virtual CallFunction *getCallFunction(Code::ByteCode *code, Inter &inter) = 0;
+        virtual CallFunction *getCallFunction(const Code::ByteCode *code, Inter &inter) = 0;
         virtual inline bool isInfix();
     };
 
@@ -35,12 +35,12 @@ namespace aFuncore {
         CallFunction(const CallFunction &)=delete;
         CallFunction &operator=(const CallFunction &)=delete;
 
-        virtual std::list<ArgCodeList> *getArgCodeList(Inter &inter, Activation &activation, Code::ByteCode *call) = 0;
+        virtual std::list<ArgCodeList> *getArgCodeList(Inter &inter, Activation &activation, const Code::ByteCode *call) = 0;
         virtual void runFunction() = 0;
     };
 
     struct Function::CallFunction::ArgCodeList {
-        Code::ByteCode *code = nullptr;
+        const Code::ByteCode *code = nullptr;
         Object *ret = nullptr;
     };
 

+ 8 - 8
src/core/activation.cpp

@@ -40,7 +40,7 @@ namespace aFuncore {
      * 运行代码
      * @param code
      */
-    void Activation::runCode(Code::ByteCode *code){
+    void Activation::runCode(const Code::ByteCode *code){
         auto code_type = code->getType();
         if (code_type == Code::ByteCode::code_start) {  // start 不处理 msg
             auto *none = new Object("None", inter);
@@ -66,7 +66,7 @@ namespace aFuncore {
         }
     }
 
-    void Activation::runCodeElement(Code::ByteCode *code){
+    void Activation::runCodeElement(const Code::ByteCode *code){
         std::string literaler_name;
         bool in_protect = false;
         Object *obj = nullptr;
@@ -96,19 +96,19 @@ namespace aFuncore {
         }
     }
 
-    void Activation::runCodeBlockP(Code::ByteCode *code){
+    void Activation::runCodeBlockP(const Code::ByteCode *code){
         new ExeActivation(code->getSon(), inter);
     }
 
-    void Activation::runCodeBlockC(Code::ByteCode *code){
+    void Activation::runCodeBlockC(const Code::ByteCode *code){
         new FuncActivation(code, inter);
     }
 
-    void Activation::runCodeBlockB(Code::ByteCode *code){
+    void Activation::runCodeBlockB(const Code::ByteCode *code){
         new FuncActivation(code, inter);
     }
 
-    Activation::ActivationStatus ExeActivation::getCode(Code::ByteCode *&code){
+    Activation::ActivationStatus ExeActivation::getCode(const Code::ByteCode *&code){
         code = next;
         if (code == nullptr)
             return as_end;
@@ -129,7 +129,7 @@ namespace aFuncore {
         return as_run;
     }
 
-    TopActivation::TopActivation(Code &code, Inter &inter_) : ExeActivation(code, inter_), base{code} {
+    TopActivation::TopActivation(const Code &code, Inter &inter_) : ExeActivation(code, inter_), base{code} {
         varlist->connect(inter_.getGlobalVarlist());
     }
 
@@ -147,7 +147,7 @@ namespace aFuncore {
         delete call_func;
     }
 
-    Activation::ActivationStatus FuncActivation::getCode(Code::ByteCode *&code) {
+    Activation::ActivationStatus FuncActivation::getCode(const Code::ByteCode *&code) {
         if (on_tail)
             return as_end;
 

+ 2 - 2
src/core/inter.cpp

@@ -72,7 +72,7 @@ namespace aFuncore {
                 return false;
             }
 
-            Code::ByteCode *code = nullptr;
+            const Code::ByteCode *code = nullptr;
             Activation::ActivationStatus as = activation->getCode(code);
             switch (as) {
                 case Activation::as_end: {
@@ -102,7 +102,7 @@ namespace aFuncore {
      * @param code 代码
      * @return
      */
-    bool Inter::runCode(Code &code){
+    bool Inter::runCode(const Code &code){
         if (activation != nullptr) {
             errorLog(aFunCoreLogger, "Run code with activation");
             return false;

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

@@ -6,17 +6,17 @@ using namespace aFuntool;
 class Func1 : public Function {
     class CallFunc1 : public CallFunction {
         Code &func_code;
-        Code::ByteCode *code;
+        const Code::ByteCode *code;
         Inter &inter;
         std::list<ArgCodeList> *acl;
     public:
-        CallFunc1(Code &func_code_, Code::ByteCode *code_, Inter &inter_) : func_code{func_code_}, code{code_}, inter{inter_} {
+        CallFunc1(Code &func_code_, const Code::ByteCode *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);
         }
 
-        std::list<ArgCodeList> *getArgCodeList(Inter &inter_, Activation &activation, Code::ByteCode *call) override {
+        std::list<ArgCodeList> *getArgCodeList(Inter &inter_, Activation &activation, const Code::ByteCode *call) override {
             return acl;
         }
 
@@ -40,7 +40,7 @@ public:
 
     ~Func1() override = default;
 
-    CallFunction *getCallFunction(Code::ByteCode *code, Inter &inter) override {
+    CallFunction *getCallFunction(const Code::ByteCode *code, Inter &inter) override {
         return dynamic_cast<CallFunction *>(new CallFunc1(func_code, code, inter));
     }