func.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "__func.hpp"
  2. /* FuncBody 创建与释放 */
  3. static af_FuncBody *makeFuncBody(enum af_FuncBodyType type, char **msg_type);
  4. /* msg_type 释放 */
  5. static void freeMsgType(char **msg_type);
  6. af_ArgCodeList *makeArgCodeList(af_Code *code, size_t size, bool free_code, bool run_in_func) {
  7. auto acl = calloc(1, af_ArgCodeList);
  8. acl->info = calloc_size(1, size);
  9. acl->size = size;
  10. acl->code = code;
  11. acl->free_code = free_code;
  12. acl->run_in_func = run_in_func;
  13. return acl;
  14. }
  15. static af_ArgCodeList *freeArgCodeList(af_ArgCodeList *acl, af_Environment *env){
  16. af_ArgCodeList *next = acl->next;
  17. free(acl->info);
  18. if (acl->free_code)
  19. freeAllCode(acl->code);
  20. if (acl->result != nullptr)
  21. gc_delObjectReference(acl->result, env);
  22. free(acl);
  23. return next;
  24. }
  25. void freeAllArgCodeList(af_ArgCodeList *acl, af_Environment *env){
  26. while (acl != nullptr)
  27. acl = freeArgCodeList(acl, env);
  28. }
  29. af_ArgCodeList **pushArgCodeList(af_ArgCodeList **base, af_ArgCodeList *new_acl) {
  30. while (*base != nullptr)
  31. base = &((*base)->next);
  32. *base = new_acl;
  33. while (*base != nullptr)
  34. base = &((*base)->next);
  35. return base;
  36. }
  37. void *getArgCodeListData(af_ArgCodeList *acl) {
  38. return acl->info;
  39. }
  40. af_Object *getArgCodeListResult(af_ArgCodeList *acl) {
  41. return acl->result;
  42. }
  43. af_ArgList *makeArgList(const char *name, af_Object *obj){
  44. auto arg_list = calloc(1, af_ArgList);
  45. arg_list->name = strCopy(name);
  46. arg_list->obj = obj;
  47. return arg_list;
  48. }
  49. static af_ArgList *freeArgList(af_ArgList *al, af_Environment *env){
  50. af_ArgList *next = al->next;
  51. free(al->name);
  52. if (al->obj != nullptr)
  53. gc_delObjectReference(al->obj, env);
  54. free(al);
  55. return next;
  56. }
  57. void freeAllArgList(af_ArgList *al, af_Environment *env){
  58. while (al != nullptr)
  59. al = freeArgList(al, env);
  60. }
  61. af_ArgList **pushArgList(af_ArgList **base, af_ArgList *new_al) {
  62. while (*base != nullptr)
  63. base = &((*base)->next);
  64. *base = new_al;
  65. while (*base != nullptr)
  66. base = &((*base)->next);
  67. return base;
  68. }
  69. /**
  70. * af_ArgCodeList 转 af_ArgList
  71. * @param name 参数名
  72. * @param acl ArgCodeList
  73. * @param env 运行环境
  74. * @return
  75. */
  76. af_ArgList *makeArgListFromArgCodeList(const char *name, af_ArgCodeList *acl, af_Environment *env) {
  77. af_Object *obj = getArgCodeListResult(acl);
  78. gc_addObjectReference(obj, env);
  79. af_ArgList *al = makeArgList(name, obj);
  80. return al;
  81. }
  82. bool runArgList(af_ArgList *al, af_VarList *vsl, af_Environment *env){
  83. for(NULL; al != nullptr; al = al->next) {
  84. if (!makeVarToVarSpaceList(al->name, 3, 3, 3, al->obj, vsl,
  85. env->activity->belong, env))
  86. return false;
  87. }
  88. return true;
  89. }
  90. static af_FuncBody *makeFuncBody(enum af_FuncBodyType type, char **msg_type) {
  91. auto fb = calloc(1, af_FuncBody);
  92. fb->type = type;
  93. fb->msg_type = msg_type;
  94. return fb;
  95. }
  96. af_FuncBody *makeCodeFuncBody(af_Code *code, bool free_code, char **msg_type) {
  97. af_FuncBody *fb = makeFuncBody(func_body_code, msg_type);
  98. fb->code = code;
  99. fb->free_code = free_code;
  100. return fb;
  101. }
  102. af_FuncBody *makeImportFuncBody(af_Code *code, bool free_code, char **msg_type) {
  103. af_FuncBody *fb = makeFuncBody(func_body_import, msg_type);
  104. fb->code = code;
  105. fb->free_code = free_code;
  106. return fb;
  107. }
  108. af_FuncBody *makeDynamicFuncBody() {
  109. af_FuncBody *fb = makeFuncBody(func_body_dynamic, nullptr);
  110. return fb;
  111. }
  112. af_FuncBody *makeCFuncBody(DLC_SYMBOL(callFuncBody) c_func, char **msg_type) {
  113. af_FuncBody *fb = makeFuncBody(func_body_c, msg_type);
  114. fb->c_func = COPY_SYMBOL(c_func, callFuncBody);
  115. return fb;
  116. }
  117. static void freeMsgType(char **msg_type) {
  118. for (char *tmp = *msg_type; tmp != nullptr; tmp++)
  119. free(tmp);
  120. free(msg_type);
  121. }
  122. af_FuncBody *freeFuncBody(af_FuncBody *fb) {
  123. af_FuncBody *next = fb->next;
  124. if ((fb->type == func_body_code || fb->type == func_body_import) && fb->free_code)
  125. freeAllCode(fb->code);
  126. else if (fb->type == func_body_c)
  127. FREE_SYMBOL(fb->c_func);
  128. if (fb->msg_type != nullptr)
  129. freeMsgType(fb->msg_type);
  130. free(fb);
  131. return next;
  132. }
  133. void freeAllFuncBody(af_FuncBody *fb) {
  134. while (fb != nullptr)
  135. fb = freeFuncBody(fb);
  136. }
  137. void pushFuncBody(af_FuncBody **base, af_FuncBody *body) {
  138. while (*base != nullptr)
  139. base = &((*base)->next);
  140. *base = body;
  141. }
  142. af_FuncInfo *makeFuncInfo(enum af_FuncInfoScope scope, enum af_FuncInfoEmbedded embedded, bool is_macro, bool var_this, bool var_func){
  143. auto fi = calloc(1, af_FuncInfo);
  144. fi->scope = scope;
  145. fi->embedded = embedded;
  146. fi->is_macro = is_macro;
  147. fi->var_this = var_this;
  148. fi->var_func = var_func;
  149. return fi;
  150. }
  151. void freeFuncInfo(af_FuncInfo *fi) { // vsl是不释放的
  152. freeAllFuncBody(fi->body);
  153. free(fi);
  154. }
  155. af_FuncBody *makeCFuncBodyToFuncInfo(DLC_SYMBOL(callFuncBody) c_func, char **msg_type, af_FuncInfo *fi) {
  156. af_FuncBody *fb = makeCFuncBody(c_func, msg_type);
  157. pushFuncBody(&fi->body, fb);
  158. return fb;
  159. }
  160. af_FuncBody *makeCodeFuncBodyToFuncInfo(af_Code *code, bool free_code, char **msg_type, af_FuncInfo *fi) {
  161. af_FuncBody *fb = makeCodeFuncBody(code, free_code, msg_type);
  162. pushFuncBody(&fi->body, fb);
  163. return fb;
  164. }
  165. af_FuncBody *makeImportFuncBodyToFuncInfo(af_Code *code, bool free_code, char **msg_type, af_FuncInfo *fi) {
  166. af_FuncBody *fb = makeImportFuncBody(code, free_code, msg_type);
  167. pushFuncBody(&fi->body, fb);
  168. return fb;
  169. }
  170. af_FuncBody *makeDynamicFuncBodyToFuncInfo(af_FuncInfo *fi) {
  171. af_FuncBody *fb = makeDynamicFuncBody();
  172. pushFuncBody(&fi->body, fb);
  173. return fb;
  174. }
  175. bool pushDynamicFuncBody(af_FuncBody *new_fub, af_FuncBody *body) {
  176. if (body == nullptr || body->next == nullptr || body->next->type != func_body_dynamic) {
  177. freeAllFuncBody(new_fub);
  178. return false;
  179. }
  180. if (new_fub == nullptr) {
  181. body->next = freeFuncBody(body->next); // 不添加任何新内容, 但释放func_body_dynamic
  182. } else {
  183. af_FuncBody **next = &new_fub;
  184. while ((*next) != nullptr)
  185. next = &((*next)->next);
  186. *next = freeFuncBody(body->next); // 把func_body_dynamic后的内容添加到new_fub的末尾
  187. body->next = new_fub;
  188. }
  189. return true;
  190. }
  191. af_ArgCodeList *getArgCodeListNext(af_ArgCodeList *acl) {
  192. return acl->next;
  193. }
  194. af_ArgList *getArgListNext(af_ArgList *al) {
  195. return al->next;
  196. }
  197. bool getArgCodeListRunInFunc(af_ArgCodeList *acl) {
  198. return acl->run_in_func;
  199. }