From ab564e9649b3470cd979c519bf8cd2b3f55a9fbe Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Sat, 19 Jul 2025 20:48:54 +0100 Subject: [PATCH] Initial commit --- .gitattributes | 2 ++ .gitignore | 10 ++++++ .vs/launch.vs.json | 13 ++++++++ CMakeLists.txt | 14 +++++++++ CMakePresets.json | 44 ++++++++++++++++++++++++++ Common/FileRead.h | 49 +++++++++++++++++++++++++++++ Common/LXC.h | 11 +++++++ Common/Result.h | 73 ++++++++++++++++++++++++++++++++++++++++++++ LXC/CMakeLists.txt | 9 ++++++ LXC/LXC.cpp | 16 ++++++++++ Lexer/CMakeLists.txt | 13 ++++++++ Lexer/src/Lexer.cpp | 1 + README.md | 2 ++ example/example.lx | 1 + 14 files changed, 258 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .vs/launch.vs.json create mode 100644 CMakeLists.txt create mode 100644 CMakePresets.json create mode 100644 Common/FileRead.h create mode 100644 Common/LXC.h create mode 100644 Common/Result.h create mode 100644 LXC/CMakeLists.txt create mode 100644 LXC/LXC.cpp create mode 100644 Lexer/CMakeLists.txt create mode 100644 Lexer/src/Lexer.cpp create mode 100644 README.md create mode 100644 example/example.lx diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..34ea02d --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# CMake output directory # +out/ + +# Visual studio directory (excluding launch settings) # +.vs/LXC +.vs/CMake Overview +.vs/cmake.db +.vs/ProjectSettings.json +.vs/slnx.sqlite +.vs/VSWorkspaceState.json diff --git a/.vs/launch.vs.json b/.vs/launch.vs.json new file mode 100644 index 0000000..a200c1a --- /dev/null +++ b/.vs/launch.vs.json @@ -0,0 +1,13 @@ +{ + "version": "0.2.1", + "defaults": {}, + "configurations": [ + { + "type": "default", + "project": "CMakeLists.txt", + "projectTarget": "LXC.exe (LXC\\LXC.exe)", + "currentDir": "${workspaceRoot}", + "name": "LXC.exe (LXC\\LXC.exe)" + } + ] +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..14fe39d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +# Sets miniumum CMake and C++ Standard version # +cmake_minimum_required (VERSION 3.16) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +project(LXC_Project LANGUAGES CXX) + +# Adds the sub-directories of all of the binaries # +add_subdirectory(Lexer) + +# The app subdirectory # +add_subdirectory(LXC) diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..d014ae2 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,44 @@ +{ + "version": 3, + + "configurePresets": [ + { + "name": "windows-base", + "hidden": true, + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "cacheVariables": { + "CMAKE_C_COMPILER": "cl.exe", + "CMAKE_CXX_COMPILER": "cl.exe" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Windows" + } + }, + + { + "name": "x64-debug", + "displayName": "x64 Debug", + "inherits": "windows-base", + "architecture": { + "value": "x64", + "strategy": "external" + }, + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + } + }, + + { + "name": "x64-release", + "displayName": "x64 Release", + "inherits": "x64-debug", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + } + ] +} diff --git a/Common/FileRead.h b/Common/FileRead.h new file mode 100644 index 0000000..e33aa58 --- /dev/null +++ b/Common/FileRead.h @@ -0,0 +1,49 @@ +#pragma once + +#include +#include + +namespace LXC::Util +{ + struct FileReadError + { + enum Reason + { + FileNotFound, + PermissionDenied, + NotAFile + }; + + FileReadError(const std::filesystem::path& _path, Reason _reason) + : path(_path), reason(_reason) + {} + + std::filesystem::path path; + Reason reason; + }; + + inline ReturnVal ReadFile(const std::filesystem::path& filepath) + { + // Checks the file exists // + if (!std::filesystem::exists(filepath)) + return FunctionFail(FileReadError(std::filesystem::absolute(filepath), FileReadError::FileNotFound)); + + // Checks it is a regular file // + if (!std::filesystem::is_regular_file(filepath)) + return FunctionFail(FileReadError(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(FileReadError(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); + + std::string contents(len, '\0'); + file.read(&contents[0], len); + + return contents; + } +} diff --git a/Common/LXC.h b/Common/LXC.h new file mode 100644 index 0000000..c7e0530 --- /dev/null +++ b/Common/LXC.h @@ -0,0 +1,11 @@ +#pragma once + +// Standard libraries // + +#include +#include + +// LXC util files // + +#include +#include diff --git a/Common/Result.h b/Common/Result.h new file mode 100644 index 0000000..a5a3f5c --- /dev/null +++ b/Common/Result.h @@ -0,0 +1,73 @@ +#pragma once + +#include +#include + +namespace LXC::Util +{ + // Custom version of std::unexpected // + template struct FunctionFail + { + explicit FunctionFail(ErrorType _err) + : error(_err) {} + + ErrorType error; + }; + + // Custom version of std::expected // + template + requires (!std::same_as) // ResultType being bool causes issues with operator overloads + class ReturnVal + { + 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) + {} + + // Destructor // + ~ReturnVal() {} + + // Different getters of the class // + + inline bool Failed() const { return m_FunctionFailed; } + inline bool Suceeded() const { return !m_FunctionFailed; } + + inline ResultType& Result() + { + if (Suceeded()) _LIKELY + return m_Result; + + std::exit(EXIT_FAILURE); + } + + inline ErrorType& Error() + { + if (Failed()) _LIKELY + return m_Error; + + std::exit(EXIT_FAILURE); + } + + // Operator overloads // + + 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; + }; + + // Tracks what item is currently in the union // + bool m_FunctionFailed; + }; +} diff --git a/LXC/CMakeLists.txt b/LXC/CMakeLists.txt new file mode 100644 index 0000000..1ae5a82 --- /dev/null +++ b/LXC/CMakeLists.txt @@ -0,0 +1,9 @@ +# Creates the .exe from the single source file of the app # +add_executable(LXC LXC.cpp) + +# Links the other binaries # +target_link_libraries(LXC 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) diff --git a/LXC/LXC.cpp b/LXC/LXC.cpp new file mode 100644 index 0000000..ee9e5fb --- /dev/null +++ b/LXC/LXC.cpp @@ -0,0 +1,16 @@ +#include + +int main(int argc, char** argv) +{ + using namespace LXC; + + Util::ReturnVal fileContents = Util::ReadFile("example/example.lx"); + + if (fileContents.Suceeded()) + std::cout << fileContents.Result() << std::endl; + + else + std::cout << fileContents.Error().reason << " | " << fileContents.Error().path << std::endl; + + return 0; +} diff --git a/Lexer/CMakeLists.txt b/Lexer/CMakeLists.txt new file mode 100644 index 0000000..e289a9a --- /dev/null +++ b/Lexer/CMakeLists.txt @@ -0,0 +1,13 @@ +# Fetches all files for in the binary # +file (GLOB LexerSources src/*.cpp inc/*.h) + +add_library(Lexer STATIC ${LexerSources}) + +target_include_directories ( + Lexer PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/inc +) + +# Creates the precompiled header for the binary # +target_include_directories(Lexer PRIVATE ${CMAKE_SOURCE_DIR}/Common) +target_precompile_headers(Lexer PRIVATE ${CMAKE_SOURCE_DIR}/Common/LXC.h) diff --git a/Lexer/src/Lexer.cpp b/Lexer/src/Lexer.cpp new file mode 100644 index 0000000..c5cd877 --- /dev/null +++ b/Lexer/src/Lexer.cpp @@ -0,0 +1 @@ +#include diff --git a/README.md b/README.md new file mode 100644 index 0000000..3e53ba6 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# LXC + diff --git a/example/example.lx b/example/example.lx new file mode 100644 index 0000000..02c30d5 --- /dev/null +++ b/example/example.lx @@ -0,0 +1 @@ +FILE CONTENTS GO HERE