Functions.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, int16 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. int add;
  21. if(dtGetSize(type, sts, &add)) {
  22. return true;
  23. }
  24. f->size += add;
  25. f->argumentTypes[f->arguments++] = type;
  26. return false;
  27. }
  28. static bool fCompare(Function* a, Function* b) {
  29. if(a->arguments != b->arguments || strcmp(a->name, b->name) != 0) {
  30. return false;
  31. }
  32. for(int i = 0; i < a->arguments; i++) {
  33. if(!dtCompare(a->argumentTypes[i], b->argumentTypes[i])) {
  34. return false;
  35. }
  36. }
  37. return strcmp(a->name, b->name) == 0;
  38. }
  39. void fsInit(Functions* fs) {
  40. fs->capacity = 16;
  41. fs->entries = 0;
  42. fs->data = (Function*)malloc(sizeof(Function) * (size_t)fs->capacity);
  43. }
  44. void fsDelete(Functions* fs) {
  45. free(fs->data);
  46. }
  47. static Function* fsInternSearch(Functions* fs, Function* f) {
  48. for(int i = 0; i < fs->entries; i++) {
  49. if(fCompare(fs->data + i, f)) {
  50. return fs->data + i;
  51. }
  52. }
  53. return NULL;
  54. }
  55. Function* fsSearch(Functions* fs, Function* f) {
  56. if(useGlobals) {
  57. Function* gf = fsInternSearch(&globalFunctions, f);
  58. if(gf != NULL) {
  59. return gf;
  60. }
  61. }
  62. return fsInternSearch(fs, f);
  63. }
  64. void fsAdd(Functions* fs, Function* f) {
  65. if(fs->entries >= fs->capacity) {
  66. fs->capacity *= 2;
  67. fs->data = (Function*)realloc(fs->data,
  68. sizeof(Function) * (size_t)fs->capacity);
  69. }
  70. fs->data[fs->entries++] = *f;
  71. }
  72. void gfInit(Function* f, const char* name, DataType r, ScriptFunction sf) {
  73. fInit(f, name, -1);
  74. f->returnType = r;
  75. f->scriptFunction = sf;
  76. }
  77. bool gfAddArgument(Function* f, DataType type) {
  78. return fAddArgument(f, type, gstsGet());
  79. }
  80. void gfsInit(void) {
  81. fsInit(&globalFunctions);
  82. useGlobals = true;
  83. }
  84. bool gfsAdd(Function* f) {
  85. if(fsSearch(&globalFunctions, f) != NULL) {
  86. return true;
  87. }
  88. int index = globalFunctions.entries;
  89. fsAdd(&globalFunctions, f);
  90. globalFunctions.data[index].line = (int16)index;
  91. globalFunctions.data[index].global = true;
  92. return false;
  93. }
  94. bool gfsCall(Script* sc, int id) {
  95. if(!useGlobals || id < 0 || id >= globalFunctions.entries) {
  96. return true;
  97. }
  98. globalFunctions.data[id].scriptFunction(sc);
  99. return false;
  100. }
  101. void gfsDelete(void) {
  102. fsDelete(&globalFunctions);
  103. useGlobals = false;
  104. }