sencha-lang/Sencha-lang/AST/BasicExpression.cpp

131 lines
2.1 KiB
C++
Raw Normal View History

2012-12-05 19:32:43 +00:00
/*
* BasicExpression.cpp
*
* Created on: Dec 5, 2012
* Author: attero
*/
#include "BasicExpression.h"
void BasicExpression::set_operator(std::string op)
{
this->oper = op;
}
void BasicExpression::set_left_operand(ASTNode * left)
{
2012-12-06 17:41:16 +00:00
if(!this->children_set)
{
children.push_back(left);
}
else
{
2012-12-05 19:32:43 +00:00
this->children[0] = left;
2012-12-06 17:41:16 +00:00
}
if(children.size() == 2)
children_set = true;
2012-12-05 19:32:43 +00:00
}
void BasicExpression::set_right_operand(ASTNode * right)
{
2012-12-06 17:41:16 +00:00
if(!this->children_set)
{
children.push_back(right);
}
else
{
this->children[1] = right;
}
if(children.size() == 2)
children_set = true;
2012-12-05 19:32:43 +00:00
}
void BasicExpression::execute()
{
std::cout << evaluate().repr() << std::endl;
}
2012-12-05 19:32:43 +00:00
2012-12-09 11:57:51 +00:00
void BasicExpression::execute_quietly()
{
evaluate();
}
2012-12-06 17:41:16 +00:00
SenchaObject BasicExpression::evaluate()
2012-12-05 19:32:43 +00:00
{
//TODO refactor it ;)
2012-12-08 19:59:05 +00:00
SenchaObject left = static_cast<ASTExpression *>(children[0])->evaluate();
SenchaObject right = static_cast<ASTExpression *>(children[1])->evaluate();
SenchaObject so;
2012-12-05 19:32:43 +00:00
if(oper == "+")
{
2012-12-08 19:59:05 +00:00
so = SenchaObject(left + right);
2012-12-05 19:32:43 +00:00
}
else if(oper == "-")
{
2012-12-08 19:59:05 +00:00
so = SenchaObject(left - right);
}
else if(oper == "*")
{
2012-12-08 19:59:05 +00:00
so = SenchaObject(left * right);
}
else if(oper == "/")
{
2012-12-08 19:59:05 +00:00
so = SenchaObject(left / right);
}
else if(oper == "=")
{
so = SenchaObject(right);
}
else if(oper == "==")
{
so = SenchaObject(right == left);
}
2012-12-10 11:37:29 +00:00
else if(oper == "!=")
{
so = SenchaObject(right != left);
}
else if(oper == ">")
{
so = SenchaObject(left > right);
}
else if(oper == ">=")
{
so = SenchaObject(left >= right);
}
else if(oper == "<=")
{
so = SenchaObject(left <= right);
}
else if(oper == "<")
{
so = SenchaObject(left < right);
}
2012-12-22 14:48:26 +00:00
else if(oper == "&&")
{
so = SenchaObject(left.is_true() && right.is_true());
}
else if(oper == "||")
{
so = SenchaObject(left.is_true() || right.is_true());
}
return so;
2012-12-05 19:32:43 +00:00
}
BasicExpression::BasicExpression() : children_set(false){
this->type= "BasicExpression";
2012-12-05 19:32:43 +00:00
}
BasicExpression::~BasicExpression() {
2012-12-05 19:32:43 +00:00
}
void BasicExpression::accept(Visitor * visitor)
{
visitor->visit(this);
}
2012-12-06 17:41:16 +00:00