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