Better handling of casting
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-08-18 13:17:53 +02:00
parent 0fde3d46df
commit 1d72e2eccd
8 changed files with 78 additions and 21 deletions

View File

@@ -87,6 +87,11 @@ namespace Porygon::Evaluation {
virtual EvalValue* UnaryOperation(Binder::BoundUnaryOperation operation) const{
throw EvaluationException("Unary operations are not implemented for this type.");
}
[[nodiscard]]
virtual EvalValue* Cast(shared_ptr<const ScriptType> castType) const{
throw new EvaluationException("Casting to invalid type.");
}
};
class BooleanEvalValue : public EvalValue {

View File

@@ -176,4 +176,24 @@ namespace Porygon::Evaluation {
}
}
}
EvalValue *IntegerEvalValue::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 FloatEvalValue(GetIntegerValue());
}
}
return EvalValue::Cast(castType);
}
EvalValue *FloatEvalValue::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 IntegerEvalValue(GetFloatValue());
}
}
return EvalValue::Cast(castType);
}
}

View File

@@ -68,11 +68,6 @@ namespace Porygon::Evaluation {
return _value;
}
[[nodiscard]]
inline double EvaluateFloat() const final {
return static_cast<double>(_value);
}
[[nodiscard]]
inline std::u16string EvaluateString() const final{
return Utilities::StringUtils::IntToString(_value);
@@ -98,14 +93,17 @@ namespace Porygon::Evaluation {
default: throw;
}
}
[[nodiscard]]
EvalValue* Cast(shared_ptr<const ScriptType> castType) const final;
};
class FloatEvalValue : public NumericEvalValue {
const double _value;
[[nodiscard]]
inline int64_t GetIntegerValue() const final {
return _value;
int64_t GetIntegerValue() const final {
throw EvaluationException("Attempting to retrieve int from float eval value.");
}
[[nodiscard]]
@@ -127,11 +125,6 @@ namespace Porygon::Evaluation {
return _value;
}
[[nodiscard]]
inline int64_t EvaluateInteger() const final {
return static_cast<int64_t >(_value);
}
[[nodiscard]]
inline std::u16string EvaluateString() const final{
return Utilities::StringUtils::FloatToString(_value);
@@ -196,6 +189,9 @@ namespace Porygon::Evaluation {
default: throw;
}
}
[[nodiscard]]
EvalValue* Cast(shared_ptr<const ScriptType> castType) const final;
};
}

View File

@@ -257,6 +257,8 @@ namespace Porygon::Evaluation {
return this->EvaluateComplexTableExpression(expression);
case BoundExpressionKind::Require:
return this -> EvaluateRequireExpression(expression);
case BoundExpressionKind::ImplicitCast:
return this -> EvaluateImplicitCastExpression(expression);
}
}
@@ -412,4 +414,10 @@ namespace Porygon::Evaluation {
return result.Take();
}
}
EvalValuePointer Evaluator::EvaluateImplicitCastExpression(const BoundExpression *pExpression) {
auto iCExpression = dynamic_cast<const BoundImplicitCastExpression*>(pExpression);
auto val = EvaluateExpression(iCExpression->GetExpression());
return val->Cast(iCExpression->GetType());
}
}

View File

@@ -48,6 +48,7 @@ namespace Porygon::Evaluation{
EvalValuePointer EvaluateComplexTableExpression(const BoundExpression *expression);
EvalValuePointer EvaluateRequireExpression(const BoundExpression* expression);
EvalValuePointer EvaluateImplicitCastExpression(const BoundExpression *pExpression);
EvalValuePointer GetVariable(const BoundVariableExpression *expression);
public: