Example #1
0
Type* Negative::Operate()
{
	std::stringstream ss("");
		
	Type* lValue = GetNextValue();
	Type* rValue = GetNextValue();

	if (lValue == nullptr || rValue == nullptr)
	{
		return new Error("Either or both values point to nothing!", Error::OperationError);
	}

	else if (TypeResolution() == "Error")	
	{
		return new Error(rValue->GetString(), static_cast<Error::ErrorID>(rValue->GetInt()));
	}

	else if (TypeResolution() == "Int")
	{
		ss << -lValue->GetInt();

		return new Int(ss.str());
	}

	else if (TypeResolution() == "Float")
	{
		ss << -lValue->GetFloat();

		return new Float(ss.str());
	}

	return new Error("Type must resolve to either Int or Float", Error::OperationError);
}
Example #2
0
Type* LogicalAnd::Operate()
{
	bool retVal = false;

	Type* lValue = GetNextValue();
	Type* rValue = GetNextValue();

	if (lValue == nullptr || rValue == nullptr)
	{
		return new Error("Either or both values point to nothing!", Error::OperationError);
	}

	else if (TypeResolution() == "Error")	
	{
		return new Error(rValue->GetString(), static_cast<Error::ErrorID>(rValue->GetInt()));
	}

	else if (TypeResolution() == "Int")
	{
		retVal = lValue->GetInt() && rValue->GetInt();
	}
	else if (TypeResolution() == "Bool")
	{
		retVal = lValue->GetInt() && rValue->GetInt();
	}

	else
		return new Error("Expected comparable values: bool or int.", Error::ErrorID::OperationError);

	if (retVal == true)
		return new Bool("true");
	else
		return new Bool("false");

}
Example #3
0
Type* Modulus::Operate()
{
	std::stringstream ss("");

	Type* lValue = GetNextValue();
	Type* rValue = GetNextValue();

	if (lValue == nullptr || rValue == nullptr)
	{
		return new Error("Either or both values point to nothing!", Error::OperationError);
	}

	else if (TypeResolution() == "Error")	
	{
		return new Error(rValue->GetString(), static_cast<Error::ErrorID>(rValue->GetInt()));
	}

	else if (TypeResolution() == "Int")
	{

		//this should never happen!:
		if (lValue == nullptr || rValue == nullptr)
			return nullptr;
		
		if (rValue->GetInt() == 0);
		{
			return new Error("Can't divide by zero!", Error::OperationError);
		}

		ss << lValue->GetInt() % rValue->GetInt();

		return new Int(ss.str());
	}

	return new Error("Type must resolve to an Int", Error::OperationError);
}