Пример #1
0
        // pop out the last element
        T pop_back() {

            T e = last->element;

            // remove the first node
            remove_last_node();

            return e;

        }
Пример #2
0
command_t build_command_tree(command_stream* iterate_me) {
  command_stream operator_stack;
  operator_stack.head = NULL;
  operator_stack.tail = NULL;
  command_stream command_stack;
  command_stack.head = NULL;
  command_stack.tail = NULL;

  command_t popped_operator, doncare, right_child_command, left_child_command;
  /*
  1) If it's a simple command, push it onto the command stack
  2) If '(' push on to operator stack
  3) If operator and operator stack is empty, push operator on operator stack
  4) if operator and operator stack not empty, pop all operators with greater or equal precedence
    a) for each popped operator, pop two commands off the command stack
    b) combine them into a new command
    c) push it on the command stack
  5) If encounter ')', pop operators off the stack for each operator, pop 2 commands, do the thing
  6) Advance to next word (simple and or operator and go to #2)
  7) When all words are gone, pop each operator and combine with 2 commands, similar to 4a
  */

  int top_command_type, current_type;
  int operator_precedence[] = {1, // AND
                               0, // SEQ
                               1, // OR
                               2, // PIPE
                               5, // SIMPLE
                               -1, // SUBSHELL_COM
                               -1}; // SUBSHELL OPE

  // printf("initialzing curr...\n");
  command_node* curr = iterate_me->head;
  while (curr != NULL) {
    popped_operator = malloc(3*sizeof(struct command));
    right_child_command = malloc(sizeof(struct command));
    left_child_command = malloc(sizeof(struct command));

    current_type = curr->command->type;
    switch(current_type) {
      case SIMPLE_COMMAND:
      case SUBSHELL_COMMAND:
        // printf("node is simple command\n");
        // print_command(curr->command);
        append_to_list(curr->command, &command_stack);
        break;
      case AND_COMMAND:
      case OR_COMMAND:
      case PIPE_COMMAND:
      case SEQUENCE_COMMAND:
        // printf("node is operator\n");
        //if the operator stack is empty
        if (operator_stack.head == NULL) {
          append_to_list(curr->command, &operator_stack);
        }
        // not empty
        else {
          top_command_type = operator_stack.tail->command->type;
          while (operator_precedence[current_type] <= operator_precedence[top_command_type] && 
                 operator_stack.head != NULL) {
            remove_last_node(&operator_stack, &popped_operator);
            remove_last_node(&command_stack, &right_child_command);
            remove_last_node(&command_stack, &left_child_command);

            if (left_child_command->type == SIMPLE_COMMAND){
              if (left_child_command->u.word[0] == '\0'){
                error(1,0, "incomplete command");
              }
            }

            if (right_child_command->type == SIMPLE_COMMAND){
              if (right_child_command->u.word[0] == '\0'){
                error(1,0, "incomplete command");
              }
            }

            // printf("left child command\n");
            // print_command(left_child_command);
            popped_operator->u.command[0] = left_child_command;
            popped_operator->u.command[1] = right_child_command;
            // print_command(popped_operator);
            append_to_list(popped_operator, &command_stack);
            if (operator_stack.head != NULL) {
              top_command_type = operator_stack.tail->command->type;
            }
          }
          append_to_list(curr->command, &operator_stack);
        }
        break; //end of operator case
    }


    curr = curr->next;
  }//end of reading 

  while (operator_stack.head!=NULL){
    remove_last_node(&operator_stack, &popped_operator);
    remove_last_node(&command_stack, &right_child_command);
    remove_last_node(&command_stack, &left_child_command);
    // print_command(left_child_command);
    // print_command(right_child_command);

    if (left_child_command->type == SIMPLE_COMMAND){
      if (left_child_command->u.word[0] == '\0'){
        error(1,0, "incomplete command");
      }
    }
    if (right_child_command->type == SIMPLE_COMMAND){
      if (right_child_command->u.word[0] == '\0'){
        error(1,0, "incomplete command");
      }
    }

    popped_operator->u.command[0] = left_child_command;
    popped_operator->u.command[1] = right_child_command;
    append_to_list(popped_operator, &command_stack);
  }
  // check and make sure tail command has words
  
  return command_stack.tail->command;
}