spacepaste

  1.  
  2. // A utility function to create an empty queue
  3. struct Queue *createQueue()
  4. {
  5. struct Queue *q = (struct Queue*)malloc(sizeof(*q));
  6. q->front = q->rear = NULL;
  7. return q;
  8. }
  9. int prev_type = 0;
  10. int curr_type = 0;
  11. void store_asm(struct Queue *q, int data, int type)
  12. {
  13. if (curr_type) prev_type = curr_type;
  14. curr_type = type;
  15. // Create a new LL node
  16. int t = 0;
  17. if (type == NODE_TYPE_NORMAL) t = STORE_NORMAL;
  18. else if (
  19. type == NODE_TYPE_BRANCH_START
  20. ||
  21. type == NODE_TYPE_BRANCH_END
  22. ||
  23. type == NODE_TYPE_BRANCH_CHOICE_START
  24. ||
  25. type == NODE_TYPE_BRANCH_CHOICE_END
  26. ) t = STORE_EMPTY;
  27. struct QNode *temp = newNode(data, t);
  28. temp->type = type;
  29. // If queue is empty, then new node is front and rear both
  30. if (q->rear == NULL)
  31. {
  32. temp->prev = NULL;
  33. q->front = q->rear = temp;
  34. return;
  35. }
  36. if (prev_type == NODE_TYPE_NORMAL) {
  37. if (type == NODE_TYPE_BRANCH_CHOICE_END) {
  38. // we reach a the end of a branch choice, add the address of the last reference, the pointer to the next node, to an array of pointers
  39. // last_ref = q->rear->ref;
  40. // printf("adding %d to the pointer list\n", last_ref);
  41. // ptr_add(&q->rear->ref);
  42. }
  43. }
  44. else if(type == NODE_TYPE_NORMAL && prev_type == NODE_TYPE_BRANCH_END) {
  45. // we reach a node and we where previously at a branch, set all referenced logged to the value of the current node reference, then we free the pointer list
  46. // printf("setting pointer list to %d\n", last_ref);
  47. // ptr_set(last_ref);
  48. // ptr_free();
  49. }
  50. // Add the new node at the end of queue and change rear
  51. temp->prev = q->rear;
  52. // allocate a new array for our new item
  53. #if NODE_TYPE == LINKED
  54. if (!q->rear->next) {
  55. q->rear->next = malloc(sizeof(*q->rear->next));
  56. } else {
  57. abort();
  58. }
  59. // update rear->next to point to the new item
  60. q->rear->next = temp;
  61. // update rear to point to the rear->next, which is the item we just added, updating the pointer rear from our old item to our new item
  62. q->rear = q->rear->next;
  63. #elif NODE_TYPE == TREE
  64. struct QNode * p = q->rear;
  65. if (type == NODE_TYPE_BRANCH_CHOICE_START) {
  66. while(p) {
  67. if (p->type == NODE_TYPE_BRANCH_START) {
  68. puts("FOUND BRANCH START");
  69. q->rear = p;
  70. break;
  71. }
  72. if (p->prev) p = p->prev;
  73. else p = NULL;
  74. }
  75. }
  76. // allocate a new array for our new item
  77. if (!q->rear->branch) {
  78. q->rear->branch_count = 1;
  79. q->rear->branch = malloc(sizeof(struct QNode)*q->rear->branch_count);
  80. } else {
  81. q->rear->branch_count++;
  82. q->rear->branch = realloc(q->rear->branch, sizeof(struct QNode)*q->rear->branch_count);
  83. }
  84. // update rear->next to point to the new item
  85. q->rear->branch[q->rear->branch_count-1] = temp;
  86. // update rear to point to the rear->next, which is the item we just added, updating the pointer rear from our old item to our new item
  87. q->rear = q->rear->branch[q->rear->branch_count-1];
  88. #endif
  89. }
  90. void add(struct Queue **q, int data) {
  91. if (!(*q)) (*q) = createQueue();
  92. store_asm((*q), data, NODE_TYPE_NORMAL);
  93. }
  94. void add_branch(struct Queue **q) {
  95. if (!(*q)) (*q) = createQueue();
  96. store_asm((*q), 0, NODE_TYPE_BRANCH_START);
  97. global_indent++;
  98. }
  99. void end_branch(struct Queue **q) {
  100. global_indent--;
  101. if (!(*q)) (*q) = createQueue();
  102. store_asm((*q), 0, NODE_TYPE_BRANCH_END);
  103. }
  104. void add_branch_choice(struct Queue **q) {
  105. if (!(*q)) (*q) = createQueue();
  106. store_asm((*q), 0, NODE_TYPE_BRANCH_CHOICE_START);
  107. global_indent++;
  108. }
  109. void end_branch_choice(struct Queue **q) {
  110. global_indent--;
  111. if (!(*q)) (*q) = createQueue();
  112. // if we have a branch_end, we need to go backwards in out linked list to the node that came before the branch start
  113. store_asm((*q), 0, NODE_TYPE_BRANCH_CHOICE_END);
  114. }
  115.