Functions.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "utils/Functions.h"
  4. void fInit(Function* f, const char* name, int line) {
  5. f->name = name;
  6. f->arguments = 0;
  7. f->returnType = DT_VOID;
  8. f->address = -1;
  9. f->size = 0;
  10. f->line = line;
  11. }
  12. bool fAddArgument(Function* f, DataType type) {
  13. if(f->arguments >= FUNCTION_ARGUMENTS) {
  14. return true;
  15. }
  16. f->size += dtGetSize(type);
  17. f->argumentTypes[f->arguments++] = type;
  18. return false;
  19. }
  20. bool fCompare(Function* a, Function* b) {
  21. if(a->arguments != b->arguments) {
  22. return false;
  23. }
  24. for(int i = 0; i < a->arguments; i++) {
  25. if(a->argumentTypes[i] != b->argumentTypes[i]) {
  26. return false;
  27. }
  28. }
  29. return strcmp(a->name, b->name) == 0;
  30. }
  31. void fsInit(Functions* fs) {
  32. fs->capacity = 16;
  33. fs->entries = 0;
  34. fs->data = malloc(sizeof(Function) * fs->capacity);
  35. }
  36. void fsDelete(Functions* fs) {
  37. free(fs->data);
  38. }
  39. Function* fsSearch(Functions* fs, Function* f) {
  40. for(int i = 0; i < fs->entries; i++) {
  41. if(fCompare(fs->data + i, f)) {
  42. return fs->data + i;
  43. }
  44. }
  45. return NULL;
  46. }
  47. void fsAdd(Functions* fs, Function* f) {
  48. if(fs->entries >= fs->capacity) {
  49. fs->capacity *= 2;
  50. fs->data = realloc(fs->data, sizeof(Function) * fs->capacity);
  51. }
  52. fs->data[fs->entries++] = *f;
  53. }