-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathType.cpp
95 lines (82 loc) · 2.13 KB
/
Type.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "Type.h"
namespace lwscript
{
constexpr struct
{
STD_STRING_VIEW name;
TypeKind kind;
} primitiveTypeMap[] = {
{TEXT("i8"), TypeKind::I8},
{TEXT("u8"), TypeKind::U8},
{TEXT("i16"), TypeKind::I16},
{TEXT("u16"), TypeKind::U16},
{TEXT("i32"), TypeKind::I32},
{TEXT("u32"), TypeKind::U32},
{TEXT("i64"), TypeKind::I64},
{TEXT("u64"), TypeKind::U64},
{TEXT("f32"), TypeKind::F32},
{TEXT("f64"), TypeKind::F64},
{TEXT("bool"), TypeKind::BOOL},
{TEXT("char"), TypeKind::CHAR},
{TEXT("any"), TypeKind::ANY},
{TEXT("string"), TypeKind::STRING},
};
Type::Type() noexcept
: mKind(TypeKind::UNDEFINED), mCategory(TypeCategory::PRIMITIVE), mName(TEXT("undefined"))
{
}
Type::Type(STD_STRING_VIEW name, const SourceLocation &scl) noexcept
{
for (const auto &p : primitiveTypeMap)
{
if (p.name == name)
{
mName = name;
mKind = p.kind;
mCategory = TypeCategory::PRIMITIVE;
mSourceLocation = scl;
return;
}
}
}
TypeKind Type::GetKind() const noexcept
{
return mKind;
}
bool Type::Is(TypeCategory category) const noexcept
{
return mCategory == category;
}
bool Type::IsNumeric() const noexcept
{
return IsInteger() || IsFloating();
}
bool Type::IsInteger() const noexcept
{
return mKind <= TypeKind::U64;
}
bool Type::IsFloating() const noexcept
{
return mKind == TypeKind::F32 || mKind == TypeKind::F64;
}
bool Type::IsBoolean() const noexcept
{
return mKind == TypeKind::BOOL;
}
bool Type::IsString() const noexcept
{
return mKind == TypeKind::STRING;
}
bool Type::IsChar() const noexcept
{
return mKind == TypeKind::CHAR;
}
bool Type::IsAny() const noexcept
{
return mKind == TypeKind::ANY;
}
STD_STRING_VIEW Type::GetName() const noexcept
{
return mName;
}
}