コード例 #1
0
ファイル: ir-builder.cpp プロジェクト: abhiskaushik/hhvm
void IRBuilder::startBlock(Block* block) {
  assert(block);
  assert(m_savedBlocks.empty());  // No bytecode control flow in exits.

  if (block->empty()) {
    if (block != m_curBlock) {
      if (m_state.compatible(block)) {
        m_state.pauseBlock(block);
      } else {
        m_state.clearCse();
      }
      assert(m_curBlock);
      auto& prev = m_curBlock->back();
      if (!prev.hasEdges()) {
        gen(Jmp, block);
      } else if (!prev.isTerminal()) {
        prev.setNext(block);
      }
      m_curBlock = block;
      m_state.startBlock(m_curBlock);
      FTRACE(2, "IRBuilder switching to block B{}: {}\n", block->id(),
             show(m_state));
    }
  }

  if (sp() == nullptr) {
    gen(DefSP, StackOffset(spOffset() + evalStack().size() - stackDeficit()),
        fp());
  }

}
コード例 #2
0
ファイル: engine.cpp プロジェクト: muromec/qtopia-ezx
void Engine::doEvalStack(int p,bool inbrace) {
    if (state == sError || iStack.isEmpty())
        return;
    evalStack(p,inbrace);
    if (!Qtopia::mousePreferred()) {
        if (braceCount == 0)
            iStack.push(new QString("EvaluateLine")); // No tr
        if (braceCount > 0 && inbrace)
            --braceCount;
    }
    if (state != sError)
        changeState(sAppend);
    emit(stackChanged());
}
コード例 #3
0
ファイル: engine.cpp プロジェクト: muromec/qtopia-ezx
void Engine::evalStack(int p,bool inbrace) {
    if (state != sError) {
        QStack<QString*> tmpIStack;
        // could be more efficient and only resolve i once
        Instruction *i;
        while (!iStack.isEmpty ()
                && state != sError
                && *iStack.top() != "EvaluateLine" // No tr
                && (p <= resolve(*iStack.top())->precedence)) {
            // Pop the next instruction
            QString *iString = iStack.pop();
            i = resolve(*iString);
            if (Qtopia::mousePreferred())
                delete iString;
            else
                tmpIStack.push(iString);

            // Stop at the open brace
            if (i->name == "Open brace impl" && inbrace) { // No tr
                i->eval();
                if (Qtopia::mousePreferred())
                    return;
                else {
                    //--braceCount;
                    delete tmpIStack.pop(); // delete open brace instraction
                    return;
                }
            }

            // Compare precedence with the next instruction on the stack
            if (!iStack.isEmpty ()) {
                // Recurse into next instruction if necessary
                int topPrec = resolve(*iStack.top())->precedence;
                if ((p && p <= topPrec) || (!p && i->precedence <= topPrec)) {
                    QStack<Data*> holdingStack;
                    for (int c = 1;c < i->argCount;c++)
                        holdingStack.push(dStack.pop());
                    evalStack(p,inbrace);
                    for (int c = 1;c < i->argCount;c++)
                        dStack.push(holdingStack.pop());
                }
            }

            // Evaluate
            i->eval();

            if (!Qtopia::mousePreferred()) {
                if (!braceCount && !dStack.isEmpty()){
                    Data *top = dStack.pop();
                    for (int c = 0;c < i->argCount;c++)
                        dStack.push(tmpDStack.pop());
                    dStack.push(top);
                } else {
                    for (int c = 0;c < i->argCount;c++)
                        delete tmpDStack.pop();
                    delete tmpIStack.pop();
                }
            }
        }
        if (!Qtopia::mousePreferred())
            while (!tmpIStack.isEmpty())
                iStack.push(tmpIStack.pop());
    }
}
コード例 #4
0
ファイル: read-command.c プロジェクト: DxTania/CS111-Lab-1
command_stream_t
make_command_stream (int (*get_next_byte) (void *),
         void *get_next_byte_argument)
{ 
  char *operators[] = { "&&", "||", "|", "(", ")", ";" };//, "\n" };
  int precedences[] = {   1,    1,   2,  4,   -1,   0, 0 };

  int i, j, isOperator, numChars = 0, inComment = 0;
  int operatorNumber = 0;
  int possibleNewCommand = 0;
  unsigned int currentWordSize = 16 * sizeof(char);

  int lastOperatorWasRightP = 0;
  char *operand, *lastCommand, *currentWord = checked_malloc(16 * sizeof(char));
  char c = (char) get_next_byte(get_next_byte_argument);

  command_t operand1, operand2;

  // Initialize command stream
  command_stream_t commandStream = checked_malloc(10 * sizeof(struct command));
  commandStream->currentStreamSize = 10 * sizeof(struct command);
  commandStream->numCommands = 0;
  commandStream->currentCommand = 0;

  g_lineNumber = 0;

  init_stacks();
  while(c != EOF) 
  {
    //printf("loop entered\n");
    if( c == '\n') 
    {
      g_lineNumber++;
    }

    if(inComment)
    {
      if( c == '\n' ) 
      {
        inComment = 0;
        c = get_next_byte(get_next_byte_argument);
        continue;
      }
      else
      {
        c = get_next_byte(get_next_byte_argument);
        continue;
      }
    }
    else if( c == '#' )
    {
      inComment = 1;
      c = get_next_byte(get_next_byte_argument);
      continue;
    }

    /**
     * Basically if there is a non-operator followed by a newline, there is a possibility
     * that the current command is done. The current command will be over if the newline
     * is then followed by another non-operator. Also ignore newlines inside commands.
     */

    //printf("possible New COmmand is set to %i\n",possibleNewCommand);
    if ( (!isOperator || lastOperatorWasRightP)  && c == '\n') 
    {
      possibleNewCommand = 1;
      //printf("PossibleNewCommand set to 1 char is %c\n",c);
      // Used if a new command is actually started to finish the current one
      lastCommand = currentWord;
      fpos_t pos;
      fgetpos(get_next_byte_argument, &pos);
      c = get_next_byte(get_next_byte_argument);
      if (c != '\n') 
      {
        //printf("continuing after setting possibleNewCommand to 1\n");
       // if(c == EOF)
        // printf("new character is EOF and num of operands left is %i\n",g_iOperand_stack_size);
        fsetpos(get_next_byte_argument, &pos);
        lastOperatorWasRightP = 0;
        continue;
      }
      else {
        g_lineNumber++;
      }
    } 
    else if (c == '\n') 
    {
      c = get_next_byte(get_next_byte_argument);
      //printf("Operator followed by newlien detected. continiuing\n");
      
      lastOperatorWasRightP = 0;
      continue;
    }
    //printf("Checkpoint 2\n");
    /**
     * This loop checks if the current char is an operator. If it is, we create the operator word
     * and then we check the next char in case of double char operators such as || or &&
     */
    lastOperatorWasRightP = 0;
    isOperator = 0;
    for (i = 0; i < 6; i++) 
    { // check if start of an operator
      if (c == operators[i][0]) 
      {
        isOperator = 1;
        operand = currentWord; // Save old operand before creating operator
        currentWord = checked_malloc(3 * sizeof(char));
        // Create the operator word
        currentWord[0] = c;
        fpos_t pos;
        fgetpos(get_next_byte_argument, &pos);
        
    
        if (c == '|' || c == '&') 
        {
          if(g_iOperand_stack_size <= 0 && numChars <= 0)
            error(1,0,"%i: Did not expect binary operator",g_lineNumber);
          
          char current_char = c;
          c = get_next_byte(get_next_byte_argument);
          currentWord[1] = c;
          currentWord[2] = '\0';
          if (c == '|' && c == current_char) 
          {
            i = OR;
          } 
          else if (c == '&' && c == current_char) 
          {
            i = AND;
          }
          else if( current_char == '&' && c != current_char)
          {
            //TODO: Throw syntax error here. Single &!
            //fprintf(stderr,"%i: Isolated & found. Not a valid word character, and it is not an &&",lineNumber);
            error(1,0,"%i: Isolated & found",g_lineNumber);
          }
          else 
          {
            i = PIPE;
            currentWord[1] = '\0';
            fsetpos(get_next_byte_argument, &pos);
          }
        }
        else if( c == '(')
        {
          //printf("Found leftP\n");
          i = LEFTP;
          currentWord[1] = '\0';
          fsetpos(get_next_byte_argument, &pos);
        } 
        else if( c == ')')
        {
          //printf("Found rightP\n");
          lastOperatorWasRightP = 1;
          i = RIGHTP;
          currentWord[1] = '\0';
          fsetpos(get_next_byte_argument, &pos);
        }
        else if( c == ';')  
        {  
          if(g_iOperand_stack_size <= 0 && numChars <= 0)
            error(1,0,"%i: Did not expect binary operator",g_lineNumber);
          currentWord[1] = '\0';
          fsetpos(get_next_byte_argument, &pos);
        }
        else
        {
          currentWord[1] = '\0';
          fsetpos(get_next_byte_argument, &pos);
        }
        break;
      }
    }

    /**
     * When we encounter an operator, we can create a simple command out of the previous word
     * if there was a word (operand) there. This should always happen??
     */
    if (isOperator) 
    {
      //printf("isOperator\n");
      if (numChars > 0) 
      {
        operand[numChars] = '\0';
        //printf("Pushed %s on operand stack\n", operand);
        //get rid of whitespaces in operand here.
        int onlyWhite = getRidOfExtraWhitespaces(operand);
        if(onlyWhite == 0)
          push_operand(createSimpleCommand(operand));
        // printf("simple out of operand %s\n", operand);
        numChars = 0;
      }

      /**
       * While the top of the stack contains an operator with equal or higher precedance than the
       * operator we are currently dealing with, keeping popping one operator and two operands,
       * creating a command out of them, and pushing it onto the operand stack.
       */
      //printf("Charly:top of stack is %i\n",operator_stack_top());
     // if(operator_stack_top() == RIGHTP ) //eval stack until a LEFTP is found
      if(i == RIGHTP)
      {
        //printf("EvalStackuntilLeftP found. possibleNewCommand is %i\n",possibleNewCommand);
        evalStackUntilLeftP();
        //printf("current char is %c\n",c);
        c = get_next_byte(get_next_byte_argument);
        //printf("nextchar is %c\n",c);
        continue;
      }
      while(g_iOperator_stack_size > 0 && g_iOperand_stack_size > 1 
          && (precedences[operator_stack_top()] >= precedences[i]) 
          && ((operator_stack_top() != LEFTP) && i != RIGHTP )) 
      {
        operand2 = pop_operand();
        operand1 = pop_operand();
        operatorNumber = pop_operator();
        //printf("popped operands types %d %d\n", operand1->type, operand2->type);
        //printf("popped operator %s\n", operators[operatorNumber]);
        //printf("pushed type %d operand on stack\n", operatorNumber);
        push_operand(createCommand(operand1, operand2, operatorNumber));
      }

      // Get the operator number for the current word
      for (j = 0; j < 6; j++)
      {
        if (strcmp(currentWord, operators[j]) == 0)
        {
          break;
        }
      }
      //printf("pushed operators %s %d on stack\n", currentWord, j);
      push_operator(j);
      currentWord = checked_malloc(3 * sizeof(char));
    } 
    else if (!possibleNewCommand) 
    {
      //if last operator was rightP. do not run this.
      //printf("not a new command. new char is %c, numChars is %i, currentWordSize is %i\n",c,numChars,currentWordSize);
      if(c != ' ' || numChars > 0)
      {
        //printf("growing current word %s currentWordSize is %i and numChars is %i\n",currentWord,currentWordSize,numChars);
        // Grow current word which is an operand if necessary
        if ((numChars * sizeof(char)) >= currentWordSize)
        {
          //printf("doubling size of word %s\n",currentWord);
          currentWordSize *= 2;
          //char * buffer = checked_malloc(currentWordSize);
          //strncpy(buffer,currentWord,numChars);
          //free((void *)currentWord);
          //currentWord = checked_malloc(currentWordSize);
          //strncpy(currentWord,buffer,numChars);
          currentWord = checked_realloc(currentWord, currentWordSize );// for some reason this was messing up
          //printf("it is now %s\n",currentWord);
        }
        currentWord[numChars] = c;
        numChars++;
      }
    } 
    else 
    {
      //printf("Going to new command. operand stack size is %i\n",g_iOperand_stack_size);
      //printf("Operator stack size is %i. numChars is %i\n",g_iOperator_stack_size,numChars);
       
      //if(g_iOperand_stack_size <= 0)
      //{
       // c =  get_next_byte(get_next_byte_argument);
       // possibleNewCommand = 0;
        //continue;
      //} 
      /**
       * This means that we are about to go onto a new command. We stop the current command and 
       * finish building it with the lastCommand variable, and an operand/operator if necessary.
       * This command is then put into the command stream.
       */
      lastCommand[numChars] = '\0';
      //printf("Last Command getting rid of whites\n"); 
      if(numChars > 0)
      {
        //printf("numchars is > 0 so word is %s\n",lastCommand);
        getRidOfExtraWhitespaces(lastCommand);
        push_operand(createSimpleCommand(lastCommand));
      }
      else if(g_iOperand_stack_size <= 0)
      {
        c =  get_next_byte(get_next_byte_argument);
        possibleNewCommand = 0;
        continue;
      } 

      //else
      //{
       // printf("numChars == 0 so we are continuing\n");
        //continue
       // c = get_next_byte(get_next_byte_argument);
       // possibleNewCommand = 0;
       // continue;
      //}
      evalStack();
      //printf("%s\n", "Finished one command");

      if ((commandStream->numCommands * sizeof(struct command)) == commandStream->currentStreamSize)
      {
        commandStream->currentStreamSize *= 2;
        commandStream = checked_realloc(commandStream, commandStream->currentStreamSize);
      }
      command_t commandToPushToStream = pop_operand();
      commandToPushToStream->status = -1;
      commandStream->stream[commandStream->numCommands] = commandToPushToStream;
      // TODO: if stack not empty, there was an error?
      commandStream->numCommands++;
      numChars = 0;
      currentWord = checked_malloc(3 * sizeof(char)); // prevent overwriting
    }
    //printf("PossibleNewCOmmand set to 0 right before checkpoint1\n");
    c = get_next_byte(get_next_byte_argument);
    possibleNewCommand = 0;
  }

  //printf("Checkpoint1. possibleNEwCommand is %i num of operands is %i\n",possibleNewCommand,g_iOperand_stack_size);
  // Push last word onto operand stack and evaluate the rest
  if (!isOperator) 
  {
    currentWord[numChars] = '\0';
    //printf("pushed simple word %s with word count %i strlen is %i\n", currentWord,numChars,(int)strlen(currentWord));
    getRidOfExtraWhitespaces(currentWord);
    if(strlen(currentWord) > 0)
      push_operand(createSimpleCommand(currentWord));
  } else {
    // if a semicolon, valid?
  }
  //printf("evalstack at the end of loop\n");
  evalStack();
  // Put last command in command stream
  // if there is one!
  if(operand_stack_top() != NULL)
  {
    command_t commandToPushToStream = pop_operand();
    commandToPushToStream->status = -1;
    commandStream->stream[commandStream->numCommands] = commandToPushToStream;
    commandStream->numCommands++;
  }
  //printf("Stack sizes: %d, %d\n", g_iOperator_stack_size, g_iOperand_stack_size);
  //printf("Final command type is %d\n", commandStream->stream[0]->type);

  return commandStream;
}