Fix issues with casting when type is the same.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-09-19 14:54:18 +02:00
parent a7fee1437c
commit 8c13c2c84c
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
5 changed files with 16 additions and 2 deletions

View File

@ -748,6 +748,9 @@ namespace Porygon::Binder {
this->_scriptData->Diagnostics->LogInfo(Diagnostics::DiagnosticCode::UnvalidatedCast, exp->GetStartPosition(),
exp->GetLength());
}
else if (castResult == CastResult::Unchanged){
return toCastParameter;
}
return new BoundCastExpression(toCastParameter, destinationType);
}

View File

@ -100,7 +100,13 @@ namespace Porygon::Evaluation {
[[nodiscard]]
virtual EvalValue* Cast(shared_ptr<const ScriptType> castType) const{
throw new EvaluationException("Casting to invalid type.");
if (castType->GetClass() == this->GetTypeClass()){
return this->Clone();
}
std::stringstream s;
s << "Casting to invalid type. From: " << this->ToString();
s << ". To: " << castType->ToString();
throw EvaluationException(s.str());
}
virtual std::string ToString() const{

View File

@ -7,6 +7,7 @@ namespace Porygon{
InvalidCast,
UncheckedCast,
DataLoss,
Unchanged,
};
}

View File

@ -55,6 +55,10 @@ namespace Porygon {
diagnostics->LogWarning(Diagnostics::DiagnosticCode::DataLossOnCast, parameter->GetStartPosition(),
parameter->GetLength());
}
else if(castResult == CastResult::Unchanged){
parameters->at(i) = parameter;
continue;
}
parameters->at(i) = new Binder::BoundCastExpression(parameter, _parameterTypes[i]);
}
}

View File

@ -115,7 +115,7 @@ namespace Porygon{
[[nodiscard]] CastResult CastableTo(const shared_ptr<const ScriptType>& castType, bool explicitCast) const final{
if (this->operator==(castType))
return CastResult ::ValidCast;
return CastResult ::Unchanged;
if (!explicitCast){
if (castType->GetClass() != TypeClass::Number )
return CastResult::InvalidCast;