Skip to content

Commit

Permalink
Got strings times int working
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsVind committed May 15, 2024
1 parent 2249f39 commit f9b6184
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ target_include_directories(dpli PRIVATE ${PROJECT_SOURCE_DIR}/include)

target_link_libraries(dpli PRIVATE dpllib antlr4_static)

#add_subdirectory(tests)
add_subdirectory(tests)

# Strip debug symbols in Release mode
if(CMAKE_BUILD_TYPE STREQUAL "Release")
Expand Down
2 changes: 2 additions & 0 deletions include/src/Evaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <DplLexer.h>

#include <cmath>
#include <string>
#include <utility>
#include <filesystem>

#include "AllNodeIncludes.hpp"
Expand Down
6 changes: 3 additions & 3 deletions src/Evaluator.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include "Evaluator.hpp"

Check notice on line 1 in src/Evaluator.cpp

View workflow job for this annotation

GitHub Actions / lint

Run clang-format on src/Evaluator.cpp

File src/Evaluator.cpp does not conform to Custom style guidelines. (lines 1, 526, 550, 724, 767, 786, 805, 820)

#include <cmath>
#include <string>
#include <utility>

using namespace dplsrc;

Expand Down Expand Up @@ -846,6 +843,9 @@ void Evaluator::initPtable() {
if (val.is<Value::FLOAT>()) {
return static_cast<Value::INT>(std::floor(val.get<Value::FLOAT>()));
}
if (val.is<Value::INT>()) {
return val.get<Value::INT>();
}

throw RuntimeException("Floor called with invalid type " + val.toTypeString() +
". Expected: " + Value(0.0).toTypeString());
Expand Down
42 changes: 33 additions & 9 deletions src/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,10 @@ Value Value::operator*(const Value& other) const {
const std::function<Value(Value, Value)> op = [](const Value& val1,
const Value& val2) -> Value {

if ((val1.is<INT>() || val1.is<BOOL>()) && (val2.is<INT>() || val2.is<BOOL>())) {
bool isVal1Int = val1.is<INT>() || val1.is<BOOL>();
bool isVal2Int = val2.is<INT>() || val2.is<BOOL>();

if (isVal1Int && isVal2Int) {
Value::INT int1 = (val1.is<INT>()) ? val1.get<INT>() : val1.get<BOOL>();
Value::INT int2 = (val2.is<INT>()) ? val2.get<INT>() : val2.get<BOOL>();

Expand All @@ -411,8 +414,8 @@ Value Value::operator*(const Value& other) const {

return Value(float1 * float2);
}
if (((val1.is<INT>() || val1.is<BOOL>()) && val2.is<STR>()) ||
((val2.is<INT>() || val2.is<BOOL>()) && val1.is<STR>())) {
if ((isVal1Int && val2.is<STR>()) ||
(isVal2Int && val1.is<STR>())) {
int intVal = 0;
std::string strVal;

Expand Down Expand Up @@ -452,8 +455,8 @@ Value Value::operator*(const Value& other) const {

return Value(result);
}
throw InternalException("Cannot multiply values of type " + val1.toTypeString() +
" and " + val2.toTypeString());
throw InternalException("Cannot multiply values of type " + val1.toTypeString()+ ": " + val1.toString() +
" and " + val2.toTypeString() + ": " + val2.toString());
};
return Value::binaryOperator(other, errOpWord, op);
}
Expand All @@ -472,8 +475,19 @@ Value Value::operator/(const Value& other) const {
(val2.is<BOOL>() && val2.get<BOOL>() == 0)) {
throw InternalException("Cannot take remainder of values with divisor 0");
}
return Value((val1.is<INT>() ? val1.get<INT>() : val1.get<FLOAT>()) /
(val2.is<INT>() ? val2.get<INT>() : val2.get<FLOAT>()));
if (!val1.is<FLOAT>() && !val2.is<FLOAT>()) {
Value::INT int1 = (val1.is<INT>()) ? val1.get<INT>() : val1.get<BOOL>();
Value::INT int2 = (val2.is<INT>()) ? val2.get<INT>() : val2.get<BOOL>();

return Value::INT(int1 / int2);
}
auto float1 = (val1.is<INT>()) ? val1.get<INT>() :
(val1.is<FLOAT>()) ? val1.get<FLOAT>() : val1.get<BOOL>();

auto float2 = (val2.is<INT>()) ? val2.get<INT>() :
(val2.is<FLOAT>()) ? val2.get<FLOAT>() : val2.get<BOOL>();

return Value(float1 / float2);
};
return Value::binaryOperator(other, errOpWord, op);
}
Expand All @@ -492,7 +506,7 @@ Value Value::operator%(const Value& other) const {
(val2.is<BOOL>() && val2.get<BOOL>() == false)) {
throw InternalException("Cannot take remainder of values with divisor 0");
}
if (!val1.is<FLOAT>() && !val2.is<FLOAT>()) {
if (!val1.is<FLOAT>() && !val2.is<FLOAT>()) {
Value::INT int1 = (val1.is<INT>()) ? val1.get<INT>() : val1.get<BOOL>();
Value::INT int2 = (val2.is<INT>()) ? val2.get<INT>() : val2.get<BOOL>();

Expand Down Expand Up @@ -543,7 +557,7 @@ Value Value::operator-() const {
return Value(-get<FLOAT>());
}
if (is<BOOL>()) {
return Value(!get<BOOL>());
return Value(-Value::INT(get<BOOL>()));
}
if (is<COLUMN>()) {
Value::COLUMN col = get<COLUMN>();
Expand Down Expand Up @@ -668,6 +682,16 @@ Value Value::binaryOperator(const Value& other, const std::string& errOpWord,
}
return Value(result);
}
if ((is<STR>() && (other.isNumeric() || other.is<STR>())) ||
(other.is<STR>() && (isNumeric() || is<STR>()))) {

return Value(op(*this, other));
}
if (is<LIST>() && other.isNumeric() ||
other.is<LIST>() && isNumeric()) {

return Value(op(*this, other));
}
if ((is<COLUMN>() && (other.isNumeric() || other.is<STR>())) ||
(other.is<COLUMN>() && (isNumeric() || is<STR>()))) {
const Value::LIST& list1 = get<COLUMN>()->data;
Expand Down

0 comments on commit f9b6184

Please sign in to comment.