inter.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef AFUN_INTER_H
  2. #define AFUN_INTER_H
  3. #include <list>
  4. #include "aFuntool.h"
  5. #include "aFunCoreExport.h"
  6. #include "code.h"
  7. #include "env-var.h"
  8. #include "msg.h"
  9. namespace aFuncore {
  10. class Activation;
  11. class Var;
  12. class ProtectVarSpace;
  13. class VarSpace;
  14. class VarList;
  15. class Object;
  16. class AFUN_CORE_EXPORT Environment {
  17. friend class Object;
  18. friend class Var;
  19. friend class VarSpace;
  20. friend class Inter;
  21. public:
  22. Environment();
  23. ~Environment() noexcept(false);
  24. Environment(Environment &) = delete;
  25. Environment &operator=(Environment &) = delete;
  26. inline size_t operator++();
  27. inline size_t operator--();
  28. inline size_t operator++(int);
  29. inline size_t operator--(int);
  30. private:
  31. Object *obj;
  32. Var *var;
  33. VarSpace *varspace;
  34. ProtectVarSpace *protect; // 保护变量空间
  35. VarSpace *global; // 全局变量空间
  36. VarList *global_varlist; // global + protect
  37. EnvVarSpace envvar;
  38. size_t reference; // 引用计数
  39. };
  40. class AFUN_CORE_EXPORT Inter {
  41. friend class Activation;
  42. struct LiteralRegex;
  43. public:
  44. typedef enum InterStatus {
  45. inter_creat = 0,
  46. inter_init = 1, // 执行初始化程序
  47. inter_normal = 2, // 正常执行
  48. inter_stop = 3, // 当前运算退出
  49. inter_exit = 4, // 解释器退出
  50. } InterStatus;
  51. typedef enum ExitFlat {
  52. ef_activity = 0, // 主动退出
  53. ef_passive = 1, // 被动退出
  54. ef_none = 2,
  55. } ExitFlat;
  56. typedef enum ExitMode {
  57. em_activity = ef_activity, // 主动退出
  58. em_passive = ef_passive, // 被动退出
  59. } ExitMode;
  60. typedef enum Prefix {
  61. prefix_quote = 0, // 变量引用
  62. prefix_exec_first = 1,
  63. } Prefix;
  64. static const int PREFIX_COUNT = 2;
  65. constexpr static const char *E_PREFIX = "$`'"; /* NOLINT element前缀 */
  66. constexpr static const char *B_PREFIX = "$`'%^&<?>"; /* NOLINT block前缀 */
  67. explicit Inter(Environment &env_, int argc = 0, char **argv = nullptr, ExitMode em = em_activity);
  68. Inter(const Inter &base_inter, ExitMode em = em_activity);
  69. ~Inter();
  70. Inter &operator=(const Inter &) = delete;
  71. void enable();
  72. [[nodiscard]] inline InterStatus getStatus() const;
  73. [[nodiscard]] inline bool isExit() const;
  74. [[nodiscard]] inline Environment &getEnvironment();
  75. [[nodiscard]] inline ProtectVarSpace *getProtectVarSpace() const;
  76. [[nodiscard]] inline VarSpace *getGlobalVarSpace() const;
  77. [[nodiscard]] inline VarList *getGlobalVarlist() 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. bool pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect);
  83. bool runCode();
  84. bool runCode(Code &code);
  85. private:
  86. InterStatus status;
  87. Environment &env;
  88. Activation *activation; // 活动记录
  89. InterMessage out;
  90. InterMessage in;
  91. std::list<LiteralRegex> literal;
  92. Object *result; // 线程执行的结果
  93. ExitFlat exit_flat; // 外部设置退出
  94. ExitMode exit_mode; // 退出模式
  95. inline void pushActivation(Activation *new_activation);
  96. };
  97. struct Inter::LiteralRegex {
  98. aFuntool::Regex rg;
  99. std::string pattern; // 派生 LiteralRegex 时使用
  100. std::string literaler; // 调用的函数
  101. bool in_protect; // 是否在protect空间
  102. };
  103. }
  104. #include "inter.inline.h"
  105. #endif //AFUN_INTER_H