1
2 #include "TypeDefinition.h"
3
4 namespace actions {
5 namespace expression {
6
7 // Integers can be safely converted to floats (for the most part)
8 TypeDefinition TypeDefinition::s_integer("Integer", {ValueType::Float});
9 // No implicit conversions here
10 TypeDefinition TypeDefinition::s_float("Float", {});
11 TypeDefinition TypeDefinition::s_vector("Vector", {});
12 // Identifiers are a technicality needed for making non-math values work but they have no further use other than their
13 // string content
14 TypeDefinition TypeDefinition::s_identifier("Identifier", {});
15
TypeDefinition(SCP_string name,SCP_vector<actions::expression::ValueType> allowedImplicitConversion)16 TypeDefinition::TypeDefinition(SCP_string name, SCP_vector<actions::expression::ValueType> allowedImplicitConversion)
17 : m_name(std::move(name)), m_allowedImplicitConversions(std::move(allowedImplicitConversion))
18 {
19 }
20
getName() const21 const SCP_string& TypeDefinition::getName() const
22 {
23 return m_name;
24 }
getAllowedImplicitConversions() const25 const SCP_vector<actions::expression::ValueType>& TypeDefinition::getAllowedImplicitConversions() const
26 {
27 return m_allowedImplicitConversions;
28 }
forValueType(ValueType type)29 const TypeDefinition& TypeDefinition::forValueType(ValueType type)
30 {
31 switch (type) {
32 case ValueType::Integer:
33 return s_integer;
34 case ValueType::Float:
35 return s_float;
36 case ValueType::Vector:
37 return s_vector;
38 case ValueType::String:
39 return s_identifier;
40 default:
41 UNREACHABLE("Invalid value type!");
42 return s_integer; // Make compiler happy
43 }
44 }
45
checkTypeWithImplicitConversion(ValueType currentType,ValueType expectedType)46 bool checkTypeWithImplicitConversion(ValueType currentType, ValueType expectedType)
47 {
48 if (currentType == expectedType) {
49 // No need to consider implicit conversions;
50 return true;
51 }
52
53 const auto& currentTypeDef = TypeDefinition::forValueType(currentType);
54 const auto& allowedConversions = currentTypeDef.getAllowedImplicitConversions();
55
56 // For now, no transitiv conversions are allowed
57 return std::find(allowedConversions.cbegin(), allowedConversions.cend(), expectedType) != allowedConversions.cend();
58 }
59 } // namespace expression
60 } // namespace actions
61