Initial commit
This commit is contained in:
2
.gitattributes
vendored
Normal file
2
.gitattributes
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# Auto detect text files and perform LF normalization
|
||||||
|
* text=auto
|
||||||
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@@ -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
|
||||||
13
.vs/launch.vs.json
Normal file
13
.vs/launch.vs.json
Normal file
@@ -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)"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
14
CMakeLists.txt
Normal file
14
CMakeLists.txt
Normal file
@@ -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)
|
||||||
44
CMakePresets.json
Normal file
44
CMakePresets.json
Normal file
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
49
Common/FileRead.h
Normal file
49
Common/FileRead.h
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
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<std::string, FileReadError> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Common/LXC.h
Normal file
11
Common/LXC.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Standard libraries //
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
// LXC util files //
|
||||||
|
|
||||||
|
#include <Result.h>
|
||||||
|
#include <FileRead.h>
|
||||||
73
Common/Result.h
Normal file
73
Common/Result.h
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
namespace LXC::Util
|
||||||
|
{
|
||||||
|
// Custom version of std::unexpected //
|
||||||
|
template<typename ErrorType> struct FunctionFail
|
||||||
|
{
|
||||||
|
explicit FunctionFail(ErrorType _err)
|
||||||
|
: error(_err) {}
|
||||||
|
|
||||||
|
ErrorType error;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Custom version of std::expected //
|
||||||
|
template<typename ResultType, typename ErrorType>
|
||||||
|
requires (!std::same_as<ResultType, bool>) // 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<ErrorType> 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;
|
||||||
|
};
|
||||||
|
}
|
||||||
9
LXC/CMakeLists.txt
Normal file
9
LXC/CMakeLists.txt
Normal file
@@ -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)
|
||||||
16
LXC/LXC.cpp
Normal file
16
LXC/LXC.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include <LXC.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
13
Lexer/CMakeLists.txt
Normal file
13
Lexer/CMakeLists.txt
Normal file
@@ -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)
|
||||||
1
Lexer/src/Lexer.cpp
Normal file
1
Lexer/src/Lexer.cpp
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#include <LXC.h>
|
||||||
1
example/example.lx
Normal file
1
example/example.lx
Normal file
@@ -0,0 +1 @@
|
|||||||
|
FILE CONTENTS GO HERE
|
||||||
Reference in New Issue
Block a user