Skip to content

Commit

Permalink
Add more crash protection, config.ini
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-exe committed Sep 24, 2023
1 parent 2b12125 commit 6d92b9a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 8 deletions.
19 changes: 18 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.15)
project(LogiXpr)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

# Google C++ Testing Framework
cmake_policy(SET CMP0135 NEW)
Expand Down Expand Up @@ -39,3 +39,20 @@ target_link_libraries(

include(GoogleTest)
gtest_discover_tests(tests)

# copy config file to build directory
# check if debug or release folder exists
message("Copying config file...")
if(NOT EXISTS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Debug)
file(MAKE_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Debug)
configure_file(config.ini ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Debug/config.ini COPYONLY)
else()
configure_file(config.ini ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Debug/config.ini COPYONLY)
endif()

if(NOT EXISTS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Release)
file(MAKE_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Release)
configure_file(config.ini ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Release/config.ini COPYONLY)
else()
configure_file(config.ini ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Release/config.ini COPYONLY)
endif()
2 changes: 2 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MAX_QUEUE_SIZE=2500
MAX_EXPRESSION_LENGTH=50
10 changes: 10 additions & 0 deletions include/solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
*/
extern std::unordered_map<EquivLaws::EquivLaw, std::string> equivalences;

/**
* @brief Maximum size of the queue of expressions to be processed
*/
extern int MAX_QUEUE_SIZE;

/**
* @brief Maximum length of the expressions in the queue
*/
extern int MAX_EXPRESSION_LENGTH;

/**
* @brief Fill the map of equivalences to be applied to the expressions.
* Must be called before any other functions in this file.
Expand Down
54 changes: 53 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <iostream>
#include <string>
#include <fstream>

#include "include/evaluator.h"
#include "include/parser.h"
Expand All @@ -23,6 +24,39 @@ int main() {
====================================================================)"
<< std::endl;

std::cout << ALERT << "Reading config.ini..." << std::endl;
// check if config.ini exists
std::ifstream configFile("config.ini");
if (!configFile.good()) {
std::cout << FAILURE << "config.ini not found :(" << std::endl;
return 1;
}

// read config.ini
std::string line;
int maxQueueSize = 0;
int maxExprLength = 0;
while (std::getline(configFile, line)) {
if (line.find("MAX_QUEUE_SIZE") != std::string::npos) {
maxQueueSize = std::stoi(line.substr(line.find("=") + 1));
} else if (line.find("MAX_EXPRESSION_LENGTH") != std::string::npos) {
maxExprLength = std::stoi(line.substr(line.find("=") + 1));
}
}

// check if config.ini is valid
if (maxQueueSize <= 0 || maxExprLength <= 0) {
std::cout << FAILURE << "config.ini has bad values!" << std::endl;
return 1;
}

std::cout << SUCCESS << "Read config.ini successfully!" << std::endl;
std::cout << ALERT << "Setting config values... " << std::endl;

// set config values
MAX_QUEUE_SIZE = maxQueueSize;
MAX_EXPRESSION_LENGTH = maxExprLength;

std::string lhs;
std::string rhs;

Expand Down Expand Up @@ -103,7 +137,24 @@ int main() {
// process the steps to make it aesthetically pleasing
std::vector<std::vector<std::string>> processedSteps;
int longestStrLen = 0;
for (auto step : steps) {
// check if the last step is not "Too many steps :("
if (steps.back()[1] == "Too many steps :(") {
std::cout << FAILURE << "Too many in queue :(. " << std::endl;
std::cout << ALERT << "Tweak config ini by reducing logical expression length or increasing queue size" << std::endl;
std::cout << ALERT << "Press any key to exit..." << std::endl;
std::cin.get();
return 1;
}
else if (steps.back()[1] == "Couldn't find a solution :(") {
// check if the last step is not "Couldn't find a solution :("
std::cout << FAILURE << "Couldn't find a solution :(. " << std::endl;
std::cout << ALERT << "Tweak config ini by reducing logical expression length or increasing queue size" << std::endl;
std::cout << ALERT << "Press any key to exit..." << std::endl;
std::cin.get();
return 1;
}
else {
for (auto step : steps) {
std::shared_ptr<Expression> currentStep;
parse(step[0], currentStep);
std::string processedStr = currentStep->toStringMinimal();
Expand All @@ -118,6 +169,7 @@ int main() {
else if (processedSteps.size() > 0 &&
processedSteps.back()[0] != processedStr)
processedSteps.push_back({processedStr, step[1]});
}
}

// now add padding to the right side of the string to make it look nice
Expand Down
15 changes: 9 additions & 6 deletions src/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

std::unordered_map<EquivLaws::EquivLaw, std::string> equivalences = {};

int MAX_QUEUE_SIZE = 2500;
int MAX_EXPRESSION_LENGTH = 50;

void preprocess(std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs)
{
// include laws
Expand Down Expand Up @@ -42,14 +45,14 @@ std::vector<std::vector<std::string>> proveEquivalence(std::shared_ptr<Expressio
visited[lhs->toStringTree()] = {"", "Given"};

bool found = false;

while (!queue.empty())
{
// if queue is too long, stop
if (queue.size() > 2500)
if (queue.size() > MAX_QUEUE_SIZE)
{
steps.push_back({"", "Too many steps :("});
break;
return steps;
}
std::shared_ptr<Expression> expr = queue.front();
queue.pop();
Expand All @@ -64,12 +67,12 @@ std::vector<std::vector<std::string>> proveEquivalence(std::shared_ptr<Expressio
currentExprString = visited[currentExprString].first;
}
std::reverse(steps.begin(), steps.end());
break;
return steps;
}

generateNextSteps(expr, rhs, found, queue, visited);
}
return steps;
return {{"", "Couldn't find a solution :("}};
}

void generateNextSteps(std::shared_ptr<Expression> expr, std::shared_ptr<Expression> end, bool &found, std::queue<std::shared_ptr<Expression>> &queue, std::unordered_map<std::string, std::pair<std::string, std::string>> &visited)
Expand All @@ -87,7 +90,7 @@ void generateNextSteps(std::shared_ptr<Expression> expr, std::shared_ptr<Express
if (funct(newExpr))
{
std::string newExprString = newExpr->toStringTree();
if (newExprString.length() > 50)
if (newExprString.length() > MAX_EXPRESSION_LENGTH)
continue;
// ignore extremely long expressions

Expand Down

0 comments on commit 6d92b9a

Please sign in to comment.