inter.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef AFUN_INTER_H
  2. #define AFUN_INTER_H
  3. #include <list>
  4. #include "aFuntool.h"
  5. #include "aFunCoreExport.h"
  6. #include "core.h"
  7. namespace aFuncore {
  8. AFUN_CORE_EXPORT class Inter {
  9. friend class Object;
  10. friend class Var;
  11. friend class VarSpace;
  12. friend class Activation;
  13. /* 解释器原信息记录 */
  14. pthread_mutex_t status_lock; // status 可能被外部使用, 因此需要用锁保护
  15. InterStatus status;
  16. /* GC 记录器 */
  17. struct GcRecord {
  18. Object *obj;
  19. Var *var;
  20. VarSpace *varspace;
  21. } *gc;
  22. [[nodiscard]] struct GcRecord *getGcRecord() const {return gc;}
  23. /* 运行相关 */
  24. ProtectVarSpace *protect; // 保护变量空间
  25. VarSpace *global; // 全局变量空间
  26. VarList *global_varlist; // global + protect
  27. Activation *activation; // 活动记录
  28. void pushActivation(Activation *new_activation) {activation = new_activation;}
  29. struct LiteralRegex {
  30. Regex *rg;
  31. std::string pattern; // 派生 LiteralRegex 时使用
  32. std::string literaler; // 调用的函数
  33. bool in_protect; // 是否在protect空间
  34. };
  35. std::list<LiteralRegex> *literal;
  36. /* 配置信息记录器 */
  37. EnvVarSpace *envvar;
  38. /* 线程信息 */
  39. public:
  40. const bool is_derive; // 是否派生
  41. Inter *const base; // 主线程
  42. private:
  43. Object *result; // 线程执行的结果
  44. std::list<Inter *> *son_inter; // 派生线程链表, 由主线程负责管理
  45. pthread_t monitor; // 守护线程
  46. ExitFlat exit_flat; // 外部设置退出
  47. ExitMode exit_mode; // 退出模式
  48. pthread_mutex_t monitor_lock;
  49. pthread_cond_t monitor_cond;
  50. public:
  51. explicit Inter(int argc=0, char **argv=nullptr, ExitMode em=em_activity);
  52. ~Inter();
  53. Inter(const Inter &)=delete;
  54. Inter &operator=(const Inter &)=delete;
  55. void enable();
  56. [[nodiscard]] InterStatus getStatus() const {return status;}
  57. [[nodiscard]] bool isExit() const {return (status == inter_exit || status == inter_stop);}
  58. [[nodiscard]] ProtectVarSpace *getProtectVarSpace() const {return protect;}
  59. [[nodiscard]] VarSpace *getGlobalVarSpace() const {return global;}
  60. [[nodiscard]] VarList *getGlobalVarlist() const {return global_varlist;}
  61. [[nodiscard]] Activation *getActivation() const {return activation;}
  62. [[nodiscard]] bool checkLiteral(const std::string &element) const;
  63. [[nodiscard]] bool checkLiteral(const std::string &element, std::string &literaler, bool &in_protect) const;
  64. [[nodiscard]] EnvVarSpace *getEnvVarSpace() const {return envvar;}
  65. bool pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect);
  66. bool runCode();
  67. bool runCode(Code *code);
  68. };
  69. }
  70. #endif //AFUN_INTER_H