func.c 5.9 KB

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