Compiles on UNIX based systems

This commit is contained in:
2025-07-21 19:57:05 +01:00
parent d6568a8d2f
commit cde435ba55
9 changed files with 65 additions and 28 deletions

View File

@@ -73,7 +73,7 @@ namespace LXC::Util
}; };
// Finds the location of a given index within a file // // 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 // // Resets location //
location.line = 1; location.line = 1;
@@ -84,7 +84,7 @@ namespace LXC::Util
return false; return false;
// Finds the location // // Finds the location //
__int32 localIndex = 0; uint32_t localIndex = 0;
while (localIndex != index) while (localIndex != index)
{ {
if (file[localIndex] == '\n') if (file[localIndex] == '\n')

View File

@@ -9,7 +9,7 @@
namespace LXC::Util namespace LXC::Util
{ {
// Enum to translate to the Win32 code for the colors // // Enum to translate to the Win32 code for the colors //
enum Color : WORD enum Color
{ {
DEFAULT = 0x07, DEFAULT = 0x07,
@@ -57,6 +57,8 @@ namespace LXC::Internal
return sLog; return sLog;
} }
#if defined(_WIN32)
// Base function for all derived Print() functions // // Base function for all derived Print() functions //
template<Util::Color col, bool newLine, typename... Args> template<Util::Color col, bool newLine, typename... Args>
inline void WriteImpl(std::ostream& os, Args&&... args) inline void WriteImpl(std::ostream& os, Args&&... args)
@@ -73,6 +75,19 @@ namespace LXC::Internal
if constexpr (col != Util::Color::DEFAULT) if constexpr (col != Util::Color::DEFAULT)
SetConsoleTextAttribute(hConsole, static_cast<WORD>(Util::Color::DEFAULT)); SetConsoleTextAttribute(hConsole, static_cast<WORD>(Util::Color::DEFAULT));
} }
#elif defined(__unix__)
template<Util::Color clo, bool newLine, typename... Args>
inline void WriteImpl(std::ostream& os, Args&&... args)
{
(os << ... << std::forward<Args>(args));
if constexpr (newLine)
os << std::endl;
}
#endif
} }
namespace LXC::Util namespace LXC::Util

View File

@@ -1,8 +1,20 @@
#pragma once #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 // // Standard libraries //
#include <unordered_map> #include <unordered_map>
#include <cstdint>
#include <cstring>
#include <vector> #include <vector>
// LXC util files // // LXC util files //

View File

@@ -1,10 +1,13 @@
#pragma once #pragma once
// Platform specific includes // // Platform specific includes //
#ifdef _WIN32 #if defined(_WIN32)
#define NOMINMAX #define NOMINMAX
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>
#elif defined(__unix__)
// No platform specific includes are needed for UNIX based systems //
#else #else
#error "Code is currently only supported on Win32" #error "OS is not supported"
#endif // _WIN32 #endif

View File

@@ -1,9 +1,9 @@
# Creates the .exe from the single source file of the app # # 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 # # Links the other binaries #
target_link_libraries(LXC PRIVATE Lexer) target_link_libraries(LXCR PRIVATE Lexer)
# Creates the precompiled header for the binary # # Creates the precompiled header for the binary #
target_include_directories(LXC PRIVATE ${CMAKE_SOURCE_DIR}/Common) target_include_directories(LXCR PRIVATE ${CMAKE_SOURCE_DIR}/Common)
target_precompile_headers(LXC PRIVATE ${CMAKE_SOURCE_DIR}/Common/LXC.h) target_precompile_headers(LXCR PRIVATE ${CMAKE_SOURCE_DIR}/Common/LXC.h)

View File

@@ -11,10 +11,10 @@ namespace LXC::Lexer
// Trackers for the Lexer itself // // Trackers for the Lexer itself //
const std::string& source; const std::string& source;
__int32 index; uint32_t index;
LexerOutput out; LexerOutput out;
const __int32 len; const uint32_t len;
// Trackers for where the Lexer is within the user version of source // // Trackers for where the Lexer is within the user version of source //
unsigned short column; unsigned short column;
@@ -32,7 +32,7 @@ namespace LXC::Lexer
}; };
// Constructor to pass arguments through to the struct // // 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) : reason(_reason), index(errorIndex), info(_info)
{} {}
@@ -51,7 +51,7 @@ namespace LXC::Lexer
// Error information // // Error information //
const Reason reason; const Reason reason;
const __int32 index; const uint32_t index;
const std::string info; const std::string info;
}; };

View File

@@ -83,11 +83,20 @@ namespace LXC::Lexer
}; };
// Util function calculating wether a token is of a given class // // Util function calculating wether a token is of a given class //
template<TokenClass::ClassMask mask> static constexpr bool IsTypeClass(TokenType type) { return type & mask; } template<TokenClass::ClassMask mask> static constexpr bool IsTypeClass(TokenType type)
template<TokenClass::ClassMask mask> static constexpr bool IsTypeClass(Token token) { return token.type & mask; } {
using T = std::underlying_type_t<TokenType>;
return static_cast<T>(type) & static_cast<T>(mask);
}
template<TokenClass::ClassMask mask> static constexpr bool IsTypeClass(Token token)
{
using T = std::underlying_type_t<TokenType>;
return static_cast<T>(token.type) & static_cast<T>(mask);
}
// Constructor to set the data of the token for more complex token types // // 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 // // Deconstructor to clean up the allocated memory //
~Token(); ~Token();
@@ -105,7 +114,7 @@ namespace LXC::Lexer
const unsigned short length; const unsigned short length;
// Start index of the token // // Start index of the token //
const unsigned __int32 index; const uint32_t index;
private: private:
// The data of the token // // The data of the token //

View File

@@ -15,7 +15,7 @@ namespace LXC::Internal
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); 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'; return c == ' ' || c == '\t' || c == '\n' || c == '\r';
} }
@@ -69,7 +69,7 @@ namespace LXC::Internal
namespace LXC::Lexer namespace LXC::Lexer
{ {
LexerContext::LexerContext(const std::string& _source) : 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<LexerOutput, LexerError> TokenizeFile(const std::string& fileContents) Util::ReturnVal<LexerOutput, LexerError> TokenizeFile(const std::string& fileContents)
@@ -86,7 +86,7 @@ namespace LXC::Lexer
bool inComment = false; bool inComment = false;
unsigned __int32 sectionStart = 0; uint32_t sectionStart = 0;
} trackers; } trackers;
@@ -111,7 +111,7 @@ namespace LXC::Lexer
// Creates the token (if at the end of the string literal) // // Creates the token (if at the end of the string literal) //
if (!trackers.inStrLiteral) 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) {} } else if (trackers.inStrLiteral) {}
@@ -125,9 +125,7 @@ namespace LXC::Lexer
// Checks for the end of the number literal to create the token // // Checks for the end of the number literal to create the token //
if (!Internal::IsNumeric(next)) _UNLIKELY if (!Internal::IsNumeric(next)) _UNLIKELY
{ {
ctx.out.emplace_back(ctx, trackers.sectionStart, (unsigned short)(ctx.index - trackers.sectionStart + 1), Token::NumLiteral);
ctx.out.emplace_back(ctx, trackers.sectionStart, (USHORT)(ctx.index - trackers.sectionStart + 1), Token::NumLiteral);
trackers.inNumLiteral = false; trackers.inNumLiteral = false;
} }
} }
@@ -147,7 +145,7 @@ namespace LXC::Lexer
auto it = Internal::keywords.find(fullWord); auto it = Internal::keywords.find(fullWord);
Token::TokenType tType = (it != Internal::keywords.end()) ? it->second : Token::Identifier; 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; trackers.inIdentifier = false;
} }
} }
@@ -168,7 +166,7 @@ namespace LXC::Lexer
std::string_view fullSymbol(ctx.source.data() + trackers.sectionStart, ctx.index - trackers.sectionStart + 1); std::string_view fullSymbol(ctx.source.data() + trackers.sectionStart, ctx.index - trackers.sectionStart + 1);
auto it = Internal::symbolAndOpMap.find(fullSymbol); auto it = Internal::symbolAndOpMap.find(fullSymbol);
if (it != Internal::symbolAndOpMap.end()) 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 else
return Util::FunctionFail<LexerError>(LexerError::UnknownSymbolOrOperand, trackers.sectionStart, std::string(fullSymbol)); return Util::FunctionFail<LexerError>(LexerError::UnknownSymbolOrOperand, trackers.sectionStart, std::string(fullSymbol));

View File

@@ -8,7 +8,7 @@
namespace LXC::Lexer namespace LXC::Lexer
{ {
// Constructor to assign the members of the token class // // 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) type(_type), length(len), index(start), contents(nullptr)
{ {
// Only user defined class tokens need to store c-string // // Only user defined class tokens need to store c-string //