Selaa lähdekoodia

refactor: 拆分core和runtime

SongZihuan 3 vuotta sitten
vanhempi
sitoutus
8d632b79df
42 muutettua tiedostoa jossa 948 lisäystä ja 883 poistoa
  1. 1 0
      cmake/export-head.cmake
  2. 2 2
      include/core/aFuncore.h
  3. 12 109
      include/core/core-activation.h
  4. 0 76
      include/core/core-activation.inline.h
  5. 0 14
      include/core/core-activation.template.h
  6. 0 13
      include/core/core-exception.h
  7. 0 12
      include/core/core-exception.inline.h
  8. 78 0
      include/core/core-message-stream.h
  9. 8 8
      include/core/core-message-stream.template.h
  10. 16 0
      include/core/core-message.h
  11. 7 2
      include/core/env-var.h
  12. 13 23
      include/core/inter.h
  13. 7 7
      include/core/inter.inline.h
  14. 0 129
      include/core/msg.h
  15. 0 129
      include/core/object-value.h
  16. 0 54
      include/core/object-value.inline.h
  17. 10 0
      include/runtime/aFunrt.h
  18. 120 0
      include/runtime/rt-activation.h
  19. 61 0
      include/runtime/rt-activation.inline.h
  20. 14 0
      include/runtime/rt-activation.template.h
  21. 27 0
      include/runtime/rt-exception.h
  22. 23 0
      include/runtime/rt-exception.inline.h
  23. 12 0
      include/runtime/rt-logger.h
  24. 12 0
      include/runtime/rt-logger.inline.h
  25. 48 0
      include/runtime/rt-message.h
  26. 9 8
      include/runtime/rt-message.inline.h
  27. 109 0
      include/runtime/rt-object.h
  28. 39 0
      include/runtime/rt-object.inline.h
  29. 5 5
      include/runtime/rt-object.template.h
  30. 4 0
      src/CMakeLists.txt
  31. 35 4
      src/core/env-var.cpp
  32. 10 48
      src/core/inter.cpp
  33. 8 35
      src/core/message-stream.cpp
  34. 40 0
      src/runtime/CMakeLists.txt
  35. 8 64
      src/runtime/object.cpp
  36. 97 74
      src/runtime/rt-activation.cpp
  37. 5 0
      src/runtime/rt-logger.cpp
  38. 30 0
      src/runtime/rt-message.cpp
  39. 1 1
      test/src/CMakeLists.txt
  40. 2 2
      test/src/core-down-msg.cpp
  41. 3 3
      test/src/core-up-msg.cpp
  42. 72 61
      test/src/run-code.cpp

+ 1 - 0
cmake/export-head.cmake

@@ -19,6 +19,7 @@ define_export_head(tool-shared tool-static aFunToolExport AFUN_TOOL)
 define_export_head(code-shared code-static aFunCodeExport AFUN_CODE)
 define_export_head(core-shared core-static aFunCoreExport AFUN_CORE)
 define_export_head(parser-shared parser-static aFunParserExport AFUN_PARSER)
+define_export_head(rt-shared rt-static aFunRuntimeExport AFUN_RT)
 define_export_head(it-shared it-static aFunlangExport AFUN_LANG)
 
 # 两个库需要额外定义

+ 2 - 2
include/core/aFuncore.h

@@ -2,11 +2,11 @@
 #define AFUN_AFUNCORE_H
 
 #include "core-logger.h"
-#include "msg.h"
+#include "core-message.h"
+#include "core-message-stream.h"
 #include "env-var.h"
 #include "inter.h"
 #include "object.h"
-#include "object-value.h"
 #include "core-activation.h"
 #include "core-exception.h"
 

+ 12 - 109
include/core/core-activation.h

@@ -2,14 +2,11 @@
 #define AFUN_CORE_ACTIVATION_H
 #include "aFuntool.h"
 #include "aFunCoreExport.h"
-#include "msg.h"
+#include "core-message-stream.h"
 #include "aFuncode.h"
-#include "object-value.h"
 
 namespace aFuncore {
-    class Inter;
-
-    class AFUN_CORE_EXPORT Activation {
+    class Activation {
     public:
         typedef enum ActivationStatus {
             as_run = 0,
@@ -17,117 +14,23 @@ namespace aFuncore {
             as_end_run = 2,
         } ActivationStatus;
 
-        class AFUN_CORE_EXPORT VarList {
-        public:
-            AFUN_INLINE explicit VarList();
-            virtual ~VarList();
-            AFUN_INLINE VarList(VarList &&new_varlist);
-            AFUN_INLINE VarList &operator=(VarList &&new_varlist) noexcept;
-            VarList(const VarList &) = delete;
-            VarList &operator=(const VarList &) = delete;
-
-            void clear();
-            void connect(VarList &new_varlist);
-            AFUN_INLINE void push(VarSpace *varspace_);
-            AFUN_INLINE size_t count();
-
-            template <typename Callable,typename...T>
-            void forEach(Callable func, T...arg);
-
-            [[nodiscard]] virtual Var *findVar(const std::string &name);
-            virtual bool defineVar(const std::string &name, Object *data);
-            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]] AFUN_INLINE Object *findObject(const std::string &name);
-
-        private:
-            std::list<VarSpace *> varspace;
-        };
-
-        Inter &inter;
-
-        explicit Activation(Inter &inter_);
-        virtual ~Activation();
-        Activation &operator=(const Activation &)=delete;
+        Activation() = default;
+        virtual ~Activation() = default;
+        Activation(const Activation &) = delete;
+        Activation &operator=(Activation &) = delete;
 
         virtual ActivationStatus getCode(const aFuncode::Code::ByteCode *&code) = 0;
-        virtual void runCode(const aFuncode::Code::ByteCode *code);
-        virtual void endRun();
+        virtual void runCode(const aFuncode::Code::ByteCode *code) = 0;
+        virtual void endRun() = 0;
 
-        [[nodiscard]] AFUN_INLINE VarList &getVarlist();
-        [[nodiscard]] AFUN_INLINE UpMessage &getUpStream();
-        [[nodiscard]] AFUN_INLINE DownMessage &getDownStream();
+        [[nodiscard]] virtual UpMessageStream &getUpStream() = 0;
+        [[nodiscard]] virtual DownMessageStream &getDownStream() = 0;
 
-        [[nodiscard]] AFUN_INLINE aFuntool::FileLine getFileLine() const;
-        [[nodiscard]] AFUN_INLINE  const aFuntool::FilePath &getFilePath() const;
-
-    protected:
-        VarList varlist;
-
-        UpMessage up;
-        DownMessage down;
-
-        aFuntool::FilePath path;
-        aFuntool::FileLine line;
-
-        virtual void runCodeElement(const aFuncode::Code::ByteCode *code);
-        virtual void runCodeBlockP(const aFuncode::Code::ByteCode *code);
-        virtual void runCodeBlockC(const aFuncode::Code::ByteCode *code);
-        virtual void runCodeBlockB(const aFuncode::Code::ByteCode *code);
+        [[nodiscard]] virtual aFuntool::FileLine getFileLine() = 0;
+        [[nodiscard]] virtual const aFuntool::FilePath &getFilePath() = 0;
     };
 
-    class AFUN_CORE_EXPORT ExeActivation : public Activation {
-    public:
-        AFUN_INLINE ExeActivation(const aFuncode::Code &code, Inter &inter_);
-        AFUN_INLINE ExeActivation(const aFuncode::Code::ByteCode *code, Inter &inter_);
-        ActivationStatus getCode(const aFuncode::Code::ByteCode *&code) override;
-        [[nodiscard]] AFUN_INLINE const aFuncode::Code::ByteCode *getStart() const;
 
-    private:
-        const aFuncode::Code::ByteCode *start;
-        const aFuncode::Code::ByteCode *next;
-        bool first=true;
-    };
-
-    class AFUN_CORE_EXPORT TopActivation : public ExeActivation {
-    public:
-        explicit TopActivation(const aFuncode::Code &code, Inter &inter_);
-        ~TopActivation() override = default;
-        [[nodiscard]] AFUN_INLINE const aFuncode::Code &getBase() const;
-
-    private:
-        const aFuncode::Code &base;
-    };
-
-    class AFUN_CORE_EXPORT FuncActivation : public Activation {
-    public:
-        AFUN_INLINE explicit FuncActivation(const aFuncode::Code::ByteCode *code, Inter &inter_);
-        explicit FuncActivation(Function *func, Inter &inter_);
-        ~FuncActivation() override;
-        ActivationStatus getCode(const aFuncode::Code::ByteCode *&code) override;
-        void endRun() override;
-
-    private:
-        enum {
-            func_first = 0,  // 获取函数体前准备
-            func_get_func = 1,  // 获取函数体后,开始获取参数前
-            func_get_arg = 2,  // 获取参数过程
-        } status = func_first;
-
-        bool on_tail = false;
-        const aFuncode::Code::ByteCode *call;
-
-        Function *func = nullptr;
-        Function::CallFunction *call_func = nullptr;
-
-        std::list<Function::CallFunction::ArgCodeList> *acl = nullptr;
-        std::list<Function::CallFunction::ArgCodeList>::iterator acl_begin;
-        std::list<Function::CallFunction::ArgCodeList>::iterator acl_end;
-    };
 }
 
-#include "core-activation.inline.h"
-#include "core-activation.template.h"
-
 #endif //AFUN_CORE_ACTIVATION_H

+ 0 - 76
include/core/core-activation.inline.h

@@ -1,76 +0,0 @@
-#ifndef AFUN_CORE_ACTIVATION_INLINE_H
-#define AFUN_CORE_ACTIVATION_INLINE_H
-
-#include "core-activation.h"
-
-namespace aFuncore {
-    Activation::VarList &Activation::getVarlist(){
-        return varlist;
-    }
-
-    UpMessage &Activation::getUpStream() {
-        return up;
-    }
-
-    DownMessage &Activation::getDownStream() {
-        return down;
-    }
-
-    aFuntool::FileLine Activation::getFileLine() const{
-        return line;
-    }
-
-    const aFuntool::FilePath &Activation::getFilePath() const{
-        return path;
-    }
-
-    Activation::VarList::VarList() : varspace{} {
-
-    }
-
-    Activation::VarList::VarList(VarList &&new_varlist) : varspace{std::move(new_varlist.varspace)} {
-
-    }
-
-    Activation::VarList &Activation::VarList::operator=(VarList &&new_varlist)  noexcept {
-        clear();
-        varspace = std::move(new_varlist.varspace);
-        return *this;
-    }
-
-    void Activation::VarList::push(VarSpace *varspace_) {
-        varspace_->addReference();
-        varspace.push_front(varspace_);
-    }
-
-    size_t Activation::VarList::count() {
-        return varspace.size();
-    }
-
-    Object *Activation::VarList::findObject(const std::string &name) {
-        Var *var = findVar(name);
-        return var ? var->getData() : nullptr;
-    }
-
-    ExeActivation::ExeActivation(const aFuncode::Code &code, Inter &inter_) : Activation(inter_), start{code.getByteCode()}, next{code.getByteCode()} {
-
-    }
-
-    ExeActivation::ExeActivation(const aFuncode::Code::ByteCode *code, Inter &inter_) : Activation(inter_), start{code}, next{code} {
-
-    }
-
-    const aFuncode::Code::ByteCode *ExeActivation::getStart() const{
-        return start;
-    }
-
-    const aFuncode::Code &TopActivation::getBase() const {
-        return base;
-    }
-
-    FuncActivation::FuncActivation(const aFuncode::Code::ByteCode *code, Inter &inter_) : Activation(inter_), call{code} {
-
-    }
-}
-
-#endif //AFUN_CORE_ACTIVATION_INLINE_H

+ 0 - 14
include/core/core-activation.template.h

@@ -1,14 +0,0 @@
-#ifndef AFUN_CORE_ACTIVATION_TEMPLATE_H
-#define AFUN_CORE_ACTIVATION_TEMPLATE_H
-
-#include "core-activation.h"
-
-namespace aFuncore {
-    template <typename Callable, typename...T>
-    void Activation::VarList::forEach(Callable func, T...arg) {
-        for (auto &vs : varspace)
-            func(vs, arg...);
-    }
-}
-
-#endif //AFUN_CORE_ACTIVATION_TEMPLATE_H

+ 0 - 13
include/core/core-exception.h

@@ -12,19 +12,6 @@ namespace aFuncore {
     public:
         AFUN_INLINE EnvironmentDestructException();
     };
-
-    class RuntimeError : public aFuncoreException {
-        std::string type;
-    public:
-        AFUN_INLINE RuntimeError(const std::string &msg, std::string type);
-        AFUN_INLINE const std::string &getType() const;
-    };
-
-    class ArgumentError : public RuntimeError {
-        std::string type;
-    public:
-        AFUN_INLINE ArgumentError();
-    };
 }
 
 #include "core-exception.inline.h"

+ 0 - 12
include/core/core-exception.inline.h

@@ -12,18 +12,6 @@ namespace aFuncore {
     EnvironmentDestructException::EnvironmentDestructException() : aFuncoreException("Environment Destruct Error") {
 
     }
-
-    RuntimeError::RuntimeError(const std::string &msg, std::string type_) : aFuncoreException(msg), type{std::move(type_)} {
-
-    }
-
-    const std::string &RuntimeError::getType() const {
-        return type;
-    }
-
-    ArgumentError::ArgumentError() : RuntimeError("Argument mismatch", "ArgumentError") {
-
-    }
 }
 
 #endif //AFUN_CORE_EXCEPTION_INLINE_H

+ 78 - 0
include/core/core-message-stream.h

@@ -0,0 +1,78 @@
+#ifndef AFUN_CORE_MESSAGE_STREAM_H
+#define AFUN_CORE_MESSAGE_STREAM_H
+#include <list>
+#include <mutex>
+#include <map>
+#include "aFuntool.h"
+#include "aFunCoreExport.h"
+#include "core-message.h"
+
+namespace aFuncore {
+    class AFUN_CORE_EXPORT MessageStream {
+    public:
+        MessageStream() = default;
+        virtual ~MessageStream();
+        MessageStream(const MessageStream &)=delete;
+        MessageStream &operator=(const MessageStream &)=delete;
+
+        template<class T>
+        [[nodiscard]] T *getMessage(const std::string &type) const;
+
+        Message *popMessage(const std::string &type);
+        void pushMessage(const std::string &type, Message *msg);
+
+        template <typename Callable, typename...T>
+        void forEach(Callable func, T...arg);
+
+    protected:
+        std::map<std::string, Message *> stream;
+        [[nodiscard]] virtual Message *_getMessage(const std::string &type) const;
+    };
+
+    class AFUN_CORE_EXPORT UpMessageStream : public MessageStream {
+    public:
+        explicit UpMessageStream(const UpMessageStream *old=nullptr);
+        ~UpMessageStream() override = default;
+
+        template <typename Callable, typename...T>
+        void forEachAll(Callable func, T...arg);
+
+    protected:
+        const UpMessageStream *old;
+        [[nodiscard]] Message *_getMessage(const std::string &type) const override;
+    };
+
+    class AFUN_CORE_EXPORT DownMessageStream : public MessageStream {
+    public:
+        void joinMsg(DownMessageStream &msg);
+    };
+
+    class AFUN_CORE_EXPORT InterMessageStream : public MessageStream {
+    public:
+        Message *popFrontMessage(std::string &type);
+        Message *popMessage(const std::string &type);
+        void pushMessage(const std::string &type, Message *msg);
+
+        template <typename Callable, typename...T>
+        void forEach(Callable func, T...arg);
+
+        template <typename Callable, typename...T>
+        void forEachLock(Callable func, T...arg);
+    private:
+        std::mutex lock;
+    };
+
+    class InterOutMessageStream : public InterMessageStream {
+    public:
+        template<class T>
+        [[nodiscard]] T *getMessage(const std::string &type) const = delete;  // 对外不设置 getMessage 以避免线程问题
+    };
+
+    class InterInMessageStream : public InterMessageStream {
+
+    };
+}
+
+#include "core-message-stream.template.h"
+
+#endif //AFUN_CORE_MESSAGE_STREAM_H

+ 8 - 8
include/core/msg.template.h → include/core/core-message-stream.template.h

@@ -1,6 +1,6 @@
-#ifndef AFUN_MSG_TEMPLATE_H
-#define AFUN_MSG_TEMPLATE_H
-#include "msg.h"
+#ifndef AFUN_CORE_MESSAGE_STREAM_TEMPLATE_H
+#define AFUN_CORE_MESSAGE_STREAM_TEMPLATE_H
+#include "core-message-stream.h"
 
 namespace aFuncore {
     template<class T>
@@ -18,13 +18,13 @@ namespace aFuncore {
     }
 
     template<typename Callable, typename... T>
-    void UpMessage::forEachAll(Callable func, T... arg) {
-        for (const UpMessage *up = this; up != nullptr; up = up->old)
+    void UpMessageStream::forEachAll(Callable func, T... arg) {
+        for (const UpMessageStream *up = this; up != nullptr; up = up->old)
             up->MessageStream::forEach(func, arg...);
     }
 
     template<typename Callable, typename... T>
-    void InterMessage::forEach(Callable func, T... arg) {
+    void InterMessageStream::forEach(Callable func, T... arg) {
         std::unique_lock<std::mutex> mutex{lock};
         for (auto &msg : stream) {
             mutex.unlock();
@@ -34,11 +34,11 @@ namespace aFuncore {
     }
 
     template<typename Callable, typename... T>
-    void InterMessage::forEachLock(Callable func, T... arg) {
+    void InterMessageStream::forEachLock(Callable func, T... arg) {
         std::unique_lock<std::mutex> mutex{lock};
         for (auto &msg : stream)
             func(msg.second, arg...);
     }
 }
 
-#endif //AFUN_MSG_TEMPLATE_H
+#endif //AFUN_CORE_MESSAGE_STREAM_TEMPLATE_H

+ 16 - 0
include/core/core-message.h

@@ -0,0 +1,16 @@
+#ifndef AFUN_CORE_MESSAGE_H
+#define AFUN_CORE_MESSAGE_H
+#include "aFuntool.h"
+#include "aFunCoreExport.h"
+
+namespace aFuncore {
+    class AFUN_CORE_EXPORT Message {
+    public:
+        AFUN_INLINE explicit Message() = default;
+        virtual ~Message() = default;
+        Message &operator=(const Message &)=delete;
+
+    };
+}
+
+#endif //AFUN_CORE_MESSAGE_H

+ 7 - 2
include/core/env-var.h

@@ -7,19 +7,23 @@
 #include "aFunCoreExport.h"
 
 namespace aFuncore {
+    class Object;
+
     class AFUN_CORE_EXPORT EnvVarSpace {  // 环境变量
     public:
         EnvVarSpace() = default;
-        ~EnvVarSpace() = default;
+        ~EnvVarSpace();
         EnvVarSpace(const EnvVarSpace &)=delete;
         EnvVarSpace &operator=(const EnvVarSpace &)=delete;
 
         [[nodiscard]] AFUN_INLINE size_t getCount();
         bool findString(const std::string &name, std::string &str);
         bool findNumber(const std::string &name, int32_t &num);
+        bool findObject(const std::string &name, Object *&obj);
 
         void setString(const std::string &name, const std::string &str);
         void setNumber(const std::string &name, int32_t num);
+        void setObject(const std::string &name, Object *obj);
 
         void addString(const std::string &name, const std::string &str);
         void addNumber(const std::string &name, int32_t num);
@@ -28,7 +32,8 @@ namespace aFuncore {
         AFUN_STATIC const size_t ENV_VAR_HASH_SIZE = 100;  // 环境变量哈希表大小
         struct EnvVar {  // 环境变量
             std::string str;
-            int32_t num;  // 可以同时记录字符串和数字
+            int32_t num;
+            Object *object;
         };
 
         std::unordered_map<std::string, EnvVar> var;

+ 13 - 23
include/core/inter.h

@@ -2,20 +2,15 @@
 #define AFUN_INTER_H
 #include <list>
 #include <mutex>
-#include "aFuntool.h"
 #include "aFunCoreExport.h"
-
+#include "aFuntool.h"
 #include "aFuncode.h"
 #include "env-var.h"
-#include "msg.h"
+#include "core-message-stream.h"
+#include "core-activation.h"
+#include "object.h"
 
 namespace aFuncore {
-    class Activation;
-    class Object;
-    class Var;
-    class ProtectVarSpace;
-    class VarSpace;
-    class Object;
     class Inter;
 
     class AFUN_CORE_EXPORT Environment {
@@ -33,6 +28,7 @@ namespace aFuncore {
         AFUN_INLINE size_t operator++(int);
         AFUN_INLINE size_t operator--(int);
 
+        [[nodiscard]] AFUN_INLINE EnvVarSpace &getEnvVarSpace();
     private:
         std::mutex lock;
         size_t reference;  // 引用计数
@@ -43,8 +39,7 @@ namespace aFuncore {
         void gcThread();
 
     protected:  // 位于 mutex 之下
-        ProtectVarSpace *const protect;  // 保护变量空间
-        EnvVarSpace envvar;
+        EnvVarSpace &env_var;
     };
 
     class AFUN_CORE_EXPORT Inter {
@@ -59,8 +54,6 @@ namespace aFuncore {
             inter_exit = 3,  // 解释器退出
         } InterStatus;
 
-        AFUN_STATIC const int PREFIX_COUNT = 2;  // env 记录的前缀  TODO-szh 取消
-
         explicit Inter(Environment &env_);
         Inter(const Inter &base_inter);
         ~Inter();
@@ -72,20 +65,20 @@ namespace aFuncore {
         [[nodiscard]] AFUN_INLINE bool isInterStop() const;
         [[nodiscard]] AFUN_INLINE bool isInterExit() const;
         [[nodiscard]] AFUN_INLINE Environment &getEnvironment();
-        [[nodiscard]] AFUN_INLINE ProtectVarSpace *getProtectVarSpace() const;
         [[nodiscard]] AFUN_INLINE const std::list<Activation *> &getStack() const;
         [[nodiscard]] AFUN_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]] AFUN_INLINE EnvVarSpace &getEnvVarSpace();
-        [[nodiscard]] AFUN_INLINE InterOutMessage &getOutMessageStream();
-        [[nodiscard]] AFUN_INLINE InterInMessage &getInMessageStream();
+        [[nodiscard]] AFUN_INLINE InterOutMessageStream &getOutMessageStream();
+        [[nodiscard]] AFUN_INLINE InterInMessageStream &getInMessageStream();
 
         bool pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect);
 
+        AFUN_INLINE void pushActivation(Activation *new_activation);
+        AFUN_INLINE Activation *popActivation();
+
         bool runCode();
-        bool runCode(const aFuncode::Code &code);
-        bool runCode(Object *obj);
 
         AFUN_INLINE InterStatus setInterStop();
         AFUN_INLINE InterStatus setInterExit();
@@ -97,13 +90,10 @@ namespace aFuncore {
         std::list<Activation *> stack;
         Activation *activation;  // 活动记录
 
-        InterOutMessage out;
-        InterInMessage in;
+        InterOutMessageStream out;
+        InterInMessageStream in;
 
         std::list<LiteralRegex> literal;
-
-        AFUN_INLINE void pushActivation(Activation *new_activation);
-        AFUN_INLINE Activation *popActivation();
     };
 
     struct Inter::LiteralRegex {

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

@@ -3,6 +3,10 @@
 #include "inter.h"
 
 namespace aFuncore {
+    EnvVarSpace &Environment::getEnvVarSpace() {
+        return env_var;
+    }
+
     Environment &Inter::getEnvironment() {
         return env;
     }
@@ -37,10 +41,6 @@ namespace aFuncore {
         return (status == inter_exit);
     }
 
-    ProtectVarSpace *Inter::getProtectVarSpace() const {
-        return env.protect;
-    }
-
     const std::list<Activation *> &Inter::getStack() const {
         return stack;
     }
@@ -50,14 +50,14 @@ namespace aFuncore {
     }
 
     EnvVarSpace &Inter::getEnvVarSpace() {
-        return env.envvar;
+        return env.env_var;
     }
 
-    InterOutMessage &Inter::getOutMessageStream() {
+    InterOutMessageStream &Inter::getOutMessageStream() {
         return out;
     }
 
-    InterInMessage &Inter::getInMessageStream() {
+    InterInMessageStream &Inter::getInMessageStream() {
         return in;
     }
 

+ 0 - 129
include/core/msg.h

@@ -1,129 +0,0 @@
-#ifndef AFUN_MSG_H
-#define AFUN_MSG_H
-#include <list>
-#include <mutex>
-#include <map>
-#include "aFuntool.h"
-#include "aFunCoreExport.h"
-
-namespace aFuncore {
-    class AFUN_CORE_EXPORT Message {
-    public:
-        AFUN_INLINE explicit Message() = default;
-        virtual ~Message() = default;
-        Message &operator=(const Message &)=delete;
-
-    };
-
-    class Object;
-    class Activation;
-    class Inter;
-
-    class TopMessage : public virtual Message {
-    public:
-        virtual void topProgress(Inter &inter, Activation &activation) = 0;
-    };
-
-    class AFUN_CORE_EXPORT NormalMessage : public TopMessage {
-    public:
-        explicit NormalMessage(Object *obj_);
-        AFUN_INLINE NormalMessage(NormalMessage &&msg) noexcept;
-        ~NormalMessage() override;
-        void topProgress(Inter &inter, Activation &activation) override;
-        AFUN_INLINE Object *getObject();
-
-    private:
-        Object *obj;
-    };
-
-    class AFUN_CORE_EXPORT ErrorMessage : public TopMessage {
-    public:
-        struct TrackBack {
-            const aFuntool::FilePath path;
-            aFuntool::FileLine line;
-        };
-
-        explicit ErrorMessage(std::string error_type_, std::string error_info_, Activation *start);
-        AFUN_INLINE ErrorMessage(ErrorMessage &&msg) noexcept;
-        void topProgress(Inter &inter_, Activation &activation) override;
-        [[nodiscard]] AFUN_INLINE std::string getErrorType() const;
-        [[nodiscard]] AFUN_INLINE std::string getErrorInfo() const;
-        [[nodiscard]] AFUN_INLINE const std::list<TrackBack> &getTrackBack() const;
-
-    private:
-        Inter &inter;
-
-        std::string error_type;
-        std::string error_info;
-        std::list<TrackBack> trackback;
-    };
-
-    class AFUN_CORE_EXPORT MessageStream {
-    public:
-        MessageStream() = default;
-        virtual ~MessageStream();
-        MessageStream(const MessageStream &)=delete;
-        MessageStream &operator=(const MessageStream &)=delete;
-
-        template<class T>
-        [[nodiscard]] T *getMessage(const std::string &type) const;
-
-        Message *popMessage(const std::string &type);
-        void pushMessage(const std::string &type, Message *msg);
-
-        template <typename Callable, typename...T>
-        void forEach(Callable func, T...arg);
-
-    protected:
-        std::map<std::string, Message *> stream;
-        [[nodiscard]] virtual Message *_getMessage(const std::string &type) const;
-    };
-
-    class AFUN_CORE_EXPORT UpMessage : public MessageStream {
-    public:
-        explicit UpMessage(const UpMessage *old=nullptr);
-        ~UpMessage() override = default;
-
-        template <typename Callable, typename...T>
-        void forEachAll(Callable func, T...arg);
-
-    protected:
-        const UpMessage *old;
-        [[nodiscard]] Message *_getMessage(const std::string &type) const override;
-    };
-
-    class AFUN_CORE_EXPORT DownMessage : public MessageStream {
-    public:
-        void joinMsg(DownMessage &msg);
-    };
-
-    class AFUN_CORE_EXPORT InterMessage : public MessageStream {
-    public:
-        Message *popFrontMessage(std::string &type);
-        Message *popMessage(const std::string &type);
-        void pushMessage(const std::string &type, Message *msg);
-
-        template <typename Callable, typename...T>
-        void forEach(Callable func, T...arg);
-
-        template <typename Callable, typename...T>
-        void forEachLock(Callable func, T...arg);
-    private:
-        std::mutex lock;
-    };
-
-    class InterOutMessage : public InterMessage {
-    public:
-        template<class T>
-        [[nodiscard]] T *getMessage(const std::string &type) const = delete;  // 对外不设置 getMessage 以避免线程问题
-    };
-
-    class InterInMessage : public InterMessage {
-
-    };
-}
-
-#include "msg.inline.h"
-#include "msg.template.h"
-
-#endif //AFUN_MSG_H

+ 0 - 129
include/core/object-value.h

@@ -1,129 +0,0 @@
-#ifndef AFUN_OBJECT_VALUE_H
-#define AFUN_OBJECT_VALUE_H
-#include <list>
-#include <mutex>
-#include "aFuntool.h"
-#include "aFunCoreExport.h"
-#include "object.h"
-#include "aFuncode.h"
-#include "inter.h"
-
-namespace aFuncore {
-    class AFUN_CORE_EXPORT Var : public Object {
-    public:
-        Environment &env;
-
-        Var(Object *data_, Inter &inter);
-        Var(Object *data_, Environment &env_);
-        ~Var() override = default;
-
-        [[nodiscard]] virtual Object *getData();
-        virtual void setData(Object *data_);
-        void linkObject(std::queue<Object *> &queue) override;
-
-    private:
-        Object *data;
-    };
-
-    class AFUN_CORE_EXPORT VarSpace : public Object {
-    public:
-        typedef enum VarOperationFlat {
-            vof_success = 0,  // 成功
-            vof_not_var = 1,  // 变量不存在
-            vof_redefine_var = 2,  // 变量重复定义
-            vof_fail = 3,  // 存在其他错误
-        } VarOperationFlat;
-
-        Environment &env;
-
-        explicit VarSpace(Inter &inter);
-        explicit VarSpace(Environment &env_);
-        ~VarSpace() override = default;
-
-        template <typename Callable,typename...T>
-        void forEach(Callable func, T...arg);
-
-        template <typename Callable,typename...T>
-        void forEachLock(Callable func, T...arg);
-
-        [[nodiscard]] AFUN_INLINE size_t getCount();
-        [[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]] AFUN_INLINE Object *findObject(const std::string &name);
-        void linkObject(std::queue<Object *> &queue) override;
-
-        AFUN_STATIC const size_t VAR_HASH_SIZE = 100;  // 环境变量哈希表大小
-
-    private:
-        std::unordered_map<std::string, Var *> var;
-    };
-
-    class AFUN_CORE_EXPORT ProtectVarSpace : public VarSpace {
-    public:
-        AFUN_INLINE explicit ProtectVarSpace(Inter &inter);
-        AFUN_INLINE explicit ProtectVarSpace(Environment &env_);
-
-        [[nodiscard]] AFUN_INLINE bool getProtect() const;
-        AFUN_INLINE bool setProtect(bool protect);
-
-        VarOperationFlat defineVar(const std::string &name, Object *data) override;
-        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 Function : public virtual Object {
-    public:
-        class AFUN_CORE_EXPORT CallFunction;
-
-        virtual CallFunction *getCallFunction(const aFuncode::Code::ByteCode *code, Inter &inter) = 0;
-        virtual bool isInfix();
-    };
-
-    class AFUN_CORE_EXPORT Function::CallFunction {
-    public:
-        class ArgCodeList;
-
-        CallFunction() = default;
-        virtual ~CallFunction() = default;
-        CallFunction(const CallFunction &)=delete;
-        CallFunction &operator=(const CallFunction &)=delete;
-
-        virtual std::list<ArgCodeList> *getArgCodeList(Inter &inter, Activation &activation, const aFuncode::Code::ByteCode *call) = 0;
-        virtual void runFunction() = 0;
-    };
-
-    class Function::CallFunction::ArgCodeList {
-    public:
-        const aFuncode::Code::ByteCode *code = nullptr;
-        AFUN_INLINE explicit ArgCodeList(const aFuncode::Code::ByteCode *code = nullptr);
-        AFUN_INLINE ~ArgCodeList();
-        AFUN_INLINE Object *setObject(Object *res);
-        AFUN_INLINE Object *getObject();
-    private:
-        Object *ret;
-    };
-
-    class AFUN_CORE_EXPORT Literaler : public virtual Object {
-    public:
-        virtual void getObject(const std::string &literal, char prefix, Inter &inter, Activation &activation) = 0;
-    };
-
-    class AFUN_CORE_EXPORT CallBackVar : public virtual Object {
-    public:
-        virtual bool isCallBack(Inter &inter, Activation &activation);
-        virtual void callBack(Inter &inter, Activation &activation) = 0;
-    };
-};
-
-#include "object-value.inline.h"
-#include "object-value.template.h"
-
-#endif //AFUN_OBJECT_VALUE_H

+ 0 - 54
include/core/object-value.inline.h

@@ -1,54 +0,0 @@
-#ifndef AFUN_OBJECT_VALUE_INLINE_H
-#define AFUN_OBJECT_VALUE_INLINE_H
-#include "object-value.h"
-
-namespace aFuncore {
-    size_t VarSpace::getCount() {
-        std::unique_lock<std::mutex> mutex{lock};
-        return var.size();
-    }
-
-    Object *VarSpace::findObject(const std::string &name) {
-        Var *ret = findVar(name);
-        return ret ? ret->getData() : nullptr;
-    }
-
-    ProtectVarSpace::ProtectVarSpace(Inter &inter) : VarSpace(inter), is_protect{false} {
-
-    }
-
-    ProtectVarSpace::ProtectVarSpace(Environment &env_) : VarSpace(env_), is_protect{false} {
-
-    }
-
-    bool ProtectVarSpace::getProtect() const {
-        return is_protect;
-    }
-
-    bool ProtectVarSpace::setProtect(bool protect) {
-        bool ret = is_protect; is_protect = protect; return ret;
-    }
-
-    Function::CallFunction::ArgCodeList::ArgCodeList(const aFuncode::Code::ByteCode *code_) : code{code_}, ret{nullptr} {
-
-    }
-
-    Function::CallFunction::ArgCodeList::~ArgCodeList() {
-        if (ret != nullptr)
-            ret->delReference();
-    }
-
-    Object *Function::CallFunction::ArgCodeList::setObject(Object *res) {
-        Object *obj = ret;
-        ret = res;
-        if (ret != nullptr)
-            ret->addReference();
-        return obj;
-    }
-
-    Object *Function::CallFunction::ArgCodeList::getObject() {
-        return ret;
-    }
-};
-
-#endif //AFUN_OBJECT_VALUE_INLINE_H

+ 10 - 0
include/runtime/aFunrt.h

@@ -0,0 +1,10 @@
+#ifndef AFUN_AFUNRT_H
+#define AFUN_AFUNRT_H
+
+#include "rt-activation.h"
+#include "rt-logger.h"
+#include "rt-message.h"
+#include "rt-object.h"
+#include "rt-exception.h"
+
+#endif //AFUN_AFUNRT_H

+ 120 - 0
include/runtime/rt-activation.h

@@ -0,0 +1,120 @@
+#ifndef AFUN_RT_ACTIVATION_H
+#define AFUN_RT_ACTIVATION_H
+#include "aFunRuntimeExport.h"
+#include "aFuncore.h"
+#include "rt-object.h"
+
+namespace aFunrt {
+    class AFUN_RT_EXPORT NormalActivation : public aFuncore::Activation {
+    public:
+        class AFUN_RT_EXPORT VarList {
+        public:
+            AFUN_INLINE explicit VarList();
+            virtual ~VarList();
+            AFUN_INLINE VarList(VarList &&new_varlist) noexcept ;
+            AFUN_INLINE VarList &operator=(VarList &&new_varlist) noexcept;
+            VarList(const VarList &) = delete;
+            VarList &operator=(const VarList &) = delete;
+
+            void clear();
+            void connect(VarList &new_varlist);
+            AFUN_INLINE void push(VarSpace *varspace_);
+            AFUN_INLINE size_t count();
+
+            template <typename Callable,typename...T>
+            void forEach(Callable func, T...arg);
+
+            [[nodiscard]] virtual Var *findVar(const std::string &name);
+            virtual bool defineVar(const std::string &name, aFuncore::Object *data);
+            virtual bool defineVar(const std::string &name, Var *data);
+            virtual bool setVar(const std::string &name, aFuncore::Object *data);
+            virtual bool delVar(const std::string &name);
+            [[nodiscard]] AFUN_INLINE aFuncore::Object *findObject(const std::string &name);
+
+        private:
+            std::list<VarSpace *> varspace;
+        };
+
+        aFuncore::Inter &inter;
+
+        explicit NormalActivation(aFuncore::Inter &inter_);
+        ~NormalActivation() override;
+
+        void runCode(const aFuncode::Code::ByteCode *code) override;
+        void endRun() override;
+
+        [[nodiscard]] AFUN_INLINE VarList &getVarlist();
+        [[nodiscard]] aFuncore::UpMessageStream &getUpStream() override;
+        [[nodiscard]] aFuncore::DownMessageStream &getDownStream() override;
+        [[nodiscard]] aFuntool::FileLine getFileLine() override;
+        [[nodiscard]] const aFuntool::FilePath &getFilePath() override;
+
+    protected:
+        VarList varlist;
+
+        aFuncore::UpMessageStream up;
+        aFuncore::DownMessageStream down;
+
+        aFuntool::FilePath path;
+        aFuntool::FileLine line;
+
+        virtual void runCodeElement(const aFuncode::Code::ByteCode *code);
+        virtual void runCodeBlockP(const aFuncode::Code::ByteCode *code);
+        virtual void runCodeBlockC(const aFuncode::Code::ByteCode *code);
+        virtual void runCodeBlockB(const aFuncode::Code::ByteCode *code);
+    };
+
+    class AFUN_RT_EXPORT ExeActivation : public NormalActivation {
+    public:
+        AFUN_INLINE ExeActivation(const aFuncode::Code &code, aFuncore::Inter &inter_);
+        AFUN_INLINE ExeActivation(const aFuncode::Code::ByteCode *code, aFuncore::Inter &inter_);
+        ActivationStatus getCode(const aFuncode::Code::ByteCode *&code) override;
+        [[nodiscard]] AFUN_INLINE const aFuncode::Code::ByteCode *getStart() const;
+
+    private:
+        const aFuncode::Code::ByteCode *start;
+        const aFuncode::Code::ByteCode *next;
+        bool first=true;
+    };
+
+    class AFUN_RT_EXPORT TopActivation : public ExeActivation {
+    public:
+        explicit TopActivation(const aFuncode::Code &code, aFuncore::Inter &inter_);
+        ~TopActivation() override = default;
+        [[nodiscard]] AFUN_INLINE const aFuncode::Code &getBase() const;
+
+    private:
+        const aFuncode::Code &base;
+    };
+
+    class AFUN_RT_EXPORT FuncActivation : public NormalActivation {
+    public:
+        AFUN_INLINE explicit FuncActivation(const aFuncode::Code::ByteCode *code, aFuncore::Inter &inter_);
+        explicit FuncActivation(Function *func, aFuncore::Inter &inter_);
+        ~FuncActivation() override;
+        ActivationStatus getCode(const aFuncode::Code::ByteCode *&code) override;
+        void endRun() override;
+
+    private:
+        enum {
+            func_first = 0,  // 获取函数体前准备
+            func_get_func = 1,  // 获取函数体后,开始获取参数前
+            func_get_arg = 2,  // 获取参数过程
+        } status = func_first;
+
+        bool on_tail = false;
+        const aFuncode::Code::ByteCode *call;
+
+        Function *func = nullptr;
+        Function::CallFunction *call_func = nullptr;
+
+        std::list<Function::CallFunction::ArgCodeList> *acl = nullptr;
+        std::list<Function::CallFunction::ArgCodeList>::iterator acl_begin;
+        std::list<Function::CallFunction::ArgCodeList>::iterator acl_end;
+    };
+}
+
+#include "rt-activation.inline.h"
+#include "rt-activation.template.h"
+
+#endif //AFUN_RT_ACTIVATION_H

+ 61 - 0
include/runtime/rt-activation.inline.h

@@ -0,0 +1,61 @@
+#ifndef AFUN_RT_ACTIVATION_INLINE_H
+#define AFUN_RT_ACTIVATION_INLINE_H
+
+#include "rt-activation.h"
+
+namespace aFunrt {
+    NormalActivation::VarList &NormalActivation::getVarlist(){
+        return varlist;
+    }
+
+    NormalActivation::VarList::VarList() : varspace{} {
+
+    }
+
+    NormalActivation::VarList::VarList(VarList &&new_varlist) noexcept : varspace{std::move(new_varlist.varspace)} {
+
+    }
+
+    NormalActivation::VarList &NormalActivation::VarList::operator=(VarList &&new_varlist)  noexcept {
+        clear();
+        varspace = std::move(new_varlist.varspace);
+        return *this;
+    }
+
+    void NormalActivation::VarList::push(VarSpace *varspace_) {
+        varspace_->addReference();
+        varspace.push_front(varspace_);
+    }
+
+    size_t NormalActivation::VarList::count() {
+        return varspace.size();
+    }
+
+    aFuncore::Object *NormalActivation::VarList::findObject(const std::string &name) {
+        Var *var = findVar(name);
+        return var ? var->getData() : nullptr;
+    }
+
+    ExeActivation::ExeActivation(const aFuncode::Code &code, aFuncore::Inter &inter_) : NormalActivation(inter_), start{code.getByteCode()}, next{code.getByteCode()} {
+
+    }
+
+    ExeActivation::ExeActivation(const aFuncode::Code::ByteCode *code, aFuncore::Inter &inter_) : NormalActivation(inter_), start{code}, next{code} {
+
+    }
+
+    const aFuncode::Code::ByteCode *ExeActivation::getStart() const{
+        return start;
+    }
+
+    const aFuncode::Code &TopActivation::getBase() const {
+        return base;
+    }
+
+    FuncActivation::FuncActivation(const aFuncode::Code::ByteCode *code, aFuncore::Inter &inter_) : NormalActivation(inter_), call{code} {
+
+    }
+}
+
+
+#endif //AFUN_RT_ACTIVATION_INLINE_H

+ 14 - 0
include/runtime/rt-activation.template.h

@@ -0,0 +1,14 @@
+#ifndef AFUN_RT_ACTIVATION_TEMPLATE_H
+#define AFUN_RT_ACTIVATION_TEMPLATE_H
+#include "rt-activation.h"
+
+namespace aFunrt {
+    template <typename Callable, typename...T>
+    void NormalActivation::VarList::forEach(Callable func, T...arg) {
+        for (auto &vs : varspace)
+            func(vs, arg...);
+    }
+}
+
+
+#endif //AFUN_RT_ACTIVATION_TEMPLATE_H

+ 27 - 0
include/runtime/rt-exception.h

@@ -0,0 +1,27 @@
+#ifndef AFUN_RT_EXCEPTION_H
+#define AFUN_RT_EXCEPTION_H
+#include "aFuntool.h"
+
+namespace aFunrt {
+    class aFunrtException : public aFuntool::aFunException {
+    public:
+        AFUN_INLINE explicit aFunrtException(const std::string &msg);
+    };
+
+    class RuntimeError : public aFunrtException {
+        std::string type;
+    public:
+        AFUN_INLINE RuntimeError(const std::string &msg, std::string type);
+        AFUN_INLINE const std::string &getType() const;
+    };
+
+    class ArgumentError : public RuntimeError {
+        std::string type;
+    public:
+        AFUN_INLINE ArgumentError();
+    };
+}
+
+#include "rt-exception.inline.h"
+
+#endif //AFUN_RT_EXCEPTION_H

+ 23 - 0
include/runtime/rt-exception.inline.h

@@ -0,0 +1,23 @@
+#ifndef AFUN_RT_EXCEPTION_INLINE_H
+#define AFUN_RT_EXCEPTION_INLINE_H
+#include "rt-exception.h"
+
+namespace aFunrt {
+    aFunrtException::aFunrtException(const std::string &msg) : aFunException{msg} {
+
+    }
+
+    RuntimeError::RuntimeError(const std::string &msg, std::string type_) : aFunrtException(msg), type{std::move(type_)} {
+
+    }
+
+    const std::string &RuntimeError::getType() const {
+        return type;
+    }
+
+    ArgumentError::ArgumentError() : RuntimeError("Argument mismatch", "ArgumentError") {
+
+    }
+}
+
+#endif //AFUN_RT_EXCEPTION_INLINE_H

+ 12 - 0
include/runtime/rt-logger.h

@@ -0,0 +1,12 @@
+#ifndef AFUN_RT_LOGGER_H
+#define AFUN_RT_LOGGER_H
+#include "aFunRuntimeExport.h"
+#include "aFuntool.h"
+
+namespace aFunrt {
+    AFUN_RT_EXPORT extern aFuntool::Logger *aFunRuntimeLogger;
+    AFUN_STATIC void setRuntimeLogger(aFuntool::Logger *log);
+}
+
+#include "rt-logger.inline.h"
+#endif //AFUN_RT_LOGGER_H

+ 12 - 0
include/runtime/rt-logger.inline.h

@@ -0,0 +1,12 @@
+#ifndef AFUN_RT_LOGGER_INLINE_H
+#define AFUN_RT_LOGGER_INLINE_H
+
+#include "rt-logger.h"
+
+namespace aFunrt {
+    void setRuntimeLogger(aFuntool::Logger *log) {
+        aFunRuntimeLogger = log;
+    }
+}
+
+#endif //AFUN_RT_LOGGER_INLINE_H

+ 48 - 0
include/runtime/rt-message.h

@@ -0,0 +1,48 @@
+#ifndef AFUN_RT_MESSAGE_H
+#define AFUN_RT_MESSAGE_H
+#include "aFunRuntimeExport.h"
+#include "aFuncore.h"
+
+namespace aFunrt {
+    class TopMessage : public virtual aFuncore::Message {
+    public:
+        virtual void topProgress(aFuncore::Inter &inter, aFuncore::Activation &activation) = 0;
+    };
+
+    class AFUN_RT_EXPORT NormalMessage : public TopMessage {
+    public:
+        explicit NormalMessage(aFuncore::Object *obj_);
+        AFUN_INLINE NormalMessage(NormalMessage &&msg) noexcept;
+        ~NormalMessage() override;
+        void topProgress(aFuncore::Inter &inter, aFuncore::Activation &activation) override;
+        AFUN_INLINE aFuncore::Object *getObject();
+
+    private:
+        aFuncore::Object *obj;
+    };
+
+    class AFUN_RT_EXPORT ErrorMessage : public TopMessage {
+    public:
+        struct TrackBack {
+            const aFuntool::FilePath path;
+            aFuntool::FileLine line;
+        };
+
+        explicit ErrorMessage(std::string error_type_, std::string error_info_, aFuncore::Inter &inter);
+        AFUN_INLINE ErrorMessage(ErrorMessage &&msg) noexcept;
+        void topProgress(aFuncore::Inter &inter_, aFuncore::Activation &activation) override;
+        [[nodiscard]] AFUN_INLINE std::string getErrorType() const;
+        [[nodiscard]] AFUN_INLINE std::string getErrorInfo() const;
+        [[nodiscard]] AFUN_INLINE const std::list<TrackBack> &getTrackBack() const;
+
+    private:
+        aFuncore::Inter &inter;
+
+        std::string error_type;
+        std::string error_info;
+        std::list<TrackBack> trackback;
+    };
+}
+
+#include "rt-message.inline.h"
+#endif //AFUN_RT_MESSAGE_H

+ 9 - 8
include/core/msg.inline.h → include/runtime/rt-message.inline.h

@@ -1,19 +1,19 @@
-#ifndef AFUN_MSG_INLINE_H
-#define AFUN_MSG_INLINE_H
-#include "msg.h"
+#ifndef AFUN_RT_MESSAGE_INLINE_H
+#define AFUN_RT_MESSAGE_INLINE_H
+#include "rt-message.h"
 
-namespace aFuncore {
+namespace aFunrt {
     NormalMessage::NormalMessage(NormalMessage &&msg) noexcept : obj {msg.obj}{
         msg.obj = nullptr;
     }
 
-    Object *NormalMessage::getObject() {
+    aFuncore::Object *NormalMessage::getObject() {
         return obj;
     }
 
     ErrorMessage::ErrorMessage(ErrorMessage &&msg) noexcept
-        : inter{msg.inter}, error_type{std::move(msg.error_type)},
-          error_info{std::move(msg.error_info)}, trackback{std::move(msg.trackback)}{
+            : inter{msg.inter}, error_type{std::move(msg.error_type)},
+              error_info{std::move(msg.error_info)}, trackback{std::move(msg.trackback)}{
 
     }
 
@@ -30,4 +30,5 @@ namespace aFuncore {
     }
 }
 
-#endif //AFUN_MSG_INLINE_H
+
+#endif //AFUN_RT_MESSAGE_INLINE_H

+ 109 - 0
include/runtime/rt-object.h

@@ -0,0 +1,109 @@
+#ifndef AFUN_RT_OBJECT_H
+#define AFUN_RT_OBJECT_H
+#include <list>
+#include <mutex>
+#include "aFunRuntimeExport.h"
+#include "aFuncore.h"
+
+namespace aFunrt {
+    class AFUN_RT_EXPORT Var : public aFuncore::Object {
+    public:
+        aFuncore::Environment &env;
+
+        Var(Object *data_, aFuncore::Inter &inter);
+        Var(Object *data_, aFuncore::Environment &env_);
+        ~Var() override = default;
+
+        [[nodiscard]] virtual Object *getData();
+        virtual void setData(Object *data_);
+        void linkObject(std::queue<Object *> &queue) override;
+
+    private:
+        Object *data;
+    };
+
+    class AFUN_RT_EXPORT VarSpace : public aFuncore::Object {
+    public:
+        typedef enum VarOperationFlat {
+            vof_success = 0,  // 成功
+            vof_not_var = 1,  // 变量不存在
+            vof_redefine_var = 2,  // 变量重复定义
+            vof_fail = 3,  // 存在其他错误
+        } VarOperationFlat;
+
+        aFuncore::Environment &env;
+
+        explicit VarSpace(aFuncore::Inter &inter);
+        explicit VarSpace(aFuncore::Environment &env_);
+        ~VarSpace() override = default;
+
+        template <typename Callable,typename...T>
+        void forEach(Callable func, T...arg);
+
+        template <typename Callable,typename...T>
+        void forEachLock(Callable func, T...arg);
+
+        [[nodiscard]] AFUN_INLINE size_t getCount();
+        [[nodiscard]] virtual Var *findVar(const std::string &name);
+        virtual VarOperationFlat defineVar(const std::string &name, aFuncore::Object *data);
+        virtual VarOperationFlat defineVar(const std::string &name, Var *data);
+        virtual VarOperationFlat setVar(const std::string &name, aFuncore::Object *data);
+        virtual VarOperationFlat delVar(const std::string &name);
+
+        [[nodiscard]] AFUN_INLINE aFuncore::Object *findObject(const std::string &name);
+        void linkObject(std::queue<aFuncore::Object *> &queue) override;
+
+        AFUN_STATIC const size_t VAR_HASH_SIZE = 100;  // 环境变量哈希表大小
+
+    private:
+        std::unordered_map<std::string, Var *> var;
+    };
+
+    class AFUN_RT_EXPORT Function : public virtual aFuncore::Object {
+    public:
+        class AFUN_RT_EXPORT CallFunction;
+
+        virtual CallFunction *getCallFunction(const aFuncode::Code::ByteCode *code, aFuncore::Inter &inter) = 0;
+        virtual bool isInfix();
+    };
+
+    class AFUN_RT_EXPORT Function::CallFunction {
+    public:
+        class ArgCodeList;
+
+        CallFunction() = default;
+        virtual ~CallFunction() = default;
+        CallFunction(const CallFunction &)=delete;
+        CallFunction &operator=(const CallFunction &)=delete;
+
+        virtual std::list<ArgCodeList> *getArgCodeList(aFuncore::Inter &inter, aFuncore::Activation &activation, const aFuncode::Code::ByteCode *call) = 0;
+        virtual void runFunction() = 0;
+    };
+
+    class Function::CallFunction::ArgCodeList {
+    public:
+        const aFuncode::Code::ByteCode *code = nullptr;
+        AFUN_INLINE explicit ArgCodeList(const aFuncode::Code::ByteCode *code = nullptr);
+        AFUN_INLINE ~ArgCodeList();
+        AFUN_INLINE aFuncore::Object *setObject(aFuncore::Object *res);
+        AFUN_INLINE aFuncore::Object *getObject();
+    private:
+        aFuncore::Object *ret;
+    };
+
+    class AFUN_RT_EXPORT Literaler : public virtual aFuncore::Object {
+    public:
+        virtual void getObject(const std::string &literal, char prefix, aFuncore::Inter &inter, aFuncore::Activation &activation) = 0;
+    };
+
+    class AFUN_RT_EXPORT CallBackVar : public virtual aFuncore::Object {
+    public:
+        virtual bool isCallBack(aFuncore::Inter &inter, aFuncore::Activation &activation);
+        virtual void callBack(aFuncore::Inter &inter, aFuncore::Activation &activation) = 0;
+    };
+};
+
+#include "rt-object.inline.h"
+#include "rt-object.template.h"
+
+#endif //AFUN_RT_OBJECT_H

+ 39 - 0
include/runtime/rt-object.inline.h

@@ -0,0 +1,39 @@
+#ifndef AFUN_RT_OBJECT_INLINE_H
+#define AFUN_RT_OBJECT_INLINE_H
+#include "rt-object.h"
+
+namespace aFunrt {
+    size_t VarSpace::getCount() {
+        std::unique_lock<std::mutex> mutex{lock};
+        return var.size();
+    }
+
+    aFuncore::Object *VarSpace::findObject(const std::string &name) {
+        Var *ret = findVar(name);
+        return ret ? ret->getData() : nullptr;
+    }
+
+    Function::CallFunction::ArgCodeList::ArgCodeList(const aFuncode::Code::ByteCode *code_) : code{code_}, ret{nullptr} {
+
+    }
+
+    Function::CallFunction::ArgCodeList::~ArgCodeList() {
+        if (ret != nullptr)
+            ret->delReference();
+    }
+
+    aFuncore::Object *Function::CallFunction::ArgCodeList::setObject(Object *res) {
+        Object *obj = ret;
+        ret = res;
+        if (ret != nullptr)
+            ret->addReference();
+        return obj;
+    }
+
+    aFuncore::Object *Function::CallFunction::ArgCodeList::getObject() {
+        return ret;
+    }
+};
+
+
+#endif //AFUN_RT_OBJECT_INLINE_H

+ 5 - 5
include/core/object-value.template.h → include/runtime/rt-object.template.h

@@ -1,9 +1,9 @@
-#ifndef AFUN_OBJECT_VALUE_TEMPLATE_H
-#define AFUN_OBJECT_VALUE_TEMPLATE_H
+#ifndef AFUN_RT_OBJECT_TEMPLATE_H
+#define AFUN_RT_OBJECT_TEMPLATE_H
 
-#include "object-value.h"
+#include "rt-object.h"
 
-namespace aFuncore {
+namespace aFunrt {
     template <typename Callable, typename...T>
     void VarSpace::forEach(Callable func, T...arg) {
         std::unique_lock<std::mutex> mutex{lock};
@@ -22,4 +22,4 @@ namespace aFuncore {
     }
 }
 
-#endif //AFUN_OBJECT_VALUE_TEMPLATE_H
+#endif //AFUN_RT_OBJECT_TEMPLATE_H

+ 4 - 0
src/CMakeLists.txt

@@ -2,12 +2,14 @@
 set(build_include_code ${PROJECT_SOURCE_DIR}/include/code)
 set(build_include_core ${PROJECT_SOURCE_DIR}/include/core)
 set(build_include_parser ${PROJECT_SOURCE_DIR}/include/parser)
+set(build_include_rt ${PROJECT_SOURCE_DIR}/include/runtime)
 set(build_include_interface ${PROJECT_SOURCE_DIR}/include/interface)
 
 set(install_include_tool ${INSTALL_INCLUDEDIR})
 set(install_include_code ${INSTALL_INCLUDEDIR})
 set(install_include_core ${INSTALL_INCLUDEDIR})
 set(install_include_parser ${INSTALL_INCLUDEDIR})
+set(install_include_rt ${INSTALL_INCLUDEDIR})
 set(install_include_interface ${INSTALL_INCLUDEDIR})
 
 set(build_include)
@@ -18,6 +20,7 @@ foreach(dir
         ${build_include_code}
         ${build_include_core}
         ${build_include_parser}
+        ${build_include_rt}
         ${build_include_interface})
     list(APPEND build_include $<BUILD_INTERFACE:${dir}>)
 endforeach()
@@ -28,6 +31,7 @@ add_subdirectory(tool)
 add_subdirectory(code)
 add_subdirectory(core)
 add_subdirectory(parser)
+add_subdirectory(runtime)
 add_subdirectory(interface)
 
 ## source在子目录中被使用, 为了避免子目录访问到source, 子目录将在此前面被执行

+ 35 - 4
src/core/env-var.cpp

@@ -1,5 +1,14 @@
 #include "env-var.h"
+#include "object.h"
+
 namespace aFuncore {
+    EnvVarSpace::~EnvVarSpace() {
+        for (auto &i : var) {
+            if (i.second.object != nullptr)
+                i.second.object->delReference();
+        }
+    }
+
     /**
      * 获取环境变量文本
      * @param name 变量名
@@ -30,6 +39,15 @@ namespace aFuncore {
         return true;
     }
 
+    bool EnvVarSpace::findObject(const std::string &name, Object *&obj) {
+        std::unique_lock<std::mutex> mutex{lock};
+        auto env_var = var.find(name);
+        if (env_var == var.end())
+            return false;
+        obj = env_var->second.object;
+        return true;
+    }
+
     /**
      * 设置环境变量文本
      * @param name 变量名
@@ -39,7 +57,7 @@ namespace aFuncore {
         std::unique_lock<std::mutex> mutex{lock};
         auto env_var = var.find(name);
         if (env_var == var.end())
-            var.insert({name, {str, 0}});
+            var.insert({name, {str, 0, nullptr}});
         else
             env_var->second.str = str;
     }
@@ -53,11 +71,24 @@ namespace aFuncore {
         std::unique_lock<std::mutex> mutex{lock};
         auto env_var = var.find(name);
         if (env_var == var.end())
-            var.insert({name, {"", num}});
+            var.insert({name, {"", num, nullptr}});
         else
             env_var->second.num = num;
     }
 
+    void EnvVarSpace::setObject(const std::string &name, Object *obj) {
+        std::unique_lock<std::mutex> mutex{lock};
+        obj->addReference();
+        auto env_var = var.find(name);
+        if (env_var == var.end())
+            var.insert({name, {"", 0, obj}});
+        else {
+            if (env_var->second.object != nullptr)
+                env_var->second.object->delReference();
+            env_var->second.object = obj;
+        }
+    }
+
     /**
      * 设置环境变量文本 (加法)
      * @param name 变量名
@@ -67,7 +98,7 @@ namespace aFuncore {
         std::unique_lock<std::mutex> mutex{lock};
         auto env_var = var.find(name);
         if (env_var == var.end())
-            var.insert({name, {str, 0}});
+            var.insert({name, {str, 0, nullptr}});
         else
             env_var->second.str += str;
     }
@@ -81,7 +112,7 @@ namespace aFuncore {
         std::unique_lock<std::mutex> mutex{lock};
         auto env_var = var.find(name);
         if (env_var == var.end())
-            var.insert({name, {"", num}});
+            var.insert({name, {"", num, nullptr}});
         else
             env_var->second.num += num;
     }

+ 10 - 48
src/core/inter.cpp

@@ -1,8 +1,9 @@
 #include "inter.h"
 #include "core-activation.h"
 #include "core-logger.h"
-#include "msg.h"
+#include "core-message-stream.h"
 #include "core-exception.h"
+#include "object.h"
 
 namespace aFuncore {
     Inter::Inter(Environment &env_)
@@ -26,7 +27,6 @@ namespace aFuncore {
      */
     void Inter::enable(){
         if (status == inter_init) {
-            env.protect->setProtect(true);
             status = inter_normal;
         }
     }
@@ -61,50 +61,13 @@ namespace aFuncore {
                     break;
                 default:
                     errorLog(aFunCoreLogger, "Error activation status.");
-                    activation->getDownStream().pushMessage("ERROR",
-                                                            new ErrorMessage("RuntimeError", "Error activation status.", activation));
+                    delete popActivation();
                     break;
             }
         }
         return true;
     }
 
-    /**
-     * 运行代码
-     * @param code 代码
-     * @return
-     */
-    bool Inter::runCode(const aFuncode::Code &code){
-        if (activation != nullptr) {
-            errorLog(aFunCoreLogger, "Run code with activation");
-            return false;
-        }
-
-        new TopActivation(code, *this);
-        return runCode();
-    }
-
-    /**
-     * 函数调用
-     * @param code 代码
-     * @return
-     */
-    bool Inter::runCode(Object *func){
-        if (activation != nullptr) {
-            errorLog(aFunCoreLogger, "Run function with activation");
-            return false;
-        }
-
-        auto func_obj = dynamic_cast<Function *>(func);
-        if (func_obj == nullptr) {
-            errorLog(aFunCoreLogger, "Run without function");
-            return false;
-        }
-
-        new FuncActivation(func_obj, *this);
-        return runCode();
-    }
-
     /**
      * 检查字面量是否匹配
      * @param element 字面量
@@ -175,7 +138,7 @@ namespace aFuncore {
             Object::destructUnreachable(des, gc_inter);
 
             int32_t intervals = 1000;
-            envvar.findNumber("sys:gc-intervals", intervals);
+            env_var.findNumber("sys:gc-intervals", intervals);
             if (intervals < 100)
                 intervals = 100;
             std::this_thread::sleep_for(std::chrono::milliseconds(intervals));
@@ -185,16 +148,16 @@ namespace aFuncore {
     }
 
     Environment::Environment(int argc, char **argv)
-        : reference{0}, destruct{false}, gc_inter{*(new Inter(*this))}, protect{new ProtectVarSpace(*this)} {
+        : reference{0}, destruct{false}, gc_inter{*(new Inter(*this))}, env_var{*new EnvVarSpace()} {
         /* 生成 gc_inter 后, reference == 1 */
 
-        envvar.setNumber("sys:gc-intervals", 1000);
-        envvar.setNumber("sys:exit-code", 0);
-        envvar.setNumber("sys:argc", argc);
+        env_var.setNumber("sys:gc-intervals", 1000);
+        env_var.setNumber("sys:exit-code", 0);
+        env_var.setNumber("sys:argc", argc);
         for (int i = 0; i < argc; i++) {
             char buf[20];
             snprintf(buf, 10, "sys:arg%d", i);
-            envvar.setString(buf, argv[i]);
+            env_var.setString(buf, argv[i]);
         }
 
         gc_thread = std::thread([this](){this->gcThread();});
@@ -214,8 +177,7 @@ namespace aFuncore {
 
         gc_thread.join();
         delete &gc_inter;
-
-        protect->delReference();
+        delete &env_var;
 
         Object::deleteAll(gc); /* 不需要mutex锁 */
 

+ 8 - 35
src/core/msg.cpp → src/core/message-stream.cpp

@@ -1,36 +1,9 @@
-#include "msg.h"
+#include "core-message-stream.h"
 #include "core-activation.h"
 #include "inter.h"
 #include "env-var.h"
 
 namespace aFuncore {
-    NormalMessage::NormalMessage(Object *obj_) : obj {obj_} {
-        obj->addReference();
-    }
-
-    NormalMessage::~NormalMessage(){
-        if (obj != nullptr) {
-            obj->delReference();
-            obj = nullptr;
-        }
-    }
-
-    void NormalMessage::topProgress(Inter &inter, Activation &){
-        inter.getOutMessageStream().pushMessage("NORMAL", new NormalMessage(std::move(*this)));
-    }
-
-    ErrorMessage::ErrorMessage(std::string error_type_, std::string error_info_, Activation *start)
-        : inter{start->inter}, error_type{std::move(error_type_)}, error_info{std::move(error_info_)} {
-        for (const auto activation : inter.getStack()) {
-            if (activation->getFileLine() != 0)
-                trackback.push_front({activation->getFilePath(), activation->getFileLine()});
-        }
-    }
-
-    void ErrorMessage::topProgress(Inter &inter_, Activation &){
-        inter_.getOutMessageStream().pushMessage("ERROR", new ErrorMessage(std::move(*this)));
-    }
-
     MessageStream::~MessageStream(){
         for (auto &msg : stream)
             delete msg.second;
@@ -70,12 +43,12 @@ namespace aFuncore {
         return msg;
     }
 
-    UpMessage::UpMessage(const UpMessage *old_) : MessageStream(), old{old_} {
+    UpMessageStream::UpMessageStream(const UpMessageStream *old_) : MessageStream(), old{old_} {
 
     }
 
-    Message *UpMessage::_getMessage(const std::string &type) const {
-        for (const UpMessage *up = this; up != nullptr; up = up->old) {
+    Message *UpMessageStream::_getMessage(const std::string &type) const {
+        for (const UpMessageStream *up = this; up != nullptr; up = up->old) {
             Message *ret = up->MessageStream::_getMessage(type);
             if (ret != nullptr)
                 return ret;
@@ -87,11 +60,11 @@ namespace aFuncore {
      * 拼接数据流 (将this合并到msg)
      * @param msg
      */
-    void DownMessage::joinMsg(DownMessage &msg){
+    void DownMessageStream::joinMsg(DownMessageStream &msg){
         msg.stream.merge(stream);
     }
 
-    Message *InterMessage::popFrontMessage(std::string &type) {
+    Message *InterMessageStream::popFrontMessage(std::string &type) {
         std::unique_lock<std::mutex> mutex{lock};
         if (stream.empty())
             return nullptr;
@@ -101,12 +74,12 @@ namespace aFuncore {
         return ret;
     }
 
-    Message *InterMessage::popMessage(const std::string &type) {
+    Message *InterMessageStream::popMessage(const std::string &type) {
         std::unique_lock<std::mutex> mutex{lock};
         return MessageStream::popMessage(type);
     }
 
-    void InterMessage::pushMessage(const std::string &type, Message *msg) {
+    void InterMessageStream::pushMessage(const std::string &type, Message *msg) {
         std::unique_lock<std::mutex> mutex{lock};
         MessageStream::pushMessage(type, msg);
     }

+ 40 - 0
src/runtime/CMakeLists.txt

@@ -0,0 +1,40 @@
+file(GLOB source
+        LIST_DIRECTORIES FALSE
+        ${CMAKE_CURRENT_LIST_DIR}/*.cpp)
+
+file(GLOB public_h
+        LIST_DIRECTORIES FALSE
+        RELATIVE "${build_include_rt}"
+        "${build_include_rt}/*.h")
+
+set(public_h_build)
+set(public_h_install)
+
+foreach(h IN LISTS public_h)
+    file(RELATIVE_PATH _path ${CMAKE_CURRENT_LIST_DIR} "${build_include_rt}/${h}")
+    list(APPEND public_h_build   "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${_path}>")  # 相对路径的转换, 此处只能使用相对路径
+    list(APPEND public_h_install "$<INSTALL_INTERFACE:${install_include_rt}/${h}>")
+endforeach()
+
+add_library(rt-shared SHARED "")
+add_library(rt-static STATIC "")
+
+foreach(tgt rt-shared rt-static)
+    target_sources(${tgt} PRIVATE ${source} PUBLIC ${public_h_build} ${public_h_install})
+    target_include_directories(${tgt} PUBLIC ${build_include} ${install_include})
+    set_target_properties(${tgt} PROPERTIES PUBLIC_HEADER "${public_h_build}")
+    define_filename(${tgt})
+endforeach()
+
+set_target_properties(rt-shared PROPERTIES OUTPUT_NAME "aFunrt")
+set_target_properties(rt-static PROPERTIES OUTPUT_NAME "aFunrt-s")
+
+target_link_libraries(rt-shared PUBLIC tool-shared code-shared core-shared)
+target_link_libraries(rt-static PUBLIC tool-static code-static core-static)
+
+#install(TARGETS rt-shared rt-static
+#        EXPORT aFunlang
+#        RUNTIME DESTINATION ${INSTALL_BINDIR} COMPONENT base-runtime
+#        ARCHIVE DESTINATION ${INSTALL_LIBDIR} COMPONENT dev
+#        LIBRARY DESTINATION ${INSTALL_LIBDIR} COMPONENT base-runtime
+#        PUBLIC_HEADER DESTINATION ${INSTALL_INCLUDEDIR} COMPONENT dev)

+ 8 - 64
src/core/object-value.cpp → src/runtime/object.cpp

@@ -1,24 +1,20 @@
-#include "object-value.h"
-#include "inter.h"
-#include "core-logger.h"
-#include "core-exception.h"
-#include "core-activation.h"
+#include "rt-object.h"
 
-namespace aFuncore {
-    Var::Var(Object *data_, Inter &inter) : Object("Var", inter), env{inter.getEnvironment()}, data{data_}{
+namespace aFunrt {
+    Var::Var(Object *data_, aFuncore::Inter &inter) : Object("Var", inter), env{inter.getEnvironment()}, data{data_}{
 
     }
 
-    Var::Var(Object *data_, Environment &env_) : Object("Var", env_), env{env_}, data{data_}{
+    Var::Var(Object *data_, aFuncore::Environment &env_) : Object("Var", env_), env{env_}, data{data_}{
 
     }
 
 
-    VarSpace::VarSpace(Inter &inter) : Object("VarSpace", inter), env{inter.getEnvironment()}{
+    VarSpace::VarSpace(aFuncore::Inter &inter) : Object("VarSpace", inter), env{inter.getEnvironment()}{
 
     }
 
-    VarSpace::VarSpace(Environment &env_) : Object("VarSpace", env_), env{env_}{
+    VarSpace::VarSpace(aFuncore::Environment &env_) : Object("VarSpace", env_), env{env_}{
 
     }
 
@@ -26,7 +22,7 @@ namespace aFuncore {
         queue.push(getData());
     }
 
-    Object *Var::getData() {
+    aFuncore::Object *Var::getData() {
         std::unique_lock<std::mutex> mutex{lock};
         return data;
     }
@@ -113,63 +109,11 @@ namespace aFuncore {
             queue.push(tmp.second);
     }
 
-    /**
-     * 定义变量
-     * 若启用保护且变量名存在,则返回错误redefine
-     * 若启用保护则返回错误fail
-     * @param name 变量名
-     * @param data 变量(Object)
-     * @return
-     */
-    VarSpace::VarOperationFlat ProtectVarSpace::defineVar(const std::string &name, Object *data){
-        return VarSpace::defineVar(name, data);
-    }
-
-    /**
-     * 定义变量
-     * 若启用保护且变量名存在,则返回错误redefine
-     * 若启用保护则返回错误fail
-     * @param name 变量名
-     * @param data 变量(Var)
-     * @return
-     */
-    VarSpace::VarOperationFlat ProtectVarSpace::defineVar(const std::string &name, Var *data){
-        return VarSpace::defineVar(name, data);
-    }
-
-    /**
-     * 设定变量的值
-     * 若启用保护且变量名存在,则返回错误fail
-     * 若启用保护则返回错误 not_var
-     * @param name 变量名
-     * @param data 变量(Var)
-     * @return
-     */
-    VarSpace::VarOperationFlat ProtectVarSpace::setVar(const std::string &name, Object *data){
-        if (is_protect)
-            return findVar(name) ? vof_fail : vof_not_var;
-        return VarSpace::setVar(name, data);
-    }
-
-    /**
-     * 删除变量
-     * 若启用保护且变量名存在,则返回错误fail
-     * 若启用保护则返回错误 not_var
-     * @param name 变量名
-     * @param data 变量(Var)
-     * @return
-     */
-    VarSpace::VarOperationFlat ProtectVarSpace::delVar(const std::string &name){
-        if (is_protect)
-            return findVar(name) ? vof_fail : vof_not_var;
-        return VarSpace::delVar(name);
-    }
-
     bool Function::isInfix() {
         return false;
     }
 
-    bool CallBackVar::isCallBack(Inter &, Activation &) {
+    bool CallBackVar::isCallBack(aFuncore::Inter &, aFuncore::Activation &) {
         return true;
     }
 }

+ 97 - 74
src/core/activation.cpp → src/runtime/rt-activation.cpp

@@ -1,35 +1,31 @@
-#include "core-activation.h"
-#include "inter.h"
-#include "core-logger.h"
-#include "msg.h"
-#include "aFuncode.h"
-#include "core-exception.h"
-
-namespace aFuncore {
+#include "rt-activation.h"
+#include "rt-exception.h"
+#include "rt-message.h"
+#include "rt-logger.h"
+
+namespace aFunrt {
     /**
      * 创建基本Activation
      * 自动继承上层VarList和UpMessage
      * 自动压入inter
      * @param inter_
      */
-    Activation::Activation(Inter &inter_)
-            : inter{inter_}, up{inter_.activation == nullptr ? nullptr : &inter_.activation->up}, down{}, line{0} {
-        if (inter_.activation != nullptr) {
-            varlist.connect(inter_.activation->varlist);
-            line = inter_.activation->line;
-            path = inter_.activation->path;
+    NormalActivation::NormalActivation(aFuncore::Inter &inter_)
+            : Activation(), inter{inter_}, up{inter_.getActivation() == nullptr ? nullptr : &inter_.getActivation()->getUpStream()}, down{}, line{0} {
+        auto activation = dynamic_cast<NormalActivation *>(inter_.getActivation());
+        if (activation != nullptr) {
+            varlist.connect(activation->getVarlist());
+            line = inter_.getActivation()->getFileLine();
+            path = inter_.getActivation()->getFilePath();
         } else {
             auto global = new VarSpace(inter);
-            varlist.push(inter_.getProtectVarSpace());
             varlist.push(global);
             global->delReference();
             path = "";
         }
-
-        inter.pushActivation(this);
     }
 
-    static void ActivationTopProgress(Message *msg, Inter &inter, Activation &activation){
+    static void ActivationTopProgress(aFuncore::Message *msg, aFuncore::Inter &inter, NormalActivation &activation){
         auto *t = dynamic_cast<TopMessage *>(msg);
         if (t)
             t->topProgress(inter, activation);
@@ -40,14 +36,14 @@ namespace aFuncore {
      * 注意: 不会自动从inter中弹出
      * 释放Varlist并且将DownMessage压入上层
      */
-    Activation::~Activation(){
-        if (inter.activation != nullptr)
-            down.joinMsg(inter.activation->down);
+    NormalActivation::~NormalActivation(){
+        if (inter.getActivation() != nullptr)
+            down.joinMsg(inter.getActivation()->getDownStream());
         else
             down.forEach(ActivationTopProgress, std::ref(inter), std::ref(*this));
     }
 
-    void Activation::endRun() {
+    void NormalActivation::endRun() {
 
     }
 
@@ -55,10 +51,10 @@ namespace aFuncore {
      * 运行代码
      * @param code
      */
-    void Activation::runCode(const aFuncode::Code::ByteCode *code){
+    void NormalActivation::runCode(const aFuncode::Code::ByteCode *code){
         auto code_type = code->getType();
         if (code_type == aFuncode::Code::ByteCode::code_start) {  // start 不处理 msg
-            auto *none = new Object("None", inter);
+            auto *none = new aFuncore::Object("None", inter);
             down.pushMessage("NORMAL", new NormalMessage(none));
             none->delReference();
         } else {
@@ -76,28 +72,34 @@ namespace aFuncore {
                         runCodeBlockC(code);
                         break;
                     default:
-                        errorLog(aFunCoreLogger, "Error block type.");
+                        errorLog(aFunRuntimeLogger, "Error block type.");
                         break;
                 }
         }
     }
 
-    void Activation::runCodeElement(const aFuncode::Code::ByteCode *code){
+    void NormalActivation::runCodeElement(const aFuncode::Code::ByteCode *code){
         std::string literaler_name;
         bool in_protect = false;
-        Object *obj = nullptr;
+        aFuncore::Object *obj = nullptr;
         if (inter.checkLiteral(code->getElement(), literaler_name, in_protect)) {
             if (in_protect)
-                obj = inter.getProtectVarSpace()->findObject(literaler_name);
-            else
+                inter.getEnvVarSpace().findObject(literaler_name, obj);
+            else {
                 obj = varlist.findObject(literaler_name);
+                if (obj == nullptr)
+                    inter.getEnvVarSpace().findObject(literaler_name, obj);
+            }
             auto literaler = dynamic_cast<Literaler *>(obj);
             if (literaler != nullptr)
                 literaler->getObject(code->getElement(), code->getPrefix(), inter, *this);
             else
-                down.pushMessage("ERROR", new ErrorMessage("TypeError", "Error type of literal.", this));
+                down.pushMessage("ERROR", new ErrorMessage("TypeError", "Error type of literal.", inter));
         } else {
             obj = varlist.findObject(code->getElement());
+            if (obj == nullptr)
+                inter.getEnvVarSpace().findObject(code->getElement(), obj);
+
             if (obj != nullptr) {
                 auto cbv = dynamic_cast<CallBackVar *>(obj);
                 if (cbv != nullptr && cbv->isCallBack(inter, *this))
@@ -106,45 +108,40 @@ namespace aFuncore {
                     down.pushMessage("NORMAL", new NormalMessage(obj));
             } else
                 down.pushMessage("ERROR",
-                        new ErrorMessage("NameError", std::string("Variable ") + code->getElement() + " not fount.",
-                                         this));
+                                 new ErrorMessage("NameError",
+                                                  std::string("Variable ") + code->getElement() + " not fount.", inter));
         }
     }
 
-    void Activation::runCodeBlockP(const aFuncode::Code::ByteCode *code){
-        new ExeActivation(code->getSon(), inter);
+    void NormalActivation::runCodeBlockP(const aFuncode::Code::ByteCode *code){
+        inter.pushActivation(new ExeActivation(code->getSon(), inter));
     }
 
-    void Activation::runCodeBlockC(const aFuncode::Code::ByteCode *code){
-        new FuncActivation(code, inter);
+    void NormalActivation::runCodeBlockC(const aFuncode::Code::ByteCode *code){
+        inter.pushActivation(new FuncActivation(code, inter));
     }
 
-    void Activation::runCodeBlockB(const aFuncode::Code::ByteCode *code){
-        new FuncActivation(code, inter);
+    void NormalActivation::runCodeBlockB(const aFuncode::Code::ByteCode *code){
+        inter.pushActivation(new FuncActivation(code, inter));
     }
 
-    Activation::ActivationStatus ExeActivation::getCode(const aFuncode::Code::ByteCode *&code){
-        code = next;
-        if (code == nullptr)
-            return as_end;
+    aFuncore::UpMessageStream &NormalActivation::getUpStream() {
+        return up;
+    }
 
-        if (!first) {
-            auto msg = down.getMessage<NormalMessage>("NORMAL");
-            if (msg == nullptr)
-                return as_end;
-            else
-                down.popMessage("NORMAL");
-            delete msg;
-        }
+    aFuncore::DownMessageStream &NormalActivation::getDownStream() {
+        return down;
+    }
 
-        first = false;
-        line = code->getFileLine();
-        path = code->getFilePath();
-        next = code->toNext();
-        return as_run;
+    aFuntool::FileLine NormalActivation::getFileLine() {
+        return line;
     }
 
-    Activation::VarList::~VarList() {
+    const aFuntool::FilePath &NormalActivation::getFilePath() {
+        return path;
+    }
+
+    NormalActivation::VarList::~VarList() {
         for (auto &t : varspace)
             t->delReference();
     }
@@ -154,7 +151,7 @@ namespace aFuncore {
      * @param name 变量名
      * @return
      */
-    Var *Activation::VarList::findVar(const std::string &name){
+    Var *NormalActivation::VarList::findVar(const std::string &name){
         Var *ret = nullptr;
         for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == nullptr; tmp++)
             ret = (*tmp)->findVar(name);
@@ -169,7 +166,7 @@ namespace aFuncore {
      * @param data 变量(Object)
      * @return
      */
-    bool Activation::VarList::defineVar(const std::string &name, Object *data){
+    bool NormalActivation::VarList::defineVar(const std::string &name, aFuncore::Object *data){
         VarSpace::VarOperationFlat ret = VarSpace::vof_fail;
         for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_fail; tmp++)
             ret = (*tmp)->defineVar(name, data);
@@ -184,7 +181,7 @@ namespace aFuncore {
      * @param data 变量(Var)
      * @return
      */
-    bool Activation::VarList::defineVar(const std::string &name, Var *data){
+    bool NormalActivation::VarList::defineVar(const std::string &name, Var *data){
         VarSpace::VarOperationFlat ret = VarSpace::vof_fail;
         for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_fail; tmp++)
             ret = (*tmp)->defineVar(name, data);
@@ -199,7 +196,7 @@ namespace aFuncore {
      * @param data 数据
      * @return
      */
-    bool Activation::VarList::setVar(const std::string &name, Object *data){
+    bool NormalActivation::VarList::setVar(const std::string &name, aFuncore::Object *data){
         VarSpace::VarOperationFlat ret = VarSpace::vof_not_var;
         for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_not_var; tmp++)
             ret = (*tmp)->setVar(name, data);
@@ -213,32 +210,53 @@ namespace aFuncore {
      * @param name
      * @return
      */
-    bool Activation::VarList::delVar(const std::string &name){
+    bool NormalActivation::VarList::delVar(const std::string &name){
         VarSpace::VarOperationFlat ret = VarSpace::vof_not_var;
         for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_not_var; tmp++)
             ret = (*tmp)->delVar(name);
         return ret == VarSpace::vof_success;
     }
 
-    void Activation::VarList::clear(){
+    void NormalActivation::VarList::clear(){
         for (auto &t: varspace)
             t->delReference();
         varspace.clear();
     }
 
-    void Activation::VarList::connect(VarList &new_varlist){
+    void NormalActivation::VarList::connect(VarList &new_varlist){
         for (auto &t: new_varlist.varspace) {
             t->addReference();
             this->varspace.push_back(t);
         }
     }
 
-    TopActivation::TopActivation(const aFuncode::Code &code, Inter &inter_) : ExeActivation(code, inter_), base{code} {
+    NormalActivation::ActivationStatus ExeActivation::getCode(const aFuncode::Code::ByteCode *&code){
+        code = next;
+        if (code == nullptr)
+            return as_end;
+
+        if (!first) {
+            auto msg = down.getMessage<NormalMessage>("NORMAL");
+            if (msg == nullptr)
+                return as_end;
+            else
+                down.popMessage("NORMAL");
+            delete msg;
+        }
+
+        first = false;
+        line = code->getFileLine();
+        path = code->getFilePath();
+        next = code->toNext();
+        return as_run;
+    }
+
+    TopActivation::TopActivation(const aFuncode::Code &code, aFuncore::Inter &inter_) : ExeActivation(code, inter_), base{code} {
 
     }
 
-    FuncActivation::FuncActivation(Function *func_, Inter &inter_)
-        : Activation(inter_), status{func_get_func}, on_tail{false}, call{nullptr}, func{func_}, acl_begin{}, acl_end{}  {
+    FuncActivation::FuncActivation(Function *func_, aFuncore::Inter &inter_)
+            : NormalActivation(inter_), status{func_get_func}, on_tail{false}, call{nullptr}, func{func_}, acl_begin{}, acl_end{}  {
         func->addReference();
     }
 
@@ -248,7 +266,7 @@ namespace aFuncore {
         delete call_func;
     }
 
-    Activation::ActivationStatus FuncActivation::getCode(const aFuncode::Code::ByteCode *&code) {
+    NormalActivation::ActivationStatus FuncActivation::getCode(const aFuncode::Code::ByteCode *&code) {
         if (on_tail)
             return as_end;
 
@@ -259,7 +277,8 @@ namespace aFuncore {
                     code = call->getSon();
                     if (code == nullptr) {
                         line = 0;
-                        down.pushMessage("ERROR", new ErrorMessage("SyntaxError", "Callback without code.", this));
+                        down.pushMessage("ERROR", new ErrorMessage("SyntaxError", "Callback without code.",
+                                                                   inter));
                         return as_end;
                     }
                     line = code->getFileLine();
@@ -275,7 +294,9 @@ namespace aFuncore {
                         if (var->getType() != aFuncode::Code::ByteCode::code_element || var->getPrefix() == quote ||
                             inter.checkLiteral(var->getElement()))
                             continue;
-                        Object *obj = varlist.findObject(var->getElement());
+                        aFuncore::Object *obj = varlist.findObject(var->getElement());
+                        if (obj == nullptr)
+                            inter.getEnvVarSpace().findObject(var->getElement(), obj);
                         if (obj == nullptr || !dynamic_cast<Function *>(obj) ||
                             !dynamic_cast<Function *>(obj)->isInfix())
                             continue;
@@ -288,15 +309,17 @@ namespace aFuncore {
                     }
                     if (status != func_get_func) {
                         line = 0;
-                        down.pushMessage("ERROR", new ErrorMessage("SyntaxError", "Callback without code.", this));
+                        down.pushMessage("ERROR", new ErrorMessage("SyntaxError", "Callback without code.",
+                                                                   inter));
                         return as_end;
                     }
                     break;
                 }
                 default:
-                    errorLog(aFunCoreLogger, "Error FuncActivation block type");
+                    errorLog(aFunRuntimeLogger, "Error FuncActivation block type");
                     line = 0;
-                    down.pushMessage("ERROR", new ErrorMessage("RuntimeError", "Error FuncActivation block type.", this));
+                    down.pushMessage("ERROR", new ErrorMessage("RuntimeError", "Error FuncActivation block type.",
+                                                               inter));
                     return as_end;
             }
         }
@@ -311,7 +334,7 @@ namespace aFuncore {
                 func = dynamic_cast<Function *>(msg->getObject());
                 delete msg;
                 if (func == nullptr) {
-                    down.pushMessage("ERROR", new ErrorMessage("TypeError", "Callback without function.", this));
+                    down.pushMessage("ERROR", new ErrorMessage("TypeError", "Callback without function.", inter));
                     return as_end;
                 }
                 func->addReference();
@@ -323,7 +346,7 @@ namespace aFuncore {
                 call_func = func->getCallFunction(call, inter);
                 acl = call_func->getArgCodeList(inter, *this, call);
             } catch (RuntimeError &e) {
-                down.pushMessage("ERROR", new ErrorMessage(e.getType(), e.getMessage(), this));
+                down.pushMessage("ERROR", new ErrorMessage(e.getType(), e.getMessage(), inter));
                 return as_end;
             }
             acl_begin = acl->begin();

+ 5 - 0
src/runtime/rt-logger.cpp

@@ -0,0 +1,5 @@
+#include "rt-logger.h"
+
+namespace aFunrt {
+    aFuntool::Logger *aFunRuntimeLogger = nullptr;
+}

+ 30 - 0
src/runtime/rt-message.cpp

@@ -0,0 +1,30 @@
+#include "rt-message.h"
+
+namespace aFunrt {
+    NormalMessage::NormalMessage(aFuncore::Object *obj_) : obj {obj_} {
+        obj->addReference();
+    }
+
+    NormalMessage::~NormalMessage(){
+        if (obj != nullptr) {
+            obj->delReference();
+            obj = nullptr;
+        }
+    }
+
+    void NormalMessage::topProgress(aFuncore::Inter &inter, aFuncore::Activation &){
+        inter.getOutMessageStream().pushMessage("NORMAL", new NormalMessage(std::move(*this)));
+    }
+
+    ErrorMessage::ErrorMessage(std::string error_type_, std::string error_info_, aFuncore::Inter &inter)
+            : inter{inter}, error_type{std::move(error_type_)}, error_info{std::move(error_info_)} {
+        for (const auto activation : inter.getStack()) {
+            if (activation->getFileLine() != 0)
+                trackback.push_front({activation->getFilePath(), activation->getFileLine()});
+        }
+    }
+
+    void ErrorMessage::topProgress(aFuncore::Inter &inter_, aFuncore::Activation &){
+        inter_.getOutMessageStream().pushMessage("ERROR", new ErrorMessage(std::move(*this)));
+    }
+}

+ 1 - 1
test/src/CMakeLists.txt

@@ -4,7 +4,7 @@ foreach(src IN LISTS src_list)
     cmake_path(GET src STEM file_name)
     add_executable(${file_name})
     target_sources(${file_name} PRIVATE ${src})
-    target_link_libraries(${file_name} PUBLIC tool-static code-static core-static parser-static it-static)  # 链接静态库 (导出所有符号)
+    target_link_libraries(${file_name} PUBLIC tool-static code-static core-static parser-static rt-static it-static)  # 链接静态库 (导出所有符号)
     set_target_properties(${file_name}
                           PROPERTIES OUTPUT_NAME "test-${file_name}")
     target_compile_definitions(${file_name} PRIVATE IN_CTEST)

+ 2 - 2
test/src/core-down-msg.cpp

@@ -1,6 +1,6 @@
-#include "msg.h"
+#include "core-message-stream.h"
 int main() {
-    auto *um = new aFuncore::DownMessage();
+    auto *um = new aFuncore::DownMessageStream();
     um->pushMessage("test-1", new aFuncore::Message());
     std::cout << um->getMessage<aFuncore::Message>("test-1") << std::endl;
     delete um;

+ 3 - 3
test/src/core-up-msg.cpp

@@ -1,11 +1,11 @@
-#include "msg.h"
+#include "core-message-stream.h"
 
 int main() {
-    auto *um = new aFuncore::UpMessage(nullptr);
+    auto *um = new aFuncore::UpMessageStream(nullptr);
     um->pushMessage("test-1", new aFuncore::Message());
     std::cout << um->getMessage<aFuncore::Message>("test-1") << std::endl;
 
-    auto *um2 = new aFuncore::UpMessage(um);
+    auto *um2 = new aFuncore::UpMessageStream(um);
     um2->pushMessage("test-2", new aFuncore::Message());
     std::cout << um2->getMessage<aFuncore::Message>("test-1") << std::endl;
     std::cout << um2->getMessage<aFuncore::Message>("test-2") << std::endl;

+ 72 - 61
test/src/run-code.cpp

@@ -1,8 +1,54 @@
 #include "aFunit.h"
+#include "aFunrt.h"
 
-void progressInterEvent(aFuncore::Inter &inter);
+void printMessage(const std::string &type, aFuncore::Message *msg, aFuncore::Inter &) {
+    if (type == "NORMAL") {
+        auto *msg_ = dynamic_cast<aFunrt::NormalMessage *>(msg);
+        if (msg_ == nullptr)
+            return;
+        aFuntool::printf_stdout(0, "NORMAL: %p\n", msg_->getObject());
+    } else if (type == "ERROR") {
+        auto *msg_ = dynamic_cast<aFunrt::ErrorMessage *>(msg);
+        if (msg_ == nullptr)
+            return;
+        aFuntool::printf_stdout(0, "Error TrackBack\n");
+        for (auto &begin: msg_->getTrackBack())
+            aFuntool::printf_stdout(0, "  File \"%s\", line %d\n", begin.path.c_str(), begin.line);
+        aFuntool::printf_stdout(0, "%s: %s\n", msg_->getErrorType().c_str(), msg_->getErrorInfo().c_str());
+    }
+}
+
+void progressInterEvent(aFuncore::Inter &inter) {
+    std::string type;
+    for (auto msg = inter.getOutMessageStream().popFrontMessage(type); msg != nullptr; msg = inter.getOutMessageStream().popFrontMessage(type)) {
+        printMessage(type, msg, inter);
+        delete msg;
+    }
+}
+
+bool runCode(aFuncore::Inter &inter, aFuncode::Code &code) {
+    auto activation = new aFunrt::TopActivation(code, inter);
+    inter.pushActivation(activation);
+    return inter.runCode();
+}
+
+bool runCode(aFuncore::Inter &inter, aFuncore::Object *func) {
+    auto func_obj = dynamic_cast<aFunrt::Function *>(func);
+    auto activation = new aFunrt::FuncActivation(func_obj, inter);
+    inter.pushActivation(activation);
+    return inter.runCode();
+}
 
-class Func1 : public aFuncore::Function {
+void thread_test(aFuncore::Inter &son) {
+    auto code = aFuncode::Code("run-code.aun");
+    code.getByteCode()->connect(new aFuncode::Code::ByteCode(code, aFuncode::Code::ByteCode::block_p,
+                                                             new aFuncode::Code::ByteCode(code, "test-var", 1), 0));
+    runCode(son, code);
+    progressInterEvent(son);
+    aFuntool::fputs_stdout("\n");
+}
+
+class Func1 : public aFunrt::Function {
     class CallFunc1 : public CallFunction {
         aFuncode::Code &func_code;
         const aFuncode::Code::ByteCode *call_code;
@@ -27,7 +73,7 @@ class Func1 : public aFuncore::Function {
                 aFuntool::printf_stdout(0, "runFunction No AegCodeList\n");
             else
                 aFuntool::printf_stdout(0, "runFunction : %p\n", acl->begin()->getObject());
-            new aFuncore::ExeActivation(func_code, inter);
+            inter.pushActivation(new aFunrt::ExeActivation(func_code, inter));
         }
 
         ~CallFunc1() override {
@@ -56,12 +102,12 @@ public:
         auto code = aFuncode::Code("run-code.aun");
         code.getByteCode()->connect(new aFuncode::Code::ByteCode(code, aFuncode::Code::ByteCode::block_p,
                                                                  new aFuncode::Code::ByteCode(code, "test-var", 1), 0));
-        gc_inter.runCode(code);
+        runCode(gc_inter, code);
         progressInterEvent(gc_inter);
     };
 };
 
-class Literaler1 : public aFuncore::Literaler {
+class Literaler1 : public aFunrt::Literaler {
     aFuncode::Code func_code;
 public:
     explicit Literaler1(aFuncore::Inter &inter_) : Object("Data", inter_), func_code{"run-code.aun"} {
@@ -74,11 +120,11 @@ public:
 
     void getObject(const std::string &literal, char prefix, aFuncore::Inter &inter, aFuncore::Activation &) override {
         aFuntool::cout << "Literaler1: " << literal  << "prefix: " << (prefix == aFuntool::NUL ? '-' : prefix) << "\n";
-        new aFuncore::ExeActivation(func_code, inter);
+        inter.pushActivation(new aFunrt::ExeActivation(func_code, inter));
     }
 };
 
-class CBV1 : public aFuncore::CallBackVar {
+class CBV1 : public aFunrt::CallBackVar {
     aFuncode::Code func_code;
 public:
     explicit CBV1(aFuncore::Inter &inter_) : Object("CBV1", inter_), func_code{"run-code.aun"} {
@@ -91,11 +137,11 @@ public:
 
     void callBack(aFuncore::Inter &inter, aFuncore::Activation &) override {
         aFuntool::cout << "CallBackVar callback\n";
-        new aFuncore::ExeActivation(func_code, inter);
+        inter.pushActivation(new aFunrt::ExeActivation(func_code, inter));
     }
 };
 
-class ImportFunction : public aFuncore::Function {
+class ImportFunction : public aFunrt::Function {
     class CallFunc : public CallFunction {
         const aFuncode::Code::ByteCode *call_code;
         aFuncore::Inter &inter;
@@ -107,7 +153,7 @@ class ImportFunction : public aFuncore::Function {
                 code_->getSon() == nullptr ||
                 code_->getSon()->toNext() == nullptr ||
                 code_->getSon()->toNext()->getType() != aFuncode::Code::ByteCode::code_element)
-                throw aFuncore::ArgumentError();
+                throw aFunrt::ArgumentError();
             acl = new std::list<ArgCodeList>;
             import = code_->getSon()->toNext()->getElement();
         }
@@ -121,7 +167,7 @@ class ImportFunction : public aFuncore::Function {
         void runFunction() override {
             auto &stream = inter.getActivation()->getDownStream();
             auto none = new Object("None", inter);
-            stream.pushMessage("NORMAL", new aFuncore::NormalMessage(none));
+            stream.pushMessage("NORMAL", new aFunrt::NormalMessage(none));
             none->delReference();
             aFuntool::cout << "Import " << import << " : " << call_code << "\n";
         }
@@ -147,67 +193,32 @@ public:
     }
 };
 
-void printMessage(const std::string &type, aFuncore::Message *msg, aFuncore::Inter &inter) {
-    if (type == "NORMAL") {
-        auto *msg_ = dynamic_cast<aFuncore::NormalMessage *>(msg);
-        if (msg_ == nullptr)
-            return;
-        aFuntool::printf_stdout(0, "NORMAL: %p\n", msg_->getObject());
-    } else if (type == "ERROR") {
-        auto *msg_ = dynamic_cast<aFuncore::ErrorMessage *>(msg);
-        if (msg_ == nullptr)
-            return;
-        int32_t error_std = 0;
-        aFuntool::printf_stdout(0, "Error TrackBack\n");
-        for (auto &begin: msg_->getTrackBack())
-            aFuntool::printf_stdout(0, "  File \"%s\", line %d\n", begin.path.c_str(), begin.line);
-        aFuntool::printf_stdout(0, "%s: %s\n", msg_->getErrorType().c_str(), msg_->getErrorInfo().c_str());
-    }
-}
-
-void progressInterEvent(aFuncore::Inter &inter) {
-    std::string type;
-    for (auto msg = inter.getOutMessageStream().popFrontMessage(type); msg != nullptr; msg = inter.getOutMessageStream().popFrontMessage(type)) {
-        printMessage(type, msg, inter);
-        delete msg;
-    }
-}
-
-void thread_test(aFuncore::Inter &son) {
-    auto code = aFuncode::Code("run-code.aun");
-    code.getByteCode()->connect(new aFuncode::Code::ByteCode(code, aFuncode::Code::ByteCode::block_p,
-                                                             new aFuncode::Code::ByteCode(code, "test-var", 1), 0));
-    son.runCode(code);
-    progressInterEvent(son);
-    aFuntool::fputs_stdout("\n");
-}
-
 int Main() {
     aFuncore::Environment env{};
     aFuncore::Inter inter {env};
 
     auto obj = new aFuncore::Object("Object", inter);
-    inter.getProtectVarSpace()->defineVar("test-var", obj);
+    inter.getEnvVarSpace().setObject("test-var", obj);
     aFuntool::cout << "obj: " << obj << "\n";
     obj->delReference();
 
     auto func = new Func1(inter);
-    inter.getProtectVarSpace()->defineVar("test-func", func);
+    inter.getEnvVarSpace().setObject("test-func", func);
     aFuntool::cout << "func: " << func << "\n";
     func->delReference();
 
     auto literaler = new Literaler1(inter);
-    inter.getProtectVarSpace()->defineVar("test-literaler", literaler);
+    inter.getEnvVarSpace().setObject("test-literaler", literaler);
     aFuntool::cout << "literaler: " << literaler << "\n";
     literaler->delReference();
 
     auto cbv = new CBV1(inter);
-    inter.getProtectVarSpace()->defineVar("test-cbv", cbv);
+    inter.getEnvVarSpace().setObject("test-cbv", cbv);
     aFuntool::cout << "cbv: " << cbv << "\n";
     cbv->delReference();
 
     auto import = new ImportFunction(inter);
-    inter.getProtectVarSpace()->defineVar("import", import);
+    inter.getEnvVarSpace().setObject("import", import);
     aFuntool::cout << "import: " << import << "\n";
     import->delReference();
 
@@ -228,7 +239,7 @@ int Main() {
         auto code = aFuncode::Code("run-code.aun");
         code.getByteCode()->connect(new aFuncode::Code::ByteCode(code, aFuncode::Code::ByteCode::block_p,
                                                                  new aFuncode::Code::ByteCode(code, "test-var", 1), 0));
-        inter.runCode(code);
+        runCode(inter, code);
         progressInterEvent(inter);
         aFuntool::fputs_stdout("\n");
     }
@@ -242,7 +253,7 @@ int Main() {
 
         code.getByteCode()->connect(new aFuncode::Code::ByteCode(code, aFuncode::Code::ByteCode::block_c, arg, 0));
 
-        inter.runCode(code);
+        runCode(inter, code);
         progressInterEvent(inter);
         aFuntool::fputs_stdout("\n");
     }
@@ -255,7 +266,7 @@ int Main() {
         arg->connect(new aFuncode::Code::ByteCode(code, "test-func", 1));
 
         code.getByteCode()->connect(new aFuncode::Code::ByteCode(code, aFuncode::Code::ByteCode::block_b, arg, 0));
-        inter.runCode(code);
+        runCode(inter, code);
         progressInterEvent(inter);
         aFuntool::fputs_stdout("\n");
     }
@@ -265,7 +276,7 @@ int Main() {
         inter.pushLiteral("data[0-9]", "test-literaler", false);
         auto code = aFuncode::Code("run-code.aun");
         code.getByteCode()->connect(new aFuncode::Code::ByteCode(code, "data4", 1));
-        inter.runCode(code);
+        runCode(inter, code);
         progressInterEvent(inter);
         aFuntool::fputs_stdout("\n");
     }
@@ -274,14 +285,14 @@ int Main() {
         aFuntool::fputs_stdout("Test-5: test-cbv\n");
         auto code = aFuncode::Code("run-code.aun");
         code.getByteCode()->connect(new aFuncode::Code::ByteCode(code, "test-cbv", 1));
-        inter.runCode(code);
+        runCode(inter, code);
         progressInterEvent(inter);
         aFuntool::fputs_stdout("\n");
     }
 
     {
         aFuntool::fputs_stdout("Test-6: run-function\n");
-        inter.runCode(func);
+        runCode(inter, func);
         progressInterEvent(inter);
         aFuntool::fputs_stdout("\n");
     }
@@ -295,7 +306,7 @@ int Main() {
 
         code.getByteCode()->connect(new aFuncode::Code::ByteCode(code, aFuncode::Code::ByteCode::block_c, arg, 0));
 
-        inter.runCode(code);
+        runCode(inter, code);
         progressInterEvent(inter);
         aFuntool::fputs_stdout("\n");
     }
@@ -310,7 +321,7 @@ int Main() {
             auto code = aFuncode::Code("run-code.aun");
             code.getByteCode()->connect(new aFuncode::Code::ByteCode(code, aFuncode::Code::ByteCode::block_p,
                                                                      new aFuncode::Code::ByteCode(code, "test-var", 1), 0));
-            inter.runCode(code);
+            runCode(inter, code);
             progressInterEvent(inter);
             aFuntool::fputs_stdout("\n");
         }
@@ -323,7 +334,7 @@ int Main() {
         aFuntool::fputs_stdout("Test-a: error not var\n");
         auto code = aFuncode::Code("run-code.aun");
         code.getByteCode()->connect(new aFuncode::Code::ByteCode(code, "test-not-var", 1));
-        inter.runCode(code);
+        runCode(inter, code);
         progressInterEvent(inter);
         aFuntool::fputs_stdout("\n");
     }
@@ -334,7 +345,7 @@ int Main() {
         auto code = aFuncode::Code("run-code.aun");
         code.getByteCode()->connect(new aFuncode::Code::ByteCode(code, aFuncode::Code::ByteCode::block_p,
                                                                  new aFuncode::Code::ByteCode(code, "test-var", 1), 0));
-        inter.runCode(code);
+        runCode(inter, code);
         progressInterEvent(inter);
     }