diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-08 01:57:30 -0500 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-12-08 01:57:30 -0500 |
| commit | 2ddaf86857828500235e8b8a65c11bb2dd91b8be (patch) | |
| tree | 46899c14955bf356be2331e63c181afd4089d982 /include | |
| parent | 9f7c44f1577f194cb76b03ac45a2af542a86c8b9 (diff) | |
chore! breaking API changes and new APIs introduced.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'include')
| -rw-r--r-- | include/CompilerKit/AST.h | 52 | ||||
| -rw-r--r-- | include/CompilerKit/AST.inl | 8 | ||||
| -rw-r--r-- | include/CompilerKit/Detail/32x0.h | 4 | ||||
| -rw-r--r-- | include/CompilerKit/Detail/64x0.h | 4 | ||||
| -rw-r--r-- | include/CompilerKit/Detail/AMD64.h | 4 | ||||
| -rw-r--r-- | include/CompilerKit/ErrorOr.h | 8 | ||||
| -rw-r--r-- | include/CompilerKit/Utilities/Assembler.h | 16 | ||||
| -rw-r--r-- | include/CompilerKit/Utilities/DLL.h | 30 |
8 files changed, 67 insertions, 59 deletions
diff --git a/include/CompilerKit/AST.h b/include/CompilerKit/AST.h index 3987f13..2cefed7 100644 --- a/include/CompilerKit/AST.h +++ b/include/CompilerKit/AST.h @@ -8,15 +8,16 @@ #include <CompilerKit/CodeGenerator.h> #include <vector> +#include "CompilerKit/Detail/Config.h" -#define CK_COMPILER_FRONTEND : public ::CompilerKit::CompilerFrontendInterface +#define CK_COMPILER_FRONTEND : public ::CompilerKit::ICompilerFrontend namespace CompilerKit { inline static auto kInvalidFrontend = "(null)"; struct SyntaxLeafList; struct SyntaxLeafList; -struct CompilerKeyword; +struct SyntaxKeyword; /// =========================================================== /// /// @note we want to do that to separate keywords. @@ -59,34 +60,40 @@ enum KeywordKind { kKeywordKindGreaterEq, kKeywordKindLessEq, kKeywordKindPtr, + kKeywordKindClassStart, + kKeywordKindClassEnd, + kKeywordKindMethodStart, + kKeywordKindMethodEnd, kKeywordKindCount = kKeywordKindPtr - kKeywordKindNamespace + 1, }; /// =========================================================== /// /// \brief Compiler keyword information struct. /// =========================================================== /// -struct CompilerKeyword { - CompilerKeyword(STLString name, KeywordKind kind) : keyword_name(name), keyword_kind(kind) {} +struct SyntaxKeyword { + SyntaxKeyword(STLString name, KeywordKind kind) : fKeywordName(name), fKeywordKind(kind) {} - STLString keyword_name{""}; - KeywordKind keyword_kind{kKeywordKindInvalid}; + STLString fKeywordName{""}; + KeywordKind fKeywordKind{kKeywordKindInvalid}; }; struct SyntaxLeafList final { struct SyntaxLeaf final { - Int32 fUserType{0U}; - CompilerKeyword fUserData{"", kKeywordKindInvalid}; + Int32 fUserType{0U}; + SyntaxKeyword fUserData{"", kKeywordKindInvalid}; STLString fUserValue{""}; struct SyntaxLeaf* fNext{nullptr}; }; - std::vector<SyntaxLeaf> fLeafList; - SizeType fNumLeafs{0}; + using ArrayType = std::vector<SyntaxLeaf>; - SizeType SizeOf() { return fNumLeafs; } - std::vector<SyntaxLeaf>& Get() { return fLeafList; } - SyntaxLeaf& At(SizeType index) { return fLeafList[index]; } + ArrayType fLeafList; + SizeType fNumLeafs{0}; + + SizeType NumLeafs() { return fNumLeafs; } + ArrayType& Get() { return fLeafList; } + SyntaxLeaf& At(const SizeType& index) { return fLeafList[index]; } }; /// =========================================================== /// @@ -95,7 +102,7 @@ struct SyntaxLeafList final { /// \param needle the string we search for. /// \return if we found it or not. /// =========================================================== /// -bool find_word(STLString haystack, STLString needle) noexcept; +bool ast_find_needle(STLString haystack, STLString needle) noexcept; /// =========================================================== /// /// find a word within strict conditions and returns a range of it. @@ -103,30 +110,29 @@ bool find_word(STLString haystack, STLString needle) noexcept; /// \param needle /// \return position of needle. /// =========================================================== /// -SizeType find_word_range(STLString haystack, STLString needle) noexcept; +SizeType ast_find_needle_range(STLString haystack, STLString needle) noexcept; /// =========================================================== /// /// @brief Compiler backend, implements a frontend, such as C, C++... /// See Toolchain, for some examples. /// =========================================================== /// -class CompilerFrontendInterface { +class ICompilerFrontend { public: - explicit CompilerFrontendInterface() = default; - virtual ~CompilerFrontendInterface() = default; + explicit ICompilerFrontend() = default; + virtual ~ICompilerFrontend() = default; - NECTI_COPY_DEFAULT(CompilerFrontendInterface); + NECTI_COPY_DEFAULT(ICompilerFrontend) /// =========================================================== /// - // NOTE: cast this to your user defined ast. + /// NOTE: cast this to your user defined ast. /// =========================================================== /// - typedef VoidPtr AstType; + using AstType = VoidPtr; /// =========================================================== /// //! @brief Compile a syntax tree ouf of the text. //! Also takes the source file name for metadata. /// =========================================================== /// - - virtual CompilerKit::SyntaxLeafList::SyntaxLeaf Compile(std::string text, std::string file) = 0; + virtual SyntaxLeafList::SyntaxLeaf Compile(STLString text, STLString file) = 0; /// =========================================================== /// //! @brief What language are we dealing with? diff --git a/include/CompilerKit/AST.inl b/include/CompilerKit/AST.inl index 3dc9456..c051815 100644 --- a/include/CompilerKit/AST.inl +++ b/include/CompilerKit/AST.inl @@ -9,7 +9,7 @@ namespace CompilerKit { /// \param haystack base string /// \param needle the string we search for. /// \return if we found it or not. -inline bool find_word(STLString haystack, STLString needle) noexcept { +inline bool ast_find_needle(STLString haystack, STLString needle) noexcept { auto index = haystack.find(needle); // check for needle validity. @@ -31,7 +31,7 @@ inline bool find_word(STLString haystack, STLString needle) noexcept { /// \param haystack /// \param needle /// \return position of needle. -inline SizeType find_word_range(STLString haystack, STLString needle) noexcept { +inline SizeType ast_find_needle_range(STLString haystack, STLString needle) noexcept { auto index = haystack.find(needle); // check for needle validity. @@ -50,14 +50,14 @@ inline SizeType find_word_range(STLString haystack, STLString needle) noexcept { /// =========================================================== /// //! @brief What language are we dealing with? /// =========================================================== /// -inline const char* CompilerFrontendInterface::Language() { +inline const char* ICompilerFrontend::Language() { return kInvalidFrontend; } /// =========================================================== /// /// @brief Checks if language is a valid frontend. /// =========================================================== /// -inline bool CompilerFrontendInterface::IsValid() { +inline bool ICompilerFrontend::IsValid() { return strcmp(this->Language(), kInvalidFrontend) > 0; } } // namespace CompilerKit
\ No newline at end of file diff --git a/include/CompilerKit/Detail/32x0.h b/include/CompilerKit/Detail/32x0.h index fc3e0e9..7172a2d 100644 --- a/include/CompilerKit/Detail/32x0.h +++ b/include/CompilerKit/Detail/32x0.h @@ -9,8 +9,8 @@ #include <CompilerKit/Detail/Config.h> #include <vector> -// @brief Open32x0 support. -// @file Detail/32x0.h +/// @brief Open32x0 support. +/// @file Detail/32x0.h #define CK_ASM_OPCODE(__NAME, __OPCODE, __FUNCT3, __FUNCT7) \ {.fName = __NAME, .fOpcode = __OPCODE, .fFunct3 = __FUNCT3, .fFunct7 = __FUNCT7}, diff --git a/include/CompilerKit/Detail/64x0.h b/include/CompilerKit/Detail/64x0.h index ba539f6..ad909dc 100644 --- a/include/CompilerKit/Detail/64x0.h +++ b/include/CompilerKit/Detail/64x0.h @@ -9,8 +9,8 @@ #include <CompilerKit/Detail/Config.h> #include <vector> -// @brief Open64x0 support. -// @file Detail/64x0.h +/// @brief Open64x0 support. +/// @file Detail/64x0.h #define CK_ASM_OPCODE(__NAME, __OPCODE, __FUNCT3, __FUNCT7) \ {.fName = __NAME, .fOpcode = __OPCODE, .fFunct3 = __FUNCT3, .fFunct7 = __FUNCT7}, diff --git a/include/CompilerKit/Detail/AMD64.h b/include/CompilerKit/Detail/AMD64.h index a123c02..053b0b0 100644 --- a/include/CompilerKit/Detail/AMD64.h +++ b/include/CompilerKit/Detail/AMD64.h @@ -9,8 +9,8 @@ #include <CompilerKit/Detail/Config.h> #include <vector> -// @brief AMD64 support. -// @file Detail/AMD64.h +/// @brief AMD64 support. +/// @file Detail/AMD64.h #define CK_ASM_OPCODE(__NAME, __OPCODE) {.fName = __NAME, .fOpcode = __OPCODE}, diff --git a/include/CompilerKit/ErrorOr.h b/include/CompilerKit/ErrorOr.h index 100624e..db8339a 100644 --- a/include/CompilerKit/ErrorOr.h +++ b/include/CompilerKit/ErrorOr.h @@ -29,6 +29,8 @@ class ErrorOr final { ~ErrorOr() = default; public: + using RefType = Ref<T>; + explicit ErrorOr(ErrorT err) : mId(err) {} explicit ErrorOr(std::nullptr_t null) {} explicit ErrorOr(T klass) : mRef(klass) {} @@ -36,7 +38,7 @@ class ErrorOr final { ErrorOr& operator=(const ErrorOr&) = default; ErrorOr(const ErrorOr&) = default; - Ref<T>& Leak() { return mRef; } + RefType& Leak() { return mRef; } ErrorT Error() { return mId; } @@ -45,8 +47,8 @@ class ErrorOr final { explicit operator bool() { return mRef; } private: - Ref<T> mRef; - ErrorT mId{0}; + RefType mRef; + ErrorT mId{0}; }; using ErrorOrAny = ErrorOr<VoidPtr>; diff --git a/include/CompilerKit/Utilities/Assembler.h b/include/CompilerKit/Utilities/Assembler.h index afe95d1..ef1cf18 100644 --- a/include/CompilerKit/Utilities/Assembler.h +++ b/include/CompilerKit/Utilities/Assembler.h @@ -17,18 +17,20 @@ namespace CompilerKit { /// @return A numbercast of 32-bit width. inline NumberCast32 GetNumber32(STLString lineBuffer, STLString numberKey) { if (lineBuffer.empty()) return {}; + if (lineBuffer.find(numberKey) == STLString::npos) return {}; auto pos = lineBuffer.find(numberKey) + numberKey.size(); - while (lineBuffer[pos] == ' ') { - ++pos; - } + if (pos > lineBuffer.size()) return {}; + if ((pos + 1) > lineBuffer.size()) return {}; + + while (lineBuffer[pos] == ' ') ++pos; switch (lineBuffer[pos + 1]) { case 'x': { if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 16); !res) { if (errno != 0) { - CompilerKit::Detail::print_error("invalid hex number: " + lineBuffer, "CompilerKit"); + Detail::print_error("invalid hex number: " + lineBuffer, "CompilerKit"); throw std::runtime_error("invalid_hex"); } } @@ -44,7 +46,7 @@ inline NumberCast32 GetNumber32(STLString lineBuffer, STLString numberKey) { case 'b': { if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 2); !res) { if (errno != 0) { - CompilerKit::Detail::print_error("invalid binary number:" + lineBuffer, "CompilerKit"); + Detail::print_error("invalid binary number:" + lineBuffer, "CompilerKit"); throw std::runtime_error("invalid_bin"); } } @@ -60,7 +62,7 @@ inline NumberCast32 GetNumber32(STLString lineBuffer, STLString numberKey) { case 'o': { if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 7); !res) { if (errno != 0) { - CompilerKit::Detail::print_error("invalid octal number: " + lineBuffer, "CompilerKit"); + Detail::print_error("invalid octal number: " + lineBuffer, "CompilerKit"); throw std::runtime_error("invalid_octal"); } } @@ -76,7 +78,7 @@ inline NumberCast32 GetNumber32(STLString lineBuffer, STLString numberKey) { default: { if (auto res = strtol(lineBuffer.substr(pos).c_str(), nullptr, 10); !res) { if (errno != 0) { - CompilerKit::Detail::print_error("invalid hex number: " + lineBuffer, "CompilerKit"); + Detail::print_error("invalid hex number: " + lineBuffer, "CompilerKit"); throw std::runtime_error("invalid_hex"); } } diff --git a/include/CompilerKit/Utilities/DLL.h b/include/CompilerKit/Utilities/DLL.h index 5bfe032..490017e 100644 --- a/include/CompilerKit/Utilities/DLL.h +++ b/include/CompilerKit/Utilities/DLL.h @@ -10,21 +10,18 @@ #include <dlfcn.h> #include <mutex> -struct CompilerKitDylibTraits; +namespace CompilerKit { +struct DLLTraits final { + typedef Int32 (*Entrypoint)(Int32 argc, Char const* argv[]); + using DLL = VoidPtr; -typedef Int32 (*CompilerKitEntrypoint)(Int32 argc, Char const* argv[]); -typedef VoidPtr CompilerKitDylib; + DLL fDylib{nullptr}; + Entrypoint fEntrypoint{nullptr}; + std::mutex fMutex; -struct CompilerKitDylibTraits final { - CompilerKitDylib fDylib{nullptr}; - CompilerKitEntrypoint fEntrypoint{nullptr}; - std::mutex fMutex; + explicit operator bool() { return fDylib && fEntrypoint; } - explicit operator bool() { - return fDylib && fEntrypoint; - } - - CompilerKitDylibTraits& operator()(const Char* path, const Char* fEntrypoint) { + DLLTraits& operator()(const Char* path, const Char* fEntrypoint) { std::lock_guard<std::mutex> lock(this->fMutex); if (!path || !fEntrypoint) return *this; @@ -40,7 +37,7 @@ struct CompilerKitDylibTraits final { return *this; } - this->fEntrypoint = (CompilerKitEntrypoint) dlsym(this->fDylib, fEntrypoint); + this->fEntrypoint = (Entrypoint) dlsym(this->fDylib, fEntrypoint); if (!this->fEntrypoint) { dlclose(this->fDylib); @@ -52,11 +49,11 @@ struct CompilerKitDylibTraits final { return *this; } - NECTI_COPY_DELETE(CompilerKitDylibTraits); + NECTI_COPY_DELETE(DLLTraits) - CompilerKitDylibTraits() = default; + explicit DLLTraits() = default; - ~CompilerKitDylibTraits() { + ~DLLTraits() { if (this->fDylib) { dlclose(this->fDylib); this->fDylib = nullptr; @@ -65,3 +62,4 @@ struct CompilerKitDylibTraits final { this->fEntrypoint = nullptr; } }; +} // namespace CompilerKit
\ No newline at end of file |
