DataType.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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(sts->data[dt.structId].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 = sts->data + dt.structId;
  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. return sts->data + dt.structId;
  147. }
  148. DataType dtToVariable(DataType dt) {
  149. dt.type |= 8;
  150. return dt;
  151. }
  152. bool dtRemoveVariable(DataType* dt) {
  153. if(dtIsVariable(*dt)) {
  154. dt->type &= 7;
  155. return true;
  156. }
  157. return false;
  158. }
  159. DataType dtConst(DataType dt) {
  160. dt.constant = 1;
  161. return dt;
  162. }
  163. void stAddVariable(Struct* st, const char* name, DataType type) {
  164. int index = st->amount;
  165. st->amount++;
  166. st->vars = realloc(st->vars, sizeof(StructVariable) * st->amount);
  167. st->vars[index].name = name;
  168. st->vars[index].type = type;
  169. }
  170. void stsInit(Structs* sts) {
  171. sts->capacity = 4;
  172. sts->entries = 0;
  173. sts->data = malloc(sizeof(Struct) * sts->capacity);
  174. }
  175. void stsDelete(Structs* sts) {
  176. for(int i = 0; i < sts->entries; i++) {
  177. free(sts->data[i].vars);
  178. }
  179. free(sts->data);
  180. }
  181. static Struct* stsInternSearch(Structs* sts, const char* name) {
  182. for(int i = 0; i < sts->entries; i++) {
  183. if(strcmp(sts->data[i].name, name) == 0) {
  184. return sts->data + i;
  185. }
  186. }
  187. return NULL;
  188. }
  189. Struct* stsSearch(Structs* sts, const char* name) {
  190. if(useGlobals) {
  191. Struct* st = stsInternSearch(&globalStructs, name);
  192. if(st != NULL) {
  193. return st;
  194. }
  195. }
  196. return stsInternSearch(sts, name);
  197. }
  198. Struct* stsAdd(Structs* sts, const char* name) {
  199. if(sts->entries >= sts->capacity) {
  200. sts->capacity *= 2;
  201. sts->data = realloc(sts->data, sizeof(Struct) * sts->capacity);
  202. }
  203. int index = sts->entries++;
  204. sts->data[index].id = index;
  205. sts->data[index].amount = 0;
  206. sts->data[index].name = name;
  207. sts->data[index].vars = NULL;
  208. return sts->data + index;
  209. }
  210. void gstsInit() {
  211. stsInit(&globalStructs);
  212. useGlobals = true;
  213. }
  214. void gstsDelete() {
  215. stsDelete(&globalStructs);
  216. useGlobals = false;
  217. }
  218. Structs* gstsGet() {
  219. return &globalStructs;
  220. }
  221. Struct* gstsAdd(const char* name) {
  222. return stsAdd(&globalStructs, name);
  223. }