DataType.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. const char* vtGetName(ValueType vt) {
  12. switch(vt) {
  13. case VT_INVALID: return "invalid";
  14. case VT_NOT_SET: return "?";
  15. case VT_INT: return "int";
  16. case VT_FLOAT: return "float";
  17. case VT_POINTER: return "pointer";
  18. case VT_ARRAY: return "array";
  19. case VT_LAST: return "invalid";
  20. }
  21. return "invalid";
  22. }
  23. #define VALUE_TYPE_MASK ((1u << VALUE_TYPE_BIT_SIZE) - 1u)
  24. #define VALUE_MAX_OFFSET ((1u << (32 - VALUE_TYPE_BIT_SIZE)) - 1u)
  25. ValueType vGetType(Value v) {
  26. uint32 type = v.typeAndOffset & VALUE_TYPE_MASK;
  27. if(type >= VT_LAST) {
  28. return VT_INVALID;
  29. }
  30. return (ValueType)type;
  31. }
  32. bool vSetType(Value* v, ValueType vt) {
  33. if(v == NULL || vt < 0 || vt >= VT_LAST) {
  34. return true;
  35. }
  36. v->typeAndOffset = (v->typeAndOffset & ~VALUE_TYPE_MASK) | vt;
  37. return false;
  38. }
  39. uint32 vGetOffset(Value v) {
  40. return v.typeAndOffset >> VALUE_TYPE_BIT_SIZE;
  41. }
  42. bool vSetOffset(Value* v, uint32 offset) {
  43. if(v == NULL || offset > VALUE_MAX_OFFSET) {
  44. return true;
  45. }
  46. v->typeAndOffset =
  47. (v->typeAndOffset & VALUE_TYPE_MASK) | (offset << VALUE_TYPE_BIT_SIZE);
  48. return false;
  49. }
  50. static void dtAppend(const char* s) {
  51. int index = 0;
  52. while(typeNameIndex < (ARRAY_NAME - 1) && s[index] != '\0') {
  53. typeName[typeNameSwap][typeNameIndex] = s[index];
  54. index++;
  55. typeNameIndex++;
  56. }
  57. typeName[typeNameSwap][typeNameIndex] = '\0';
  58. }
  59. const char* dtGetName(const Structs* sts, DataType dt) {
  60. typeNameSwap = !typeNameSwap;
  61. typeNameIndex = 0;
  62. switch(dtGetType(dt)) {
  63. case RDT_INT: dtAppend("int"); break;
  64. case RDT_FLOAT: dtAppend("float"); break;
  65. case RDT_STRUCT: {
  66. Struct* s = dtGetStruct(sts, dt);
  67. if(s != NULL) {
  68. dtAppend(s->name);
  69. } else {
  70. dtAppend("?-struct");
  71. }
  72. break;
  73. }
  74. case RDT_VOID: dtAppend("void"); break;
  75. case RDT_INVALID: dtAppend("invalid"); break;
  76. default: dtAppend("?");
  77. }
  78. if(dtIsArray(dt)) {
  79. dtAppend("[]");
  80. }
  81. if(dtIsPointer(dt)) {
  82. dtAppend("*");
  83. }
  84. return typeName[typeNameSwap];
  85. }
  86. int dtGetSize(DataType dt, const Structs* sts) {
  87. if(dtIsPointer(dt)) {
  88. return 1;
  89. }
  90. switch(dtGetType(dt)) {
  91. case RDT_INT: return 1;
  92. case RDT_FLOAT: return 1;
  93. case RDT_STRUCT: {
  94. int size = 0;
  95. Struct* st = dtGetStruct(sts, dt);
  96. if(st == NULL) {
  97. return 0;
  98. }
  99. for(int i = 0; i < st->amount; i++) {
  100. size += dtGetSize(st->vars[i].type, sts);
  101. }
  102. return size;
  103. }
  104. case RDT_LAST:
  105. case RDT_VOID:
  106. case RDT_INVALID: return 0;
  107. }
  108. return 0;
  109. }
  110. static DataType dtBuild(RawDataType rdt, bool isPointer, bool isArray,
  111. int32 structId) {
  112. if(rdt < 0 || rdt >= RDT_LAST || structId < 0 ||
  113. structId > DT_MAX_STRUCT_ID) {
  114. return (DataType){RDT_INVALID};
  115. }
  116. return (DataType){((uint32)rdt << DT_TYPE_OFFSET) |
  117. ((uint32)isPointer << DT_IS_POINTER_OFFSET) |
  118. ((uint32)isArray << DT_IS_ARRAY_OFFSET) |
  119. ((uint32)structId << DT_STRUCT_ID_OFFSET)};
  120. }
  121. DataType dtInt(void) {
  122. return dtBuild(RDT_INT, false, false, 0);
  123. }
  124. DataType dtFloat(void) {
  125. return dtBuild(RDT_FLOAT, false, false, 0);
  126. }
  127. DataType dtVoid(void) {
  128. return dtBuild(RDT_VOID, false, false, 0);
  129. }
  130. DataType dtStruct(const Struct* st) {
  131. return dtBuild(RDT_STRUCT, false, false, st->id);
  132. }
  133. DataType dtText(void) {
  134. return dtBuild(RDT_INT, false, true, 0);
  135. }
  136. DataType dtToArray(DataType dt) {
  137. dt.data |= 1 << DT_IS_ARRAY_OFFSET;
  138. return dt;
  139. }
  140. DataType dtRemoveArray(DataType dt) {
  141. dt.data &= ~(((1u << DT_IS_ARRAY_BIT_SIZE) - 1u) << DT_IS_ARRAY_OFFSET);
  142. return dt;
  143. }
  144. DataType dtToPointer(DataType dt) {
  145. dt.data |= 1 << DT_IS_POINTER_OFFSET;
  146. return dt;
  147. }
  148. bool dtRemovePointer(DataType* dt) {
  149. if(dtIsPointer(*dt)) {
  150. dt->data &=
  151. ~(((1u << DT_IS_POINTER_BIT_SIZE) - 1u) << DT_IS_POINTER_OFFSET);
  152. return true;
  153. }
  154. return false;
  155. }
  156. RawDataType dtGetType(DataType dt) {
  157. uint32 type = dt.data & (((1u << DT_TYPE_BIT_SIZE) - 1u) << DT_TYPE_OFFSET);
  158. if(type >= RDT_LAST) {
  159. return RDT_INVALID;
  160. }
  161. return (RawDataType)type;
  162. }
  163. bool dtCompare(DataType a, DataType b) {
  164. return a.data == b.data;
  165. }
  166. bool dtIsInt(DataType dt) {
  167. return dtCompare(dt, dtInt());
  168. }
  169. bool dtIsFloat(DataType dt) {
  170. return dtCompare(dt, dtFloat());
  171. }
  172. bool dtIsVoid(DataType dt) {
  173. return dtCompare(dt, dtVoid());
  174. }
  175. bool dtIsArray(DataType dt) {
  176. return dt.data &
  177. (((1u << DT_IS_ARRAY_BIT_SIZE) - 1u) << DT_IS_ARRAY_OFFSET);
  178. }
  179. bool dtIsStruct(DataType dt) {
  180. return dtGetType(dt) == RDT_STRUCT;
  181. }
  182. bool dtIsPointer(DataType dt) {
  183. return dt.data &
  184. (((1u << DT_IS_POINTER_BIT_SIZE) - 1u) << DT_IS_POINTER_OFFSET);
  185. }
  186. Struct* dtGetStruct(const Structs* sts, DataType dt) {
  187. if(!dtIsStruct(dt)) {
  188. return NULL;
  189. }
  190. int32 structId = (int32)((dt.data >> DT_STRUCT_ID_OFFSET) &
  191. ((1u << DT_STRUCT_ID_BIT_SIZE) - 1u));
  192. if(structId & 1) {
  193. if(!useGlobals) {
  194. return NULL;
  195. }
  196. structId >>= 1;
  197. if(structId < 0 || structId >= globalStructs.entries) {
  198. return NULL;
  199. }
  200. return globalStructs.data + structId;
  201. }
  202. structId >>= 1;
  203. if(structId < 0 || structId >= sts->entries) {
  204. return NULL;
  205. }
  206. return sts->data + structId;
  207. }
  208. void stAddVariable(Struct* st, const char* name, DataType type) {
  209. int index = st->amount;
  210. st->amount++;
  211. st->vars = (StructVariable*)realloc(st->vars, sizeof(StructVariable) *
  212. (size_t)st->amount);
  213. st->vars[index].name = name;
  214. st->vars[index].type = type;
  215. }
  216. void stsInit(Structs* sts) {
  217. sts->capacity = 4;
  218. sts->entries = 0;
  219. sts->data = (Struct*)malloc(sizeof(Struct) * (size_t)sts->capacity);
  220. }
  221. void stsDelete(Structs* sts) {
  222. for(int i = 0; i < sts->entries; i++) {
  223. free(sts->data[i].vars);
  224. }
  225. free(sts->data);
  226. }
  227. static Struct* stsInternSearch(Structs* sts, const char* name) {
  228. for(int i = 0; i < sts->entries; i++) {
  229. if(strcmp(sts->data[i].name, name) == 0) {
  230. return sts->data + i;
  231. }
  232. }
  233. return NULL;
  234. }
  235. Struct* stsSearch(Structs* sts, const char* name) {
  236. if(useGlobals) {
  237. Struct* st = stsInternSearch(&globalStructs, name);
  238. if(st != NULL) {
  239. return st;
  240. }
  241. }
  242. return stsInternSearch(sts, name);
  243. }
  244. Struct* stsAdd(Structs* sts, const char* name) {
  245. if(sts->entries >= sts->capacity) {
  246. sts->capacity *= 2;
  247. sts->data =
  248. (Struct*)realloc(sts->data, sizeof(Struct) * (size_t)sts->capacity);
  249. }
  250. uint16 index = (uint16)sts->entries++;
  251. sts->data[index].id = (uint16)(index << 1);
  252. sts->data[index].amount = 0;
  253. sts->data[index].name = name;
  254. sts->data[index].vars = NULL;
  255. return sts->data + index;
  256. }
  257. void gstsInit(void) {
  258. stsInit(&globalStructs);
  259. useGlobals = true;
  260. }
  261. void gstsDelete(void) {
  262. stsDelete(&globalStructs);
  263. useGlobals = false;
  264. }
  265. Structs* gstsGet(void) {
  266. return &globalStructs;
  267. }
  268. Struct* gstsAdd(const char* name) {
  269. Struct* st = stsAdd(&globalStructs, name);
  270. st->id |= 1;
  271. return st;
  272. }