inter.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. }
  26. #include "env-var.hpp"
  27. #include "code.hpp"
  28. #include "var.hpp"
  29. #include "value.hpp"
  30. #include "activation.hpp"
  31. namespace aFuncore {
  32. class Inter {
  33. /* 解释器原信息记录 */
  34. friend class Object;
  35. friend class Var;
  36. friend class VarSpace;
  37. pthread_mutex_t status_lock; // status 可能被外部使用, 因此需要用锁保护
  38. InterStatus status;
  39. /* GC 记录器 */
  40. struct GcRecord {
  41. Object *obj;
  42. Var *var;
  43. VarSpace *varspace;
  44. } *gc;
  45. /* 运行相关 */
  46. ProtectVarSpace *protect; // 保护变量空间
  47. VarSpace *global; // 全局变量空间
  48. VarList *global_varlist; // global + protect
  49. Activation *activation; // 活动记录
  50. struct LiteralRegex {
  51. Regex *rg;
  52. std::string pattern; // 派生 LiteralRegex 时使用
  53. char *func; // 调用的函数
  54. bool in_protect; // 是否在protect空间
  55. };
  56. std::list<LiteralRegex *> *literal;
  57. /* 配置信息记录器 */
  58. EnvVarSpace *envvar;
  59. EnvVarSpace::EnvVar *gc_runtime;
  60. EnvVarSpace::EnvVar *prefix;
  61. EnvVarSpace::EnvVar *exit_code; // 退出代码
  62. EnvVarSpace::EnvVar *argc; // 参数个数
  63. EnvVarSpace::EnvVar *error_std; // Error输出的位置 0-stdout 其他-stderr
  64. /* 线程信息 */
  65. bool is_derive; // 是否派生
  66. Inter *base; // 主线程
  67. Object *result; // 线程执行的结果
  68. std::list<Inter *> *son_inter; // 派生线程链表, 由主线程负责管理
  69. pthread_t monitor; // 守护线程
  70. ExitFlat exit_flat; // 外部设置退出
  71. ExitMode exit_mode; // 退出模式
  72. pthread_mutex_t monitor_lock;
  73. pthread_cond_t monitor_cond;
  74. public:
  75. explicit Inter(int argc=0, char **argv=nullptr, ExitMode em=em_activity);
  76. ~Inter();
  77. void enable();
  78. Var *findGlobalVar(const std::string &name);
  79. VarOperationFlat defineGlobalVar(const std::string &name, Object *data);
  80. VarOperationFlat defineGlobalVar(const std::string &name, Var *data);
  81. VarOperationFlat setGlobalVar(const std::string &name, Object *data);
  82. VarOperationFlat delGlobalVar(const std::string &name);
  83. Object *findGlobalObject(const std::string &name);
  84. bool runCode(Code *code);
  85. };
  86. }
  87. #endif //AFUN_INTER_HPP