Year 2 compilers coureswork

list.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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, ENV* prenv) {
  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. n_env->counter = 0;
  55. n_env->param_counter = 0;
  56. n_env->arg_counter = 0;
  57. n_env->previous = prenv;
  58. return n_env;
  59. }
  60. BIND* create_binding(TOKEN* name, int sp_offset, ENV* env_ptr) {
  61. BIND* n_bind = (BIND*) malloc(sizeof(BIND));
  62. if (n_bind == NULL) {
  63. perror("Failed to create binding: ");
  64. exit(1);
  65. }
  66. n_bind->name = name;
  67. n_bind->env_offset = sp_offset;
  68. n_bind->env = env_ptr;
  69. n_bind->is_saved = 0;
  70. return n_bind;
  71. }
  72. void append_list(BLIST *head, BIND *binding) {
  73. BLIST *ptr = head;
  74. while (ptr->next != NULL) {
  75. ptr = ptr->next;
  76. }
  77. ptr->next = create_list(binding, NULL);
  78. }
  79. BIND* find_name_in_list(TOKEN* name, BLIST *head) {
  80. BLIST *ptr = head;
  81. while (ptr != NULL) {
  82. if (name == ptr->binding->name) {
  83. return ptr->binding;
  84. }
  85. ptr = ptr->next;
  86. }
  87. return NULL;
  88. }
  89. BIND* find_name_in_env(TOKEN* name, ENV* env) {
  90. BIND* ptr = NULL;
  91. ENV* env_ptr = env;
  92. while (env_ptr != NULL &&
  93. (ptr = find_name_in_list(name, env_ptr->bindings)) == NULL) env_ptr = env_ptr->parent;
  94. return ptr;
  95. }
  96. TLIST* new_tac_list(TAC_T* elem, TLIST* second) {
  97. TLIST *head = (TLIST*) malloc(sizeof(TLIST));
  98. if (head == NULL) {
  99. perror("Failed to create list: ");
  100. exit(1);
  101. }
  102. head->elem = elem;
  103. head->next = second;
  104. return head;
  105. }
  106. TAC_T* create_tac(int op, TOKEN* src, TOKEN* tgt, TOKEN* dst) {
  107. TAC_T* new_t = (TAC_T*) malloc(sizeof(TAC_T));
  108. if (new_t == NULL) {
  109. perror("Failed to create tac: ");
  110. exit(1);
  111. }
  112. new_t->op = op;
  113. new_t->src = src;
  114. new_t->tgt = tgt;
  115. new_t->dst = dst;
  116. return new_t;
  117. }
  118. void add_tac(TLIST *head, int op, TOKEN* src, TOKEN* tgt, TOKEN* dst) {
  119. TLIST *ptr = head;
  120. while (ptr->next != NULL) {
  121. ptr = ptr->next;
  122. }
  123. ptr->next = (TLIST*) malloc(sizeof(TLIST));
  124. if (ptr->next == NULL) {
  125. perror("Failed to create new tac_element: ");
  126. exit(1);
  127. }
  128. ptr->next->elem = create_tac(op, src, tgt, dst);
  129. ptr->next->next = NULL;
  130. }