DataType.c 4.1 KB

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