From 730b76e1aae96f9a5cafc13634d6f014e598abaf Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 15 Nov 2025 22:35:55 +0100 Subject: feat: API and codebase tweaks. Signed-off-by: Amlal El Mahrouss --- dev/CompilerKit/ErrorOr.h | 2 +- dev/CompilerKit/Frontend.h | 3 +- dev/CompilerKit/Macros.h | 4 -- dev/CompilerKit/Ref.h | 6 +-- dev/CompilerKit/Version.h | 2 +- dev/CompilerKit/src/AssemblyFactory.cc | 2 +- dev/CompilerKit/src/Backend/Assembler32x0.cc | 2 +- dev/CompilerKit/src/Backend/Assembler64x0.cc | 2 +- dev/CompilerKit/src/Backend/AssemblerAMD64.cc | 2 +- dev/CompilerKit/src/Backend/AssemblerARM64.cc | 2 +- dev/CompilerKit/src/Backend/AssemblerPowerPC.cc | 2 +- dev/CompilerKit/src/Frontend.cc | 61 ++++++++++++++++++++++ dev/CompilerKit/src/Frontend/CCompiler64x0.cc | 2 +- dev/CompilerKit/src/Frontend/CCompilerARM64.cc | 2 +- dev/CompilerKit/src/Frontend/CCompilerPower64.cc | 2 +- .../src/Frontend/CPlusPlusCompilerAMD64.cc | 53 ++++++++++--------- dev/CompilerKit/src/FrontendHelpers.cc | 51 ------------------ dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc | 2 +- dev/CompilerKit/src/Macro/CPlusPlusPreprocessor.cc | 2 +- dev/CompilerKit/src/StringKit.cc | 2 +- 20 files changed, 109 insertions(+), 97 deletions(-) create mode 100644 dev/CompilerKit/src/Frontend.cc delete mode 100644 dev/CompilerKit/src/FrontendHelpers.cc (limited to 'dev/CompilerKit') diff --git a/dev/CompilerKit/ErrorOr.h b/dev/CompilerKit/ErrorOr.h index 3eebb12..c86e38d 100644 --- a/dev/CompilerKit/ErrorOr.h +++ b/dev/CompilerKit/ErrorOr.h @@ -44,6 +44,6 @@ class ErrorOr final { ErrorT mId{0}; }; -using ErrorOrAny = ErrorOr; +using ErrorOrAny = ErrorOr; using ErrorOrString = ErrorOr; } // namespace CompilerKit diff --git a/dev/CompilerKit/Frontend.h b/dev/CompilerKit/Frontend.h index efafae8..8ae43e4 100644 --- a/dev/CompilerKit/Frontend.h +++ b/dev/CompilerKit/Frontend.h @@ -11,7 +11,7 @@ #define CK_COMPILER_FRONTEND : public ::CompilerKit::CompilerFrontendInterface namespace CompilerKit { -inline static auto kInvalidFrontend = "?"; +inline static auto kInvalidFrontend = "(null)"; struct SyntaxLeafList; struct SyntaxLeafList; @@ -113,6 +113,7 @@ class CompilerFrontendInterface { //! @brief What language are we dealing with? virtual const char* Language() { return kInvalidFrontend; } + /// @brief Checks if language is a valid frontend. virtual bool IsValid() { return strcmp(this->Language(), kInvalidFrontend) > 0; } }; } // namespace CompilerKit diff --git a/dev/CompilerKit/Macros.h b/dev/CompilerKit/Macros.h index 17e1b21..5d6f9bc 100644 --- a/dev/CompilerKit/Macros.h +++ b/dev/CompilerKit/Macros.h @@ -25,9 +25,5 @@ KLASS& operator=(KLASS&&) = default; \ KLASS(KLASS&&) = default; -/// @note xxxx is the error placeholder, in hexadecimal. -#define NECTI_ERROR_PREFIX_CXX "CXXxxxx" -#define NECTI_ERROR_PREFIX_CL "CLxxxx" -#define NECTI_ERROR_PREFIX_ASM "ASMxxxx" #endif /* ifndef _NECTI_MACROS_H_ */ diff --git a/dev/CompilerKit/Ref.h b/dev/CompilerKit/Ref.h index 381b628..10082b3 100644 --- a/dev/CompilerKit/Ref.h +++ b/dev/CompilerKit/Ref.h @@ -13,8 +13,8 @@ #include namespace CompilerKit { -// @author EL Mahrouss Amlal -// @brief Reference holder class, refers to a pointer of data in static memory. +/// @author El Mahrouss Amlal +/// @brief Reference holder class, refers to a pointer of data in static memory. template class Ref final { public: @@ -54,7 +54,7 @@ class Ref final { Bool m_Strong{false}; }; -// @author EL Mahrouss Amlal +// @author El Mahrouss Amlal // @brief Non null Reference holder class, refers to a pointer of data in static memory. template class NonNullRef final { diff --git a/dev/CompilerKit/Version.h b/dev/CompilerKit/Version.h index 93a7c00..2836165 100644 --- a/dev/CompilerKit/Version.h +++ b/dev/CompilerKit/Version.h @@ -6,7 +6,7 @@ #pragma once -#define kDistVersion "v0.0.2-libcompiler" +#define kDistVersion "v0.0.6-compilerkit" #define kDistVersionBCD 0x0002 #define ToString(X) Stringify(X) diff --git a/dev/CompilerKit/src/AssemblyFactory.cc b/dev/CompilerKit/src/AssemblyFactory.cc index 1f08b32..b047c04 100644 --- a/dev/CompilerKit/src/AssemblyFactory.cc +++ b/dev/CompilerKit/src/AssemblyFactory.cc @@ -10,7 +10,7 @@ /** * @file AssemblyFactory.cc * @author Amlal El Mahrouss (amlal@nekernel.org) - * @brief Compiler API of NeCTI + * @brief Assembly API of NeCTI * @version 0.0.2 * * @copyright Copyright (c) 2024-2025 Amlal El Mahrouss diff --git a/dev/CompilerKit/src/Backend/Assembler32x0.cc b/dev/CompilerKit/src/Backend/Assembler32x0.cc index 5534f59..0fd83d2 100644 --- a/dev/CompilerKit/src/Backend/Assembler32x0.cc +++ b/dev/CompilerKit/src/Backend/Assembler32x0.cc @@ -9,7 +9,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// // @file 32asm.cc -// @author EL Mahrouss Amlal +// @author El Mahrouss Amlal // @brief 32x0 Assembler. // REMINDER: when dealing with an undefined symbol use (string diff --git a/dev/CompilerKit/src/Backend/Assembler64x0.cc b/dev/CompilerKit/src/Backend/Assembler64x0.cc index 7aa991b..435af3c 100644 --- a/dev/CompilerKit/src/Backend/Assembler64x0.cc +++ b/dev/CompilerKit/src/Backend/Assembler64x0.cc @@ -9,7 +9,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// // @file Assembler64x0.cc -// @author EL Mahrouss Amlal +// @author El Mahrouss Amlal // @brief 64x000 Assembler. // REMINDER: when dealing with an undefined symbol use (string diff --git a/dev/CompilerKit/src/Backend/AssemblerAMD64.cc b/dev/CompilerKit/src/Backend/AssemblerAMD64.cc index ec71f9b..03e3db0 100644 --- a/dev/CompilerKit/src/Backend/AssemblerAMD64.cc +++ b/dev/CompilerKit/src/Backend/AssemblerAMD64.cc @@ -7,7 +7,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// /// @file AssemblerAMD64.cc -/// @author EL Mahrouss Amlal +/// @author El Mahrouss Amlal /// @brief AMD64 Assembler. /// REMINDER: when dealing with an undefined symbol use (string /// size):LinkerFindSymbol:(string) so that ld will look for it. diff --git a/dev/CompilerKit/src/Backend/AssemblerARM64.cc b/dev/CompilerKit/src/Backend/AssemblerARM64.cc index fb03da8..59002a3 100644 --- a/dev/CompilerKit/src/Backend/AssemblerARM64.cc +++ b/dev/CompilerKit/src/Backend/AssemblerARM64.cc @@ -7,7 +7,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// /// @file AssemblerARM64.cc -/// @author EL Mahrouss Amlal +/// @author El Mahrouss Amlal /// @brief 'ACORN' Assembler. /// REMINDER: when dealing with an undefined symbol use (string diff --git a/dev/CompilerKit/src/Backend/AssemblerPowerPC.cc b/dev/CompilerKit/src/Backend/AssemblerPowerPC.cc index ffcc102..7fddffc 100644 --- a/dev/CompilerKit/src/Backend/AssemblerPowerPC.cc +++ b/dev/CompilerKit/src/Backend/AssemblerPowerPC.cc @@ -7,7 +7,7 @@ ///////////////////////////////////////////////////////////////////////////////////////// /// @file AssemblerPower.cc -/// @author EL Mahrouss Amlal +/// @author El Mahrouss Amlal /// @brief POWER Assembler. /// REMINDER: when dealing with an undefined symbol use (string diff --git a/dev/CompilerKit/src/Frontend.cc b/dev/CompilerKit/src/Frontend.cc new file mode 100644 index 0000000..ef7b79f --- /dev/null +++ b/dev/CompilerKit/src/Frontend.cc @@ -0,0 +1,61 @@ +/* ------------------------------------------- + + Copyright (C) 2025 Amlal EL Mahrouss, all rights reserved + +------------------------------------------- */ + +#include + +/** + * @file Frontend.cc + * @author Amlal El Mahrouss (amlal@nekernel.org) + * @brief Frontend API of NeCTI + * @version 0.0.2 + * + * @copyright Copyright (c) 2025 Amlal El Mahrouss and NeKernel.org Contributors + * + */ + +namespace CompilerKit { +/// 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. +Bool find_word(STLString haystack, STLString needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == STLString::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. +SizeType find_word_range(STLString haystack, STLString needle) noexcept { + auto index = haystack.find(needle); + + // check for needle validity. + if (index == STLString::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 STLString::npos; +} +} // namespace CompilerKit \ No newline at end of file diff --git a/dev/CompilerKit/src/Frontend/CCompiler64x0.cc b/dev/CompilerKit/src/Frontend/CCompiler64x0.cc index c23f255..1b04260 100644 --- a/dev/CompilerKit/src/Frontend/CCompiler64x0.cc +++ b/dev/CompilerKit/src/Frontend/CCompiler64x0.cc @@ -27,7 +27,7 @@ /* This is part of the CompilerKit. */ /* (c) Amlal El Mahrouss */ -/// @author EL Mahrouss Amlal (amlel) +/// @author El Mahrouss Amlal (amlel) /// @file 64x0-cc.cc /// @brief 64x0 C Compiler. diff --git a/dev/CompilerKit/src/Frontend/CCompilerARM64.cc b/dev/CompilerKit/src/Frontend/CCompilerARM64.cc index 88e2113..5c03d41 100644 --- a/dev/CompilerKit/src/Frontend/CCompilerARM64.cc +++ b/dev/CompilerKit/src/Frontend/CCompilerARM64.cc @@ -28,7 +28,7 @@ /* This is part of the CompilerKit. */ /* (c) Amlal El Mahrouss */ -/// @author EL Mahrouss Amlal (amlel) +/// @author El Mahrouss Amlal (amlel) /// @file ARM64-cc.cc /// @brief ARM64 C Compiler. diff --git a/dev/CompilerKit/src/Frontend/CCompilerPower64.cc b/dev/CompilerKit/src/Frontend/CCompilerPower64.cc index 39bb58c..2167e21 100644 --- a/dev/CompilerKit/src/Frontend/CCompilerPower64.cc +++ b/dev/CompilerKit/src/Frontend/CCompilerPower64.cc @@ -22,7 +22,7 @@ #define kExitOK 0 -/// @author EL Mahrouss Amlal (amlal@nekernel.org) +/// @author El Mahrouss Amlal (amlal@nekernel.org) /// @file cc.cc /// @brief POWER64 C Compiler. diff --git a/dev/CompilerKit/src/Frontend/CPlusPlusCompilerAMD64.cc b/dev/CompilerKit/src/Frontend/CPlusPlusCompilerAMD64.cc index 2539f1f..7ef3dba 100644 --- a/dev/CompilerKit/src/Frontend/CPlusPlusCompilerAMD64.cc +++ b/dev/CompilerKit/src/Frontend/CPlusPlusCompilerAMD64.cc @@ -9,12 +9,28 @@ /// BUGS: 0 +/////////////////////// + +// ANSI ESCAPE CODES // + +/////////////////////// + +/////////////////////// + +// MACROS // + +/////////////////////// + #define kPrintF printf #define kPrintErr std::cerr #define kExitOK (EXIT_SUCCESS) #define kExitNO (EXIT_FAILURE) +#define kBlank "\e[0;30m" +#define kRed "\e[0;31m" +#define kWhite "\e[0;97m" + #include #include #include @@ -27,20 +43,10 @@ /* This is part of the CompilerKit. */ /* (c) Amlal El Mahrouss 2024-2025 */ -/// @author EL Mahrouss Amlal (amlal@nekernel.org) +/// @author El Mahrouss Amlal (amlal@nekernel.org) /// @file CPlusPlusCompilerAMD64.cxx /// @brief Optimized C++ Compiler Driver. -/////////////////////// - -// ANSI ESCAPE CODES // - -/////////////////////// - -#define kBlank "\e[0;30m" -#define kRed "\e[0;31m" -#define kWhite "\e[0;97m" - ///////////////////////////////////// // INTERNALS OF THE C++ COMPILER @@ -49,7 +55,7 @@ /// @internal // Avoids relative_path which could discard parts of the original. -std::filesystem::path expand_home(const std::filesystem::path& input) { +std::filesystem::path necti_expand_home(const std::filesystem::path& input) { const std::string& raw = input.string(); if (!raw.empty() && raw[0] == '~') { @@ -115,7 +121,7 @@ static std::vector kKeywords; ///////////////////////////////////////// -static CompilerKit::AssemblyFactory kFactory; +static CompilerKit::AssemblyFactory kAssembler; static Boolean kInStruct = false; static Boolean kOnWhileLoop = false; static Boolean kOnForLoop = false; @@ -138,7 +144,7 @@ class CompilerFrontendCPlusPlusAMD64 final CK_COMPILER_FRONTEND { /// @internal compiler variables -static CompilerFrontendCPlusPlusAMD64* kCompilerFrontend = nullptr; +static CompilerFrontendCPlusPlusAMD64* kFrontend = nullptr; static std::vector kRegisterMap; @@ -739,7 +745,7 @@ class AssemblyCPlusPlusInterfaceAMD64 final CK_ASSEMBLY_INTERFACE { UInt32 Arch() noexcept override { return CompilerKit::AssemblyFactory::kArchAMD64; } Int32 CompileToFormat(CompilerKit::STLString src, Int32 arch) override { - if (kCompilerFrontend == nullptr) return kExitNO; + if (kFrontend == nullptr) return kExitNO; CompilerKit::STLString dest = src; dest += ".pp.masm"; @@ -753,7 +759,7 @@ class AssemblyCPlusPlusInterfaceAMD64 final CK_ASSEMBLY_INTERFACE { out_fp << "#org " << kOrigin << "\n\n"; while (std::getline(src_fp, line_source)) { - out_fp << kCompilerFrontend->Compile(line_source, src).fUserValue; + out_fp << kFrontend->Compile(line_source, src).fUserValue; } return kExitOK; @@ -830,15 +836,15 @@ NECTI_MODULE(CompilerCPlusPlusAMD64) { kErrorLimit = 0; - kCompilerFrontend = new CompilerFrontendCPlusPlusAMD64(); - kFactory.Mount(new AssemblyCPlusPlusInterfaceAMD64()); + kFrontend = new CompilerFrontendCPlusPlusAMD64(); + kAssembler.Mount(new AssemblyCPlusPlusInterfaceAMD64()); CompilerKit::install_signal(SIGSEGV, Detail::drvi_crash_handler); // Ensure cleanup on exit std::atexit([]() { - delete kCompilerFrontend; - kCompilerFrontend = nullptr; + delete kFrontend; + kFrontend = nullptr; }); for (auto index = 1UL; index < argc; ++index) { @@ -852,12 +858,11 @@ NECTI_MODULE(CompilerCPlusPlusAMD64) { if (strcmp(argv[index], "-cxx-verbose") == 0) { kVerbose = true; - continue; } if (strcmp(argv[index], "-cxx-dialect") == 0) { - if (kCompilerFrontend) std::cout << kCompilerFrontend->Language() << "\n"; + if (kFrontend) std::cout << kFrontend->Language() << "\n"; return NECTI_SUCCESS; } @@ -890,7 +895,7 @@ NECTI_MODULE(CompilerCPlusPlusAMD64) { for (CompilerKit::STLString ext : exts) { if (argv_i.ends_with(ext)) { - if (kFactory.Compile(argv_i, kMachine) != kExitOK) { + if (kAssembler.Compile(argv_i, kMachine) != kExitOK) { return NECTI_INVALID_DATA; } @@ -899,7 +904,7 @@ NECTI_MODULE(CompilerCPlusPlusAMD64) { } } - kFactory.Unmount(); + kAssembler.Unmount(); return NECTI_SUCCESS; } diff --git a/dev/CompilerKit/src/FrontendHelpers.cc b/dev/CompilerKit/src/FrontendHelpers.cc deleted file mode 100644 index dc81fa2..0000000 --- a/dev/CompilerKit/src/FrontendHelpers.cc +++ /dev/null @@ -1,51 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2025 Amlal EL Mahrouss, all rights reserved - -------------------------------------------- */ - -#include - -namespace CompilerKit { -/// 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. -Bool find_word(STLString haystack, STLString needle) noexcept { - auto index = haystack.find(needle); - - // check for needle validity. - if (index == STLString::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. -SizeType find_word_range(STLString haystack, STLString needle) noexcept { - auto index = haystack.find(needle); - - // check for needle validity. - if (index == STLString::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 STLString::npos; -} -} // namespace CompilerKit \ No newline at end of file diff --git a/dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc b/dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc index 617ee03..efc4c83 100644 --- a/dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc +++ b/dev/CompilerKit/src/Linker/DynamicLinker64PEF.cc @@ -7,7 +7,7 @@ ------------------------------------------- */ -/// @author EL Mahrouss Amlal (amlal@nekernel.org) +/// @author El Mahrouss Amlal (amlal@nekernel.org) /// @brief NeKernel.org 64-bit PEF Linker. /// Last Rev: Sat Apr 19 CET 2025 /// @note Do not look up for anything with .code64/.data64/.zero64! diff --git a/dev/CompilerKit/src/Macro/CPlusPlusPreprocessor.cc b/dev/CompilerKit/src/Macro/CPlusPlusPreprocessor.cc index 3a649a7..2f16d01 100644 --- a/dev/CompilerKit/src/Macro/CPlusPlusPreprocessor.cc +++ b/dev/CompilerKit/src/Macro/CPlusPlusPreprocessor.cc @@ -20,7 +20,7 @@ #define kMacroPrefix '#' -/// @author EL Mahrouss Amlal (amlel) +/// @author El Mahrouss Amlal (amlel) /// @file CPlusPlusPreprocessor.cc /// @brief Preprocessor. diff --git a/dev/CompilerKit/src/StringKit.cc b/dev/CompilerKit/src/StringKit.cc index 67f2f55..35775f1 100644 --- a/dev/CompilerKit/src/StringKit.cc +++ b/dev/CompilerKit/src/StringKit.cc @@ -8,7 +8,7 @@ */ /** - * @file BasicString.cc + * @file StringKit.cc * @author Amlal (amlal@nekernel.org) * @brief C++ string manipulation API. * @version 0.2 -- cgit v1.2.3