Functions.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. bool fCompare(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(!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 = malloc(sizeof(Function) * 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 = realloc(fs->data, sizeof(Function) * fs->capacity);
  64. }
  65. fs->data[fs->entries++] = *f;
  66. }
  67. void gfInit(Function* f, const char* name, DataType r, ScriptFunction sf) {
  68. fInit(f, name, -1);
  69. f->returnType = r;
  70. f->scriptFunction = sf;
  71. }
  72. void gfsInit() {
  73. fsInit(&globalFunctions);
  74. useGlobals = true;
  75. }
  76. bool gfsAdd(Function* f) {
  77. if(fsSearch(&globalFunctions, f) != NULL) {
  78. return true;
  79. }
  80. int index = globalFunctions.entries;
  81. fsAdd(&globalFunctions, f);
  82. globalFunctions.data[index].line = index;
  83. globalFunctions.data[index].global = true;
  84. return false;
  85. }
  86. bool gfsCall(Script* sc, int id) {
  87. if(!useGlobals || id < 0 || id >= globalFunctions.entries) {
  88. return true;
  89. }
  90. globalFunctions.data[id].scriptFunction(sc);
  91. return false;
  92. }
  93. void gfsDelete() {
  94. fsDelete(&globalFunctions);
  95. useGlobals = false;
  96. }