Logo Search packages:      
Sourcecode: ktechlab version File versions  Download package

void Expression::buildTree ( const QString &  expression,
BTreeBase tree,
BTreeNode node,
int  level 
) [private]

Parses an expression, and generates a tree structure from it.

Definition at line 252 of file expression.cpp.

References BTreeBase::addNode(), BTreeNode::setChildOp(), BTreeNode::setType(), and BTreeNode::setValue().

Referenced by compileExpression(), and processConstant().

{
      int firstEnd = 0;
      int secondStart = 0;
      bool unary = false;
      Operation op;
      QString expression = stripBrackets( unstrippedExpression );
      switch(level)
      {
            // ==, !=
            case 0:
            {
            int equpos = findSkipBrackets(expression, "==");
            int neqpos = findSkipBrackets(expression, "!=");
            if( equpos != -1 )
            {
                  op = equals;
                  firstEnd = equpos;
                  secondStart = equpos + 2;
            }
            else if( neqpos != -1 )
            {
                  op = notequals;
                  firstEnd = neqpos;
                  secondStart = neqpos + 2;
            }
            else op = noop;
            break;
            }

            // <, <=, >=, >
            case 1:
            {
            int ltpos = findSkipBrackets(expression, "<");
            int lepos = findSkipBrackets(expression, "<=");
            int gepos = findSkipBrackets(expression, ">=");
            int gtpos = findSkipBrackets(expression, ">");
            // Note: if (for example) "<=" is present, "<" will also be present. This
            // means that we have to check for "<=" before "<", etc.
            if( lepos != -1 )
            {
                  op = le;
                  firstEnd = lepos;
                  secondStart = lepos + 2;
            }
            else if( gepos != -1 )
            {
                  op = ge;
                  firstEnd = gepos;
                  secondStart = gepos + 2;
            }
            else if( ltpos != -1 )
            {
                  op = lt;
                  firstEnd = ltpos;
                  secondStart = ltpos + 1;
            }
            else if( gtpos != -1 )
            {
                  op = gt;
                  firstEnd = gtpos;
                  secondStart = gtpos + 1;
            }
            else op = noop;
            break;
            }

            // +,-
            case 2:
            {
            int addpos = findSkipBrackets(expression, '+');
            int subpos = findSkipBrackets(expression, '-');
            if( subpos != -1 )
            {
                  op = subtraction;
                  firstEnd = subpos;
                  secondStart = subpos + 1;
            }
            else if( addpos != -1 )
            {
                  op = addition;
                  firstEnd = addpos;
                  secondStart = addpos + 1;
            }
            else op = noop;
            break;
            }
            
            // *,/
            case 3:
            {
            int mulpos = findSkipBrackets(expression, '*');
            int divpos = findSkipBrackets(expression, '/');
            if( divpos != -1 )
            {
                  op = division;
                  firstEnd = divpos;
                  secondStart = divpos + 1;
            }
            else if( mulpos != -1 )
            {
                  op = multiplication;
                  firstEnd = mulpos;
                  secondStart = mulpos + 1;
            }
            else op = noop;
            break;
            }
            
            // ^
            case 4:
            {
            int exppos = findSkipBrackets(expression, '^');
            if( exppos != -1 )
            {
                  op = exponent;
                  firstEnd = exppos;
                  secondStart = exppos + 1;
            }
            else op = noop;
            break;
            }
            
            // AND, OR, XOR
            case 5:
            {
            int bwAndPos = findSkipBrackets(expression, " AND ");
            int bwOrPos = findSkipBrackets(expression, " OR ");
            int bwXorPos = findSkipBrackets(expression, " XOR ");
            if( bwAndPos != -1 )
            {
                  op = bwand;
                  firstEnd = bwAndPos;
                  secondStart = bwAndPos + 5;
            }
            else if( bwOrPos != -1 )
            {
                  op = bwor;
                  firstEnd = bwOrPos;
                  secondStart = bwOrPos + 4;
            }
            else if( bwXorPos != -1 )
            {
                  op = bwxor;
                  firstEnd = bwXorPos;
                  secondStart = bwXorPos + 5;
            }
            else op = noop;
            break;
            }
            
            // NOT
            case 6:
            {
            int bwNotPos = findSkipBrackets(expression, " NOT ");
            if( bwNotPos != -1 )
            {
                  op = bwnot;
                  unary = true;
                  firstEnd = bwNotPos; // this line is not needed for unary things/
                  secondStart = bwNotPos + 5;
            }
            else op = noop;
            break;
            }
      }
      
      node->setChildOp(op);
      
      QString tokens[2];
      tokens[0] = expression.left(firstEnd).stripWhiteSpace();
      tokens[1] = expression.mid(secondStart).stripWhiteSpace();
      
      if( op != noop )
      {     
            for( int j = 0; j < 2; j++ )
            {
                  
                  BTreeNode *newNode = new BTreeNode();
                  tree->addNode( node, newNode, (j == 0) );
                  // we need to strip any brackets from the sub-expression
                  
                  // try each token again at the same level, if they 
                  // don't have any of this level's operators, then the function
                  // will go to the next level as below.
                  
                  // For unary opertaions, e.g NOT, we have no special 
                  // code for nodes with only one child, so we leave the left
                  // hand child blank and put the rest in the right hand node.
                  if( unary && j == 0 )
                  {
                        newNode->setValue("");
                        newNode->setType(number);
                  }
                  else buildTree(tokens[j], tree, newNode, 0 );
            }
      }
      else
      {
            // if there was no relevant operation i.e. " 3*4 / 6" as opposed to " 3*4 + 6"
            // then just pass the node onto the next parsing level.
            // unless we are at the lowest level, in which case we have reached a final value.
            if( level == 6 ) expressionValue(expression,tree,node);
            else 
            {
                  buildTree(expression,tree,node,level + 1);
            }
      }
}


Generated by  Doxygen 1.6.0   Back to index