Functions.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "utils/Functions.h"
  4. static bool useGlobals = false;
  5. static Functions globalFunctions;
  6. void fInit(Function* f, const char* name, int line) {
  7. f->name = name;
  8. f->arguments = 0;
  9. f->returnType = dtVoid();
  10. f->address = -1;
  11. f->size = 0;
  12. f->line = line;
  13. f->global = false;
  14. f->scriptFunction = NULL;
  15. }
  16. bool fAddArgument(Function* f, DataType type, Structs* sts) {
  17. if(f->arguments >= FUNCTION_ARGUMENTS) {
  18. return true;
  19. }
  20. f->size += dtGetSize(type, sts);
  21. f->argumentTypes[f->arguments++] = type;
  22. return false;
  23. }
  24. static bool fConstCompare(Function* a, Function* b) {
  25. if(a->arguments != b->arguments) {
  26. return false;
  27. }
  28. for(int i = 0; i < a->arguments; i++) {
  29. if(!dtNullCompare(a->argumentTypes[i], b->argumentTypes[i])) {
  30. return false;
  31. }
  32. }
  33. return strcmp(a->name, b->name) == 0;
  34. }
  35. static bool fCompare(Function* a, Function* b) {
  36. if(a->arguments != b->arguments || strcmp(a->name, b->name) != 0) {
  37. return false;
  38. }
  39. for(int i = 0; i < a->arguments; i++) {
  40. DataType dt = b->argumentTypes[i];
  41. if(!dtNullCompare(a->argumentTypes[i], dt) &&
  42. !dtNullCompare(a->argumentTypes[i], dtConst(dt))) {
  43. return false;
  44. }
  45. }
  46. return strcmp(a->name, b->name) == 0;
  47. }
  48. void fsInit(Functions* fs) {
  49. fs->capacity = 16;
  50. fs->entries = 0;
  51. fs->data = malloc(sizeof(Function) * fs->capacity);
  52. }
  53. void fsDelete(Functions* fs) {
  54. free(fs->data);
  55. }
  56. static Function* fsInternSearch(Functions* fs, Function* f, bool constMatch) {
  57. for(int i = 0; i < fs->entries; i++) {
  58. if(fConstCompare(fs->data + i, f)) {
  59. return fs->data + i;
  60. }
  61. }
  62. if(constMatch) {
  63. return NULL;
  64. }
  65. for(int i = 0; i < fs->entries; i++) {
  66. if(fCompare(fs->data + i, f)) {
  67. return fs->data + i;
  68. }
  69. }
  70. return NULL;
  71. }
  72. Function* fsSearch(Functions* fs, Function* f, bool constMatch) {
  73. if(useGlobals) {
  74. Function* gf = fsInternSearch(&globalFunctions, f, constMatch);
  75. if(gf != NULL) {
  76. return gf;
  77. }
  78. }
  79. return fsInternSearch(fs, f, constMatch);
  80. }
  81. void fsAdd(Functions* fs, Function* f) {
  82. if(fs->entries >= fs->capacity) {
  83. fs->capacity *= 2;
  84. fs->data = realloc(fs->data, sizeof(Function) * fs->capacity);
  85. }
  86. fs->data[fs->entries++] = *f;
  87. }
  88. void gfInit(Function* f, const char* name, DataType r, ScriptFunction sf) {
  89. fInit(f, name, -1);
  90. f->returnType = r;
  91. f->scriptFunction = sf;
  92. }
  93. bool gfAddArgument(Function* f, DataType type) {
  94. return fAddArgument(f, type, gstsGet());
  95. }
  96. void gfsInit() {
  97. fsInit(&globalFunctions);
  98. useGlobals = true;
  99. }
  100. bool gfsAdd(Function* f) {
  101. if(fsSearch(&globalFunctions, f, true) != NULL) {
  102. return true;
  103. }
  104. int index = globalFunctions.entries;
  105. fsAdd(&globalFunctions, f);
  106. globalFunctions.data[index].line = index;
  107. globalFunctions.data[index].global = true;
  108. return false;
  109. }
  110. bool gfsCall(Script* sc, int id) {
  111. if(!useGlobals || id < 0 || id >= globalFunctions.entries) {
  112. return true;
  113. }
  114. globalFunctions.data[id].scriptFunction(sc);
  115. return false;
  116. }
  117. void gfsDelete() {
  118. fsDelete(&globalFunctions);
  119. useGlobals = false;
  120. }