| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #include "list.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- 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* prenv) {
- 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;
- n_env->counter = 0;
- n_env->param_counter = 0;
- n_env->arg_counter = 0;
- n_env->previous = prenv;
- return n_env;
- }
- BIND* create_binding(TOKEN* name, int sp_offset, 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->env_offset = sp_offset;
- n_bind->env = env_ptr;
- n_bind->is_saved = 0;
- 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(TOKEN* name, BLIST *head) {
- BLIST *ptr = head;
- while (ptr != NULL) {
- if (name == ptr->binding->name) {
- return ptr->binding;
- }
- ptr = ptr->next;
- }
- return NULL;
- }
- BIND* find_name_in_env(TOKEN* 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 append_tac(TLIST *head, TLIST *tac) {
- TLIST *ptr = head;
- while (ptr->next != NULL) {
- ptr = ptr->next;
- }
- ptr->next = tac;
- }
- 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;
- }
|