1
0

bytecode.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * 文件名: bytecode.c
  3. * 目标: 管理ByteCode结构体的函数
  4. */
  5. #include "aFun.h"
  6. #include "bytecode.h"
  7. static af_ByteCode *makeByteCode(char prefix, FileLine line, FilePath path) {
  8. af_ByteCode *bt = calloc(1, sizeof(af_ByteCode));
  9. bt->line = line;
  10. bt->prefix = prefix;
  11. if (path != NULL)
  12. bt->path = pathCopy(path);
  13. return bt;
  14. }
  15. af_ByteCode *makeLiteralByteCode(char *literal_data, char *func, char prefix, FileLine line, FilePath path) {
  16. af_ByteCode *bt = makeByteCode(prefix, line, path);
  17. bt->type = literal;
  18. bt->literal.literal_data = strCopy(literal_data);
  19. bt->literal.func = strCopy(func);
  20. return bt;
  21. }
  22. af_ByteCode *makeVariableByteCode(char *var, char prefix, FileLine line, FilePath path) {
  23. af_ByteCode *bt = makeByteCode(prefix, line, path);
  24. bt->type = variable;
  25. bt->variable.name = strCopy(var);
  26. return bt;
  27. }
  28. /*
  29. * 函数名: countElement
  30. * 目标: 统计元素个数(不包括元素的子元素)
  31. */
  32. static bool countElement(af_ByteCode *element, ByteCodeUint *count, af_ByteCode **next) {
  33. ByteCodeUint to_next = 0; // 表示紧接着的元素都不纳入统计(指block的子元素)
  34. for (*count = 0; element != NULL; *next = element, element = element->next) {
  35. if (to_next == 0)
  36. (*count)++;
  37. else
  38. to_next--;
  39. if (element->type == block)
  40. to_next += element->block.elements;
  41. }
  42. if (to_next != 0)
  43. return false;
  44. return true;
  45. }
  46. af_ByteCode *makeBlockByteCode(enum af_BlockType type, af_ByteCode *element, char prefix, FileLine line, FilePath path, af_ByteCode **next) {
  47. af_ByteCode *bt = NULL;
  48. ByteCodeUint count = 0;
  49. if (!countElement(element, &count, next))
  50. return NULL;
  51. bt = makeByteCode(prefix, line, path);
  52. bt->type = block;
  53. bt->block.type = type;
  54. bt->block.elements = count;
  55. bt->next = element;
  56. return bt;
  57. }
  58. af_ByteCode *CopyByteCode(af_ByteCode *base, FilePath *path) {
  59. af_ByteCode *dest = NULL;
  60. af_ByteCode **pdest = &dest;
  61. for (NULL; base != NULL; base = base->next) {
  62. *pdest = makeByteCode(base->prefix, base->line, base->path);
  63. (*pdest)->type = base->type;
  64. switch (base->type) {
  65. case literal:
  66. (*pdest)->literal.literal_data = strCopy(base->literal.literal_data);
  67. (*pdest)->literal.func = strCopy(base->literal.func);
  68. break;
  69. case variable:
  70. (*pdest)->variable.name = strCopy(base->variable.name);
  71. break;
  72. case block:
  73. (*pdest)->block.elements = base->block.elements;
  74. (*pdest)->block.type = base->block.type;
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. if (dest != NULL && path != NULL) {
  81. free(dest->path);
  82. dest->path = pathCopy(path);
  83. }
  84. return dest;
  85. }
  86. af_ByteCode *freeByteCode(af_ByteCode *bt) {
  87. if (bt == NULL)
  88. return NULL;
  89. af_ByteCode *next = bt->next;
  90. switch (bt->type) {
  91. case literal:
  92. free(bt->literal.literal_data);
  93. free(bt->literal.func);
  94. break;
  95. case variable:
  96. free(bt->variable.name);
  97. break;
  98. default:
  99. break;
  100. }
  101. return next;
  102. }
  103. bool freeByteCodeWithElement(af_ByteCode *bt, af_ByteCode **next) {
  104. ByteCodeUint count = 1; // 要释放的元素个数
  105. for (NULL; count != 0; count--) {
  106. if (bt == NULL)
  107. return false;
  108. if (bt->type == block)
  109. count += bt->block.elements;
  110. bt = freeByteCode(bt);
  111. }
  112. *next = bt;
  113. return true;
  114. }
  115. void freeAllByteCode(af_ByteCode *bt) {
  116. while (bt != NULL)
  117. bt = freeByteCode(bt);
  118. }