Exemple #1
0
std::shared_ptr<ExpressionNode> DivisionNode::evaluate(Environment* e)
{
    std::shared_ptr<ExpressionNode> left = a->evaluate(e);
    std::shared_ptr<ExpressionNode> right = b->evaluate(e);
    ConstantNode* cLeft = dynamic_cast<ConstantNode*>(&*left);
    ConstantNode* cRight = dynamic_cast<ConstantNode*>(&*right);
    
    if (cLeft != 0 && cRight != 0) {
        IntegerNode* iLeft = dynamic_cast<IntegerNode*>(cLeft);
        IntegerNode* iRight = dynamic_cast<IntegerNode*>(cRight);
        RealNode* rLeft = dynamic_cast<RealNode*>(cLeft);
        RealNode* rRight = dynamic_cast<RealNode*>(cRight);
        
        if (iLeft != 0 && iRight != 0) {
            return shared_from_this();
        }
        if (iLeft != 0 && rRight != 0) {
            return std::make_shared<RealNode>
                    (iLeft->getValue() / rRight->getValue());
        }
        if (rLeft != 0 && iRight != 0) {
            return std::make_shared<RealNode>
                    (rLeft->getValue() / iRight->getValue());
        }
        if (rLeft != 0 && rRight != 0) {
            return std::make_shared<RealNode>
                    (rLeft->getValue() / rRight->getValue());
        }
        
        return 0;
    } else {
        return std::make_shared<DivisionNode>(left, right);
    }
}
Exemple #2
0
std::string SubtractionNode::getString(void) const
{
    bool zero = false;
    IntegerNode* in;
    if ((in = dynamic_cast<IntegerNode*>(a.get())) != 0) {
        if (in->getValue() == 0)
            zero = true;
    }
    if (dynamic_cast<PlusMinus*> (b.get()) != 0) {
        return (!zero ? (a->getString() + " ") : "") + getOperator() +
            (zero ? "" : " ") + "(" + b->getString() + ")";
    }
    else
        return (!zero ? (a->getString() + " ") : "") + getOperator() +
            (zero ? "" : " ") + b->getString();
}
/**
 * Transform a IntegerNode to a XML node.
 *
 * @param node		A settings tree node representing a integer node.
 * @param parent	The parent node of the XML document.
 */
 Void XmlSettingsTree::settingsTreeToXml( IntegerNode& node, TiXmlNode& parent ) {
	TiXmlElement* 	element	= new TiXmlElement( XMLSETTINGSTREE_INTEGER_ELEMENT );
	TiXmlText* 		text 	= new TiXmlText( TextUtils::doubleToString( node.getValue(), 0 ).c_str() );

	element->LinkEndChild( text );
	parent.LinkEndChild( element );
}
Exemple #4
0
std::shared_ptr<ExpressionNode> PowerNode::evaluate(Environment* e)
{
    std::shared_ptr<ExpressionNode> left = a->evaluate(e);
    std::shared_ptr<ExpressionNode> right = b->evaluate(e);
    ConstantNode* cLeft = dynamic_cast<ConstantNode*>(&*left);
    ConstantNode* cRight = dynamic_cast<ConstantNode*>(&*right);
    
    if (cLeft != 0 && cRight != 0) {
        IntegerNode* iLeft = dynamic_cast<IntegerNode*>(cLeft);
        IntegerNode* iRight = dynamic_cast<IntegerNode*>(cRight);
        RealNode* rLeft = dynamic_cast<RealNode*>(cLeft);
        RealNode* rRight = dynamic_cast<RealNode*>(cRight);
        
        if (iLeft != 0 && iRight != 0) {
            return std::make_shared<IntegerNode>
                    (::pow(iLeft->getValue(), iRight->getValue()));
        }
        if (iLeft != 0 && rRight != 0) {
            return std::make_shared<RealNode>
                    (::pow(iLeft->getValue(), rRight->getValue()));
        }
        if (rLeft != 0 && iRight != 0) {
            return std::make_shared<RealNode>
                    (::pow(rLeft->getValue(), iRight->getValue()));
        }
        if (rLeft != 0 && rRight != 0) {
            return std::make_shared<RealNode>
                    (::pow(rLeft->getValue(), rRight->getValue()));
        }
        
        return 0;
    } else {
        IntegerNode* iLeft = dynamic_cast<IntegerNode*>(cLeft);
        IntegerNode* iRight = dynamic_cast<IntegerNode*>(cRight);
        
        if (iLeft != 0 && iLeft->getValue() == 1) {
            return std::make_shared<IntegerNode>(1);
        }
        if (iRight != 0 && iRight->getValue() == 1) {
            return left;
        }
        return std::make_shared<PowerNode>(left, right);
    }
}
Exemple #5
0
std::shared_ptr<ExpressionNode> ModuloNode::evaluate(Environment* e)
{
    std::shared_ptr<ExpressionNode> left = a->evaluate(e);
    std::shared_ptr<ExpressionNode> right = b->evaluate(e);
    ConstantNode* cLeft = dynamic_cast<ConstantNode*>(&*left);
    ConstantNode* cRight = dynamic_cast<ConstantNode*>(&*right);
    
    if (cLeft != 0 && cRight != 0) {
        IntegerNode* iLeft = dynamic_cast<IntegerNode*>(cLeft);
        IntegerNode* iRight = dynamic_cast<IntegerNode*>(cRight);
        RealNode* rLeft = dynamic_cast<RealNode*>(cLeft);
        RealNode* rRight = dynamic_cast<RealNode*>(cRight);
        
        if (iLeft != 0 && iRight != 0) {
            return std::make_shared<IntegerNode>
                    (iLeft->getValue() % iRight->getValue());
        }
        throw ArithmeticException("modulo operator only defined for integer operands!");
    } else {
        return std::make_shared<ModuloNode>(left, right);
    }
}
Exemple #6
0
 // [<value>I]
 void visit( IntegerNode aNode ) {
   std::cout << " [" << aNode.getValue() << "I]";
 }