Example #1
0
ExprResult CondNode::eval()
{
	ExprResult result;
	ExprResult left = mLeft->eval();
	ExprResult right = mRight->eval();
	// @TODO Realizar cast automático para string nos casos necessários
	if (left.isNumeric() && right.isNumeric())
	{
		result.setType("bool");
		if (left.isInteger())
		{
			long leftVal = left.getIntegerValue();
			if (right.isInteger())
			{
				long rightVal = right.getIntegerValue();
				result.setValue((Val*)new BoolVal(compare(leftVal, rightVal)));
			}
			else
			{
				double rightVal = right.getFloatValue();
				result.setValue((Val*)new BoolVal(compare(leftVal, rightVal)));
			}
		}
		else
		{
			double leftVal = left.getFloatValue();
			if (right.isInteger())
			{
				long rightVal = right.getIntegerValue();
				result.setValue((Val*)new BoolVal(compare(leftVal, rightVal)));
			}
			else
			{
				double rightVal = right.getFloatValue();
				result.setValue((Val*)new BoolVal(compare(leftVal, rightVal)));
			}
		}
	}
	else if (left.getType() == "string" && right.getType() == "string")
	{
		result.setType("bool");
		std::string leftVal = ((StringVal*)left.getValue())->getValue();
		std::string rightVal = ((StringVal*)right.getValue())->getValue();
		result.setValue((Val*)new BoolVal(compare(leftVal, rightVal)));
	}
	if (result.getType() == "void")
	{
		Log::fatal("Tipos incompatíveis para comparação");
	}
	return result;
}
ExprResult IdentifierNode::eval()
{
	ExprResult result;
	ActivationReg* areg = ActivationReg::getInstance();
	SymbolTable* table = areg->top();
	Symbol* sym = table->get(mName);
	if (sym != NULL && sym->isVar())
	{
		result.setType(sym->getType());
		result.setValue(sym->getValue());
	}
	else
	{
		Log::fatal("Uso de identificador não definido");
	}
	return result;
}