diff --git a/Cpp/CMakeLists.txt b/Cpp/CMakeLists.txt index 72bbd5c..5bcbad9 100755 --- a/Cpp/CMakeLists.txt +++ b/Cpp/CMakeLists.txt @@ -128,6 +128,10 @@ if (WITH_TEST) target_link_libraries(ArcscriptTest PRIVATE nlohmann_json::nlohmann_json) target_link_libraries(ArcscriptTest PRIVATE ArcscriptTranspiler) + if (CMAKE_HOST_UNIX) + find_package(Threads REQUIRED) + target_link_libraries(ArcscriptTest PRIVATE Threads::Threads) + endif() set_target_properties(ArcscriptTest PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/demo/${PLATFORM}" @@ -156,7 +160,7 @@ if (WITH_TEST) "$/${PROJECT_NAME}.exp" "$/${PROJECT_NAME}.pdb" ) - elseif (APPLE) + else() set(ARCSCRIPT_TRANSPILER_LIB_FILES "$/${DLL_NAME}" "$/${LIBRARY_NAME}" diff --git a/Cpp/demo/ArcscriptTest.cpp b/Cpp/demo/ArcscriptTest.cpp index 995f7bf..eb5a561 100644 --- a/Cpp/demo/ArcscriptTest.cpp +++ b/Cpp/demo/ArcscriptTest.cpp @@ -92,6 +92,13 @@ UVisit* getVisits(json initVisits) { return visits; } +void freeVisits(UVisit* visits, size_t visitsLen) { + for (size_t i = 0; i < visitsLen; i++) { + free(const_cast(visits[i].elId)); + } + delete[] visits; +} + std::vector events; void onEvent(const char* eventName) { @@ -147,29 +154,28 @@ std::string test(json testCase, size_t caseIndex, UVariable* initVars, size_t in errorOutput << "Received Parse Error: " << e.what() << std::endl; } } - } catch (std::exception &e) { - if (!hasError) { - errorOutput << "Unexpected Exception: " << e.what() << std::endl; - } else { - if (errorType != "exception") { - errorOutput << "Received Exception: " << e.what() << std::endl; - } - } } + free(const_cast(currentElement)); + if (visits != nullptr) { + freeVisits(visits, visitsLen); + } + + if (result == nullptr) { if (!errorOutput.str().empty()) { std::stringstream temp; temp << "Test case " << caseIndex << " failed: \"" << code << "\"" << std::endl << errorOutput.rdbuf(); errorOutput.swap(temp); } + free(const_cast(code)); return errorOutput.str(); } if (hasError) { errorOutput << "Test case " << caseIndex << " failed: \"" << code << "\"" << std::endl; errorOutput << "Expected error of type: " << errorType << " but no error thrown." << std::endl; - + free(const_cast(code)); return errorOutput.str(); } @@ -251,14 +257,6 @@ std::string test(json testCase, size_t caseIndex, UVariable* initVars, size_t in } } - if (visits != nullptr) { - for (int i = 0; i < visitsLen; i++) { - free(const_cast(visits[i].elId)); - } - delete visits; - } - free(const_cast(currentElement)); - deallocateOutput(result); if (!errorOutput.str().empty()) { @@ -321,7 +319,7 @@ int testFile(const std::filesystem::path& path, int testIndex = -1) { free(const_cast(initVars[j].string_val)); } } - delete initVars; + delete[] initVars; if (fileError) { return 1; diff --git a/Cpp/src/ArcscriptErrorExceptions.h b/Cpp/src/ArcscriptErrorExceptions.h index 8e5c559..f08b087 100755 --- a/Cpp/src/ArcscriptErrorExceptions.h +++ b/Cpp/src/ArcscriptErrorExceptions.h @@ -2,6 +2,7 @@ #include #include +#include #include #include diff --git a/Cpp/src/ArcscriptExpression.cpp b/Cpp/src/ArcscriptExpression.cpp index 4b75888..fefd147 100755 --- a/Cpp/src/ArcscriptExpression.cpp +++ b/Cpp/src/ArcscriptExpression.cpp @@ -1,6 +1,7 @@ #include "ArcscriptExpression.h" #include #include +#include #include "ArcscriptErrorExceptions.h" @@ -88,14 +89,13 @@ Expression Expression::operator+ (const Expression &other) const { return {valueToString(value) + valueToString(other.value)}; } NumberValues values = doubleValues(value, other.value); - Expression* result; + if (!values.hasDoubles) { int intValue = static_cast(values.value1 + values.value2); - result = new Expression(intValue); - } else { - result = new Expression(values.value1 + values.value2); + return Expression(intValue); } - return *result; + + return Expression(values.value1 + values.value2); } Expression Expression::operator- (const Expression &other) const { @@ -103,56 +103,46 @@ Expression Expression::operator- (const Expression &other) const { throw RuntimeErrorException("Cannot subtract strings"); } NumberValues values = doubleValues(value, other.value); - Expression* result; + if (!values.hasDoubles) { int intValue = static_cast(values.value1 - values.value2); - result = new Expression(intValue); - } else { - result = new Expression(values.value1 - values.value2); + return Expression(intValue); } - return *result; + return Expression(values.value1 - values.value2); } Expression Expression::operator* (const Expression &other) const { NumberValues values = doubleValues(value, other.value); - Expression* result; + if (!values.hasDoubles) { int intValue = static_cast(values.value1 * values.value2); - result = new Expression(intValue); - } else { - result = new Expression(values.value1 * values.value2); + return Expression(intValue); } - return *result; + return Expression(values.value1 * values.value2); } Expression Expression::operator* (const int other) const { NumberValues values = doubleValues(value, other); - Expression* result; + if (!values.hasDoubles) { int intValue = static_cast(values.value1 * values.value2); - result = new Expression(intValue); - } else { - result = new Expression(values.value1 * values.value2); + return Expression(intValue); } - return *result; + return Expression(values.value1 * values.value2); } Expression Expression::operator/ (const Expression &other) const { NumberValues values = doubleValues(value, other.value); - Expression* result; if (values.value2 == 0) { throw RuntimeErrorException("Division by zero is not allowed."); } - result = new Expression(values.value1 / values.value2); - - return *result; + return Expression(values.value1 / values.value2); } Expression Expression::operator% (const Expression &other) const { NumberValues values = doubleValues(value, other.value); - Expression* result; if (values.value2 == 0) { throw RuntimeErrorException("Modulo by zero is not allowed."); @@ -160,14 +150,18 @@ Expression Expression::operator% (const Expression &other) const { if (!values.hasDoubles) { int intValue = static_cast(static_cast(values.value1) % static_cast(values.value2)); - result = new Expression(intValue); - } else { - double modValue = std::fmod(values.value1, values.value2); - result = new Expression(modValue); + return Expression(intValue); } - return *result; + double modValue = std::fmod(values.value1, values.value2); + return Expression(modValue); } +Expression Expression::operator=(const Expression &other) { + value = other.value; + return *this; +} + + Expression Expression::operator+= (const Expression &other) { if (value.type() == typeid(std::string) || other.value.type() == typeid(std::string)) { auto val1 = valueToString(value); @@ -243,10 +237,10 @@ Expression Expression::operator%= (const Expression &other) { if (!values.hasDoubles) { int intValue = static_cast(static_cast(values.value1) % static_cast(values.value2)); - value = new Expression(intValue); + value = intValue; } else { double modValue = std::fmod(values.value1, values.value2); - value = new Expression(modValue); + value = modValue; } return *this; } diff --git a/Cpp/src/ArcscriptExpression.h b/Cpp/src/ArcscriptExpression.h index 0bcf11e..be1d04a 100755 --- a/Cpp/src/ArcscriptExpression.h +++ b/Cpp/src/ArcscriptExpression.h @@ -53,6 +53,7 @@ class Expression { Expression operator* (const int other) const; Expression operator/ (const Expression &other) const; Expression operator% (const Expression &other) const; + Expression operator= (const Expression &other); Expression operator+= (const Expression &other); Expression operator-= (const Expression &other); Expression operator*= (const Expression &other); diff --git a/Cpp/src/ArcscriptFunctions.cpp b/Cpp/src/ArcscriptFunctions.cpp index 9ac2cb0..326180e 100755 --- a/Cpp/src/ArcscriptFunctions.cpp +++ b/Cpp/src/ArcscriptFunctions.cpp @@ -21,6 +21,7 @@ namespace Arcweave { break; case 'f': result += '\f'; + break; case 'n': result += '\n'; break; @@ -181,7 +182,7 @@ namespace Arcweave { std::any ArcscriptFunctions::Show(std::vector args) { std::string result; - for (int i = 0; i < args.size(); i++) { + for (size_t i = 0; i < args.size(); i++) { std::any arg = args[i]; if (arg.type() == typeid(int)) { result += std::to_string(std::any_cast(arg)); diff --git a/Cpp/src/ArcscriptHelpers.h b/Cpp/src/ArcscriptHelpers.h index 0713a90..492899d 100755 --- a/Cpp/src/ArcscriptHelpers.h +++ b/Cpp/src/ArcscriptHelpers.h @@ -34,7 +34,7 @@ class ArcscriptState { std::string currentElement; std::map visits; - std::function(emit); + std::function emit; ArcscriptState(std::string elementId, std::map varValues, std::map _visits, std::function _emit) { currentElement = elementId; @@ -66,7 +66,7 @@ class ArcscriptState { variableChanges[varId] = value; } inline void setVarValues(std::vector names, std::vector values) { - for (int i = 0; i < names.size(); i++) { + for (size_t i = 0; i < names.size(); i++) { variableChanges[names[i]] = values[i]; } } diff --git a/Cpp/src/ArcscriptParserBase.cpp b/Cpp/src/ArcscriptParserBase.cpp index 1ae9582..2b9788b 100755 --- a/Cpp/src/ArcscriptParserBase.cpp +++ b/Cpp/src/ArcscriptParserBase.cpp @@ -39,7 +39,7 @@ bool ArcscriptParserBase::assertMention(std::any attrCtxList) { } if ((attrValue.rfind("\"", 0) == 0 && ends_with(attrValue, "\"")) || - attrValue.rfind("'", 0) == 0 && ends_with(attrValue, "'")) { + (attrValue.rfind("'", 0) == 0 && ends_with(attrValue, "'"))) { attrValue = attrValue.substr(1, attrValue.size() - 2); diff --git a/Cpp/src/ArcscriptVisitor.cpp b/Cpp/src/ArcscriptVisitor.cpp index da05015..85be54c 100755 --- a/Cpp/src/ArcscriptVisitor.cpp +++ b/Cpp/src/ArcscriptVisitor.cpp @@ -460,7 +460,7 @@ std::any ArcscriptVisitor::visitMention_attributes(ArcscriptParser::Mention_attr if (valueNode != NULL) { std::string strvalue = valueNode->getText(); if ((strvalue.rfind("\"", 0) == 0 && ends_with(strvalue, "\"")) || - strvalue.rfind("'", 0) == 0 && ends_with(strvalue, "'")) { + (strvalue.rfind("'", 0) == 0 && ends_with(strvalue, "'"))) { strvalue = strvalue.substr(1, strvalue.size() - 2); diff --git a/Cpp/src/ArcscriptVisitor.h b/Cpp/src/ArcscriptVisitor.h index 41ec406..41fa6e4 100755 --- a/Cpp/src/ArcscriptVisitor.h +++ b/Cpp/src/ArcscriptVisitor.h @@ -18,6 +18,9 @@ class ArcscriptVisitor : public ArcscriptParserBaseVisitor { explicit ArcscriptVisitor(ArcscriptState* _state) : state(_state) { functions = new ArcscriptFunctions(state); } + ~ArcscriptVisitor() override { + delete functions; + } std::any visitInput(ArcscriptParser::InputContext *ctx) override; std::any visitScript_section(ArcscriptParser::Script_sectionContext *ctx) override;