Year 2 compilers coureswork

list.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "list.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. typedef void* any_t;
  6. typedef struct list_t {
  7. any_t elem;
  8. struct list_t *next;
  9. } LIST_T;
  10. LIST_T* new_list(any_t element) {
  11. LIST_T *head = (LIST_T *) malloc(sizeof(LIST_T));
  12. if (head == NULL) {
  13. perror("failed to create list: ");
  14. exit(1);
  15. }
  16. head->elem = element;
  17. head->next = NULL;
  18. return head;
  19. }
  20. void push_list(LIST_T **head, any_t element) {
  21. LIST_T* n_head = (LIST_T *) malloc(sizeof(LIST_T));
  22. if (n_head == NULL) perror("failed to allocate new element: ");
  23. n_head->elem = element;
  24. n_head->next = *head;
  25. *head = n_head;
  26. }
  27. void append_to_list(LIST_T *head, any_t element) {
  28. LIST_T* ptr = head;
  29. LIST_T* tail = (LIST_T *) malloc(sizeof(LIST_T));
  30. if (tail == NULL) perror("failed to allocate new element: ");
  31. while (ptr->next != NULL) ptr = ptr->next;
  32. tail->elem = element;
  33. tail->next = NULL;
  34. ptr->next = tail;
  35. }
  36. BLIST* create_list(BIND *binding, BLIST *second) {
  37. BLIST *head = (BLIST*) malloc(sizeof(BLIST));
  38. if (head == NULL) {
  39. perror("Failed to create list: ");
  40. exit(1);
  41. }
  42. head->binding = binding;
  43. head->next = second;
  44. return head;
  45. }
  46. ENV* create_new_function_env(ENV* env) {
  47. ENV* n_env = (ENV*) malloc(sizeof(ENV));
  48. if (n_env == NULL) {
  49. perror("Failed to create environment");
  50. exit(1);
  51. }
  52. n_env->bindings = NULL;
  53. n_env->parent = env;
  54. return n_env;
  55. }
  56. BIND* create_binding(char* name, NODE* tree, ENV* env_ptr) {
  57. BIND* n_bind = (BIND*) malloc(sizeof(BIND));
  58. if (n_bind == NULL) {
  59. perror("Failed to create binding: ");
  60. exit(1);
  61. }
  62. n_bind->name = name;
  63. n_bind->tree = tree;
  64. n_bind->env = env_ptr;
  65. return n_bind;
  66. }
  67. void append_list(BLIST *head, BIND *binding) {
  68. BLIST *ptr = head;
  69. while (ptr->next != NULL) {
  70. ptr = ptr->next;
  71. }
  72. ptr->next = create_list(binding, NULL);
  73. }
  74. BIND* find_name_in_list(char* name, BLIST *head) {
  75. BLIST *ptr = head;
  76. while (ptr != NULL) {
  77. if (strcmp(ptr->binding->name, name) == 0) {
  78. return ptr->binding;
  79. }
  80. ptr = ptr->next;
  81. }
  82. return NULL;
  83. }
  84. BIND* find_name_in_env(char* name, ENV* env) {
  85. BIND* ptr = NULL;
  86. ENV* env_ptr = env;
  87. while (env_ptr != NULL &&
  88. (ptr = find_name_in_list(name, env_ptr->bindings)) == NULL) env_ptr = env_ptr->parent;
  89. return ptr;
  90. }
  91. TLIST* new_tac_list(TAC_T* elem, TLIST* second) {
  92. TLIST *head = (TLIST*) malloc(sizeof(TLIST));
  93. if (head == NULL) {
  94. perror("Failed to create list: ");
  95. exit(1);
  96. }
  97. head->elem = elem;
  98. head->next = second;
  99. return head;
  100. }
  101. TAC_T* create_tac(int op, TOKEN* src, TOKEN* tgt, TOKEN* dst) {
  102. TAC_T* new_t = (TAC_T*) malloc(sizeof(TAC_T));
  103. if (new_t == NULL) {
  104. perror("Failed to create tac: ");
  105. exit(1);
  106. }
  107. new_t->op = op;
  108. new_t->src = src;
  109. new_t->tgt = tgt;
  110. new_t->dst = dst;
  111. return new_t;
  112. }
  113. void add_tac(TLIST *head, int op, TOKEN* src, TOKEN* tgt, TOKEN* dst) {
  114. TLIST *ptr = head;
  115. while (ptr->next != NULL) {
  116. ptr = ptr->next;
  117. }
  118. ptr->next = (TLIST*) malloc(sizeof(TLIST));
  119. if (ptr->next == NULL) {
  120. perror("Failed to create new tac_element: ");
  121. exit(1);
  122. }
  123. ptr->next->elem = create_tac(op, src, tgt, dst);
  124. ptr->next->next = NULL;
  125. }