DataType.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 void dtAppend(const char* s) {
  10. int index = 0;
  11. while(typeNameIndex < (ARRAY_NAME - 1) && s[index] != '\0') {
  12. typeName[typeNameSwap][typeNameIndex] = s[index];
  13. index++;
  14. typeNameIndex++;
  15. }
  16. typeName[typeNameSwap][typeNameIndex] = '\0';
  17. }
  18. const char* dtGetName(Structs* sts, DataType dt) {
  19. typeNameSwap = !typeNameSwap;
  20. typeNameIndex = 0;
  21. switch(dt.type) {
  22. case DT_INT32: dtAppend("int"); break;
  23. case DT_INT64: dtAppend("long"); break;
  24. case DT_FLOAT: dtAppend("float"); break;
  25. case DT_BOOL: dtAppend("bool"); break;
  26. case DT_NULL: dtAppend("null"); break;
  27. case DT_STRUCT: dtAppend(sts->data[dt.structId].name); break;
  28. case DT_VOID: dtAppend("void"); break;
  29. default: dtAppend("unknown");
  30. }
  31. for(unsigned int i = 0; i < dt.pointers; i++) {
  32. dtAppend("*");
  33. }
  34. return typeName[typeNameSwap];
  35. }
  36. int dtGetSize(DataType dt, Structs* sts) {
  37. if(dt.pointers > 0 || dtIsNull(dt)) {
  38. return sizeof(Pointer);
  39. }
  40. switch(dt.type) {
  41. case DT_INT32: return sizeof(int32);
  42. case DT_INT64: return sizeof(int64);
  43. case DT_FLOAT: return sizeof(float);
  44. case DT_BOOL: return sizeof(bool);
  45. case DT_STRUCT:
  46. {
  47. int size = 0;
  48. Struct* st = sts->data + dt.structId;
  49. for(int i = 0; i < st->amount; i++) {
  50. size += dtGetSize(st->vars[i].type, sts);
  51. }
  52. return size;
  53. }
  54. default: return 0;
  55. }
  56. }
  57. DataType dtInt32() {
  58. DataType dt = {DT_INT32, 0, 0};
  59. return dt;
  60. }
  61. DataType dtInt64() {
  62. DataType dt = {DT_INT64, 0, 0};
  63. return dt;
  64. }
  65. DataType dtFloat() {
  66. DataType dt = {DT_FLOAT, 0, 0};
  67. return dt;
  68. }
  69. DataType dtBool() {
  70. DataType dt = {DT_BOOL, 0, 0};
  71. return dt;
  72. }
  73. DataType dtNull() {
  74. DataType dt = {DT_NULL, 0, 0};
  75. return dt;
  76. }
  77. DataType dtVoid() {
  78. DataType dt = {DT_VOID, 0, 0};
  79. return dt;
  80. }
  81. DataType dtStruct(Struct* st) {
  82. DataType dt = {DT_STRUCT, 0, st->id};
  83. return dt;
  84. }
  85. DataType dtReference(DataType dt) {
  86. dt.pointers--;
  87. return dt;
  88. }
  89. bool dtDereference(DataType* dt) {
  90. if(dt->pointers == 15) {
  91. return true;
  92. }
  93. dt->pointers++;
  94. return false;
  95. }
  96. bool dtCompare(DataType a, DataType b) {
  97. return a.pointers == b.pointers && a.structId == b.structId &&
  98. a.type == b.type;
  99. }
  100. bool dtIsInt32(DataType dt) {
  101. return dtCompare(dt, dtInt32());
  102. }
  103. bool dtIsInt64(DataType dt) {
  104. return dtCompare(dt, dtInt64());
  105. }
  106. bool dtIsFloat(DataType dt) {
  107. return dtCompare(dt, dtFloat());
  108. }
  109. bool dtIsBool(DataType dt) {
  110. return dtCompare(dt, dtBool());
  111. }
  112. bool dtIsNull(DataType dt) {
  113. return dtCompare(dt, dtNull());
  114. }
  115. bool dtIsVoid(DataType dt) {
  116. return dtCompare(dt, dtVoid());
  117. }
  118. bool dtIsPointer(DataType dt) {
  119. return dt.pointers > 0;
  120. }
  121. bool dtIsVariable(DataType dt) {
  122. return dt.type & 8;
  123. }
  124. Struct* dtGetStruct(Structs* sts, DataType dt) {
  125. if(dt.type != DT_STRUCT) {
  126. return NULL;
  127. }
  128. return sts->data + dt.structId;
  129. }
  130. DataType dtToVariable(DataType dt) {
  131. dt.type |= 8;
  132. return dt;
  133. }
  134. bool dtRemoveVariable(DataType* dt) {
  135. if(dtIsVariable(*dt)) {
  136. dt->type &= 7;
  137. return true;
  138. }
  139. return false;
  140. }
  141. void stAddVariable(Struct* st, const char* name, DataType type) {
  142. int index = st->amount;
  143. st->amount++;
  144. st->vars = realloc(st->vars, sizeof(StructVariable) * st->amount);
  145. st->vars[index].name = name;
  146. st->vars[index].type = type;
  147. }
  148. void stsInit(Structs* sts) {
  149. sts->capacity = 4;
  150. sts->entries = 0;
  151. sts->data = malloc(sizeof(Struct) * sts->capacity);
  152. }
  153. void stsDelete(Structs* sts) {
  154. for(int i = 0; i < sts->entries; i++) {
  155. free(sts->data[i].vars);
  156. }
  157. free(sts->data);
  158. }
  159. Struct* stsSearch(Structs* sts, const char* name) {
  160. for(int i = 0; i < sts->entries; i++) {
  161. if(strcmp(sts->data[i].name, name) == 0) {
  162. return sts->data + i;
  163. }
  164. }
  165. return NULL;
  166. }
  167. Struct* stsAdd(Structs* sts, const char* name) {
  168. if(sts->entries >= sts->capacity) {
  169. sts->capacity *= 2;
  170. sts->data = realloc(sts->data, sizeof(Struct) * sts->capacity);
  171. }
  172. int index = sts->entries++;
  173. sts->data[index].id = index;
  174. sts->data[index].amount = 0;
  175. sts->data[index].name = name;
  176. sts->data[index].vars = NULL;
  177. return sts->data + index;
  178. }