DataType.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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(Structs* sts, DataType dt) {
  21. typeNameSwap = !typeNameSwap;
  22. typeNameIndex = 0;
  23. if(dt.constant) {
  24. dtAppend("const ");
  25. }
  26. switch(dt.type) {
  27. case DT_INT32: dtAppend("int"); break;
  28. case DT_INT64: dtAppend("long"); break;
  29. case DT_FLOAT: dtAppend("float"); break;
  30. case DT_BOOL: dtAppend("bool"); break;
  31. case DT_NULL: dtAppend("null"); break;
  32. case DT_STRUCT: dtAppend(dtGetStruct(sts, dt)->name); break;
  33. case DT_VOID: dtAppend("void"); break;
  34. default: dtAppend("unknown");
  35. }
  36. for(unsigned int i = 0; i < dt.pointers; i++) {
  37. dtAppend("*");
  38. }
  39. return typeName[typeNameSwap];
  40. }
  41. int dtGetSize(DataType dt, Structs* sts) {
  42. if(dt.pointers > 0 || dtIsNull(dt)) {
  43. return sizeof(Pointer);
  44. }
  45. switch(dt.type) {
  46. case DT_INT32: return sizeof(int32);
  47. case DT_INT64: return sizeof(int64);
  48. case DT_FLOAT: return sizeof(float);
  49. case DT_BOOL: return sizeof(bool);
  50. case DT_STRUCT:
  51. {
  52. int size = 0;
  53. Struct* st = dtGetStruct(sts, dt);
  54. for(int i = 0; i < st->amount; i++) {
  55. size += dtGetSize(st->vars[i].type, sts);
  56. }
  57. return size;
  58. }
  59. default: return 0;
  60. }
  61. }
  62. DataType dtInt32() {
  63. DataType dt = {DT_INT32, 0, 0, 0};
  64. return dt;
  65. }
  66. DataType dtInt64() {
  67. DataType dt = {DT_INT64, 0, 0, 0};
  68. return dt;
  69. }
  70. DataType dtFloat() {
  71. DataType dt = {DT_FLOAT, 0, 0, 0};
  72. return dt;
  73. }
  74. DataType dtBool() {
  75. DataType dt = {DT_BOOL, 0, 0, 0};
  76. return dt;
  77. }
  78. DataType dtNull() {
  79. DataType dt = {DT_NULL, 0, 0, 0};
  80. return dt;
  81. }
  82. DataType dtText() {
  83. DataType dt = {DT_INT32, 1, 0, 0};
  84. return dt;
  85. }
  86. DataType dtVoid() {
  87. DataType dt = {DT_VOID, 0, 0, 0};
  88. return dt;
  89. }
  90. DataType dtStruct(Struct* st) {
  91. DataType dt = {DT_STRUCT, 0, 0, st->id};
  92. return dt;
  93. }
  94. DataType dtReference(DataType dt) {
  95. dt.pointers--;
  96. return dt;
  97. }
  98. bool dtDereference(DataType* dt) {
  99. if(dt->pointers == 15) {
  100. return true;
  101. }
  102. dt->pointers++;
  103. return false;
  104. }
  105. static bool dtInternCompare(DataType a, DataType b) {
  106. return a.pointers == b.pointers && a.structId == b.structId &&
  107. a.type == b.type && a.constant == b.constant;
  108. }
  109. bool dtCompare(DataType a, DataType b) {
  110. return dtInternCompare(a, b);
  111. }
  112. bool dtNullCompare(DataType a, DataType bOrNull) {
  113. return dtInternCompare(a, bOrNull) ||
  114. (dtIsPointer(a) && dtIsNull(bOrNull) &&
  115. a.constant == bOrNull.constant);
  116. }
  117. bool dtIsInt32(DataType dt) {
  118. return dtCompare(dt, dtInt32());
  119. }
  120. bool dtIsInt64(DataType dt) {
  121. return dtCompare(dt, dtInt64());
  122. }
  123. bool dtIsFloat(DataType dt) {
  124. return dtCompare(dt, dtFloat());
  125. }
  126. bool dtIsBool(DataType dt) {
  127. return dtCompare(dt, dtBool());
  128. }
  129. bool dtIsNull(DataType dt) {
  130. dt.constant = 0;
  131. return dtCompare(dt, dtNull());
  132. }
  133. bool dtIsVoid(DataType dt) {
  134. return dtCompare(dt, dtVoid());
  135. }
  136. bool dtIsPointer(DataType dt) {
  137. return dt.pointers > 0;
  138. }
  139. bool dtIsVariable(DataType dt) {
  140. return dt.type & 8;
  141. }
  142. Struct* dtGetStruct(Structs* sts, DataType dt) {
  143. if(dt.type != DT_STRUCT) {
  144. return NULL;
  145. }
  146. if(dt.structId & 1) {
  147. if(!useGlobals) {
  148. return NULL;
  149. }
  150. return globalStructs.data + (dt.structId >> 1);
  151. }
  152. return sts->data + (dt.structId >> 1);
  153. }
  154. DataType dtToVariable(DataType dt) {
  155. dt.type |= 8;
  156. return dt;
  157. }
  158. bool dtRemoveVariable(DataType* dt) {
  159. if(dtIsVariable(*dt)) {
  160. dt->type &= 7;
  161. return true;
  162. }
  163. return false;
  164. }
  165. DataType dtConst(DataType dt) {
  166. dt.constant = 1;
  167. return dt;
  168. }
  169. void stAddVariable(Struct* st, const char* name, DataType type) {
  170. int index = st->amount;
  171. st->amount++;
  172. st->vars = realloc(st->vars, sizeof(StructVariable) * st->amount);
  173. st->vars[index].name = name;
  174. st->vars[index].type = type;
  175. }
  176. void stsInit(Structs* sts) {
  177. sts->capacity = 4;
  178. sts->entries = 0;
  179. sts->data = malloc(sizeof(Struct) * sts->capacity);
  180. }
  181. void stsDelete(Structs* sts) {
  182. for(int i = 0; i < sts->entries; i++) {
  183. free(sts->data[i].vars);
  184. }
  185. free(sts->data);
  186. }
  187. static Struct* stsInternSearch(Structs* sts, const char* name) {
  188. for(int i = 0; i < sts->entries; i++) {
  189. if(strcmp(sts->data[i].name, name) == 0) {
  190. return sts->data + i;
  191. }
  192. }
  193. return NULL;
  194. }
  195. Struct* stsSearch(Structs* sts, const char* name) {
  196. if(useGlobals) {
  197. Struct* st = stsInternSearch(&globalStructs, name);
  198. if(st != NULL) {
  199. return st;
  200. }
  201. }
  202. return stsInternSearch(sts, name);
  203. }
  204. Struct* stsAdd(Structs* sts, const char* name) {
  205. if(sts->entries >= sts->capacity) {
  206. sts->capacity *= 2;
  207. sts->data = realloc(sts->data, sizeof(Struct) * sts->capacity);
  208. }
  209. int index = sts->entries++;
  210. sts->data[index].id = index << 1;
  211. sts->data[index].amount = 0;
  212. sts->data[index].name = name;
  213. sts->data[index].vars = NULL;
  214. return sts->data + index;
  215. }
  216. void gstsInit() {
  217. stsInit(&globalStructs);
  218. useGlobals = true;
  219. }
  220. void gstsDelete() {
  221. stsDelete(&globalStructs);
  222. useGlobals = false;
  223. }
  224. Structs* gstsGet() {
  225. return &globalStructs;
  226. }
  227. Struct* gstsAdd(const char* name) {
  228. Struct* st = stsAdd(&globalStructs, name);
  229. st->id |= 1;
  230. return st;
  231. }