Support for explicit casting
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -3,8 +3,9 @@
|
||||
|
||||
namespace Porygon{
|
||||
enum class CastResult{
|
||||
Success,
|
||||
Failure,
|
||||
ValidCast,
|
||||
InvalidCast,
|
||||
UncheckedCast,
|
||||
DataLoss,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -47,15 +47,15 @@ namespace Porygon {
|
||||
const auto& parameterType = parameter->GetType();
|
||||
if (parameterType->operator!=(_parameterTypes[i].get())){
|
||||
auto castResult = parameterType->CastableTo(_parameterTypes[i], false);
|
||||
if (castResult == CastResult::Failure){
|
||||
if (castResult == CastResult::InvalidCast){
|
||||
return false;
|
||||
}
|
||||
else{
|
||||
if (castResult == CastResult::DataLoss){
|
||||
diagnostics->LogWarning(Diagnostics::DiagnosticCode::DataLossOnImplicitCast, parameter->GetStartPosition(),
|
||||
diagnostics->LogWarning(Diagnostics::DiagnosticCode::DataLossOnCast, parameter->GetStartPosition(),
|
||||
parameter->GetLength());
|
||||
}
|
||||
parameters->at(i) = new Binder::BoundImplicitCastExpression(parameter, _parameterTypes[i]);
|
||||
parameters->at(i) = new Binder::BoundCastExpression(parameter, _parameterTypes[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,9 @@ namespace Porygon{
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual CastResult CastableTo(const shared_ptr<const ScriptType>& castType, bool explicitCast) const{
|
||||
return CastResult::Failure;
|
||||
if (explicitCast)
|
||||
return CastResult::InvalidCast;
|
||||
return CastResult::InvalidCast;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -120,12 +122,12 @@ namespace Porygon{
|
||||
[[nodiscard]] CastResult CastableTo(const shared_ptr<const ScriptType>& castType, bool explicitCast) const final{
|
||||
if (!explicitCast){
|
||||
if (castType->GetClass() != TypeClass::Number )
|
||||
return CastResult::Failure;
|
||||
return CastResult::InvalidCast;
|
||||
auto bNum = dynamic_pointer_cast<const NumericScriptType>(castType);
|
||||
if (bNum->IsFloat() && !IsFloat()) return CastResult::Success;
|
||||
if (bNum->IsFloat() && !IsFloat()) return CastResult::ValidCast;
|
||||
if (!bNum->IsFloat() && IsFloat()) return CastResult::DataLoss;
|
||||
}
|
||||
return CastResult::Success;
|
||||
return ScriptType::CastableTo(castType, explicitCast);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user