DataType.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 dtText() {
  78. DataType dt = {DT_INT32, 1, 0};
  79. return dt;
  80. }
  81. DataType dtVoid() {
  82. DataType dt = {DT_VOID, 0, 0};
  83. return dt;
  84. }
  85. DataType dtStruct(Struct* st) {
  86. DataType dt = {DT_STRUCT, 0, st->id};
  87. return dt;
  88. }
  89. DataType dtReference(DataType dt) {
  90. dt.pointers--;
  91. return dt;
  92. }
  93. bool dtDereference(DataType* dt) {
  94. if(dt->pointers == 15) {
  95. return true;
  96. }
  97. dt->pointers++;
  98. return false;
  99. }
  100. bool dtCompare(DataType a, DataType b) {
  101. return a.pointers == b.pointers && a.structId == b.structId &&
  102. a.type == b.type;
  103. }
  104. bool dtIsInt32(DataType dt) {
  105. return dtCompare(dt, dtInt32());
  106. }
  107. bool dtIsInt64(DataType dt) {
  108. return dtCompare(dt, dtInt64());
  109. }
  110. bool dtIsFloat(DataType dt) {
  111. return dtCompare(dt, dtFloat());
  112. }
  113. bool dtIsBool(DataType dt) {
  114. return dtCompare(dt, dtBool());
  115. }
  116. bool dtIsNull(DataType dt) {
  117. return dtCompare(dt, dtNull());
  118. }
  119. bool dtIsVoid(DataType dt) {
  120. return dtCompare(dt, dtVoid());
  121. }
  122. bool dtIsPointer(DataType dt) {
  123. return dt.pointers > 0;
  124. }
  125. bool dtIsVariable(DataType dt) {
  126. return dt.type & 8;
  127. }
  128. Struct* dtGetStruct(Structs* sts, DataType dt) {
  129. if(dt.type != DT_STRUCT) {
  130. return NULL;
  131. }
  132. return sts->data + dt.structId;
  133. }
  134. DataType dtToVariable(DataType dt) {
  135. dt.type |= 8;
  136. return dt;
  137. }
  138. bool dtRemoveVariable(DataType* dt) {
  139. if(dtIsVariable(*dt)) {
  140. dt->type &= 7;
  141. return true;
  142. }
  143. return false;
  144. }
  145. void stAddVariable(Struct* st, const char* name, DataType type) {
  146. int index = st->amount;
  147. st->amount++;
  148. st->vars = realloc(st->vars, sizeof(StructVariable) * st->amount);
  149. st->vars[index].name = name;
  150. st->vars[index].type = type;
  151. }
  152. void stsInit(Structs* sts) {
  153. sts->capacity = 4;
  154. sts->entries = 0;
  155. sts->data = malloc(sizeof(Struct) * sts->capacity);
  156. }
  157. void stsDelete(Structs* sts) {
  158. for(int i = 0; i < sts->entries; i++) {
  159. free(sts->data[i].vars);
  160. }
  161. free(sts->data);
  162. }
  163. Struct* stsSearch(Structs* sts, const char* name) {
  164. for(int i = 0; i < sts->entries; i++) {
  165. if(strcmp(sts->data[i].name, name) == 0) {
  166. return sts->data + i;
  167. }
  168. }
  169. return NULL;
  170. }
  171. Struct* stsAdd(Structs* sts, const char* name) {
  172. if(sts->entries >= sts->capacity) {
  173. sts->capacity *= 2;
  174. sts->data = realloc(sts->data, sizeof(Struct) * sts->capacity);
  175. }
  176. int index = sts->entries++;
  177. sts->data[index].id = index;
  178. sts->data[index].amount = 0;
  179. sts->data[index].name = name;
  180. sts->data[index].vars = NULL;
  181. return sts->data + index;
  182. }