__env.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef AFUN__ENV_H
  2. #define AFUN__ENV_H
  3. #include "macro.h"
  4. #include "tool.h"
  5. typedef struct af_Core af_Core;
  6. typedef struct af_Activity af_Activity;
  7. typedef struct af_EnvVarSpace af_EnvVarSpace;
  8. typedef struct af_EnvVar af_EnvVar;
  9. typedef struct af_TopMsgProcess af_TopMsgProcess;
  10. #include "env.h"
  11. #include "__object.h"
  12. #include "__var.h"
  13. #include "__code.h"
  14. #include "__gc.h"
  15. #include "__func.h"
  16. #define ENV_VAR_HASH_SIZE (8)
  17. typedef uint16_t ActivityCount;
  18. typedef void TopMsgProcessFunc(af_Message *msg, af_Environment *env);
  19. NEW_DLC_SYMBOL(TopMsgProcessFunc, TopMsgProcessFunc);
  20. struct af_Core { // 解释器核心
  21. // GC基本信息
  22. struct af_ObjectData *gc_ObjectData;
  23. struct af_Object *gc_Object;
  24. struct af_Var *gc_Var;
  25. struct af_VarSpace *gc_VarSpace;
  26. // 基本对象信息
  27. struct af_Object *global; // 顶级属对象
  28. struct af_Object *object; // 顶级继承对象
  29. // 保护空间
  30. bool in_init; // 是否在初始化模式
  31. struct af_VarSpace *protect; // 顶级保护变量空间
  32. char prefix[PREFIX_SIZE]; // 前缀
  33. };
  34. struct af_Message {
  35. char *type; // 消息类型
  36. void *msg; // 信息内容
  37. size_t size;
  38. struct af_Message *next;
  39. };
  40. struct af_Activity { // 活动记录器
  41. struct af_Activity *prev; // 上一个活动记录器
  42. enum af_ActivityStatus {
  43. act_func = 0,
  44. act_arg,
  45. act_normal,
  46. } status;
  47. struct af_Message *msg_down; // 被调用者向调用者传递信息
  48. struct af_Message *msg_up; // 调用者向被调用者传递信息
  49. ActivityCount msg_up_count; // msg_up 添加的个数
  50. char **msg_type; // 一个包含字符串的列表, 记录了需要处理的`msg`类型的数组
  51. bool run_in_func; // 在函数变量空间内运行 (act_arg用)
  52. struct af_VarSpaceListNode *vsl; // 变量空间
  53. struct af_VarSpaceListNode *func_var_list; // 函数内部变量空间 (运行函数体时会设置为 主变量空间)
  54. struct af_VarSpaceListNode *var_list; // 主变量空间
  55. ActivityCount new_vs_count; // 需要释放的空间数
  56. struct af_Object *belong; // 属对象 (belong通常为func的belong)
  57. struct af_Object *func; // 函数本身
  58. struct af_Code *bt_top; // 最顶层设置为NULL, 函数调用设置为block, (bt_start的上一个元素)
  59. struct af_Code *bt_start; // 代码的起始位置 (block的第一个元素)
  60. struct af_Code *bt_next; // 指示代码下一步要运行的位置
  61. bool return_first; // 顺序执行, 获取第一个返回结果
  62. struct af_Object *return_obj; // 调用者向被调用者传递信息
  63. // 函数调用专项
  64. enum af_BlockType call_type; // 函数调用类型
  65. bool must_common_arg; // 强制普通参数
  66. bool not_strict; // 非严格调用
  67. af_Object *parentheses_call; // 类前缀调用
  68. ArgCodeList *acl_start;
  69. ArgCodeList *acl_next;
  70. bool is_last; // 最后一个函数体 (允许尾调递归优化)
  71. bool in_call; // 当重新执行该活动记录器时先不执行代码, 而是处理msg
  72. };
  73. struct af_TopMsgProcess { // 顶层msg处理器
  74. char *type;
  75. DLC_SYMBOL(TopMsgProcessFunc) func; // 在 env.h 中定义
  76. struct af_TopMsgProcess *next;
  77. };
  78. struct af_EnvVar { // 环境变量
  79. char *name;
  80. char *data;
  81. struct af_EnvVar *next;
  82. };
  83. struct af_EnvVarSpace { // 环境变量
  84. struct af_EnvVar *(var[ENV_VAR_HASH_SIZE]);
  85. };
  86. struct af_Environment { // 运行环境
  87. struct af_Core *core;
  88. struct af_EnvVarSpace *esv;
  89. struct af_Activity *activity;
  90. struct af_TopMsgProcess *process;
  91. };
  92. /* Core管理寒素 */
  93. af_Object *getBaseObjectFromCore(char *name, af_Core *core);
  94. /* Activity运行初始化函数 */
  95. bool addTopActivity(af_Code *code, af_Environment *env);
  96. /* 运行时Activity设置函数 (新增Activity) */
  97. bool pushExecutionActivity(af_Code *bt, bool return_first, af_Environment *env);
  98. bool pushFuncActivity(af_Code *bt, af_Environment *env);
  99. void popActivity(af_Message *msg, af_Environment *env);
  100. /* 运行时Activity设置函数 (设置Activity) */
  101. bool setFuncActivityToArg(af_Object *func, af_Environment *env);
  102. bool setFuncActivityAddVar(bool new_vsl, bool is_protect, char **msg_type, af_Environment *env);
  103. bool setFuncActivityToNormal(bool is_first, af_Environment *env);
  104. #endif //AFUN__ENV_H