inter.h 4.2 KB

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