From 9f031e69aace747feb5bac78eccb9a1d5df81f74 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 14 May 2025 17:49:19 +0200 Subject: feat(cc): Rename Parser.h to CompilerFrontend.h, refactor codebase accordingly. why: - To make its intent clearer, and avoid future confusions. also: - Ran ./format.sh to the codebase. Signed-off-by: Amlal El Mahrouss --- dev/LibCompiler/CompilerFrontend.h | 149 +++++++++++++++++++++ dev/LibCompiler/Detail/AsmUtils.h | 2 +- dev/LibCompiler/Detail/ClUtils.h | 2 +- dev/LibCompiler/Parser.h | 149 --------------------- dev/LibCompiler/src/Assembler32x0.cc | 2 +- dev/LibCompiler/src/Assembler64x0.cc | 2 +- dev/LibCompiler/src/AssemblerAMD64.cc | 2 +- dev/LibCompiler/src/AssemblerARM64.cc | 2 +- dev/LibCompiler/src/AssemblerPowerPC.cc | 2 +- dev/LibCompiler/src/CCompiler64x0.cc | 2 +- dev/LibCompiler/src/CCompilerARM64.cc | 2 +- dev/LibCompiler/src/CCompilerPower64.cc | 2 +- dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc | 2 +- .../src/CPlusPlusCompilerPreProcessor.cc | 2 +- 14 files changed, 161 insertions(+), 161 deletions(-) create mode 100644 dev/LibCompiler/CompilerFrontend.h delete mode 100644 dev/LibCompiler/Parser.h (limited to 'dev/LibCompiler') diff --git a/dev/LibCompiler/CompilerFrontend.h b/dev/LibCompiler/CompilerFrontend.h new file mode 100644 index 0000000..858473b --- /dev/null +++ b/dev/LibCompiler/CompilerFrontend.h @@ -0,0 +1,149 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + +------------------------------------------- */ + +#pragma once + +#include + +namespace LibCompiler { +inline auto kInvalidFrontend = "NA"; + +/// @brief Compiler backend, implements a frontend, such as C, C++... +/// See Toolchain, for some examples. +class ICompilerFrontend { + public: + explicit ICompilerFrontend() = default; + virtual ~ICompilerFrontend() = default; + + LIBCOMPILER_COPY_DEFAULT(ICompilerFrontend); + + // NOTE: cast this to your user defined ast. + typedef void* AstType; + + //! @brief Compile a syntax tree ouf of the text. + //! Also takes the source file name for metadata. + + virtual bool Compile(std::string text, std::string file) = 0; + + //! @brief What language are we dealing with? + virtual const char* Language() { return kInvalidFrontend; } + + virtual bool IsValid() { return strcmp(this->Language(), kInvalidFrontend) > 0; } +}; + +struct SyntaxLeafList; +struct SyntaxLeafList; +struct CompilerKeyword; + +/// we want to do that because to separate keywords. +enum KeywordKind { + kKeywordKindNamespace, + kKeywordKindFunctionStart, + kKeywordKindFunctionEnd, + kKeywordKindVariable, + kKeywordKindVariablePtr, + kKeywordKindType, + kKeywordKindTypePtr, + kKeywordKindExpressionBegin, + kKeywordKindExpressionEnd, + kKeywordKindArgSeparator, + kKeywordKindBodyStart, + kKeywordKindBodyEnd, + kKeywordKindClass, + kKeywordKindPtrAccess, + kKeywordKindAccess, + kKeywordKindIf, + kKeywordKindElse, + kKeywordKindElseIf, + kKeywordKindVariableAssign, + kKeywordKindVariableDec, + kKeywordKindVariableInc, + kKeywordKindConstant, + kKeywordKindTypedef, + kKeywordKindEndInstr, + kKeywordKindSpecifier, + kKeywordKindInvalid, + kKeywordKindReturn, + kKeywordKindCommentInline, + kKeywordKindCommentMultiLineStart, + kKeywordKindCommentMultiLineEnd, + kKeywordKindEq, + kKeywordKindNotEq, + kKeywordKindGreaterEq, + kKeywordKindLessEq, + kKeywordKindPtr, +}; + +/// \brief Compiler keyword information struct. +struct CompilerKeyword { + std::string keyword_name; + KeywordKind keyword_kind = kKeywordKindInvalid; +}; +struct SyntaxLeafList final { + struct SyntaxLeaf final { + Int32 fUserType; +#ifdef LC_USE_STRUCTS + CompilerKeyword fUserData; +#else + std::string fUserData; +#endif + + SyntaxLeaf() = default; + + std::string fUserValue; + struct SyntaxLeaf* fNext; + }; + + std::vector fLeafList; + SizeType fNumLeafs; + + size_t SizeOf() { return fNumLeafs; } + std::vector& Get() { return fLeafList; } + SyntaxLeaf& At(size_t index) { return fLeafList[index]; } +}; + +/// find the perfect matching word in a haystack. +/// \param haystack base string +/// \param needle the string we search for. +/// \return if we found it or not. +inline bool find_word(std::string haystack, std::string needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == std::string::npos) return false; + + // declare lambda + auto not_part_of_word = [&](int index) { + if (std::isspace(haystack[index]) || std::ispunct(haystack[index])) return true; + + if (index <= 0 || index >= haystack.size()) return true; + + return false; + }; + + return not_part_of_word(index - 1) && not_part_of_word(index + needle.size()); +} + +/// find a word within strict conditions and returns a range of it. +/// \param haystack +/// \param needle +/// \return position of needle. +inline std::size_t find_word_range(std::string haystack, std::string needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == std::string::npos) return false; + + if (!isalnum((haystack[index + needle.size() + 1])) && + !isdigit(haystack[index + needle.size() + 1]) && + !isalnum((haystack[index - needle.size() - 1])) && + !isdigit(haystack[index - needle.size() - 1])) { + return index; + } + + return std::string::npos; +} +} // namespace LibCompiler diff --git a/dev/LibCompiler/Detail/AsmUtils.h b/dev/LibCompiler/Detail/AsmUtils.h index 559df5f..77805c1 100644 --- a/dev/LibCompiler/Detail/AsmUtils.h +++ b/dev/LibCompiler/Detail/AsmUtils.h @@ -7,7 +7,7 @@ #pragma once #include -#include +#include #include diff --git a/dev/LibCompiler/Detail/ClUtils.h b/dev/LibCompiler/Detail/ClUtils.h index f47101a..a809cdf 100644 --- a/dev/LibCompiler/Detail/ClUtils.h +++ b/dev/LibCompiler/Detail/ClUtils.h @@ -7,8 +7,8 @@ #pragma once #include +#include #include -#include #include #define kZero64Section ".zero64" diff --git a/dev/LibCompiler/Parser.h b/dev/LibCompiler/Parser.h deleted file mode 100644 index 6baff7e..0000000 --- a/dev/LibCompiler/Parser.h +++ /dev/null @@ -1,149 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved - -------------------------------------------- */ - -#pragma once - -#include - -namespace LibCompiler { -inline auto kInvalidFrontend = "NA"; - -/// @brief Compiler backend, implements a frontend, such as C, C++... -/// See Toolchain, for some examples. -class ICompilerFrontend { - public: - explicit ICompilerFrontend() = default; - virtual ~ICompilerFrontend() = default; - - LIBCOMPILER_COPY_DEFAULT(ICompilerFrontend); - - // NOTE: cast this to your user defined ast. - typedef void* AstType; - - //! @brief Compile a syntax tree ouf of the text. - //! Also takes the source file name for metadata. - - virtual bool Compile(std::string text, std::string file) = 0; - - //! @brief What language are we dealing with? - virtual const char* Language() { return kInvalidFrontend; } - - virtual bool IsValid() { return strcmp(this->Language(), kInvalidFrontend) > 0; } -}; - -struct SyntaxLeafList; -struct SyntaxLeafList; -struct CompilerKeyword; - -/// we want to do that because to separate keywords. -enum KeywordKind { - kKeywordKindNamespace, - kKeywordKindFunctionStart, - kKeywordKindFunctionEnd, - kKeywordKindVariable, - kKeywordKindVariablePtr, - kKeywordKindType, - kKeywordKindTypePtr, - kKeywordKindExpressionBegin, - kKeywordKindExpressionEnd, - kKeywordKindArgSeparator, - kKeywordKindBodyStart, - kKeywordKindBodyEnd, - kKeywordKindClass, - kKeywordKindPtrAccess, - kKeywordKindAccess, - kKeywordKindIf, - kKeywordKindElse, - kKeywordKindElseIf, - kKeywordKindVariableAssign, - kKeywordKindVariableDec, - kKeywordKindVariableInc, - kKeywordKindConstant, - kKeywordKindTypedef, - kKeywordKindEndInstr, - kKeywordKindSpecifier, - kKeywordKindInvalid, - kKeywordKindReturn, - kKeywordKindCommentInline, - kKeywordKindCommentMultiLineStart, - kKeywordKindCommentMultiLineEnd, - kKeywordKindEq, - kKeywordKindNotEq, - kKeywordKindGreaterEq, - kKeywordKindLessEq, - kKeywordKindPtr, -}; - -/// \brief Compiler keyword information struct. -struct CompilerKeyword { - std::string keyword_name; - KeywordKind keyword_kind = kKeywordKindInvalid; -}; -struct SyntaxLeafList final { - struct SyntaxLeaf final { - Int32 fUserType; -#ifdef LC_USE_STRUCTS - CompilerKeyword fUserData; -#else - std::string fUserData; -#endif - - SyntaxLeaf() = default; - - std::string fUserValue; - struct SyntaxLeaf* fNext; - }; - - std::vector fLeafList; - SizeType fNumLeafs; - - size_t SizeOf() { return fNumLeafs; } - std::vector& Get() { return fLeafList; } - SyntaxLeaf& At(size_t index) { return fLeafList[index]; } -}; - -/// find the perfect matching word in a haystack. -/// \param haystack base string -/// \param needle the string we search for. -/// \return if we found it or not. -inline bool find_word(std::string haystack, std::string needle) noexcept { - auto index = haystack.find(needle); - - // check for needle validity. - if (index == std::string::npos) return false; - - // declare lambda - auto not_part_of_word = [&](int index) { - if (std::isspace(haystack[index]) || std::ispunct(haystack[index])) return true; - - if (index <= 0 || index >= haystack.size()) return true; - - return false; - }; - - return not_part_of_word(index - 1) && not_part_of_word(index + needle.size()); -} - -/// find a word within strict conditions and returns a range of it. -/// \param haystack -/// \param needle -/// \return position of needle. -inline std::size_t find_word_range(std::string haystack, std::string needle) noexcept { - auto index = haystack.find(needle); - - // check for needle validity. - if (index == std::string::npos) return false; - - if (!isalnum((haystack[index + needle.size() + 1])) && - !isdigit(haystack[index + needle.size() + 1]) && - !isalnum((haystack[index - needle.size() - 1])) && - !isdigit(haystack[index - needle.size() - 1])) { - return index; - } - - return false; -} -} // namespace LibCompiler diff --git a/dev/LibCompiler/src/Assembler32x0.cc b/dev/LibCompiler/src/Assembler32x0.cc index ac24946..44850f6 100644 --- a/dev/LibCompiler/src/Assembler32x0.cc +++ b/dev/LibCompiler/src/Assembler32x0.cc @@ -21,8 +21,8 @@ #include #include +#include #include -#include ///////////////////// diff --git a/dev/LibCompiler/src/Assembler64x0.cc b/dev/LibCompiler/src/Assembler64x0.cc index 8483e9e..a7c3efc 100644 --- a/dev/LibCompiler/src/Assembler64x0.cc +++ b/dev/LibCompiler/src/Assembler64x0.cc @@ -21,9 +21,9 @@ #include #include +#include #include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/AssemblerAMD64.cc b/dev/LibCompiler/src/AssemblerAMD64.cc index 5e9a7cd..3fdb197 100644 --- a/dev/LibCompiler/src/AssemblerAMD64.cc +++ b/dev/LibCompiler/src/AssemblerAMD64.cc @@ -28,8 +28,8 @@ #include #include +#include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/AssemblerARM64.cc b/dev/LibCompiler/src/AssemblerARM64.cc index 8686edb..c8b66f7 100644 --- a/dev/LibCompiler/src/AssemblerARM64.cc +++ b/dev/LibCompiler/src/AssemblerARM64.cc @@ -19,10 +19,10 @@ #include #include +#include #include #include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/AssemblerPowerPC.cc b/dev/LibCompiler/src/AssemblerPowerPC.cc index f2c3be3..a438aed 100644 --- a/dev/LibCompiler/src/AssemblerPowerPC.cc +++ b/dev/LibCompiler/src/AssemblerPowerPC.cc @@ -19,10 +19,10 @@ #include #include +#include #include #include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/CCompiler64x0.cc b/dev/LibCompiler/src/CCompiler64x0.cc index 79d32ce..e43e16b 100644 --- a/dev/LibCompiler/src/CCompiler64x0.cc +++ b/dev/LibCompiler/src/CCompiler64x0.cc @@ -11,8 +11,8 @@ /// TODO: none #include +#include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/CCompilerARM64.cc b/dev/LibCompiler/src/CCompilerARM64.cc index a5ddf43..b60ef4f 100644 --- a/dev/LibCompiler/src/CCompilerARM64.cc +++ b/dev/LibCompiler/src/CCompilerARM64.cc @@ -11,8 +11,8 @@ /// TODO: none #include +#include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/CCompilerPower64.cc b/dev/LibCompiler/src/CCompilerPower64.cc index f2eba43..30a1ab3 100644 --- a/dev/LibCompiler/src/CCompilerPower64.cc +++ b/dev/LibCompiler/src/CCompilerPower64.cc @@ -8,8 +8,8 @@ */ #include +#include #include -#include #include #include #include diff --git a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc b/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc index 051529d..92bf9ad 100644 --- a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc +++ b/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc @@ -17,8 +17,8 @@ // extern_segment, @autodelete { ... }, fn foo() -> auto { ... } #include +#include #include -#include #include #include diff --git a/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc b/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc index 5d035da..9981f37 100644 --- a/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc +++ b/dev/LibCompiler/src/CPlusPlusCompilerPreProcessor.cc @@ -9,8 +9,8 @@ /// BUGS: 0 +#include #include -#include #include #include #include -- cgit v1.2.3