Rework identifier handling, adds typedef statement.
This commit is contained in:
38
src/CoreData/Identifier.hpp
Normal file
38
src/CoreData/Identifier.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef MALACHSCRIPT_IDENTIFIER_HPP
|
||||
#define MALACHSCRIPT_IDENTIFIER_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <string_view>
|
||||
namespace MalachScript {
|
||||
class Identifier {
|
||||
std::u8string_view _str;
|
||||
uint32_t _hash;
|
||||
|
||||
public:
|
||||
Identifier() : _hash(0) {}
|
||||
consteval Identifier(const char8_t* c) : _str(c), _hash(Hash(c)) {}
|
||||
Identifier(const std::u8string_view& c) : _str(c), _hash(HashStringView(c)) {}
|
||||
Identifier(const std::u8string_view& c, uint32_t hash) : _str(c), _hash(hash) {}
|
||||
|
||||
[[nodiscard]] constexpr inline uint32_t GetHash() const noexcept { return _hash; }
|
||||
[[nodiscard]] constexpr inline const std::u8string_view& GetString() const noexcept { return _str; }
|
||||
|
||||
constexpr inline static uint32_t Hash(const char8_t* input) {
|
||||
if (*input != 0U) {
|
||||
return static_cast<uint32_t>(*input) + 33 * Hash(input + 1);
|
||||
} else {
|
||||
return 5381;
|
||||
}
|
||||
};
|
||||
constexpr inline static uint32_t HashStringView(const std::u8string_view& sv) {
|
||||
auto init = 5381;
|
||||
for (auto it = sv.rbegin(); it != sv.rend(); ++it) {
|
||||
init *= 33;
|
||||
init += static_cast<uint32_t>(*it);
|
||||
}
|
||||
return init;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // MALACHSCRIPT_IDENTIFIER_HPP
|
||||
42
src/CoreData/PrimitiveTypes.hpp
Normal file
42
src/CoreData/PrimitiveTypes.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef MALACHSCRIPT_PRIMITIVETYPES_HPP
|
||||
#define MALACHSCRIPT_PRIMITIVETYPES_HPP
|
||||
|
||||
#include <string>
|
||||
#include "Identifier.hpp"
|
||||
|
||||
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";
|
||||
|
||||
class PrimitiveTypes {
|
||||
public:
|
||||
constexpr static const Identifier& VoidName() noexcept { return _voidName; }
|
||||
constexpr static const Identifier& IntName() noexcept { return _intName; }
|
||||
constexpr static const Identifier& Int8Name() noexcept { return _int8Name; }
|
||||
constexpr static const Identifier& Int16Name() noexcept { return _int16Name; }
|
||||
constexpr static const Identifier& Int32Name() noexcept { return _int32Name; }
|
||||
constexpr static const Identifier& Int64Name() noexcept { return _int64Name; }
|
||||
constexpr static const Identifier& UintName() noexcept { return _uintName; }
|
||||
constexpr static const Identifier& Uint8Name() noexcept { return _uint8Name; }
|
||||
constexpr static const Identifier& Uint16Name() noexcept { return _uint16Name; }
|
||||
constexpr static const Identifier& Uint32Name() noexcept { return _uint32Name; }
|
||||
constexpr static const Identifier& Uint64Name() noexcept { return _uint64Name; }
|
||||
constexpr static const Identifier& FloatName() noexcept { return _floatName; }
|
||||
constexpr static const Identifier& DoubleName() noexcept { return _doubleName; }
|
||||
constexpr static const Identifier& BoolName() noexcept { return _boolName; }
|
||||
};
|
||||
}
|
||||
|
||||
#endif // MALACHSCRIPT_PRIMITIVETYPES_HPP
|
||||
Reference in New Issue
Block a user