Skip to content

Commit

Permalink
clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
mtygesen committed May 1, 2024
1 parent 704e145 commit 20a913a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 64 deletions.
2 changes: 0 additions & 2 deletions include/src/Evaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ class Evaluator : public AstVisitor {
void initPtable();
};



} // namespace dplsrc

#endif
10 changes: 3 additions & 7 deletions include/src/ast/nodes/ListNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ class ListNode : public UnaryNodeList {
visitor->visit(std::static_pointer_cast<ListNode>(shared_from_this()));
};

void setTypes(const std::vector<dplsrc::Type> &types) {
this->types = types;
}
void setTypes(const std::vector<dplsrc::Type> &types) { this->types = types; }

std::vector<dplsrc::Type> getTypes() {
return types;
}
std::vector<dplsrc::Type> getTypes() { return types; }

private:
private:
std::vector<dplsrc::Type> types;
};

Expand Down
121 changes: 67 additions & 54 deletions src/Evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,27 @@ void Evaluator::visit(const std::shared_ptr<DivExprNode> &node) {
if (rightNode->getType() == Type::INT) {
if (rightNode->getVal().get<int>() == 0) {
throw std::runtime_error("Cannot divide with 0");
}}
else if (rightNode->getType() == Type::FLOAT) {
if (rightNode->getVal().get<double>() == 0) {
throw std::runtime_error("Cannot divide with 0");
}
}
else if (rightNode->getVal().get<bool>() == 0) {
}
} else if (rightNode->getType() == Type::FLOAT) {
if (rightNode->getVal().get<double>() == 0) {
throw std::runtime_error("Cannot divide with 0");
}
} else if (rightNode->getVal().get<bool>() == 0) {
throw std::runtime_error("Cannot divide with 0");
}


// Evaluates the value of the expression
switch (leftNode->getType()) {
case Type::INT:
if (rightNode->getType() == Type::BOOL) {
node->setVal(static_cast<double>(leftNode->getVal().get<int>()) / rightNode->getVal().get<bool>());
node->setVal(static_cast<double>(leftNode->getVal().get<int>()) /
rightNode->getVal().get<bool>());
} else if (rightNode->getType() == Type::INT) {
node->setVal(static_cast<double>(leftNode->getVal().get<int>()) / rightNode->getVal().get<int>());
node->setVal(static_cast<double>(leftNode->getVal().get<int>()) /
rightNode->getVal().get<int>());
} else {
node->setVal(static_cast<double>(leftNode->getVal().get<int>()) / rightNode->getVal().get<double>());
node->setVal(static_cast<double>(leftNode->getVal().get<int>()) /
rightNode->getVal().get<double>());
}
break;
case Type::FLOAT:
Expand All @@ -104,11 +105,14 @@ void Evaluator::visit(const std::shared_ptr<DivExprNode> &node) {
break;
case Type::BOOL:
if (rightNode->getType() == Type::BOOL) {
node->setVal(static_cast<double>(leftNode->getVal().get<bool>()) / rightNode->getVal().get<bool>());
node->setVal(static_cast<double>(leftNode->getVal().get<bool>()) /
rightNode->getVal().get<bool>());
} else if (rightNode->getType() == Type::INT) {
node->setVal(static_cast<double>(leftNode->getVal().get<bool>()) / rightNode->getVal().get<int>());
node->setVal(static_cast<double>(leftNode->getVal().get<bool>()) /
rightNode->getVal().get<int>());
} else {
node->setVal(static_cast<double>(leftNode->getVal().get<bool>()) / rightNode->getVal().get<double>());
node->setVal(static_cast<double>(leftNode->getVal().get<bool>()) /
rightNode->getVal().get<double>());
}
break;
default:
Expand Down Expand Up @@ -207,19 +211,22 @@ void Evaluator::visit(const std::shared_ptr<ExpoExprNode> &node) {
switch (leftNode->getType()) {
case Type::INT:
if (rightNode->getType() == Type::BOOL) {
node->setVal(std::pow(leftNode->getVal().get<int>(), rightNode->getVal().get<bool>()));
node->setVal(
std::pow(leftNode->getVal().get<int>(), rightNode->getVal().get<bool>()));
node->setType(Type::INT);
} else if (rightNode->getType() == Type::INT) {
if (rightNode->getVal().get<int>() >= 0) {
node->setVal((int)round(std::pow(leftNode->getVal().get<int>(), rightNode->getVal().get<int>())));
node->setVal((int)round(
std::pow(leftNode->getVal().get<int>(), rightNode->getVal().get<int>())));
node->setType(Type::INT);
}
else {
node->setVal(std::pow(leftNode->getVal().get<int>(), rightNode->getVal().get<int>()));
} else {
node->setVal(
std::pow(leftNode->getVal().get<int>(), rightNode->getVal().get<int>()));
node->setType(Type::FLOAT);
}
} else {
node->setVal(std::pow(leftNode->getVal().get<int>(), rightNode->getVal().get<double>()));
node->setVal(
std::pow(leftNode->getVal().get<int>(), rightNode->getVal().get<double>()));
node->setType(Type::FLOAT);
}
break;
Expand All @@ -229,18 +236,21 @@ void Evaluator::visit(const std::shared_ptr<ExpoExprNode> &node) {
node->setVal(
std::pow(leftNode->getVal().get<double>(), rightNode->getVal().get<bool>()));
} else if (rightNode->getType() == Type::INT) {
node->setVal(std::pow(leftNode->getVal().get<double>(), rightNode->getVal().get<int>()));
node->setVal(
std::pow(leftNode->getVal().get<double>(), rightNode->getVal().get<int>()));
} else {
node->setVal(
std::pow(leftNode->getVal().get<double>(), rightNode->getVal().get<double>()));
}
break;
case Type::BOOL:
if (rightNode->getType() == Type::BOOL) {
node->setVal(std::pow(leftNode->getVal().get<bool>(), rightNode->getVal().get<bool>()));
node->setVal(
std::pow(leftNode->getVal().get<bool>(), rightNode->getVal().get<bool>()));
node->setType(Type::INT);
} else if (rightNode->getType() == Type::INT) {
node->setVal(std::pow(leftNode->getVal().get<bool>(), rightNode->getVal().get<int>()));
node->setVal(
std::pow(leftNode->getVal().get<bool>(), rightNode->getVal().get<int>()));
node->setType(Type::INT);
} else {
node->setVal(
Expand Down Expand Up @@ -724,9 +734,9 @@ void Evaluator::visit(const std::shared_ptr<MultExprNode> &node) {
std::shared_ptr<AstNode> rightNode = node->getRightNode();
rightNode->accept(shared_from_this());

if (!((isNumeric(leftNode->getType()) && isNumeric(rightNode->getType())) ||
(leftNode->getType() == Type::INT && rightNode->getType() == Type::STR) ||
(leftNode->getType() == Type::STR && rightNode->getType() == Type::INT))) {
if (!((isNumeric(leftNode->getType()) && isNumeric(rightNode->getType())) ||
(leftNode->getType() == Type::INT && rightNode->getType() == Type::STR) ||
(leftNode->getType() == Type::STR && rightNode->getType() == Type::INT))) {
// TODO: move to error handler at some point
throw std::runtime_error("Cannot multiply with the used types");
}
Expand All @@ -745,8 +755,9 @@ void Evaluator::visit(const std::shared_ptr<MultExprNode> &node) {
node->setVal(leftNode->getVal().get<int>() * rightNode->getVal().get<double>());
node->setType(Type::FLOAT);
} else {
for (size_t i = 0; i < leftNode->getVal().get<int>(); i++)
node->setVal(node->getVal().get<std::string>() + rightNode->getVal().get<std::string>());
for (size_t i = 0; i < leftNode->getVal().get<int>(); i++)
node->setVal(node->getVal().get<std::string>() +
rightNode->getVal().get<std::string>());
}
break;
case Type::FLOAT:
Expand All @@ -772,8 +783,9 @@ void Evaluator::visit(const std::shared_ptr<MultExprNode> &node) {
}
break;
case Type::STR:
for (size_t i = 0; i < rightNode->getVal().get<int>(); i++)
node->setVal(node->getVal().get<std::string>() + leftNode->getVal().get<std::string>());
for (size_t i = 0; i < rightNode->getVal().get<int>(); i++)
node->setVal(node->getVal().get<std::string>() +
leftNode->getVal().get<std::string>());
break;
default:
throw std::runtime_error("Error: Couldn't convert string to value of nodes");
Expand Down Expand Up @@ -849,32 +861,34 @@ void Evaluator::visit(const std::shared_ptr<NotNode> &node) {
std::shared_ptr<AstNode> childNode = node->getChildNode();
childNode->accept(shared_from_this());

if (!(isNumeric(childNode->getType()) )) {
if (!(isNumeric(childNode->getType()))) {
// TODO: move to error handler at some point
throw std::runtime_error("Cannot negate the used type");
}

// Evaluates the value of the expression
switch (childNode->getType())
{
case Type::INT:
if (childNode->getVal().get<int>() != 0)
node->setVal(0);
else node->setVal(1);
break;
case Type::FLOAT:
if (childNode->getVal().get<double>() != 0)
node->setVal(0);
else node->setVal(1);
break;
case Type::BOOL:
if (childNode->getVal().get<bool>() != 0)
node->setVal(0);
else node->setVal(1);
break;
default:
throw std::runtime_error("Couldn't evaluate negation");
break;
switch (childNode->getType()) {
case Type::INT:
if (childNode->getVal().get<int>() != 0)
node->setVal(0);
else
node->setVal(1);
break;
case Type::FLOAT:
if (childNode->getVal().get<double>() != 0)
node->setVal(0);
else
node->setVal(1);
break;
case Type::BOOL:
if (childNode->getVal().get<bool>() != 0)
node->setVal(0);
else
node->setVal(1);
break;
default:
throw std::runtime_error("Couldn't evaluate negation");
break;
}
if (childNode->getVal().get<bool>() == 1)
childNode->setVal(0);
Expand Down Expand Up @@ -934,8 +948,8 @@ void Evaluator::visit(const std::shared_ptr<PlusExprNode> &node) {
std::shared_ptr<AstNode> rightNode = node->getRightNode();
rightNode->accept(shared_from_this());

if (!((isNumeric(leftNode->getType()) && isNumeric(rightNode->getType())) ||
(leftNode->getType() == Type::STR && rightNode->getType() == Type::STR))) {
if (!((isNumeric(leftNode->getType()) && isNumeric(rightNode->getType())) ||
(leftNode->getType() == Type::STR && rightNode->getType() == Type::STR))) {
// TODO: move to error handler at some point
throw std::runtime_error("Cannot do addition with the used types");
}
Expand Down Expand Up @@ -1144,4 +1158,3 @@ void Evaluator::initPtable() {
ptable.bind(Procedure("type", {"x"}, type));
ptable.bind(Procedure("str", {"x"}, str));
}

2 changes: 1 addition & 1 deletion src/ast/nodes/LeafNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ std::string LeafNode::print(std::string indent = "", std::string prefix = "") {
return "";
}

void LeafNode::addChild(std::shared_ptr<AstNode> child) {
void LeafNode::addChild([[maybe_unused]] std::shared_ptr<AstNode> child) {
throw std::runtime_error("LeafNode cannot have children");
}

Expand Down

0 comments on commit 20a913a

Please sign in to comment.