Fixes for implicit casting when assigning variables

This commit is contained in:
Deukhoofd 2019-09-14 11:44:43 +02:00
parent 54778adf82
commit 98b605a18b
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 10 additions and 2 deletions

View File

@ -85,6 +85,10 @@ namespace Porygon::Binder {
: this->_scope->AssignVariable(s->GetIdentifier(), boundExpression->GetType());
if (assignment.GetResult() == VariableAssignmentResult::Ok) {
auto key = assignment.GetKey();
auto type = assignment.GetKey()->GetType();
if (type != nullptr && type->operator!=(boundExpression->GetType())){
boundExpression = new BoundCastExpression(boundExpression, type);
}
return new BoundAssignmentStatement(key, boundExpression);
} else {
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable,

View File

@ -107,6 +107,7 @@ namespace Porygon::Binder {
if (castResult == CastResult::InvalidCast){
return VariableAssignment(VariableAssignmentResult::VariableDefinedWithDifferentType, nullptr);
}
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, exists, false, t));
}
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, exists, false, type));
}

View File

@ -82,8 +82,11 @@ namespace Porygon::Evaluation {
EvalValue *NumericEvalValue::Cast(shared_ptr<const ScriptType> castType) const {
if (castType->GetClass() == TypeClass::Number){
auto num = static_pointer_cast<const NumericScriptType>(castType);
if (num->IsFloat()){
return new NumericEvalValue(std::get<int64_t >(_intValue));
if (num->IsFloat() && !this->_isFloat){
return new NumericEvalValue(static_cast<double>(std::get<int64_t >(_intValue)));
}
else if(!num->IsFloat() && this->_isFloat){
return new NumericEvalValue(static_cast<int64_t>(std::get<double >(_floatValue)));
}
}
return EvalValue::Cast(castType);