mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-03 17:39:02 +00:00
Organized project files
This commit is contained in:
159
IR-Generator/IR-Generator.vcxproj
Normal file
159
IR-Generator/IR-Generator.vcxproj
Normal file
@@ -0,0 +1,159 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{c88042e2-0e09-4383-93f8-c79f9ee1e897}</ProjectGuid>
|
||||
<RootNamespace>IRGenerator</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<OutDir>$(SolutionDir)bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)bin\inter\$(Platform)\$(Configuration)\$(ProjectName)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<OutDir>$(SolutionDir)bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)bin\inter\$(Platform)\$(Configuration)\$(ProjectName)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)bin\$(Platform)\$(Configuration)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>Parser.lib;Lexer.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)inc;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)bin\$(Platform)\$(Configuration)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>Parser.lib;Lexer.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\Console.cpp" />
|
||||
<ClCompile Include="src\main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="inc\Console.h" />
|
||||
<ClInclude Include="inc\Lexer.h" />
|
||||
<ClInclude Include="inc\LLVM.h" />
|
||||
<ClInclude Include="inc\Parser.h" />
|
||||
<ClInclude Include="inc\Util.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
38
IR-Generator/IR-Generator.vcxproj.filters
Normal file
38
IR-Generator/IR-Generator.vcxproj.filters
Normal file
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Console.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="inc\Console.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="inc\Lexer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="inc\LLVM.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="inc\Parser.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="inc\Util.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
27
IR-Generator/inc/Console.h
Normal file
27
IR-Generator/inc/Console.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace LX
|
||||
{
|
||||
enum class Color
|
||||
{
|
||||
BLACK = 0,
|
||||
BLUE = 1,
|
||||
GREEN = 2,
|
||||
AQUA = 3,
|
||||
RED = 4,
|
||||
PURPLE = 5,
|
||||
YELLOW = 6,
|
||||
LIGHT_GRAY = 7,
|
||||
LIGHT_BLUE = 9,
|
||||
LIGHT_GREEN = 10,
|
||||
LIGHT_AQUA = 11,
|
||||
LIGHT_RED = 12,
|
||||
LIGHT_PURPLE = 13,
|
||||
LIGHT_YELLOW = 14,
|
||||
WHITE = 15
|
||||
};
|
||||
|
||||
void PrintStringAsColor(const std::string& str, Color c);
|
||||
}
|
||||
25
IR-Generator/inc/LLVM.h
Normal file
25
IR-Generator/inc/LLVM.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
// Helper file for including all neccesarry parts of LLVM //
|
||||
#ifdef _MSC_VER
|
||||
|
||||
// Disables all warnings as LLVM files have a lot of Data-loss casts that won't cause issues //
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4244)
|
||||
#pragma warning(disable : 4267)
|
||||
#pragma warning(disable : 4624)
|
||||
#pragma warning(disable : 4800)
|
||||
|
||||
// Includes the LLVM files //
|
||||
|
||||
#include <llvm/IR/IRBuilder.h>
|
||||
#include <llvm/IR/LLVMContext.h>
|
||||
#include <llvm/IR/Module.h>
|
||||
#include <llvm/IR/Verifier.h>
|
||||
|
||||
// Re-enables all warnings //
|
||||
#pragma warning(pop)
|
||||
|
||||
#else
|
||||
#error This code is only designed to work with MSVC due to the use of vcpkg and other aspects
|
||||
#endif // _MSC_VER
|
||||
122
IR-Generator/inc/Lexer.h
Normal file
122
IR-Generator/inc/Lexer.h
Normal file
@@ -0,0 +1,122 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
// Foward declarations of STD classes to minimise includes //
|
||||
namespace std
|
||||
{
|
||||
template<typename T1>
|
||||
struct char_traits;
|
||||
|
||||
template<typename T1, typename T2>
|
||||
class basic_ofstream;
|
||||
|
||||
using ofstream = basic_ofstream<char, char_traits<char>>;
|
||||
}
|
||||
|
||||
// This file contains everything that is exported from Lexer.lib
|
||||
// The rest of the items within the Lexer project are internal only
|
||||
|
||||
namespace LX
|
||||
{
|
||||
// Error type with index and character to alert the user that LX does not understand that symbol //
|
||||
struct InvalidCharInSource
|
||||
{
|
||||
std::streamsize col;
|
||||
std::streamsize line;
|
||||
std::streamsize index;
|
||||
|
||||
char invalid;
|
||||
};
|
||||
|
||||
// Struct to store the current information of the lexer //
|
||||
struct LexerInfo
|
||||
{
|
||||
// Current trackers of where in the source it is //
|
||||
|
||||
std::streamsize line = 1; // <- Lines start on 1 (probably because of non-programmer's)
|
||||
std::streamsize index = 0;
|
||||
std::streamsize column = 0; // <- Columns start on 1 (probably because of non-programmer's)
|
||||
|
||||
// Trackers for when a multi-char token started //
|
||||
|
||||
std::streamsize startOfWord = 0;
|
||||
std::streamsize startOfNumberLiteral = 0;
|
||||
std::streamsize startOfStringLiteral = 0;
|
||||
|
||||
// Different flags of the lexer //
|
||||
// Stored as a bitset to minimse memory allocated (basically no difference, because only one exists at any given time) //
|
||||
|
||||
bool isAlpha : 1 = false;
|
||||
bool isNumeric : 1 = false;
|
||||
bool inComment : 1 = false;
|
||||
bool inStringLiteral : 1 = false;
|
||||
bool isNextCharAlpha : 1 = false;
|
||||
bool isNextCharNumeric : 1 = false;
|
||||
bool wasLastCharAlpha : 1 = false;
|
||||
bool wasLastCharNumeric : 1 = false;
|
||||
bool lexingNumber : 1 = false;
|
||||
};
|
||||
|
||||
// Data type to store a more computer readable version of files
|
||||
struct __declspec(novtable) Token final
|
||||
{
|
||||
//
|
||||
static std::string* source;
|
||||
|
||||
// Enum to hold the type of the token //
|
||||
enum TokenType : short
|
||||
{
|
||||
// General tokens //
|
||||
|
||||
STRING_LITERAL,
|
||||
NUMBER_LITERAL,
|
||||
IDENTIFIER,
|
||||
RETURN,
|
||||
|
||||
// Operators //
|
||||
|
||||
ADD, SUB, MUL, DIV,
|
||||
|
||||
// Keywords //
|
||||
|
||||
FOR, WHILE,
|
||||
IF, ELSE, ELIF,
|
||||
|
||||
FUNCTION,
|
||||
|
||||
// You made a mistake somehow //
|
||||
|
||||
UNDEFINED = -1
|
||||
};
|
||||
|
||||
// Constructor of the tokens to set their info //
|
||||
Token(const TokenType _type, const LexerInfo& info, std::streamsize _length);
|
||||
|
||||
//
|
||||
std::string GetContents() const;
|
||||
|
||||
// Type of the token //
|
||||
// Const to avoid external changes //
|
||||
const TokenType type;
|
||||
|
||||
// Index in the source of the token //
|
||||
const std::streamsize index;
|
||||
|
||||
// The length of the token on the line //
|
||||
const std::streamsize length;
|
||||
|
||||
// The line the token is located on //
|
||||
const std::streamsize line;
|
||||
|
||||
// The column on the line where it is located //
|
||||
const std::streamsize column;
|
||||
};
|
||||
|
||||
// Logging function to turn a tokentype enum val into it's string //
|
||||
std::string ToString(Token::TokenType t);
|
||||
|
||||
// Lexer function to take in a file and output a vector of tokens //
|
||||
const std::vector<Token> LexicalAnalyze(const std::string& contents, const std::streamsize len, std::ofstream* log);
|
||||
}
|
||||
106
IR-Generator/inc/Parser.h
Normal file
106
IR-Generator/inc/Parser.h
Normal file
@@ -0,0 +1,106 @@
|
||||
#pragma once
|
||||
|
||||
// Lexer foward declares fstream components so we can use them here //
|
||||
#include <Lexer.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
// Foward declares STD stuff that is passed around //
|
||||
namespace std::filesystem { class path; }
|
||||
|
||||
// Foward declares all items of the llvm lib that we need //
|
||||
// Done to avoid including LLVM.h to shorten compile times //
|
||||
namespace llvm { class Value; }
|
||||
|
||||
// Foward declares the wrapper around the LLVM objects we need to pass around //
|
||||
namespace LX { struct InfoLLVM; }
|
||||
|
||||
// The nodes of the abstract syntax tree constructed by the parser from the tokens //
|
||||
namespace LX::AST
|
||||
{
|
||||
// Base node that everything else inherits from
|
||||
struct Node
|
||||
{
|
||||
// Enum for storing the type of node //
|
||||
// Used so a pointer to Node can be used and then turned into it's true type //
|
||||
enum NodeType
|
||||
{
|
||||
// General Nodes //
|
||||
|
||||
IDENTIFIER,
|
||||
NUMBER_LITERAL,
|
||||
OPERATION,
|
||||
|
||||
// Control flow Nodes //
|
||||
|
||||
RETURN_STATEMENT,
|
||||
|
||||
// If an error happened somewhere //
|
||||
UNDEFINED = -1
|
||||
};
|
||||
|
||||
// Constructor to set the node type (no others provided) //
|
||||
Node(NodeType type);
|
||||
|
||||
// Virtual destructor because of polymorphism //
|
||||
virtual ~Node() = default;
|
||||
|
||||
// Function for generating LLVN IR (Intermediate representation) //
|
||||
virtual llvm::Value* GenIR(InfoLLVM& LLVM) = 0;
|
||||
|
||||
// Function for generating C/C++ code (Currently not implemented) //
|
||||
//virtual void GenC() = 0;
|
||||
|
||||
// The type of the node //
|
||||
const NodeType m_Type;
|
||||
};
|
||||
}
|
||||
|
||||
namespace LX
|
||||
{
|
||||
// Thrown if there was an error during IR Generation //
|
||||
struct IRGenerationError {};
|
||||
|
||||
// Thrown if there was an unexpected (incorrect) token //
|
||||
struct UnexpectedToken
|
||||
{
|
||||
// The token type that should be there //
|
||||
Token::TokenType expected;
|
||||
|
||||
// If there are multiple expected types there is an option for a custom message //
|
||||
std::string override;
|
||||
|
||||
// What token was actually at that position //
|
||||
// Stored as Token not TokenType to store the location of it within the source //
|
||||
Token got;
|
||||
};
|
||||
|
||||
// Holds all needed info about a function //
|
||||
// Currently only holds the body but in the future will hold: name, params, namespace/class-member
|
||||
struct FunctionDefinition
|
||||
{
|
||||
// Defualt constructor (none other given) //
|
||||
FunctionDefinition();
|
||||
|
||||
// The name of the function //
|
||||
std::string name;
|
||||
|
||||
// The instructions of the body of the function //
|
||||
std::vector<std::unique_ptr<AST::Node>> body;
|
||||
};
|
||||
|
||||
struct FileAST
|
||||
{
|
||||
// Default constructor (none other given) //
|
||||
FileAST();
|
||||
|
||||
// All the functions within this file //
|
||||
std::vector<FunctionDefinition> functions;
|
||||
};
|
||||
|
||||
// Turns the tokens of a file into it's abstract syntax tree equivalent //
|
||||
FileAST TurnTokensIntoAbstractSyntaxTree(std::vector<Token>& tokens, std::ofstream* log);
|
||||
|
||||
// Turns an abstract binary tree into LLVM intermediate representation //
|
||||
void GenerateIR(FileAST& ast, const std::string& name, const std::filesystem::path& IRPath);
|
||||
}
|
||||
34
IR-Generator/inc/Util.h
Normal file
34
IR-Generator/inc/Util.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <fstream>
|
||||
|
||||
// Defining this is only if you are at the point where you should be using a debugger //
|
||||
#define LOG_EVERYTHING
|
||||
|
||||
namespace LX
|
||||
{
|
||||
template<typename T, typename... Args>
|
||||
// Helper function to throw given error if condition is true //
|
||||
// Also micro-optimises to predict there is no errors thrown //
|
||||
inline void ThrowIf(const bool condition, Args... args)
|
||||
{ if (condition) [[unlikely]] { throw T(args...); }}
|
||||
|
||||
template<typename... Args>
|
||||
// Helper function for logging //
|
||||
// Only logs the given args if the log is not null //
|
||||
inline void SafeLog(std::ofstream* log, Args... args)
|
||||
{
|
||||
if (log != nullptr) { (*log << ... << args); *log << "\n"; }
|
||||
}
|
||||
|
||||
inline void SafeFlush(std::ofstream* log)
|
||||
{
|
||||
if (log != nullptr)
|
||||
{
|
||||
log->flush();
|
||||
}
|
||||
}
|
||||
|
||||
// Gives a standard way to mark a change between different sections within the log output //
|
||||
constexpr const char* LOG_BREAK = "\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
|
||||
}
|
||||
22
IR-Generator/src/Console.cpp
Normal file
22
IR-Generator/src/Console.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <Console.h>
|
||||
|
||||
#include <Windows.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace LX
|
||||
{
|
||||
void PrintStringAsColor(const std::string& str, Color c)
|
||||
{
|
||||
// Gets a handle to the console //
|
||||
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
|
||||
// Sets the color of the console to the desired color //
|
||||
SetConsoleTextAttribute(hConsole, (WORD)c);
|
||||
|
||||
// Outputs the text //
|
||||
std::cout << str;
|
||||
|
||||
// Resets the color //
|
||||
SetConsoleTextAttribute(hConsole, (WORD)Color::LIGHT_GRAY);
|
||||
}
|
||||
}
|
||||
241
IR-Generator/src/main.cpp
Normal file
241
IR-Generator/src/main.cpp
Normal file
@@ -0,0 +1,241 @@
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include <Console.h>
|
||||
#include <Parser.h>
|
||||
#include <Lexer.h>
|
||||
#include <Util.h>
|
||||
|
||||
namespace LX
|
||||
{
|
||||
// Different errors thrown by main //
|
||||
|
||||
struct IncorrectCommandLineArgs {};
|
||||
struct InvalidInputFilePath {};
|
||||
struct InvalidOutputFilePath {};
|
||||
struct InvalidLogFilePath {};
|
||||
|
||||
// Util function for getting a line of the source at a given index (used for errors) //
|
||||
static std::string GetLineAtIndexOf(const std::string src, const std::streamsize index)
|
||||
{
|
||||
// Finds the start of the line //
|
||||
size_t start = src.rfind('\n', index);
|
||||
if (start == std::string::npos) { start = 0; } // None means first line
|
||||
else { start = start + 1; } // Skips new line char
|
||||
|
||||
// Finds the end of the line //
|
||||
size_t end = src.find('\n', index);
|
||||
if (end == std::string::npos) { end = src.size(); } // None means last line
|
||||
|
||||
// Returns the string between start and end //
|
||||
return src.substr(start, end - start);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// Creates the file paths outside of the try-catch so they can be used in errors //
|
||||
std::filesystem::path inpPath;
|
||||
std::filesystem::path outPath;
|
||||
std::filesystem::path logPath;
|
||||
|
||||
// Creates the contents string outside of the try-catch so they can be used in errors //
|
||||
std::string contents;
|
||||
LX::Token::source = &contents;
|
||||
|
||||
// Creates the log-file out of the try-catch so it can be closed propely if an error is thrown //
|
||||
std::unique_ptr<std::ofstream> log = nullptr;
|
||||
|
||||
try
|
||||
{
|
||||
// Checks there is the correct ammount of arguments //
|
||||
LX::ThrowIf<LX::IncorrectCommandLineArgs>((argc == 3 || argc == 4) == false);
|
||||
|
||||
// Turns the file paths into the C++ type for handling them //
|
||||
inpPath = argv[1];
|
||||
outPath = argv[2];
|
||||
|
||||
// Checks the input file exists and opens it //
|
||||
LX::ThrowIf<LX::InvalidInputFilePath>(std::filesystem::exists(inpPath) == false);
|
||||
std::ifstream inpFile(inpPath, std::ios::binary | std::ios::ate); // Opens in binary at the end for microptimisation //
|
||||
LX::ThrowIf<LX::InvalidInputFilePath>(inpFile.is_open() == false);
|
||||
|
||||
// Copies the file into the string //
|
||||
const std::streamsize len = inpFile.tellg(); // Gets length of file because it was opened at the end
|
||||
inpFile.seekg(0, std::ios::beg); // Goes back to the beginning
|
||||
contents = std::string(len, '\0'); // Allocates all the space for the string
|
||||
inpFile.read(&contents[0], len); // Transfers file contents to string
|
||||
|
||||
// Opens / Creates the output file //
|
||||
std::ofstream outFile(outPath);
|
||||
LX::ThrowIf<LX::InvalidOutputFilePath>(outFile.is_open() == false);
|
||||
outFile.close(); // Opened just to check we can
|
||||
|
||||
// Opens the log file (if there is one specified //
|
||||
if (argc == 4)
|
||||
{
|
||||
logPath = argv[3];
|
||||
log = std::make_unique<std::ofstream>(logPath);
|
||||
LX::ThrowIf<LX::InvalidLogFilePath>(log->is_open() == false);
|
||||
}
|
||||
|
||||
// Prints the full paths to the console to let the user know compiling is being done //
|
||||
std::cout << std::filesystem::absolute(inpPath) << " -> " << std::filesystem::absolute(outPath) << std::endl;
|
||||
|
||||
// Create tokens out of the input file //
|
||||
std::vector<LX::Token>tokens = LX::LexicalAnalyze(contents, len, log.get());
|
||||
LX::SafeFlush(log.get());
|
||||
|
||||
// Turns the tokens into an AST //
|
||||
LX::FileAST AST = LX::TurnTokensIntoAbstractSyntaxTree(tokens, log.get());
|
||||
LX::SafeFlush(log.get());
|
||||
|
||||
// Turns the AST into LLVM IR //
|
||||
LX::GenerateIR(AST, inpPath.filename().string(), outPath);
|
||||
LX::SafeFlush(log.get());
|
||||
|
||||
// Returns success
|
||||
return 0;
|
||||
}
|
||||
|
||||
catch (LX::IncorrectCommandLineArgs)
|
||||
{
|
||||
// Prints out how to correctly use the program //
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
std::cout << "Incorrect use of " << std::filesystem::path(argv[0]).filename().string() << ":\n\n";
|
||||
std::cout << "Usage: ";
|
||||
LX::PrintStringAsColor("[source file] [output file] [optional args]", LX::Color::WHITE);
|
||||
std::cout << "\n\nOptional arguments:\n";
|
||||
std::cout << "\tLog file for additional information\n\n";
|
||||
|
||||
// Warns the user that they are better of using a build system //
|
||||
LX::PrintStringAsColor("Warning:\n", LX::Color::LIGHT_YELLOW);
|
||||
std::cout << "\tIf you are seeing this message it is probably because you are not using a build-system\n";
|
||||
std::cout << "\tWorking with a build system is recommended for use of " << std::filesystem::path(argv[0]).filename().string() << "\n";
|
||||
std::cout << "\tOne can be found here: NULL\n\n"; // <- TODO: Make a build system
|
||||
|
||||
// Returns Exit id of 1 so other process can be alerted of the error //
|
||||
return 1;
|
||||
}
|
||||
|
||||
catch (LX::InvalidInputFilePath)
|
||||
{
|
||||
// Tells the user the input file could not be found and how to fix the issue //
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
std::cout << "Invalid file path: ";
|
||||
LX::PrintStringAsColor(inpPath.string(), LX::Color::WHITE);
|
||||
std::cout << "\n\nMake sure the file exists and the process has the correct path to the file\n";
|
||||
|
||||
// Returns Exit id of 2 so other process can be alerted of the error //
|
||||
return 2;
|
||||
}
|
||||
|
||||
catch (LX::InvalidOutputFilePath)
|
||||
{
|
||||
// Tells the user that the output file could not be found/created //
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
std::cout << "Invalid file path: ";
|
||||
LX::PrintStringAsColor(outPath.string(), LX::Color::WHITE);
|
||||
std::cout << "\n\nThe file could not be created or written to.\n";
|
||||
std::cout << "Check it is a valid file path and it has the permissions to modify the file\n";
|
||||
|
||||
// Returns Exit id of 3 so other process can be alerted of the error //
|
||||
return 3;
|
||||
}
|
||||
|
||||
catch (LX::InvalidLogFilePath)
|
||||
{
|
||||
// Tells the user that the log file cound not be found/created //
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
std::cout << "Invalid file path: ";
|
||||
LX::PrintStringAsColor(logPath.string(), LX::Color::WHITE);
|
||||
std::cout << "\n\nThe file could not be created or written to.\n";
|
||||
std::cout << "Check it is a valid file path and it has the permissions to modify the file\n";
|
||||
|
||||
// Returns Exit id of 4 so other process can be alerted of the error //
|
||||
return 4;
|
||||
}
|
||||
|
||||
catch (LX::InvalidCharInSource& e)
|
||||
{
|
||||
// Adds the error to the log and closes it to save all the output //
|
||||
LX::SafeLog(log.get(), LX::LOG_BREAK, "Error thrown from Lexer:\n\tInvalid character: ", e.invalid, " on line: ", e.line, LX::LOG_BREAK);
|
||||
if (log != nullptr) { log->close(); }
|
||||
|
||||
// Calculates the length of the line number in the console so it is formatted correctly //
|
||||
std::ostringstream oss;
|
||||
oss << std::setw(3) << e.line;
|
||||
size_t lineNumberWidthInConsole = std::max(oss.str().size(), (size_t)3);
|
||||
|
||||
// Gets the line of the error //
|
||||
std::string line = LX::GetLineAtIndexOf(contents, e.index);
|
||||
|
||||
// Prints the error with the relevant information to the console //
|
||||
std::cout << "\n";
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
std::cout << "Invalid character found in ";
|
||||
LX::PrintStringAsColor(inpPath.filename().string(), LX::Color::WHITE);
|
||||
std::cout << ":\n";
|
||||
std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << e.line << " | " << line << "\n";
|
||||
std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(e.col);
|
||||
LX::PrintStringAsColor("^", LX::Color::LIGHT_RED);
|
||||
std::cout << "\n";
|
||||
|
||||
// Returns Exit id of 5 so other process can be alerted of the error //
|
||||
return 5;
|
||||
}
|
||||
|
||||
catch (LX::UnexpectedToken& e)
|
||||
{
|
||||
// Calculates the length of the line number in the console so it is formatted correctly //
|
||||
std::ostringstream oss;
|
||||
oss << std::setw(3) << e.got.line;
|
||||
size_t lineNumberWidthInConsole = std::max(oss.str().size(), (size_t)3);
|
||||
|
||||
// Gets the line of the error //
|
||||
std::string line = LX::GetLineAtIndexOf(contents, e.got.index);
|
||||
|
||||
// Prints the error to the console with the relevant info //
|
||||
std::cout << "\n";
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
std::cout << "Incorrect syntax in ";
|
||||
LX::PrintStringAsColor(inpPath.filename().string(), LX::Color::WHITE);
|
||||
std::cout << ", found ";
|
||||
LX::PrintStringAsColor(LX::ToString(e.got.type), LX::Color::WHITE);
|
||||
std::cout << " expected: ";
|
||||
|
||||
// Allows the error to have a custom type that is printed to the console //
|
||||
if (e.expected == LX::Token::UNDEFINED) { LX::PrintStringAsColor(e.override, LX::Color::WHITE); }
|
||||
else { LX::PrintStringAsColor(LX::ToString(e.expected), LX::Color::WHITE); }
|
||||
std::cout << "\n";
|
||||
|
||||
// Prints the code with the error to the console //
|
||||
std::string errorSquiggle(e.got.length, '~');
|
||||
std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << e.got.line << " | " << line << "\n";
|
||||
std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(e.got.column) << "";
|
||||
LX::PrintStringAsColor(errorSquiggle, LX::Color::LIGHT_RED);
|
||||
std::cout << "\n";
|
||||
|
||||
// Returns Exit id of 6 so other process can be alerted of the error //
|
||||
return 6;
|
||||
}
|
||||
|
||||
// Catches any std errors, there should be none //
|
||||
catch (std::exception& e)
|
||||
{
|
||||
// Prints the std exception to the console //
|
||||
// Any errors here are problems with the code //
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
|
||||
// Default catches any non-specified errors //
|
||||
catch (...) {}
|
||||
|
||||
// Closes the log if it is open to get as much info as possible //
|
||||
if (log != nullptr) { log->close(); }
|
||||
std::cout << "An unknown error occured. Please report this on the github page.\n";
|
||||
return -1; // -1 exit code means an unknown error
|
||||
}
|
||||
Reference in New Issue
Block a user