Require Clang 13.0+ in the CMakeLists.txt file, import the tok::type enumeration in lexer.cpp + Parser.hpp, and add [[likely]] attributes to 2 branches of the switch case in lexer.cpp.

This commit is contained in:
Username404 2021-08-09 10:55:59 +02:00
parent 3c703e55b5
commit 3b991da962
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
3 changed files with 28 additions and 22 deletions

View File

@ -35,9 +35,6 @@ if (NOT MSVC)
endif()
if ((${CMAKE_CXX_COMPILER_ID} STREQUAL GNU) OR (${CMAKE_CXX_COMPILER_ID} STREQUAL Clang))
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 11.0)
message(WARNING "A ${CMAKE_CXX_COMPILER_ID} compiler version of preferably 11 or higher should be used.")
endif()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc")
set(CMAKE_CXX_FLAGS "-flto ${CMAKE_CXX_FLAGS} -pipe -fstack-protector-strong -fstack-clash-protection")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -ftree-vectorize")
@ -49,9 +46,15 @@ if ((${CMAKE_CXX_COMPILER_ID} STREQUAL GNU) OR (${CMAKE_CXX_COMPILER_ID} STREQUA
endif()
if (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 11.0)
message(WARNING "A GNU compiler version of preferably 11 or higher should be used.")
endif()
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fira-loop-pressure -ftree-loop-distribution -floop-interchange -fipa-pta -fivopts -s")
set(CMAKE_CXX_FLAGS "-fno-use-linker-plugin -fwhole-program ${CMAKE_CXX_FLAGS}")
elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 13.0)
message(FATAL_ERROR "Clang 13.0 or higher is required.")
endif()
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS "-fwhole-program-vtables ${CMAKE_CXX_FLAGS}")
endif()

View File

@ -1,11 +1,12 @@
#include "../headers/lex.hpp"
using namespace std;
using enum tok::type;
tok::type getIdentifierCharType(const char& Char) {
if (isalpha(Char)) return tok::IDENTIFIER;
else if (isdigit(Char)) return tok::NUMBER;
else if (Char == '"') return tok::STRING;
else return tok::UNEXPECTED;
if (isalpha(Char)) return IDENTIFIER;
else if (isdigit(Char)) return NUMBER;
else if (Char == '"') return STRING;
else return UNEXPECTED;
}
vector<tok> lex(const string& in)
@ -13,24 +14,25 @@ vector<tok> lex(const string& in)
vector<tok> resVal;
for (unsigned int i = 0; i < in.size(); ++i) {
const char& current = in[i];
switch (current) {
case tok::TAG: case tok::DEFINE: case tok::LPAR: case tok::RPAR:
case tok::LBRACE: case tok::RBRACE: case tok::LBRACKET: case tok::RBRACKET:
case tok::PLUS: case tok::HYPHEN: case tok::LCOMP: case tok::RCOMP:
case TAG: case DEFINE: case LPAR: case RPAR:
case LBRACE: case RBRACE: case LBRACKET: case RBRACKET:
case PLUS: case HYPHEN: case LCOMP: case RCOMP:
case '$': case '\'': resVal.emplace_back(static_cast<tok::type>(current), string(1, current));
case ' ': case '\t': case '\r':
case '\n': break;
[[likely]] case ' ': case '\t': case '\r':
[[likely]] case '\n': break;
default: {
tok::type type = getIdentifierCharType(current);
bool isTypeString = (type == tok::STRING);
bool isTypeString = (type == STRING);
switch (type) {
case tok::UNEXPECTED: break;
case tok::STRING: ++i;
case tok::IDENTIFIER: case tok::NUMBER: {
case UNEXPECTED: break;
case STRING: ++i;
case IDENTIFIER: case NUMBER: {
string formedString;
for (;i < in.size(); ++i) {
tok::type currentCharType = getIdentifierCharType(in[i]);
bool isString = currentCharType == tok::STRING;
bool isString = currentCharType == STRING;
if (i == in.size() - 1 && not isString) throw tok::LexerException("A never ending string was found");
if ((currentCharType == type || isTypeString) && !isString) {
formedString += string(1, in[i]);

View File

@ -12,20 +12,21 @@ namespace Parser {
ParseTree parseVector(const vector<tok>& lexed) {
ParseTree parseTree;
using namespace StandardComponents;
using enum tok::type;
if (lexed.size() > 1) {
for (unsigned int i = 0; i < lexed.size() - 1; ++i) {
const auto& current = lexed[i], next = lexed[i + 1];
switch (current.toktype) {
case tok::STRING: parseTree << types::String(current.toktext.data()); break;
case tok::IDENTIFIER: {
case STRING: parseTree << types::String(current.toktext.data()); break;
case IDENTIFIER: {
if (current.toktext == "class") {
if (next.toktype == tok::IDENTIFIER) {
if (next.toktype == IDENTIFIER) {
parseTree << Class(next.toktext); ++i;
} else throw ParsingException('"' + next.toktext + "\" is not a valid class identifier");
} else {
const bool isFinalDefine = next.toktype == tok::TAG && (lexed.size() - i) > 2 && lexed[i + 2].toktype == tok::DEFINE;
if (isFinalDefine || next.toktype == tok::DEFINE) {
const bool isFinalDefine = next.toktype == TAG && (lexed.size() - i) > 2 && lexed[i + 2].toktype == DEFINE;
if (isFinalDefine || next.toktype == DEFINE) {
parseTree << Define(isFinalDefine, current.toktext);
i += isFinalDefine ? 2 : 1;
} else {