Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -156,7 +160,7 @@ if (WITH_TEST)
"$<TARGET_FILE_DIR:ArcscriptTranspiler>/${PROJECT_NAME}.exp"
"$<TARGET_FILE_DIR:ArcscriptTranspiler>/${PROJECT_NAME}.pdb"
)
elseif (APPLE)
else()
set(ARCSCRIPT_TRANSPILER_LIB_FILES
"$<TARGET_FILE_DIR:ArcscriptTranspiler>/${DLL_NAME}"
"$<TARGET_FILE_DIR:ArcscriptTranspiler>/${LIBRARY_NAME}"
Expand Down
34 changes: 16 additions & 18 deletions Cpp/demo/ArcscriptTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(visits[i].elId));
}
delete[] visits;
}

std::vector<std::string> events;

void onEvent(const char* eventName) {
Expand Down Expand Up @@ -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<char *>(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<char *>(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<char *>(code));
return errorOutput.str();
}

Expand Down Expand Up @@ -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<char *>(visits[i].elId));
}
delete visits;
}
free(const_cast<char *>(currentElement));

deallocateOutput(result);

if (!errorOutput.str().empty()) {
Expand Down Expand Up @@ -321,7 +319,7 @@ int testFile(const std::filesystem::path& path, int testIndex = -1) {
free(const_cast<char *>(initVars[j].string_val));
}
}
delete initVars;
delete[] initVars;

if (fileError) {
return 1;
Expand Down
1 change: 1 addition & 0 deletions Cpp/src/ArcscriptErrorExceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <iostream>
#include <string>
#include <string.h>
#include <sstream>
#include <exception>

Expand Down
58 changes: 26 additions & 32 deletions Cpp/src/ArcscriptExpression.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ArcscriptExpression.h"
#include <sstream>
#include <cstring>
#include <cmath>

#include "ArcscriptErrorExceptions.h"

Expand Down Expand Up @@ -88,86 +89,79 @@ 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<int>(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 {
if (value.type() == typeid(std::string) || other.value.type() == typeid(std::string)) {
throw RuntimeErrorException("Cannot subtract strings");
}
NumberValues values = doubleValues(value, other.value);
Expression* result;

if (!values.hasDoubles) {
int intValue = static_cast<int>(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<int>(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<int>(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.");
}

if (!values.hasDoubles) {
int intValue = static_cast<int>(static_cast<int>(values.value1) % static_cast<int>(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);
Expand Down Expand Up @@ -243,10 +237,10 @@ Expression Expression::operator%= (const Expression &other) {

if (!values.hasDoubles) {
int intValue = static_cast<int>(static_cast<int>(values.value1) % static_cast<int>(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;
}
Expand Down
1 change: 1 addition & 0 deletions Cpp/src/ArcscriptExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion Cpp/src/ArcscriptFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Arcweave {
break;
case 'f':
result += '\f';
break;
case 'n':
result += '\n';
break;
Expand Down Expand Up @@ -181,7 +182,7 @@ namespace Arcweave {

std::any ArcscriptFunctions::Show(std::vector<std::any> 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<int>(arg));
Expand Down
4 changes: 2 additions & 2 deletions Cpp/src/ArcscriptHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ArcscriptState {
std::string currentElement;
std::map<std::string, int> visits;

std::function<void(const char*)>(emit);
std::function<void(const char*)> emit;

ArcscriptState(std::string elementId, std::map<std::string, Variable> varValues, std::map<std::string, int> _visits, std::function<void(const char*)> _emit) {
currentElement = elementId;
Expand Down Expand Up @@ -66,7 +66,7 @@ class ArcscriptState {
variableChanges[varId] = value;
}
inline void setVarValues(std::vector<std::string> names, std::vector<std::any> values) {
for (int i = 0; i < names.size(); i++) {
for (size_t i = 0; i < names.size(); i++) {
variableChanges[names[i]] = values[i];
}
}
Expand Down
2 changes: 1 addition & 1 deletion Cpp/src/ArcscriptParserBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Cpp/src/ArcscriptVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions Cpp/src/ArcscriptVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down