func.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "__func.h"
  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. /* FuncBody 操作函数 */
  7. static void pushFuncBody(af_FuncBody **base, af_FuncBody *body);
  8. af_ArgCodeList *makeArgCodeList(af_Code *code, size_t size, bool free_code, bool run_in_func) {
  9. af_ArgCodeList *acl = calloc(1, sizeof(af_ArgCodeList));
  10. acl->info = calloc(1, size);
  11. acl->size = size;
  12. acl->code = code;
  13. acl->free_code = free_code;
  14. acl->run_in_func = run_in_func;
  15. return acl;
  16. }
  17. af_ArgCodeList *freeArgCodeList(af_ArgCodeList *acl) {
  18. af_ArgCodeList *next = acl->next;
  19. free(acl->info);
  20. if (acl->free_code)
  21. freeAllCode(acl->code);
  22. if (acl->result != NULL)
  23. gc_delReference(acl->result);
  24. free(acl);
  25. return next;
  26. }
  27. void freeAllArgCodeList(af_ArgCodeList *acl) {
  28. while (acl != NULL)
  29. acl = freeArgCodeList(acl);
  30. }
  31. af_ArgCodeList **pushArgCodeList(af_ArgCodeList **base, af_ArgCodeList *new) {
  32. while (*base != NULL)
  33. base = &((*base)->next);
  34. *base = new;
  35. while (*base != NULL)
  36. base = &((*base)->next);
  37. return base;
  38. }
  39. af_ArgCodeList **pushNewArgCodeList(af_ArgCodeList **base, af_Code *code, size_t size, bool free_code, bool run_in_func) {
  40. while (*base != NULL)
  41. base = &((*base)->next);
  42. *base = makeArgCodeList(code, size, free_code, run_in_func);
  43. return &((*base)->next);
  44. }
  45. void *getArgCodeListData(af_ArgCodeList *acl) {
  46. return acl->info;
  47. }
  48. af_Object *getArgCodeListResult(af_ArgCodeList *acl) {
  49. return acl->result;
  50. }
  51. af_ArgList *makeArgList(char *name, af_Object *obj) {
  52. af_ArgList *arg_list = calloc(1, sizeof(af_ArgList));
  53. arg_list->name = strCopy(name);
  54. arg_list->obj = obj;
  55. gc_addReference(obj);
  56. return arg_list;
  57. }
  58. af_ArgList *freeArgList(af_ArgList *al) {
  59. af_ArgList *next = al->next;
  60. free(al->name);
  61. if (al->obj != NULL)
  62. gc_delReference(al->obj);
  63. free(al);
  64. return next;
  65. }
  66. void freeAllArgList(af_ArgList *al) {
  67. while (al != NULL)
  68. al = freeArgList(al);
  69. }
  70. af_ArgList **pushArgList(af_ArgList **base, af_ArgList *new) {
  71. while (*base != NULL)
  72. base = &((*base)->next);
  73. *base = new;
  74. while (*base != NULL)
  75. base = &((*base)->next);
  76. return base;
  77. }
  78. af_ArgList **pushNewArgList(af_ArgList **base, char *name, af_Object *obj) {
  79. while (*base != NULL)
  80. base = &((*base)->next);
  81. *base = makeArgList(name, obj);
  82. return &((*base)->next);
  83. }
  84. bool runArgList(af_ArgList *al, af_VarSpaceListNode *vsl, af_Environment *env){
  85. for (NULL; al != NULL; al = al->next) {
  86. if (!makeVarToVarSpaceList(al->name, 3, 3, 3, al->obj, vsl,
  87. env->activity->belong, env))
  88. return false;
  89. }
  90. return true;
  91. }
  92. static af_FuncBody *makeFuncBody(enum af_FuncBodyType type, char **msg_type) {
  93. af_FuncBody *fb = calloc(1, sizeof(af_FuncBody));
  94. fb->type = type;
  95. fb->msg_type = msg_type;
  96. return fb;
  97. }
  98. af_FuncBody *makeCodeFuncBody(af_Code *code, bool free_code, char **msg_type) {
  99. af_FuncBody *fb = makeFuncBody(func_body_code, msg_type);
  100. fb->code = code;
  101. fb->free_code = free_code;
  102. return fb;
  103. }
  104. af_FuncBody *makeDynamicFuncBody(void) {
  105. af_FuncBody *fb = makeFuncBody(func_body_dynamic, NULL);
  106. return fb;
  107. }
  108. af_FuncBody *makeCFuncBody(DLC_SYMBOL(callFuncBody) c_func, char **msg_type) {
  109. af_FuncBody *fb = makeFuncBody(func_body_c, msg_type);
  110. fb->c_func = COPY_SYMBOL(c_func, callFuncBody);
  111. return fb;
  112. }
  113. static void freeMsgType(char **msg_type) {
  114. for (char *tmp = *msg_type; tmp != NULL; tmp++)
  115. free(tmp);
  116. free(msg_type);
  117. }
  118. af_FuncBody *freeFuncBody(af_FuncBody *fb) {
  119. af_FuncBody *next = fb->next;
  120. if (fb->type == func_body_code && fb->free_code)
  121. freeAllCode(fb->code);
  122. else if (fb->type == func_body_c)
  123. FREE_SYMBOL(fb->c_func);
  124. if (fb->msg_type != NULL)
  125. freeMsgType(fb->msg_type);
  126. free(fb);
  127. return next;
  128. }
  129. void freeAllFuncBody(af_FuncBody *fb) {
  130. while (fb != NULL)
  131. fb = freeFuncBody(fb);
  132. }
  133. static void pushFuncBody(af_FuncBody **base, af_FuncBody *body) {
  134. while (*base != NULL)
  135. base = &((*base)->next);
  136. *base = body;
  137. }
  138. af_FuncInfo *makeFuncInfo(enum af_FuncInfoScope scope, enum af_FuncInfoEmbedded embedded, bool is_macro, bool var_this, bool var_func){
  139. af_FuncInfo *fi = calloc(1, sizeof(af_FuncInfo));
  140. fi->scope = scope;
  141. fi->embedded = embedded;
  142. fi->is_macro = is_macro;
  143. fi->var_this = var_this;
  144. fi->var_func = var_func;
  145. return fi;
  146. }
  147. void freeFuncInfo(af_FuncInfo *fi) { // vsl是不释放的
  148. freeAllFuncBody(fi->body);
  149. free(fi);
  150. }
  151. void makeCFuncBodyToFuncInfo(DLC_SYMBOL(callFuncBody) c_func, char **msg_type, af_FuncInfo *fi) {
  152. pushFuncBody(&fi->body, makeCFuncBody(c_func, msg_type));
  153. }
  154. void makeCodeFuncBodyToFuncInfo(af_Code *code, bool free_code, char **msg_type, af_FuncInfo *fi) {
  155. pushFuncBody(&fi->body, makeCodeFuncBody(code, free_code, msg_type));
  156. }
  157. void makeDynamicFuncBodyToFuncInfo(af_FuncInfo *fi) {
  158. pushFuncBody(&fi->body, makeDynamicFuncBody());
  159. }
  160. bool pushDynamicFuncBody(af_FuncBody *new, af_FuncBody *body) {
  161. if (body == NULL || body->next == NULL || body->next->type != func_body_dynamic) {
  162. freeAllFuncBody(new);
  163. return false;
  164. }
  165. if (new == NULL) {
  166. body->next = freeFuncBody(body->next); // 不添加任何新内容, 但释放func_body_dynamic
  167. } else {
  168. af_FuncBody **next = &new;
  169. while ((*next) != NULL)
  170. next = &((*next)->next);
  171. *next = freeFuncBody(body->next); // 把func_body_dynamic后的内容添加到new的末尾
  172. body->next = new;
  173. }
  174. return true;
  175. }