inter.h 3.4 KB

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