inter.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Prefix {
  52. prefix_quote = 0, // 变量引用
  53. prefix_exec_first = 1,
  54. } Prefix;
  55. static const int PREFIX_COUNT = 2;
  56. constexpr static const char *E_PREFIX = "$`'"; /* NOLINT element前缀 */
  57. constexpr static const char *B_PREFIX = "$`'%^&<?>"; /* NOLINT block前缀 */
  58. constexpr static const char *ALL_PREFIX = "$`'%^&<?>"; /* NOLINT block前缀 */
  59. explicit Inter(Environment &env_, int argc = 0, char **argv = nullptr);
  60. Inter(const Inter &base_inter);
  61. ~Inter();
  62. Inter &operator=(const Inter &) = delete;
  63. void enable();
  64. [[nodiscard]] inline InterStatus getStatus() const;
  65. [[nodiscard]] inline bool isInterStop() const;
  66. [[nodiscard]] inline bool isInterExit() const;
  67. [[nodiscard]] inline Environment &getEnvironment();
  68. [[nodiscard]] inline ProtectVarSpace *getProtectVarSpace() const;
  69. [[nodiscard]] inline VarSpace *getGlobalVarSpace() const;
  70. [[nodiscard]] inline VarList *getGlobalVarlist() const;
  71. [[nodiscard]] 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]] inline EnvVarSpace &getEnvVarSpace();
  75. [[nodiscard]] inline InterMessage &getOutMessageStream();
  76. [[nodiscard]] inline InterMessage &getInMessageStream();
  77. bool pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect);
  78. bool runCode();
  79. bool runCode(Code &code);
  80. inline InterStatus setInterStop();
  81. inline InterStatus setInterExit();
  82. private:
  83. InterStatus status;
  84. Environment &env;
  85. Activation *activation; // 活动记录
  86. InterMessage out;
  87. InterMessage in;
  88. std::list<LiteralRegex> literal;
  89. inline void pushActivation(Activation *new_activation);
  90. };
  91. struct Inter::LiteralRegex {
  92. aFuntool::Regex rg;
  93. std::string pattern; // 派生 LiteralRegex 时使用
  94. std::string literaler; // 调用的函数
  95. bool in_protect; // 是否在protect空间
  96. };
  97. }
  98. #include "inter.inline.h"
  99. #endif //AFUN_INTER_H