From 5bfeb75536ddf98ad4a3a41e0c61f425b25cc834 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Mon, 21 Jul 2025 17:39:43 +0100 Subject: [PATCH] Tabs -> Spaces --- Common/File.h | 156 +++++++++++++++--------------- Common/IO.h | 220 +++++++++++++++++++++---------------------- Common/OS.h | 8 +- Common/Result.h | 166 ++++++++++++++++---------------- LXC/LXC.cpp | 92 +++++++++--------- Lexer/CMakeLists.txt | 4 +- Lexer/inc/Lexer.h | 88 ++++++++--------- Lexer/inc/Token.h | 158 +++++++++++++++---------------- Lexer/src/Token.cpp | 136 +++++++++++++------------- README.md | 1 - 10 files changed, 514 insertions(+), 515 deletions(-) diff --git a/Common/File.h b/Common/File.h index 5406d4b..d5212ec 100644 --- a/Common/File.h +++ b/Common/File.h @@ -5,98 +5,98 @@ namespace LXC::Util { - // Error returned when Util::ReadFile runs into errors // - struct FileReadError final - { - // Different reasons why the error can occur // - enum Reason - { - FileNotFound, - PermissionDenied, - NotAFile - }; + // Error returned when Util::ReadFile runs into errors // + struct FileReadError final + { + // Different reasons why the error can occur // + enum Reason + { + FileNotFound, + PermissionDenied, + NotAFile + }; - // Constructor to pass arguments to the struct // - FileReadError(const std::filesystem::path& _path, Reason _reason) - : path(_path), reason(_reason) - {} + // Constructor to pass arguments to the struct // + FileReadError(const std::filesystem::path& _path, Reason _reason) + : path(_path), reason(_reason) + {} - // Error information // - const std::filesystem::path path; - const Reason reason; + // Error information // + const std::filesystem::path path; + const Reason reason; - // Turns the error into a c-string // - inline static const char* const ReasonStr(Reason reason) - { - static const char* reasons[] = - { - "File cannot be found", - "File reading permissions are denied", - "Not a file" - }; + // Turns the error into a c-string // + inline static const char* const ReasonStr(Reason reason) + { + static const char* reasons[] = + { + "File cannot be found", + "File reading permissions are denied", + "Not a file" + }; - return reasons[reason]; - } - }; + return reasons[reason]; + } + }; - // Util function to read a file as quick as possible with error handling // - inline ReturnVal ReadFile(const std::filesystem::path& filepath) - { - // Checks the file exists // - if (!std::filesystem::exists(filepath)) - return FunctionFail(std::filesystem::absolute(filepath), FileReadError::FileNotFound); + // Util function to read a file as quick as possible with error handling // + inline ReturnVal ReadFile(const std::filesystem::path& filepath) + { + // Checks the file exists // + if (!std::filesystem::exists(filepath)) + return FunctionFail(std::filesystem::absolute(filepath), FileReadError::FileNotFound); - // Checks it is a regular file // - if (!std::filesystem::is_regular_file(filepath)) - return FunctionFail(std::filesystem::absolute(filepath), FileReadError::NotAFile); + // Checks it is a regular file // + if (!std::filesystem::is_regular_file(filepath)) + return FunctionFail(std::filesystem::absolute(filepath), FileReadError::NotAFile); - // Checks it can open the file // - std::ifstream file(filepath, std::ios::binary | std::ios::ate); - if (!file) - return FunctionFail(std::filesystem::absolute(filepath), FileReadError::PermissionDenied); + // Checks it can open the file // + std::ifstream file(filepath, std::ios::binary | std::ios::ate); + if (!file) + return FunctionFail(std::filesystem::absolute(filepath), FileReadError::PermissionDenied); - // Copies the file to the output string // - const std::streamsize len = file.tellg(); - file.seekg(0, std::ios::beg); + // Copies the file to the output string // + const std::streamsize len = file.tellg(); + file.seekg(0, std::ios::beg); - std::string contents(len, '\0'); - file.read(&contents[0], len); + std::string contents(len, '\0'); + file.read(&contents[0], len); - return contents; - } + return contents; + } - // Struct to hold a position within a file // - struct FileLocation - { - unsigned short col; - unsigned short line; - }; + // Struct to hold a position within a file // + struct FileLocation + { + unsigned short col; + unsigned short line; + }; - // Finds the location of a given index within a file // - inline bool GetFileLocationAtIndex(FileLocation& location, const std::string& file, __int32 index) - { - // Resets location // - location.line = 1; - location.col = 1; + // Finds the location of a given index within a file // + inline bool GetFileLocationAtIndex(FileLocation& location, const std::string& file, __int32 index) + { + // Resets location // + location.line = 1; + location.col = 1; - // Returns false if outside the bounds // - if (index < 0 || index > file.length()) - return false; + // Returns false if outside the bounds // + if (index < 0 || index > file.length()) + return false; - // Finds the location // - __int32 localIndex = 0; - while (localIndex != index) - { - if (file[localIndex] == '\n') - { - location.line += 1; - location.col = 0; - } + // Finds the location // + __int32 localIndex = 0; + while (localIndex != index) + { + if (file[localIndex] == '\n') + { + location.line += 1; + location.col = 0; + } - location.col++; - localIndex++; - } + location.col++; + localIndex++; + } - return true; - } + return true; + } } diff --git a/Common/IO.h b/Common/IO.h index 4879cff..683c6b6 100644 --- a/Common/IO.h +++ b/Common/IO.h @@ -8,137 +8,137 @@ namespace LXC::Util { - // Enum to translate to the Win32 code for the colors // - enum Color : WORD - { - DEFAULT = 0x07, + // Enum to translate to the Win32 code for the colors // + enum Color : WORD + { + DEFAULT = 0x07, - BLACK = 0x00, - BLUE = 0x01, - GREEN = 0x02, - AQUA = 0x03, - RED = 0x04, - PURPLE = 0x05, - YELLOW = 0x06, - LIGHT_GRAY = 0x07, - LIGHT_BLUE = 0x09, - LIGHT_GREEN = 0x0a, - LIGHT_AQUA = 0x0b, - LIGHT_RED = 0x0c, - LIGHT_PURPLE = 0x0d, - LIGHT_YELLOW = 0x0e, - WHITE = 0x0f - }; + BLACK = 0x00, + BLUE = 0x01, + GREEN = 0x02, + AQUA = 0x03, + RED = 0x04, + PURPLE = 0x05, + YELLOW = 0x06, + LIGHT_GRAY = 0x07, + LIGHT_BLUE = 0x09, + LIGHT_GREEN = 0x0a, + LIGHT_AQUA = 0x0b, + LIGHT_RED = 0x0c, + LIGHT_PURPLE = 0x0d, + LIGHT_YELLOW = 0x0e, + WHITE = 0x0f + }; } namespace LXC::Internal { - // Checks if a type can be outputted to std::ostream // - template concept Logable = requires(std::ostream& os, T t) - { - // I have no idea what this part does at all // - { os << t } -> std::same_as; - }; + // Checks if a type can be outputted to std::ostream // + template concept Logable = requires(std::ostream& os, T t) + { + // I have no idea what this part does at all // + { os << t } -> std::same_as; + }; - // Checks if a list of types can be outputted to std::ostream // - template concept AllLogable = (Logable && ...); + // Checks if a list of types can be outputted to std::ostream // + template concept AllLogable = (Logable && ...); - // Checks if the type has a custom log method // - template concept HasLogStrFunc = requires(T obj) - { - // Checks the type has a function LogStr that returns a string // - { obj.LogStr() } -> std::same_as; - }; + // Checks if the type has a custom log method // + template concept HasLogStrFunc = requires(T obj) + { + // Checks the type has a function LogStr that returns a string // + { obj.LogStr() } -> std::same_as; + }; - // Returns a reference to the log file // - inline std::ofstream& Log() - { - static std::ofstream sLog; - return sLog; - } + // Returns a reference to the log file // + inline std::ofstream& Log() + { + static std::ofstream sLog; + return sLog; + } - // Base function for all derived Print() functions // - template - inline void WriteImpl(std::ostream& os, Args&&... args) - { - static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); - if constexpr(col != Util::Color::DEFAULT) - SetConsoleTextAttribute(hConsole, static_cast(col)); + // Base function for all derived Print() functions // + template + inline void WriteImpl(std::ostream& os, Args&&... args) + { + static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + if constexpr(col != Util::Color::DEFAULT) + SetConsoleTextAttribute(hConsole, static_cast(col)); - (os << ... << std::forward(args)); + (os << ... << std::forward(args)); - if constexpr (newLine) - os << std::endl; + if constexpr (newLine) + os << std::endl; - if constexpr (col != Util::Color::DEFAULT) - SetConsoleTextAttribute(hConsole, static_cast(Util::Color::DEFAULT)); - } + if constexpr (col != Util::Color::DEFAULT) + SetConsoleTextAttribute(hConsole, static_cast(Util::Color::DEFAULT)); + } } namespace LXC::Util { - // Prints arguments to the console with the given color // - template - requires Internal::AllLogable - inline void PrintAs(Args&&... args) - { - Internal::WriteImpl(std::cout, std::forward(args)...); - } + // Prints arguments to the console with the given color // + template + requires Internal::AllLogable + inline void PrintAs(Args&&... args) + { + Internal::WriteImpl(std::cout, std::forward(args)...); + } - // Prints arguments to the console // - template - requires Internal::AllLogable - inline void Print(Args&&... args) - { - Internal::WriteImpl(std::cout, std::forward(args)...); - } + // Prints arguments to the console // + template + requires Internal::AllLogable + inline void Print(Args&&... args) + { + Internal::WriteImpl(std::cout, std::forward(args)...); + } - // Prints arguments to the console with a new-line character at the end // - template - requires Internal::AllLogable - inline void PrintLn(Args&&... args) - { - Internal::WriteImpl(std::cout, std::forward(args)...); - } + // Prints arguments to the console with a new-line character at the end // + template + requires Internal::AllLogable + inline void PrintLn(Args&&... args) + { + Internal::WriteImpl(std::cout, std::forward(args)...); + } - // Logs all the arguments to the file (automatically flushes) // - template - requires Internal::AllLogable && (sizeof...(Args) > 1) - inline void Log(Args&&... args) - { - std::ofstream& log = Internal::Log(); - if (log.is_open()) _UNLIKELY - Internal::WriteImpl(log, R"([LXC] ")", std::forward(args)..., '"'); - } + // Logs all the arguments to the file (automatically flushes) // + template + requires Internal::AllLogable && (sizeof...(Args) > 1) + inline void Log(Args&&... args) + { + std::ofstream& log = Internal::Log(); + if (log.is_open()) _UNLIKELY + Internal::WriteImpl(log, R"([LXC] ")", std::forward(args)..., '"'); + } - // Logs a singular argument to the log, calls Log() if it can on the object // - template - requires Internal::HasLogStrFunc || Internal::Logable - inline void Log(T arg) - { - std::ofstream& log = Internal::Log(); - if (log.is_open()) _UNLIKELY - { - if constexpr (Internal::HasLogStrFunc) - Internal::WriteImpl(log, "[LXC] ", '{', arg.LogStr(), '}'); + // Logs a singular argument to the log, calls Log() if it can on the object // + template + requires Internal::HasLogStrFunc || Internal::Logable + inline void Log(T arg) + { + std::ofstream& log = Internal::Log(); + if (log.is_open()) _UNLIKELY + { + if constexpr (Internal::HasLogStrFunc) + Internal::WriteImpl(log, "[LXC] ", '{', arg.LogStr(), '}'); - else - Internal::WriteImpl(log, "[LXC] ", '"', arg, '"'); - } - } + else + Internal::WriteImpl(log, "[LXC] ", '"', arg, '"'); + } + } - // Intitalises the log with the given file name // - inline void CreateLog(const std::filesystem::path& path) - { - // Opens the log file with the given path // - std::ofstream& log = Internal::Log(); - log.open(path); + // Intitalises the log with the given file name // + inline void CreateLog(const std::filesystem::path& path) + { + // Opens the log file with the given path // + std::ofstream& log = Internal::Log(); + log.open(path); - // Assigns a function to close the log file on program exit // - std::atexit([]() - { - std::ofstream& log = Internal::Log(); - log.close(); - }); - } + // Assigns a function to close the log file on program exit // + std::atexit([]() + { + std::ofstream& log = Internal::Log(); + log.close(); + }); + } } diff --git a/Common/OS.h b/Common/OS.h index 95ad552..bc8cb73 100644 --- a/Common/OS.h +++ b/Common/OS.h @@ -2,9 +2,9 @@ // Platform specific includes // #ifdef _WIN32 - #define NOMINMAX - #define WIN32_LEAN_AND_MEAN - #include + #define NOMINMAX + #define WIN32_LEAN_AND_MEAN + #include #else - #error "Code is currently only supported on Win32" + #error "Code is currently only supported on Win32" #endif // _WIN32 diff --git a/Common/Result.h b/Common/Result.h index 14630b0..18886e5 100644 --- a/Common/Result.h +++ b/Common/Result.h @@ -7,110 +7,110 @@ namespace LXC::Util { - // Util function to stop/ the program // - inline void Stop() - { - // Only checks for a debugger when compiled in Debug mode // - #ifdef _DEBUG + // Util function to stop/ the program // + inline void Stop() + { + // Only checks for a debugger when compiled in Debug mode // + #ifdef _DEBUG - // Triggers a breakpoint so the debugger can work out where the program exits // - if (IsDebuggerPresent()) - DebugBreak(); + // Triggers a breakpoint so the debugger can work out where the program exits // + if (IsDebuggerPresent()) + DebugBreak(); - #endif // _DEBUG + #endif // _DEBUG - // Force exits the program // - std::exit(EXIT_FAILURE); - } + // Force exits the program // + std::exit(EXIT_FAILURE); + } - // Custom version of std::unexpected // - template struct FunctionFail final - { - // Basic constructor to copy the error across // - explicit FunctionFail(ErrorType _err) : error(_err) - { - // Only checks for a debugger when compiled in Debug mode // - #ifdef _DEBUG + // Custom version of std::unexpected // + template struct FunctionFail final + { + // Basic constructor to copy the error across // + explicit FunctionFail(ErrorType _err) : error(_err) + { + // Only checks for a debugger when compiled in Debug mode // + #ifdef _DEBUG - // Triggers a breakpoint when a debugger is attached as a function has failed // - if (IsDebuggerPresent()) - DebugBreak(); + // Triggers a breakpoint when a debugger is attached as a function has failed // + if (IsDebuggerPresent()) + DebugBreak(); - #endif // _DEBUG - } + #endif // _DEBUG + } - // Constructs the FunctionFail with the error itself // - template requires std::constructible_from - explicit FunctionFail(Args&&... args) - : error(std::forward(args)...) - { - // Only checks for a debugger when compiled in Debug mode // - #ifdef _DEBUG + // Constructs the FunctionFail with the error itself // + template requires std::constructible_from + explicit FunctionFail(Args&&... args) + : error(std::forward(args)...) + { + // Only checks for a debugger when compiled in Debug mode // + #ifdef _DEBUG - // Triggers a breakpoint when a debugger is attached as a function has failed // - if (IsDebuggerPresent()) - DebugBreak(); + // Triggers a breakpoint when a debugger is attached as a function has failed // + if (IsDebuggerPresent()) + DebugBreak(); - #endif // _DEBUG - } + #endif // _DEBUG + } - const ErrorType error; - }; + const ErrorType error; + }; - // Custom version of std::expected // - template - requires (!std::same_as) // ResultType being bool causes issues with operator overloads - class ReturnVal final - { - public: - // Constructor for function sucess // - ReturnVal(ResultType result) - : m_Result(result), m_FunctionFailed(false) - {} + // Custom version of std::expected // + template + requires (!std::same_as) // ResultType being bool causes issues with operator overloads + class ReturnVal final + { + public: + // Constructor for function sucess // + ReturnVal(ResultType result) + : m_Result(result), m_FunctionFailed(false) + {} - // Constructor for function fail // - ReturnVal(FunctionFail error) - : m_Error(error.error), m_FunctionFailed(true) - {} + // Constructor for function fail // + ReturnVal(FunctionFail error) + : m_Error(error.error), m_FunctionFailed(true) + {} - // Destructor // - ~ReturnVal() {} + // Destructor // + ~ReturnVal() {} - // Different getters of the class // + // Different getters of the class // - inline bool Failed() const { return m_FunctionFailed; } - inline bool Suceeded() const { return !m_FunctionFailed; } + inline bool Failed() const { return m_FunctionFailed; } + inline bool Suceeded() const { return !m_FunctionFailed; } - inline ResultType& Result() - { - if (Suceeded()) _LIKELY - return m_Result; + inline ResultType& Result() + { + if (Suceeded()) _LIKELY + return m_Result; - std::exit(EXIT_FAILURE); - } + std::exit(EXIT_FAILURE); + } - inline ErrorType& Error() - { - if (Failed()) _LIKELY - return m_Error; + inline ErrorType& Error() + { + if (Failed()) _LIKELY + return m_Error; - std::exit(EXIT_FAILURE); - } + std::exit(EXIT_FAILURE); + } - // Operator overloads // + // Operator overloads // - operator bool() const { return !m_FunctionFailed; } - operator ResultType() { return Result(); } + operator bool() const { return !m_FunctionFailed; } + operator ResultType() { return Result(); } - private: - // Union to hold either the result or the error // - union - { - ResultType m_Result; - ErrorType m_Error; - }; + private: + // Union to hold either the result or the error // + union + { + ResultType m_Result; + ErrorType m_Error; + }; - // Tracks what item is currently in the union // - bool m_FunctionFailed; - }; + // Tracks what item is currently in the union // + bool m_FunctionFailed; + }; } diff --git a/LXC/LXC.cpp b/LXC/LXC.cpp index 754c5a9..c8f7e8e 100644 --- a/LXC/LXC.cpp +++ b/LXC/LXC.cpp @@ -4,64 +4,64 @@ int main(int argc, char** argv) { - using namespace LXC; + using namespace LXC; - // Creates the debug log // - Util::CreateLog("LXC.log"); + // Creates the debug log // + Util::CreateLog("LXC.log"); - std::filesystem::path src = "example/example.lx"; + std::filesystem::path src = "example/example.lx"; - // Reads the given file to a string // - Util::ReturnVal fileContents = Util::ReadFile(src); - if (fileContents.Failed()) _UNLIKELY - { - // Stores the error for easier access // - Util::FileReadError& err = fileContents.Error(); + // Reads the given file to a string // + Util::ReturnVal fileContents = Util::ReadFile(src); + if (fileContents.Failed()) _UNLIKELY + { + // Stores the error for easier access // + Util::FileReadError& err = fileContents.Error(); - // Prints the error to the console // - Util::PrintAs("[LXC]"); - Util::PrintAs(" Error: "); - Util::PrintLn(Util::FileReadError::ReasonStr(err.reason), " [", std::filesystem::absolute(err.path), ']'); + // Prints the error to the console // + Util::PrintAs("[LXC]"); + Util::PrintAs(" Error: "); + Util::PrintLn(Util::FileReadError::ReasonStr(err.reason), " [", std::filesystem::absolute(err.path), ']'); - Util::Log("Opening source file failed. Stopping program."); - Util::Stop(); - } + Util::Log("Opening source file failed. Stopping program."); + Util::Stop(); + } - // Turns the file contents into a vector of tokens // - Util::ReturnVal tokens = Lexer::TokenizeFile(fileContents); - if (tokens.Failed()) _UNLIKELY - { - // Stores the error for easier access // - Lexer::LexerError& err = tokens.Error(); + // Turns the file contents into a vector of tokens // + Util::ReturnVal tokens = Lexer::TokenizeFile(fileContents); + if (tokens.Failed()) _UNLIKELY + { + // Stores the error for easier access // + Lexer::LexerError& err = tokens.Error(); - // Finds the file location of the error // - Util::FileLocation location; - Util::GetFileLocationAtIndex(location, fileContents, err.index); + // Finds the file location of the error // + Util::FileLocation location; + Util::GetFileLocationAtIndex(location, fileContents, err.index); - // Prints the error to the console // - Util::PrintAs("[LXC] "); - Util::Print(src.filename().string(), '(', location.line, ',', location.col, ')'); - Util::PrintAs(" Error: "); - Util::Print(Lexer::LexerError::ReasonStr(err.reason)); + // Prints the error to the console // + Util::PrintAs("[LXC] "); + Util::Print(src.filename().string(), '(', location.line, ',', location.col, ')'); + Util::PrintAs(" Error: "); + Util::Print(Lexer::LexerError::ReasonStr(err.reason)); - if (err.reason == Lexer::LexerError::InvalidCharacter) - Util::PrintLn(": {", fileContents.Result()[err.index], '}'); + if (err.reason == Lexer::LexerError::InvalidCharacter) + Util::PrintLn(": {", fileContents.Result()[err.index], '}'); - if (err.reason == Lexer::LexerError::UnknownSymbolOrOperand) - Util::PrintLn(": {", err.info, '}'); + if (err.reason == Lexer::LexerError::UnknownSymbolOrOperand) + Util::PrintLn(": {", err.info, '}'); - else - Util::PrintLn(); + else + Util::PrintLn(); - Util::Log("Error occured in Lexer: ", Lexer::LexerError::ReasonStr(err.reason)); - Util::Stop(); - } + Util::Log("Error occured in Lexer: ", Lexer::LexerError::ReasonStr(err.reason)); + Util::Stop(); + } - // Prints all of the tokens to the log // - for (const auto& token : tokens.Result()) - { - Util::Log(token); - } + // Prints all of the tokens to the log // + for (const auto& token : tokens.Result()) + { + Util::Log(token); + } - return 0; + return 0; } diff --git a/Lexer/CMakeLists.txt b/Lexer/CMakeLists.txt index e289a9a..5c7fdf0 100644 --- a/Lexer/CMakeLists.txt +++ b/Lexer/CMakeLists.txt @@ -4,8 +4,8 @@ file (GLOB LexerSources src/*.cpp inc/*.h) add_library(Lexer STATIC ${LexerSources}) target_include_directories ( - Lexer PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/inc + Lexer PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/inc ) # Creates the precompiled header for the binary # diff --git a/Lexer/inc/Lexer.h b/Lexer/inc/Lexer.h index 641cd79..a99ace1 100644 --- a/Lexer/inc/Lexer.h +++ b/Lexer/inc/Lexer.h @@ -4,57 +4,57 @@ namespace LXC::Lexer { - struct LexerContext final - { - // Constructor to set the information of the context // - LexerContext(const std::string& _source); + struct LexerContext final + { + // Constructor to set the information of the context // + LexerContext(const std::string& _source); - // Trackers for the Lexer itself // - const std::string& source; - __int32 index; + // Trackers for the Lexer itself // + const std::string& source; + __int32 index; - LexerOutput out; - const __int32 len; + LexerOutput out; + const __int32 len; - // Trackers for where the Lexer is within the user version of source // - unsigned short column; - unsigned short line; - }; + // Trackers for where the Lexer is within the user version of source // + unsigned short column; + unsigned short line; + }; - struct LexerError final - { - // Different reasons why the Lexer can fail // - enum Reason - { - InvalidCharacter, - UnterminatedStringLiteral, - UnknownSymbolOrOperand - }; + struct LexerError final + { + // Different reasons why the Lexer can fail // + enum Reason + { + InvalidCharacter, + UnterminatedStringLiteral, + UnknownSymbolOrOperand + }; - // Constructor to pass arguments through to the struct // - LexerError(Reason _reason, __int32 errorIndex, std::string _info = "") - : reason(_reason), index(errorIndex), info(_info) - {} + // Constructor to pass arguments through to the struct // + LexerError(Reason _reason, __int32 errorIndex, std::string _info = "") + : reason(_reason), index(errorIndex), info(_info) + {} - // Turns the error into a c-string // - inline static const char* const ReasonStr(Reason reason) - { - static const char* reasons[] = - { - "Invalid character found in source", - "Unterminated string literal in source", - "Unknown symbol or operand in source" - }; + // Turns the error into a c-string // + inline static const char* const ReasonStr(Reason reason) + { + static const char* reasons[] = + { + "Invalid character found in source", + "Unterminated string literal in source", + "Unknown symbol or operand in source" + }; - return reasons[reason]; - } + return reasons[reason]; + } - // Error information // - const Reason reason; - const __int32 index; - const std::string info; - }; + // Error information // + const Reason reason; + const __int32 index; + const std::string info; + }; - // Turns a file into a vector of tokens // - Util::ReturnVal TokenizeFile(const std::string& fileContents); + // Turns a file into a vector of tokens // + Util::ReturnVal TokenizeFile(const std::string& fileContents); } diff --git a/Lexer/inc/Token.h b/Lexer/inc/Token.h index 360d002..d44a738 100644 --- a/Lexer/inc/Token.h +++ b/Lexer/inc/Token.h @@ -4,112 +4,112 @@ namespace LXC::Lexer { - namespace TokenClass - { - // Bitmask for different token classes // - enum ClassMask : unsigned short - { - // Mathematical and logic operators // - Operator = 1 << (1 + 8), + namespace TokenClass + { + // Bitmask for different token classes // + enum ClassMask : unsigned short + { + // Mathematical and logic operators // + Operator = 1 << (1 + 8), - // Special words defined by the compiler // - Keyword = 1 << (2 + 8), + // Special words defined by the compiler // + Keyword = 1 << (2 + 8), - // Words such as literals and identifiers // - UserDefined = 1 << (3 + 8), + // Words such as literals and identifiers // + UserDefined = 1 << (3 + 8), - // Symbols in the source like (? , . ! <) // - Symbols = 1 << (4 + 8), + // Symbols in the source like (? , . ! <) // + Symbols = 1 << (4 + 8), - // Tokens not defined by previous classes // - Misc = 1 << (5 + 8) - }; - }; + // Tokens not defined by previous classes // + Misc = 1 << (5 + 8) + }; + }; - struct LexerContext; - - // Data type for storing the output of the lexer // - class Token final - { - public: - // Enum of token type organised by their token class // - enum TokenType : unsigned short - { - // === Operators === // + struct LexerContext; + + // Data type for storing the output of the lexer // + class Token final + { + public: + // Enum of token type organised by their token class // + enum TokenType : unsigned short + { + // === Operators === // - Add = TokenClass::Operator, - Sub, - Mul, - Div, - Mod, + Add = TokenClass::Operator, + Sub, + Mul, + Div, + Mod, - // === Keywords === // + // === Keywords === // - For = TokenClass::Keyword, - While, - If, - ElseIf, - Else, - Return, + For = TokenClass::Keyword, + While, + If, + ElseIf, + Else, + Return, - // === User defined === // + // === User defined === // - StringLiteral = TokenClass::UserDefined, - NumLiteral, - Identifier, + StringLiteral = TokenClass::UserDefined, + NumLiteral, + Identifier, - // === Symbols === // + // === Symbols === // - Assign = TokenClass::Symbols, + Assign = TokenClass::Symbols, - CloseBracket, - OpenBracket, + CloseBracket, + OpenBracket, - CloseBrace, - OpenBrace, + CloseBrace, + OpenBrace, - CloseParen, - OpenParen, + CloseParen, + OpenParen, - Comma, + Comma, - // === Misc === // + // === Misc === // - End_of_file = TokenClass::Misc, + End_of_file = TokenClass::Misc, - UNDEFINED = 65535 // Invalid token type (max number) - }; + UNDEFINED = 65535 // Invalid token type (max number) + }; - // 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; } + // 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; } - // 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); + // 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); - // Deconstructor to clean up the allocated memory // - ~Token(); + // Deconstructor to clean up the allocated memory // + ~Token(); - // Getters for the c-string to stop it being reassigned (or deleted) // - inline const char* const Str() const { return contents; } + // Getters for the c-string to stop it being reassigned (or deleted) // + inline const char* const Str() const { return contents; } - // Outputs all the relevant infomration in a string for logging purposes // - std::string LogStr() const; + // Outputs all the relevant infomration in a string for logging purposes // + std::string LogStr() const; - // The type of the token // - const TokenType type; + // The type of the token // + const TokenType type; - // The length of the token // - const unsigned short length; + // The length of the token // + const unsigned short length; - // Start index of the token // - const unsigned __int32 index; + // Start index of the token // + const unsigned __int32 index; - private: - // The data of the token // - char* contents; - }; + private: + // The data of the token // + char* contents; + }; - // Typedef for the output type of how the Lexer outputs // - typedef std::vector LexerOutput; + // Typedef for the output type of how the Lexer outputs // + typedef std::vector LexerOutput; } diff --git a/Lexer/src/Token.cpp b/Lexer/src/Token.cpp index d808f90..562f8cd 100644 --- a/Lexer/src/Token.cpp +++ b/Lexer/src/Token.cpp @@ -7,84 +7,84 @@ 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) : - type(_type), length(len), index(start), contents(nullptr) - { - // Only user defined class tokens need to store c-string // - if (Token::IsTypeClass(type)) - { - // Copies the memory to a c-string // - contents = new char[len + 1]; // +1 for null terminator - std::memcpy(contents, ctx.source.data() + start, len); - contents[len] = '\0'; - } - } + // Constructor to assign the members of the token class // + Token::Token(const LexerContext& ctx, unsigned __int32 start, unsigned short len, TokenType _type) : + type(_type), length(len), index(start), contents(nullptr) + { + // Only user defined class tokens need to store c-string // + if (Token::IsTypeClass(type)) + { + // Copies the memory to a c-string // + contents = new char[len + 1]; // +1 for null terminator + std::memcpy(contents, ctx.source.data() + start, len); + contents[len] = '\0'; + } + } - // Destructor to clean up the memory of the token that can be allocated // - Token::~Token() - { - // Frees any allocated memory // - //if (contents != nullptr) - // delete[] contents; + // Destructor to clean up the memory of the token that can be allocated // + Token::~Token() + { + // Frees any allocated memory // + //if (contents != nullptr) + // delete[] contents; - contents = nullptr; - } + contents = nullptr; + } - // Helper macro for converting type to string // - #define TOKEN_TYPE_CASE(type) case type: return #type; + // Helper macro for converting type to string // + #define TOKEN_TYPE_CASE(type) case type: return #type; - static constexpr const char* TokenTypeToCStr(Token::TokenType type) - { - switch (type) - { - // All the different types of tokens // - TOKEN_TYPE_CASE(Token::Add); - TOKEN_TYPE_CASE(Token::Sub); - TOKEN_TYPE_CASE(Token::Mul); - TOKEN_TYPE_CASE(Token::Div); - TOKEN_TYPE_CASE(Token::Mod); + static constexpr const char* TokenTypeToCStr(Token::TokenType type) + { + switch (type) + { + // All the different types of tokens // + TOKEN_TYPE_CASE(Token::Add); + TOKEN_TYPE_CASE(Token::Sub); + TOKEN_TYPE_CASE(Token::Mul); + TOKEN_TYPE_CASE(Token::Div); + TOKEN_TYPE_CASE(Token::Mod); - TOKEN_TYPE_CASE(Token::For); - TOKEN_TYPE_CASE(Token::While); - TOKEN_TYPE_CASE(Token::If); - TOKEN_TYPE_CASE(Token::ElseIf); - TOKEN_TYPE_CASE(Token::Else); - TOKEN_TYPE_CASE(Token::Return); + TOKEN_TYPE_CASE(Token::For); + TOKEN_TYPE_CASE(Token::While); + TOKEN_TYPE_CASE(Token::If); + TOKEN_TYPE_CASE(Token::ElseIf); + TOKEN_TYPE_CASE(Token::Else); + TOKEN_TYPE_CASE(Token::Return); - TOKEN_TYPE_CASE(Token::StringLiteral); - TOKEN_TYPE_CASE(Token::NumLiteral); - TOKEN_TYPE_CASE(Token::Identifier); + TOKEN_TYPE_CASE(Token::StringLiteral); + TOKEN_TYPE_CASE(Token::NumLiteral); + TOKEN_TYPE_CASE(Token::Identifier); - TOKEN_TYPE_CASE(Token::Assign); - TOKEN_TYPE_CASE(Token::CloseBracket); - TOKEN_TYPE_CASE(Token::OpenBracket); - TOKEN_TYPE_CASE(Token::CloseBrace); - TOKEN_TYPE_CASE(Token::OpenBrace); - TOKEN_TYPE_CASE(Token::CloseParen); - TOKEN_TYPE_CASE(Token::OpenParen); - TOKEN_TYPE_CASE(Token::Comma); + TOKEN_TYPE_CASE(Token::Assign); + TOKEN_TYPE_CASE(Token::CloseBracket); + TOKEN_TYPE_CASE(Token::OpenBracket); + TOKEN_TYPE_CASE(Token::CloseBrace); + TOKEN_TYPE_CASE(Token::OpenBrace); + TOKEN_TYPE_CASE(Token::CloseParen); + TOKEN_TYPE_CASE(Token::OpenParen); + TOKEN_TYPE_CASE(Token::Comma); - TOKEN_TYPE_CASE(Token::End_of_file); - TOKEN_TYPE_CASE(Token::UNDEFINED); + TOKEN_TYPE_CASE(Token::End_of_file); + TOKEN_TYPE_CASE(Token::UNDEFINED); - // When the case has not been defined yet // - default: - return "UNKNOWN"; - } - } + // When the case has not been defined yet // + default: + return "UNKNOWN"; + } + } - std::string LXC::Lexer::Token::LogStr() const - { - // Output stream to log to // - std::ostringstream os; - os << std::setw(25) << std::left << TokenTypeToCStr(type) << " | "; + std::string LXC::Lexer::Token::LogStr() const + { + // Output stream to log to // + std::ostringstream os; + os << std::setw(25) << std::left << TokenTypeToCStr(type) << " | "; - if (contents != nullptr) - os << std::setw(25) << std::left << std::string('"' + std::string(contents) + '"'); - else - os << std::setw(25) << std::left << "EMPTY"; + if (contents != nullptr) + os << std::setw(25) << std::left << std::string('"' + std::string(contents) + '"'); + else + os << std::setw(25) << std::left << "EMPTY"; - return os.str(); - } + return os.str(); + } } diff --git a/README.md b/README.md index 3e53ba6..732a9f2 100644 --- a/README.md +++ b/README.md @@ -1,2 +1 @@ # LXC -