Files
BlitzNext/compiler/type.cpp
T

75 lines
1.4 KiB
C++
Raw Normal View History

2014-01-31 08:23:00 +13:00
2019-01-18 15:55:51 +01:00
#include "type.hpp"
2019-01-18 17:04:57 +01:00
#include "std.hpp"
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
static struct v_type : public Type {
bool canCastTo(Type* t)
{
return t == Type::void_type;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
} v;
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
static struct i_type : public Type {
bool intType()
{
return true;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
bool canCastTo(Type* t)
{
return t == Type::int_type || t == Type::float_type || t == Type::string_type;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
} i;
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
static struct f_type : public Type {
bool floatType()
{
2014-01-31 08:23:00 +13:00
return true;
}
2019-01-18 17:04:57 +01:00
bool canCastTo(Type* t)
{
return t == Type::int_type || t == Type::float_type || t == Type::string_type;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
} f;
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
static struct s_type : public Type {
bool stringType()
{
2014-01-31 08:23:00 +13:00
return true;
}
2019-01-18 17:04:57 +01:00
bool canCastTo(Type* t)
{
return t == Type::int_type || t == Type::float_type || t == Type::string_type;
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
} s;
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
bool StructType::canCastTo(Type* t)
{
return t == this || t == Type::null_type || (this == Type::null_type && t->structType());
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
bool VectorType::canCastTo(Type* t)
{
if (this == t)
return true;
if (VectorType* v = t->vectorType()) {
if (elementType != v->elementType)
return false;
if (sizes.size() != v->sizes.size())
return false;
for (int k = 0; k < sizes.size(); ++k) {
if (sizes[k] != v->sizes[k])
return false;
2014-01-31 08:23:00 +13:00
}
return true;
}
return false;
}
2019-01-18 17:04:57 +01:00
static StructType n("Null");
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
Type* Type::void_type = &v;
Type* Type::int_type = &i;
Type* Type::float_type = &f;
Type* Type::string_type = &s;
Type* Type::null_type = &n;