DataType.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "DataType.h"
  4. #define ARRAY_NAME 20
  5. #define ARRAY_FACTOR 10
  6. static char arrayName[ARRAY_NAME];
  7. static const char* dtGetBaseName(unsigned int type) {
  8. switch(type) {
  9. case DT_INT: return "int";
  10. case DT_FLOAT: return "float";
  11. case DT_BOOL: return "bool";
  12. default: return "";
  13. }
  14. }
  15. static const char* dtGetArrayName(DataType dt) {
  16. int dimension = dt.pointers;
  17. const char* name = dtGetBaseName(dt.type);
  18. if(name[0] == '\0' || dimension <= 0 || dimension > dtMaxDimensions()) {
  19. return "unknown";
  20. }
  21. int index = 0;
  22. while(index < (ARRAY_NAME - 1) && name[index] != '\0') {
  23. arrayName[index] = name[index];
  24. index++;
  25. }
  26. while(index < (ARRAY_NAME - 1) && dimension > 0) {
  27. arrayName[index] = '*';
  28. index++;
  29. dimension--;
  30. }
  31. arrayName[index] = '\0';
  32. return arrayName;
  33. }
  34. const char* dtGetName(Structs* sts, DataType dt) {
  35. if(dt.structId > 0) {
  36. return sts->data[dt.structId - 1].name;
  37. } else if(dt.pointers > 0) {
  38. return dtGetArrayName(dt);
  39. }
  40. return dtGetBaseName(dt.type);
  41. }
  42. int dtGetSize(DataType dt) {
  43. switch(dtAsInt(dt)) {
  44. case DT_INT: return sizeof(int);
  45. case DT_FLOAT: return sizeof(float);
  46. case DT_BOOL: return sizeof(bool);
  47. default:
  48. if(dt.structId > 0) {
  49. return 0;
  50. }
  51. return sizeof(int);
  52. }
  53. }
  54. DataType dtInt() {
  55. DataType dt = {DT_INT, 0, 0, 0};
  56. return dt;
  57. }
  58. DataType dtFloat() {
  59. DataType dt = {DT_FLOAT, 0, 0, 0};
  60. return dt;
  61. }
  62. DataType dtBool() {
  63. DataType dt = {DT_BOOL, 0, 0, 0};
  64. return dt;
  65. }
  66. DataType dtVoid() {
  67. DataType dt = {DT_VOID, 0, 0, 0};
  68. return dt;
  69. }
  70. DataType dtStruct(Struct* st) {
  71. DataType dt = {DT_STRUCT, 0, 0, st->id};
  72. return dt;
  73. }
  74. DataType dtToReference(DataType dt) {
  75. dt.reference = 1;
  76. return dt;
  77. }
  78. DataType dtToArray(DataType dt, int dimension) {
  79. dt.pointers = dimension;
  80. return dt;
  81. }
  82. bool dtCompare(DataType a, DataType b) {
  83. return dtAsInt(a) == dtAsInt(b);
  84. }
  85. int dtMaxDimensions() {
  86. return 15;
  87. }
  88. unsigned int dtAsInt(DataType dt) {
  89. unsigned int i;
  90. memcpy(&i, &dt, sizeof(dt));
  91. return i;
  92. }
  93. bool dtIsArray(DataType dt) {
  94. return dt.pointers > 0;
  95. }
  96. void stAddVariable(Struct* st, const char* name, DataType type) {
  97. int index = st->amount;
  98. st->amount++;
  99. st->vars = realloc(st->vars, sizeof(StructVariable) * st->amount);
  100. st->vars[index].name = name;
  101. st->vars[index].type = type;
  102. }
  103. void stsInit(Structs* sts) {
  104. sts->capacity = 4;
  105. sts->entries = 0;
  106. sts->data = malloc(sizeof(Struct) * sts->capacity);
  107. }
  108. void stsDelete(Structs* sts) {
  109. for(int i = 0; i < sts->entries; i++) {
  110. free(sts->data[i].vars);
  111. }
  112. free(sts->data);
  113. }
  114. Struct* stsSearch(Structs* sts, const char* name) {
  115. for(int i = 0; i < sts->entries; i++) {
  116. if(strcmp(sts->data[i].name, name) == 0) {
  117. return sts->data + i;
  118. }
  119. }
  120. return NULL;
  121. }
  122. Struct* stsAdd(Structs* sts, const char* name) {
  123. if(sts->entries >= sts->capacity) {
  124. sts->capacity *= 2;
  125. sts->data = realloc(sts->data, sizeof(Struct) * sts->capacity);
  126. }
  127. int index = sts->entries++;
  128. sts->data[index].id = index;
  129. sts->data[index].amount = 0;
  130. sts->data[index].name = name;
  131. sts->data[index].vars = NULL;
  132. return sts->data + index;
  133. }