func.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "__func.h"
  2. /* FuncBody 创建与释放 */
  3. static af_FuncBody *makeFuncBody(enum af_FuncBodyType type);
  4. static af_FuncBody *makeCodeFuncBody(af_Code *code, bool free_code);
  5. static af_FuncBody *makeCFuncBody(DLC_SYMBOL(callFuncBody) c_func);
  6. static af_FuncBody *freeFuncBody(af_FuncBody *fb);
  7. static void freeAllFuncBody(af_FuncBody *fb);
  8. /* FuncBody 操作函数 */
  9. static void pushFuncBody(af_FuncBody **base, af_FuncBody *body);
  10. ArgCodeList *makeArgCodeList(af_Code *code, size_t size, bool free_code, bool run_in_func) {
  11. ArgCodeList *acl = calloc(sizeof(ArgCodeList), 1);
  12. acl->info = calloc(size, 1);
  13. acl->size = size;
  14. acl->code = code;
  15. acl->free_code = free_code;
  16. acl->run_in_func = run_in_func;
  17. return acl;
  18. }
  19. ArgCodeList *freeArgCodeList(ArgCodeList *acl) {
  20. ArgCodeList *next = acl->next;
  21. free(acl->info);
  22. if (acl->free_code)
  23. freeAllCode(acl->code);
  24. if (acl->result != NULL)
  25. gc_delReference(acl->result);
  26. free(acl);
  27. return next;
  28. }
  29. void freeAllArgCodeList(ArgCodeList *acl) {
  30. while (acl != NULL)
  31. acl = freeArgCodeList(acl);
  32. }
  33. ArgCodeList **pushArgCodeList(ArgCodeList **base, ArgCodeList *new) {
  34. while (*base != NULL)
  35. base = &((*base)->next);
  36. *base = new;
  37. while (*base != NULL)
  38. base = &((*base)->next);
  39. return base;
  40. }
  41. ArgCodeList **pushNewArgCodeList(ArgCodeList **base, af_Code *code, size_t size, bool free_code, bool run_in_func) {
  42. while (*base != NULL)
  43. base = &((*base)->next);
  44. *base = makeArgCodeList(code, size, free_code, run_in_func);
  45. return &((*base)->next);
  46. }
  47. ArgList *makeArgList(char *name, af_Object *obj) {
  48. ArgList *arg_list = calloc(sizeof(ArgList), 1);
  49. arg_list->name = strCopy(name);
  50. arg_list->obj = obj;
  51. gc_addReference(obj);
  52. return arg_list;
  53. }
  54. ArgList *freeArgList(ArgList *al) {
  55. ArgList *next = al->next;
  56. free(al->name);
  57. if (al->obj != NULL)
  58. gc_addReference(al->obj);
  59. free(al);
  60. return next;
  61. }
  62. void freeAllArgList(ArgList *al) {
  63. while (al != NULL)
  64. al = freeArgList(al);
  65. }
  66. ArgList **pushArgList(ArgList **base, ArgList *new) {
  67. while (*base != NULL)
  68. base = &((*base)->next);
  69. *base = new;
  70. while (*base != NULL)
  71. base = &((*base)->next);
  72. return base;
  73. }
  74. ArgList **pushNewArgList(ArgList **base, char *name, af_Object *obj) {
  75. while (*base != NULL)
  76. base = &((*base)->next);
  77. *base = makeArgList(name, obj);
  78. return &((*base)->next);
  79. }
  80. bool runArgList(ArgList *al, af_VarSpaceListNode *vsl) {
  81. for (NULL; al != NULL; al = al->next) {
  82. if (!makeVarToVarSpaceList(al->name, 3, 3, 3, al->obj, vsl))
  83. return false;
  84. }
  85. return true;
  86. }
  87. static af_FuncBody *makeFuncBody(enum af_FuncBodyType type) {
  88. af_FuncBody *fb = calloc(sizeof(af_FuncBody), 1);
  89. fb->type = type;
  90. return fb;
  91. }
  92. static af_FuncBody *makeCodeFuncBody(af_Code *code, bool free_code) {
  93. af_FuncBody *fb = makeFuncBody(func_body_code);
  94. fb->code = code;
  95. fb->free_code = free_code;
  96. return fb;
  97. }
  98. static af_FuncBody *makeCFuncBody(DLC_SYMBOL(callFuncBody) c_func) {
  99. af_FuncBody *fb = makeFuncBody(func_body_c);
  100. fb->c_func = COPY_SYMBOL(c_func, callFuncBody);
  101. return fb;
  102. }
  103. static af_FuncBody *freeFuncBody(af_FuncBody *fb) {
  104. af_FuncBody *next = fb->next;
  105. if (fb->type == func_body_code && fb->free_code)
  106. freeAllCode(fb->code);
  107. else if (fb->type == func_body_c)
  108. FREE_SYMBOL(fb->c_func);
  109. free(fb);
  110. return next;
  111. }
  112. static void freeAllFuncBody(af_FuncBody *fb) {
  113. while (fb != NULL)
  114. fb = freeFuncBody(fb);
  115. }
  116. static void pushFuncBody(af_FuncBody **base, af_FuncBody *body) {
  117. while (*base != NULL)
  118. base = &((*base)->next);
  119. *base = body;
  120. }
  121. af_FuncInfo *makeFuncInfo(enum af_FuncInfoScope scope, enum af_FuncInfoEmbedded embedded, bool is_macro,
  122. bool is_object, af_VarSpaceListNode *vsl) {
  123. af_FuncInfo *fi = calloc(sizeof(af_FuncInfo), 1);
  124. fi->scope = scope;
  125. fi->embedded = embedded;
  126. fi->is_macro = is_macro;
  127. fi->is_object = is_object;
  128. fi->vsl = vsl;
  129. return fi;
  130. }
  131. void freeFuncInfo(af_FuncInfo *fi) { // vsl是不释放的
  132. freeAllFuncBody(fi->body);
  133. free(fi);
  134. }
  135. void makeCFuncBodyToFuncInfo(DLC_SYMBOL(callFuncBody) c_func, af_FuncInfo *fi) {
  136. pushFuncBody(&fi->body, makeCFuncBody(c_func));
  137. }
  138. void makeCodeFuncBodyToFuncInfo(af_Code *code, bool free_code, af_FuncInfo *fi) {
  139. pushFuncBody(&fi->body, makeCodeFuncBody(code, free_code));
  140. }