inter.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef AFUN_INTER_HPP
  2. #define AFUN_INTER_HPP
  3. #include <list>
  4. #include "tool.hpp"
  5. #include "aFunCoreExport.h"
  6. namespace aFuncore {
  7. class Inter;
  8. enum InterStatus {
  9. inter_creat = 0,
  10. inter_init = 1, // 执行初始化程序
  11. inter_normal = 2, // 正常执行
  12. inter_stop = 3, // 当前运算退出
  13. inter_exit = 4, // 解释器退出
  14. };
  15. typedef enum InterStatus InterStatus;
  16. typedef enum ExitFlat {
  17. ef_activity = 0, // 主动退出
  18. ef_passive = 1, // 被动退出
  19. ef_none = 2,
  20. } ExitFlat;
  21. typedef enum ExitMode {
  22. em_activity = ef_activity, // 主动退出
  23. em_passive = ef_passive, // 被动退出
  24. } ExitMode;
  25. static const int PrefixCount = 2;
  26. typedef enum Prefix {
  27. prefix_quote = 0, // 变量引用
  28. prefix_exec_first = 1,
  29. } Prefix;
  30. static const std::string E_PREFIX = "$`'"; /* NOLINT element前缀 */
  31. static const std::string B_PREFIX = "$`'%^&<?>"; /* NOLINT block前缀 */
  32. }
  33. #include "env-var.hpp"
  34. #include "code.hpp"
  35. #include "var.hpp"
  36. #include "value.hpp"
  37. #include "activation.hpp"
  38. namespace aFuncore {
  39. class Inter {
  40. friend class Object;
  41. friend class Var;
  42. friend class VarSpace;
  43. /* 解释器原信息记录 */
  44. pthread_mutex_t status_lock; // status 可能被外部使用, 因此需要用锁保护
  45. InterStatus status;
  46. /* GC 记录器 */
  47. struct GcRecord {
  48. Object *obj;
  49. Var *var;
  50. VarSpace *varspace;
  51. } *gc;
  52. [[nodiscard]] struct GcRecord *getGcRecord() const {return gc;}
  53. /* 运行相关 */
  54. ProtectVarSpace *protect; // 保护变量空间
  55. VarSpace *global; // 全局变量空间
  56. VarList *global_varlist; // global + protect
  57. Activation *activation; // 活动记录
  58. struct LiteralRegex {
  59. Regex *rg;
  60. std::string pattern; // 派生 LiteralRegex 时使用
  61. char *func; // 调用的函数
  62. bool in_protect; // 是否在protect空间
  63. };
  64. std::list<LiteralRegex> *literal;
  65. /* 配置信息记录器 */
  66. EnvVarSpace *envvar;
  67. /* 线程信息 */
  68. public:
  69. const bool is_derive; // 是否派生
  70. Inter *const base; // 主线程
  71. private:
  72. Object *result; // 线程执行的结果
  73. std::list<Inter *> *son_inter; // 派生线程链表, 由主线程负责管理
  74. pthread_t monitor; // 守护线程
  75. ExitFlat exit_flat; // 外部设置退出
  76. ExitMode exit_mode; // 退出模式
  77. pthread_mutex_t monitor_lock;
  78. pthread_cond_t monitor_cond;
  79. public:
  80. explicit Inter(int argc=0, char **argv=nullptr, ExitMode em=em_activity);
  81. ~Inter();
  82. void enable();
  83. [[nodiscard]] InterStatus getStatus() const {return status;}
  84. [[nodiscard]] bool isExit() const {return (status == inter_exit || status == inter_stop);}
  85. [[nodiscard]] VarList *getGlobalVarlist() const {return global_varlist;}
  86. [[nodiscard]] Activation *getActivation() const {return activation;}
  87. [[nodiscard]] bool checkLiteral(const std::string &element, std::string &func, bool &in_protect) const;
  88. [[nodiscard]] EnvVarSpace *getEnvVarSpace() const {return envvar;}
  89. [[nodiscard]] int getGcRuntime() const {return gc_runtime->num;}
  90. [[nodiscard]] char getPrefx(enum Prefix pre) const {return prefix->str[pre];}
  91. [[nodiscard]] int getExitCode() const {return exit_code->num;}
  92. [[nodiscard]] int getArgc() const {return argc->num;}
  93. [[nodiscard]] int getErrorStd() const {return error_std->num == 1;}
  94. void pushActivation(Activation *new_activation) {activation = new_activation;}
  95. bool runCode();
  96. bool runCode(Code *code);
  97. };
  98. }
  99. #endif //AFUN_INTER_HPP