From cde435ba55f33799e429e1d4bc045143d55ccbcf Mon Sep 17 00:00:00 2001 From: PashaBibko Date: Mon, 21 Jul 2025 19:57:05 +0100 Subject: [PATCH] Compiles on UNIX based systems --- Common/File.h | 4 ++-- Common/IO.h | 17 ++++++++++++++++- Common/LXC.h | 12 ++++++++++++ Common/OS.h | 9 ++++++--- LXC/CMakeLists.txt | 8 ++++---- Lexer/inc/Lexer.h | 8 ++++---- Lexer/inc/Token.h | 17 +++++++++++++---- Lexer/src/Lexer.cpp | 16 +++++++--------- Lexer/src/Token.cpp | 2 +- 9 files changed, 65 insertions(+), 28 deletions(-) diff --git a/Common/File.h b/Common/File.h index d5212ec..1eea8ae 100644 --- a/Common/File.h +++ b/Common/File.h @@ -73,7 +73,7 @@ namespace LXC::Util }; // Finds the location of a given index within a file // - inline bool GetFileLocationAtIndex(FileLocation& location, const std::string& file, __int32 index) + inline bool GetFileLocationAtIndex(FileLocation& location, const std::string& file, uint32_t index) { // Resets location // location.line = 1; @@ -84,7 +84,7 @@ namespace LXC::Util return false; // Finds the location // - __int32 localIndex = 0; + uint32_t localIndex = 0; while (localIndex != index) { if (file[localIndex] == '\n') diff --git a/Common/IO.h b/Common/IO.h index e15042e..9b2f091 100644 --- a/Common/IO.h +++ b/Common/IO.h @@ -9,7 +9,7 @@ namespace LXC::Util { // Enum to translate to the Win32 code for the colors // - enum Color : WORD + enum Color { DEFAULT = 0x07, @@ -57,6 +57,8 @@ namespace LXC::Internal return sLog; } + #if defined(_WIN32) + // Base function for all derived Print() functions // template inline void WriteImpl(std::ostream& os, Args&&... args) @@ -73,6 +75,19 @@ namespace LXC::Internal if constexpr (col != Util::Color::DEFAULT) SetConsoleTextAttribute(hConsole, static_cast(Util::Color::DEFAULT)); } + + #elif defined(__unix__) + + template + inline void WriteImpl(std::ostream& os, Args&&... args) + { + (os << ... << std::forward(args)); + + if constexpr (newLine) + os << std::endl; + } + + #endif } namespace LXC::Util diff --git a/Common/LXC.h b/Common/LXC.h index 231ccc4..19ea8c3 100644 --- a/Common/LXC.h +++ b/Common/LXC.h @@ -1,8 +1,20 @@ #pragma once +// MSVC Built in macros for non-MSVC enviroments // + +#ifndef _UNLIKELY +#define _UNLIKELY [[unlikely]] +#endif // _UNLIKELY + +#ifndef _LIKELY +#define _LIKELY [[likely]] +#endif // _LIKELY + // Standard libraries // #include +#include +#include #include // LXC util files // diff --git a/Common/OS.h b/Common/OS.h index bc8cb73..1bced1a 100644 --- a/Common/OS.h +++ b/Common/OS.h @@ -1,10 +1,13 @@ #pragma once // Platform specific includes // -#ifdef _WIN32 +#if defined(_WIN32) #define NOMINMAX #define WIN32_LEAN_AND_MEAN #include +#elif defined(__unix__) + // No platform specific includes are needed for UNIX based systems // #else - #error "Code is currently only supported on Win32" -#endif // _WIN32 + #error "OS is not supported" +#endif + diff --git a/LXC/CMakeLists.txt b/LXC/CMakeLists.txt index 1ae5a82..bcd02dc 100644 --- a/LXC/CMakeLists.txt +++ b/LXC/CMakeLists.txt @@ -1,9 +1,9 @@ # Creates the .exe from the single source file of the app # -add_executable(LXC LXC.cpp) +add_executable(LXCR LXC.cpp) # Links the other binaries # -target_link_libraries(LXC PRIVATE Lexer) +target_link_libraries(LXCR PRIVATE Lexer) # Creates the precompiled header for the binary # -target_include_directories(LXC PRIVATE ${CMAKE_SOURCE_DIR}/Common) -target_precompile_headers(LXC PRIVATE ${CMAKE_SOURCE_DIR}/Common/LXC.h) +target_include_directories(LXCR PRIVATE ${CMAKE_SOURCE_DIR}/Common) +target_precompile_headers(LXCR PRIVATE ${CMAKE_SOURCE_DIR}/Common/LXC.h) diff --git a/Lexer/inc/Lexer.h b/Lexer/inc/Lexer.h index a99ace1..491d127 100644 --- a/Lexer/inc/Lexer.h +++ b/Lexer/inc/Lexer.h @@ -11,10 +11,10 @@ namespace LXC::Lexer // Trackers for the Lexer itself // const std::string& source; - __int32 index; + uint32_t index; LexerOutput out; - const __int32 len; + const uint32_t len; // Trackers for where the Lexer is within the user version of source // unsigned short column; @@ -32,7 +32,7 @@ namespace LXC::Lexer }; // Constructor to pass arguments through to the struct // - LexerError(Reason _reason, __int32 errorIndex, std::string _info = "") + LexerError(Reason _reason, uint32_t errorIndex, std::string _info = "") : reason(_reason), index(errorIndex), info(_info) {} @@ -51,7 +51,7 @@ namespace LXC::Lexer // Error information // const Reason reason; - const __int32 index; + const uint32_t index; const std::string info; }; diff --git a/Lexer/inc/Token.h b/Lexer/inc/Token.h index 6852465..f6bc2c9 100644 --- a/Lexer/inc/Token.h +++ b/Lexer/inc/Token.h @@ -83,11 +83,20 @@ namespace LXC::Lexer }; // Util function calculating wether a token is of a given class // - template static constexpr bool IsTypeClass(TokenType type) { return type & mask; } - template static constexpr bool IsTypeClass(Token token) { return token.type & mask; } + template static constexpr bool IsTypeClass(TokenType type) + { + using T = std::underlying_type_t; + return static_cast(type) & static_cast(mask); + } + + template static constexpr bool IsTypeClass(Token token) + { + using T = std::underlying_type_t; + return static_cast(token.type) & static_cast(mask); + } // Constructor to set the data of the token for more complex token types // - Token(const LexerContext& ctx, unsigned __int32 start, unsigned short len, TokenType _type); + Token(const LexerContext& ctx, uint32_t start, unsigned short len, TokenType _type); // Deconstructor to clean up the allocated memory // ~Token(); @@ -105,7 +114,7 @@ namespace LXC::Lexer const unsigned short length; // Start index of the token // - const unsigned __int32 index; + const uint32_t index; private: // The data of the token // diff --git a/Lexer/src/Lexer.cpp b/Lexer/src/Lexer.cpp index 13491ed..043ab70 100644 --- a/Lexer/src/Lexer.cpp +++ b/Lexer/src/Lexer.cpp @@ -15,7 +15,7 @@ namespace LXC::Internal return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } - static constexpr bool IsWhitespace(const char c) + bool IsWhitespace(const char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; } @@ -69,7 +69,7 @@ namespace LXC::Internal namespace LXC::Lexer { LexerContext::LexerContext(const std::string& _source) : - source(_source), index(0), out{}, len((__int32)_source.length()), column(0), line(0) + source(_source), index(0), out{}, len((uint32_t)_source.length()), column(0), line(0) {} Util::ReturnVal TokenizeFile(const std::string& fileContents) @@ -86,7 +86,7 @@ namespace LXC::Lexer bool inComment = false; - unsigned __int32 sectionStart = 0; + uint32_t sectionStart = 0; } trackers; @@ -111,7 +111,7 @@ namespace LXC::Lexer // Creates the token (if at the end of the string literal) // if (!trackers.inStrLiteral) - ctx.out.emplace_back(ctx, trackers.sectionStart + 1, (USHORT)(ctx.index - trackers.sectionStart - 1), Token::StringLiteral); + ctx.out.emplace_back(ctx, trackers.sectionStart + 1, (unsigned short)(ctx.index - trackers.sectionStart - 1), Token::StringLiteral); } else if (trackers.inStrLiteral) {} @@ -125,9 +125,7 @@ namespace LXC::Lexer // Checks for the end of the number literal to create the token // if (!Internal::IsNumeric(next)) _UNLIKELY { - - - ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), Token::NumLiteral); + ctx.out.emplace_back(ctx, trackers.sectionStart, (unsigned short)(ctx.index - trackers.sectionStart + 1), Token::NumLiteral); trackers.inNumLiteral = false; } } @@ -147,7 +145,7 @@ namespace LXC::Lexer auto it = Internal::keywords.find(fullWord); Token::TokenType tType = (it != Internal::keywords.end()) ? it->second : Token::Identifier; - ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), tType); + ctx.out.emplace_back(ctx, trackers.sectionStart, (unsigned short)(ctx.index - trackers.sectionStart + 1), tType); trackers.inIdentifier = false; } } @@ -168,7 +166,7 @@ namespace LXC::Lexer std::string_view fullSymbol(ctx.source.data() + trackers.sectionStart, ctx.index - trackers.sectionStart + 1); auto it = Internal::symbolAndOpMap.find(fullSymbol); if (it != Internal::symbolAndOpMap.end()) - ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), it->second); + ctx.out.emplace_back(ctx, trackers.sectionStart, (unsigned short)(ctx.index - trackers.sectionStart + 1), it->second); else return Util::FunctionFail(LexerError::UnknownSymbolOrOperand, trackers.sectionStart, std::string(fullSymbol)); diff --git a/Lexer/src/Token.cpp b/Lexer/src/Token.cpp index 51f0a36..c1a5a39 100644 --- a/Lexer/src/Token.cpp +++ b/Lexer/src/Token.cpp @@ -8,7 +8,7 @@ namespace LXC::Lexer { // Constructor to assign the members of the token class // - Token::Token(const LexerContext& ctx, unsigned __int32 start, unsigned short len, TokenType _type) : + Token::Token(const LexerContext& ctx, const uint32_t start, unsigned short len, TokenType _type) : type(_type), length(len), index(start), contents(nullptr) { // Only user defined class tokens need to store c-string //