DataType.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "DataType.h"
  5. #define ARRAY_NAME 256
  6. static int typeNameIndex = 0;
  7. static int typeNameSwap = 0;
  8. static char typeName[2][ARRAY_NAME];
  9. static bool useGlobals = false;
  10. static Structs globalStructs;
  11. static void dtAppend(const char* s) {
  12. int index = 0;
  13. while(typeNameIndex < (ARRAY_NAME - 1) && s[index] != '\0') {
  14. typeName[typeNameSwap][typeNameIndex] = s[index];
  15. index++;
  16. typeNameIndex++;
  17. }
  18. typeName[typeNameSwap][typeNameIndex] = '\0';
  19. }
  20. const char* dtGetName(const Structs* sts, DataType dt) {
  21. typeNameSwap = !typeNameSwap;
  22. typeNameIndex = 0;
  23. switch(dt.type) {
  24. case DT_INT: dtAppend("int"); break;
  25. case DT_FLOAT: dtAppend("float"); break;
  26. case DT_STRUCT: dtAppend(dtGetStruct(sts, dt)->name); break;
  27. case DT_VOID: dtAppend("void"); break;
  28. default: dtAppend("unknown");
  29. }
  30. if(dt.array != 0) {
  31. dtAppend("[]");
  32. }
  33. if(dt.pointer != 0) {
  34. dtAppend("*");
  35. }
  36. return typeName[typeNameSwap];
  37. }
  38. int dtGetSize(DataType dt, const Structs* sts) {
  39. if(dt.pointer != 0) {
  40. return 1;
  41. }
  42. switch(dt.type) {
  43. case DT_INT: return 1;
  44. case DT_FLOAT: return 1;
  45. case DT_STRUCT: {
  46. int size = 0;
  47. Struct* st = dtGetStruct(sts, dt);
  48. for(int i = 0; i < st->amount; i++) {
  49. size += dtGetSize(st->vars[i].type, sts);
  50. }
  51. return size;
  52. }
  53. default: return 0;
  54. }
  55. }
  56. DataType dtInt() {
  57. DataType dt = {DT_INT, 0, 0, 0};
  58. return dt;
  59. }
  60. DataType dtFloat() {
  61. DataType dt = {DT_FLOAT, 0, 0, 0};
  62. return dt;
  63. }
  64. DataType dtVoid() {
  65. DataType dt = {DT_VOID, 0, 0, 0};
  66. return dt;
  67. }
  68. DataType dtStruct(const Struct* st) {
  69. DataType dt = {DT_STRUCT, 0, 0, st->id};
  70. return dt;
  71. }
  72. DataType dtText() {
  73. DataType dt = {DT_INT, 0, 1, 0};
  74. return dt;
  75. }
  76. DataType dtToArray(DataType dt) {
  77. dt.array = 1;
  78. return dt;
  79. }
  80. DataType dtRemoveArray(DataType dt) {
  81. dt.array = 0;
  82. return dt;
  83. }
  84. DataType dtToPointer(DataType dt) {
  85. dt.pointer = 1;
  86. return dt;
  87. }
  88. bool dtRemovePointer(DataType* dt) {
  89. if(dt->pointer != 0) {
  90. dt->pointer = 0;
  91. return true;
  92. }
  93. return false;
  94. }
  95. bool dtCompare(DataType a, DataType b) {
  96. return a.array == b.array && a.structId == b.structId && a.type == b.type &&
  97. a.pointer == b.pointer;
  98. }
  99. bool dtIsInt(DataType dt) {
  100. return dtCompare(dt, dtInt());
  101. }
  102. bool dtIsFloat(DataType dt) {
  103. return dtCompare(dt, dtFloat());
  104. }
  105. bool dtIsVoid(DataType dt) {
  106. return dtCompare(dt, dtVoid());
  107. }
  108. bool dtIsArray(DataType dt) {
  109. return dt.array != 0;
  110. }
  111. Struct* dtGetStruct(const Structs* sts, DataType dt) {
  112. if(dt.type != DT_STRUCT) {
  113. return NULL;
  114. }
  115. if(dt.structId & 1) {
  116. if(!useGlobals) {
  117. return NULL;
  118. }
  119. return globalStructs.data + (dt.structId >> 1);
  120. }
  121. return sts->data + (dt.structId >> 1);
  122. }
  123. void stAddVariable(Struct* st, const char* name, DataType type) {
  124. int index = st->amount;
  125. st->amount++;
  126. st->vars = realloc(st->vars, sizeof(StructVariable) * st->amount);
  127. st->vars[index].name = name;
  128. st->vars[index].type = type;
  129. }
  130. void stsInit(Structs* sts) {
  131. sts->capacity = 4;
  132. sts->entries = 0;
  133. sts->data = malloc(sizeof(Struct) * sts->capacity);
  134. }
  135. void stsDelete(Structs* sts) {
  136. for(int i = 0; i < sts->entries; i++) {
  137. free(sts->data[i].vars);
  138. }
  139. free(sts->data);
  140. }
  141. static Struct* stsInternSearch(Structs* sts, const char* name) {
  142. for(int i = 0; i < sts->entries; i++) {
  143. if(strcmp(sts->data[i].name, name) == 0) {
  144. return sts->data + i;
  145. }
  146. }
  147. return NULL;
  148. }
  149. Struct* stsSearch(Structs* sts, const char* name) {
  150. if(useGlobals) {
  151. Struct* st = stsInternSearch(&globalStructs, name);
  152. if(st != NULL) {
  153. return st;
  154. }
  155. }
  156. return stsInternSearch(sts, name);
  157. }
  158. Struct* stsAdd(Structs* sts, const char* name) {
  159. if(sts->entries >= sts->capacity) {
  160. sts->capacity *= 2;
  161. sts->data = realloc(sts->data, sizeof(Struct) * sts->capacity);
  162. }
  163. int index = sts->entries++;
  164. sts->data[index].id = index << 1;
  165. sts->data[index].amount = 0;
  166. sts->data[index].name = name;
  167. sts->data[index].vars = NULL;
  168. return sts->data + index;
  169. }
  170. void gstsInit() {
  171. stsInit(&globalStructs);
  172. useGlobals = true;
  173. }
  174. void gstsDelete() {
  175. stsDelete(&globalStructs);
  176. useGlobals = false;
  177. }
  178. Structs* gstsGet() {
  179. return &globalStructs;
  180. }
  181. Struct* gstsAdd(const char* name) {
  182. Struct* st = stsAdd(&globalStructs, name);
  183. st->id |= 1;
  184. return st;
  185. }