#include "list.h" #include #include #include typedef void* any_t; typedef struct list_t { any_t elem; struct list_t *next; } LIST_T; LIST_T* new_list(any_t element) { LIST_T *head = (LIST_T *) malloc(sizeof(LIST_T)); if (head == NULL) { perror("failed to create list: "); exit(1); } head->elem = element; head->next = NULL; return head; } void push_list(LIST_T **head, any_t element) { LIST_T* n_head = (LIST_T *) malloc(sizeof(LIST_T)); if (n_head == NULL) perror("failed to allocate new element: "); n_head->elem = element; n_head->next = *head; *head = n_head; } void append_to_list(LIST_T *head, any_t element) { LIST_T* ptr = head; LIST_T* tail = (LIST_T *) malloc(sizeof(LIST_T)); if (tail == NULL) perror("failed to allocate new element: "); while (ptr->next != NULL) ptr = ptr->next; tail->elem = element; tail->next = NULL; ptr->next = tail; } BLIST* create_list(BIND *binding, BLIST *second) { BLIST *head = (BLIST*) malloc(sizeof(BLIST)); if (head == NULL) { perror("Failed to create list: "); exit(1); } head->binding = binding; head->next = second; return head; } ENV* create_new_function_env(ENV* env) { ENV* n_env = (ENV*) malloc(sizeof(ENV)); if (n_env == NULL) { perror("Failed to create environment"); exit(1); } n_env->bindings = NULL; n_env->parent = env; return n_env; } BIND* create_binding(char* name, NODE* tree, ENV* env_ptr) { BIND* n_bind = (BIND*) malloc(sizeof(BIND)); if (n_bind == NULL) { perror("Failed to create binding: "); exit(1); } n_bind->name = name; n_bind->tree = tree; n_bind->env = env_ptr; return n_bind; } void append_list(BLIST *head, BIND *binding) { BLIST *ptr = head; while (ptr->next != NULL) { ptr = ptr->next; } ptr->next = create_list(binding, NULL); } BIND* find_name_in_list(char* name, BLIST *head) { BLIST *ptr = head; while (ptr != NULL) { if (strcmp(ptr->binding->name, name) == 0) { return ptr->binding; } ptr = ptr->next; } return NULL; } BIND* find_name_in_env(char* name, ENV* env) { BIND* ptr = NULL; ENV* env_ptr = env; while (env_ptr != NULL && (ptr = find_name_in_list(name, env_ptr->bindings)) == NULL) env_ptr = env_ptr->parent; return ptr; } TLIST* new_tac_list(TAC_T* elem, TLIST* second) { TLIST *head = (TLIST*) malloc(sizeof(TLIST)); if (head == NULL) { perror("Failed to create list: "); exit(1); } head->elem = elem; head->next = second; return head; } TAC_T* create_tac(int op, TOKEN* src, TOKEN* tgt, TOKEN* dst) { TAC_T* new_t = (TAC_T*) malloc(sizeof(TAC_T)); if (new_t == NULL) { perror("Failed to create tac: "); exit(1); } new_t->op = op; new_t->src = src; new_t->tgt = tgt; new_t->dst = dst; return new_t; } void add_tac(TLIST *head, int op, TOKEN* src, TOKEN* tgt, TOKEN* dst) { TLIST *ptr = head; while (ptr->next != NULL) { ptr = ptr->next; } ptr->next = (TLIST*) malloc(sizeof(TLIST)); if (ptr->next == NULL) { perror("Failed to create new tac_element: "); exit(1); } ptr->next->elem = create_tac(op, src, tgt, dst); ptr->next->next = NULL; }