Initial work on type registration in the binder.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -2,7 +2,10 @@
|
||||
#define MALACHSCRIPT_IDENTIFIER_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace MalachScript {
|
||||
|
||||
constexpr static uint32_t crc_table[256] = {
|
||||
@@ -35,28 +38,30 @@ namespace MalachScript {
|
||||
0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330,
|
||||
0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
|
||||
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D};
|
||||
static int constexpr CalcLength(const char8_t* str) { return *str ? 1 + CalcLength(str + 1) : 0; }
|
||||
static int constexpr CalcLength(const char* str) { return *str ? 1 + CalcLength(str + 1) : 0; }
|
||||
|
||||
static std::u8string empty;
|
||||
static std::string empty;
|
||||
|
||||
class Identifier {
|
||||
const char8_t* _str;
|
||||
const char* _str;
|
||||
size_t _length;
|
||||
uint32_t _hash;
|
||||
|
||||
public:
|
||||
Identifier() : _str(nullptr), _length(0), _hash(0) {}
|
||||
constexpr Identifier(const char8_t* c) : _str(c), _length(CalcLength(c)), _hash(Hash(c)) {}
|
||||
constexpr Identifier(const char8_t* c, size_t length, uint32_t hash) : _str(c), _length(length), _hash(hash) {}
|
||||
constexpr Identifier(const char* c) : _str(c), _length(CalcLength(c)), _hash(Hash(c)) {}
|
||||
constexpr Identifier(const char* c, size_t length) : _str(c), _length(length), _hash(Hash(c)) {}
|
||||
constexpr Identifier(const char* c, size_t length, uint32_t hash) : _str(c), _length(length), _hash(hash) {}
|
||||
|
||||
[[nodiscard]] constexpr inline uint32_t GetHash() const noexcept { return _hash; }
|
||||
[[nodiscard]] constexpr inline std::u8string_view GetString() const noexcept {
|
||||
return std::u8string_view(_str, _length);
|
||||
[[nodiscard]] constexpr inline std::string_view GetString() const noexcept {
|
||||
return std::string_view(_str, _length);
|
||||
}
|
||||
[[nodiscard]] inline std::string GetStdString() const noexcept { return std::string((char*)_str, _length); }
|
||||
|
||||
[[nodiscard]] constexpr inline size_t GetLength() const noexcept { return _length; }
|
||||
|
||||
constexpr inline static uint32_t Hash(const std::u8string_view& sv) {
|
||||
constexpr inline static uint32_t Hash(const std::string_view& sv) {
|
||||
uint32_t crc = 0xffffffff;
|
||||
for (auto c : sv) {
|
||||
crc = (crc >> 8) ^ crc_table[(crc ^ c) & 0xff];
|
||||
@@ -64,6 +69,9 @@ namespace MalachScript {
|
||||
return crc ^ 0xffffffff;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline constexpr std::size_t operator()() const noexcept { return _hash; }
|
||||
[[nodiscard]] inline constexpr operator uint32_t() const noexcept { return _hash; }
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& out, const Identifier& c) {
|
||||
out << std::string((const char*)c._str, c._length);
|
||||
return out;
|
||||
@@ -86,4 +94,18 @@ namespace MalachScript {
|
||||
};
|
||||
}
|
||||
|
||||
namespace std {
|
||||
/// @brief Helper class for getting the hash of a string view literal.
|
||||
template <> struct hash<MalachScript::Identifier> {
|
||||
/// @brief Returns the hash of a stringview.
|
||||
/// @param s a StringView.
|
||||
/// @return The hash of the StringView.
|
||||
constexpr std::size_t operator()(MalachScript::Identifier const& s) const noexcept { return s.GetHash(); }
|
||||
};
|
||||
}
|
||||
|
||||
inline constexpr MalachScript::Identifier operator"" _id(const char* c, size_t l) {
|
||||
return MalachScript::Identifier(c, l);
|
||||
}
|
||||
|
||||
#endif // MALACHSCRIPT_IDENTIFIER_HPP
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define MALACHSCRIPT_PRIMITIVETYPES_HPP
|
||||
|
||||
#include <string>
|
||||
#include "../Binder/BoundType.hpp"
|
||||
#include "Identifier.hpp"
|
||||
|
||||
#if !__cpp_constinit
|
||||
@@ -9,22 +10,34 @@
|
||||
#endif
|
||||
|
||||
namespace MalachScript {
|
||||
static constinit Identifier _voidName = u8"void";
|
||||
static constinit Identifier _intName = u8"int";
|
||||
static constinit Identifier _int8Name = u8"int8";
|
||||
static constinit Identifier _int16Name = u8"int16";
|
||||
static constinit Identifier _int32Name = u8"int32";
|
||||
static constinit Identifier _int64Name = u8"int64";
|
||||
static constinit Identifier _uintName = u8"uint";
|
||||
static constinit Identifier _uint8Name = u8"uint8";
|
||||
static constinit Identifier _uint16Name = u8"uint16";
|
||||
static constinit Identifier _uint32Name = u8"uint32";
|
||||
static constinit Identifier _uint64Name = u8"uint64";
|
||||
static constinit Identifier _floatName = u8"float";
|
||||
static constinit Identifier _doubleName = u8"double";
|
||||
static constinit Identifier _boolName = u8"bool";
|
||||
static constinit Identifier _voidName = "void";
|
||||
static constinit Identifier _intName = "int";
|
||||
static constinit Identifier _int8Name = "int8";
|
||||
static constinit Identifier _int16Name = "int16";
|
||||
static constinit Identifier _int32Name = "int32";
|
||||
static constinit Identifier _int64Name = "int64";
|
||||
static constinit Identifier _uintName = "uint";
|
||||
static constinit Identifier _uint8Name = "uint8";
|
||||
static constinit Identifier _uint16Name = "uint16";
|
||||
static constinit Identifier _uint32Name = "uint32";
|
||||
static constinit Identifier _uint64Name = "uint64";
|
||||
static constinit Identifier _floatName = "float";
|
||||
static constinit Identifier _doubleName = "double";
|
||||
static constinit Identifier _boolName = "bool";
|
||||
|
||||
static constinit Identifier _autoName = u8"auto";
|
||||
static constinit Identifier _autoName = "auto";
|
||||
|
||||
static Binder::BoundType _int8Type = Binder::BoundType(ClassAttr::None, sizeof(int8_t));
|
||||
static Binder::BoundType _int16Type = Binder::BoundType(ClassAttr::None, sizeof(int16_t));
|
||||
static Binder::BoundType _int32Type = Binder::BoundType(ClassAttr::None, sizeof(int32_t));
|
||||
static Binder::BoundType _int64Type = Binder::BoundType(ClassAttr::None, sizeof(int64_t));
|
||||
static Binder::BoundType _uint8Type = Binder::BoundType(ClassAttr::None, sizeof(uint8_t));
|
||||
static Binder::BoundType _uint16Type = Binder::BoundType(ClassAttr::None, sizeof(uint16_t));
|
||||
static Binder::BoundType _uint32Type = Binder::BoundType(ClassAttr::None, sizeof(uint32_t));
|
||||
static Binder::BoundType _uint64Type = Binder::BoundType(ClassAttr::None, sizeof(uint64_t));
|
||||
static Binder::BoundType _floatType = Binder::BoundType(ClassAttr::None, sizeof(float));
|
||||
static Binder::BoundType _doubleType = Binder::BoundType(ClassAttr::None, sizeof(double));
|
||||
static Binder::BoundType _boolType = Binder::BoundType(ClassAttr::None, sizeof(uint8_t));
|
||||
|
||||
class PrimitiveTypes {
|
||||
public:
|
||||
@@ -43,6 +56,20 @@ namespace MalachScript {
|
||||
constexpr static const Identifier& DoubleName() noexcept { return _doubleName; }
|
||||
constexpr static const Identifier& BoolName() noexcept { return _boolName; }
|
||||
constexpr static const Identifier& AutoName() noexcept { return _autoName; }
|
||||
|
||||
constexpr static const Binder::BoundType* IntType() noexcept { return &_int32Type; }
|
||||
constexpr static const Binder::BoundType* Int8Type() noexcept { return &_int8Type; }
|
||||
constexpr static const Binder::BoundType* Int16Type() noexcept { return &_int16Type; }
|
||||
constexpr static const Binder::BoundType* Int32Type() noexcept { return &_int32Type; }
|
||||
constexpr static const Binder::BoundType* Int64Type() noexcept { return &_int64Type; }
|
||||
constexpr static const Binder::BoundType* UintType() noexcept { return &_uint32Type; }
|
||||
constexpr static const Binder::BoundType* Uint8Type() noexcept { return &_uint8Type; }
|
||||
constexpr static const Binder::BoundType* Uint16Type() noexcept { return &_uint16Type; }
|
||||
constexpr static const Binder::BoundType* Uint32Type() noexcept { return &_uint32Type; }
|
||||
constexpr static const Binder::BoundType* Uint64Type() noexcept { return &_uint64Type; }
|
||||
constexpr static const Binder::BoundType* FloatType() noexcept { return &_floatType; }
|
||||
constexpr static const Binder::BoundType* DoubleType() noexcept { return &_doubleType; }
|
||||
constexpr static const Binder::BoundType* BoolType() noexcept { return &_boolType; }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user