Functions.c 2.7 KB

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