inter.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #ifndef AFUN_INTER_H
  2. #define AFUN_INTER_H
  3. #include <list>
  4. #include <mutex>
  5. #include "aFuntool.h"
  6. #include "aFunCoreExport.h"
  7. #include "code.h"
  8. #include "env-var.h"
  9. #include "msg.h"
  10. namespace aFuncore {
  11. class Activation;
  12. class Object;
  13. class Var;
  14. class ProtectVarSpace;
  15. class VarSpace;
  16. class Object;
  17. class Inter;
  18. class AFUN_CORE_EXPORT Environment {
  19. friend class Object;
  20. friend class Inter;
  21. public:
  22. explicit Environment(int argc = 0, char **argv = nullptr);
  23. ~Environment() noexcept(false);
  24. Environment(Environment &) = delete;
  25. Environment &operator=(Environment &) = delete;
  26. AFUN_INLINE size_t operator++();
  27. AFUN_INLINE size_t operator--();
  28. AFUN_INLINE size_t operator++(int);
  29. AFUN_INLINE size_t operator--(int);
  30. private:
  31. std::mutex lock;
  32. size_t reference; // 引用计数
  33. bool destruct;
  34. std::list<Object *> gc;
  35. Inter &gc_inter; /* 需要在lock和reference后初始化 */
  36. std::thread gc_thread;
  37. void gcThread();
  38. protected: // 位于 mutex 之下
  39. ProtectVarSpace *const protect; // 保护变量空间
  40. EnvVarSpace envvar;
  41. };
  42. class AFUN_CORE_EXPORT Inter {
  43. friend class Activation;
  44. struct LiteralRegex;
  45. public:
  46. typedef enum InterStatus {
  47. inter_init = 0, // 执行初始化程序
  48. inter_normal = 1, // 正常执行
  49. inter_stop = 2, // 当前运算退出
  50. inter_exit = 3, // 解释器退出
  51. } InterStatus;
  52. typedef enum Prefix {
  53. prefix_quote = 0, // 变量引用
  54. prefix_exec_first = 1,
  55. } Prefix;
  56. AFUN_STATIC const int PREFIX_COUNT = 2;
  57. AFUN_STATIC constexpr const char *E_PREFIX = "$`'"; /* NOLINT element前缀 */
  58. AFUN_STATIC constexpr const char *B_PREFIX = "$`'%^&<?>"; /* NOLINT block前缀 */
  59. AFUN_STATIC constexpr const char *ALL_PREFIX = "$`'%^&<?>"; /* NOLINT block前缀 */
  60. explicit Inter(Environment &env_);
  61. Inter(const Inter &base_inter);
  62. ~Inter();
  63. Inter &operator=(const Inter &) = delete;
  64. void enable();
  65. [[nodiscard]] AFUN_INLINE InterStatus getStatus() const;
  66. [[nodiscard]] AFUN_INLINE bool isInterStop() const;
  67. [[nodiscard]] AFUN_INLINE bool isInterExit() const;
  68. [[nodiscard]] AFUN_INLINE Environment &getEnvironment();
  69. [[nodiscard]] AFUN_INLINE ProtectVarSpace *getProtectVarSpace() const;
  70. [[nodiscard]] AFUN_INLINE const std::list<Activation *> &getStack() const;
  71. [[nodiscard]] AFUN_INLINE Activation *getActivation() const;
  72. [[nodiscard]] bool checkLiteral(const std::string &element) const;
  73. [[nodiscard]] bool checkLiteral(const std::string &element, std::string &literaler, bool &in_protect) const;
  74. [[nodiscard]] AFUN_INLINE EnvVarSpace &getEnvVarSpace();
  75. [[nodiscard]] AFUN_INLINE InterOutMessage &getOutMessageStream();
  76. [[nodiscard]] AFUN_INLINE InterInMessage &getInMessageStream();
  77. bool pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect);
  78. bool runCode();
  79. bool runCode(const Code &code);
  80. bool runCode(Object *obj);
  81. AFUN_INLINE InterStatus setInterStop();
  82. AFUN_INLINE InterStatus setInterExit();
  83. private:
  84. InterStatus status;
  85. Environment &env;
  86. std::list<Activation *> stack;
  87. Activation *activation; // 活动记录
  88. InterOutMessage out;
  89. InterInMessage in;
  90. std::list<LiteralRegex> literal;
  91. AFUN_INLINE void pushActivation(Activation *new_activation);
  92. AFUN_INLINE Activation *popActivation();
  93. };
  94. struct Inter::LiteralRegex {
  95. aFuntool::Regex rg;
  96. std::string pattern; // 派生 LiteralRegex 时使用
  97. std::string literaler; // 调用的函数
  98. bool in_protect; // 是否在protect空间
  99. };
  100. }
  101. #include "inter.inline.h"
  102. #endif //AFUN_INTER_H