6#include "../include/expression.h"
20 return this->
left !=
nullptr;
25 return this->
right !=
nullptr;
36 return this->
value.length() == 1 && std::islower(this->
value[0]);
57 this->left->setParent(
parent);
63 this->right->setParent(
parent);
73 std::set<std::string> variables;
75 variables.insert(this->
value);
79 std::set<std::string> leftVariables = this->
getLeft()->getVariables();
80 variables.insert(leftVariables.begin(), leftVariables.end());
85 std::set<std::string> rightVariables = this->
getRight()->getVariables();
86 variables.insert(rightVariables.begin(), rightVariables.end());
94 std::shared_ptr<Expression> clonedExpression;
96 clonedExpression = std::make_shared<Expression>(this->
getValue());
98 clonedExpression->setLeft(this->
getLeft()->
clone(), clonedExpression);
100 clonedExpression->setRight(this->
getRight()->
clone(), clonedExpression);
102 return clonedExpression;
108 return this->
clone();
110 std::shared_ptr<Expression> root = this->
getParent();
111 while (root->getParent())
112 root = root->getParent();
114 std::shared_ptr<Expression> clonedTree = root->clone();
117 std::stack<std::shared_ptr<Expression>> stack;
118 stack.push(clonedTree);
120 while (!stack.empty())
122 std::shared_ptr<Expression> current = stack.top();
128 if (current->hasLeft())
129 stack.push(current->getLeft());
130 if (current->hasRight())
131 stack.push(current->getRight());
139 if (
this == other.get())
142 if (this->
getValue() != other->getValue())
145 if (this->
hasLeft() != other->hasLeft())
148 if (this->
hasRight() != other->hasRight())
165 std::shared_ptr<Expression> root = this->
getParent();
166 while (root->getParent())
167 root = root->getParent();
169 return root->compare(other);
177 std::string expressionString =
"";
180 expressionString +=
"!(" + this->
getLeft()->toString() +
")";
184 expressionString +=
"(" + this->
getLeft()->toString() +
")";
185 expressionString +=
" " + this->
getValue() +
" ";
186 expressionString +=
"(" + this->
getRight()->toString() +
")";
188 return expressionString;
196 std::shared_ptr<Expression> root = this->
getParent();
197 while (root->getParent())
198 root = root->getParent();
200 std::string treeString = root->toString();
209 std::string expressionString =
"";
217 expressionString += this->
getValue();
218 expressionString += this->
getLeft()->toStringMinimal();
222 expressionString += this->
getValue();
223 expressionString +=
"(" + this->
getLeft()->toStringMinimal() +
")";
228 std::string leftMinimal = this->
getLeft()->toStringMinimal();
230 std::string rightMinimal = this->
getRight()->toStringMinimal();
237 if (precedence.at(this->getValue()) >
238 precedence.at(this->getLeft()->getValue()))
240 leftMinimal =
"(" + leftMinimal +
")";
249 if (precedence.at(this->getValue()) >
250 precedence.at(this->getRight()->getValue()))
252 rightMinimal =
"(" + rightMinimal +
")";
256 expressionString += leftMinimal;
257 expressionString +=
" " + op +
" ";
258 expressionString += rightMinimal;
260 return expressionString;
std::shared_ptr< Expression > right
Pointer to right subexpression.
bool compareTree(std::shared_ptr< Expression > other)
Compare the entire expression tree to another expression tree.
std::string getValue()
Get the value of the current expression.
std::shared_ptr< Expression > getLeft()
Get the left subexpression of the current expression.
bool compare(std::shared_ptr< Expression > other)
Compare the current expression to another expression.
std::set< std::string > getVariables()
Get all the variables in the current expression and subexpressions.
void setRight(std::shared_ptr< Expression > right, std::shared_ptr< Expression > parent)
Sets the right child expression of this expression.
std::shared_ptr< Expression > getRight()
Get the right subexpression of the current expression.
Expression(std::string value)
Construct a new Expression object.
std::string toString()
Convert the current expression to a string.
std::string toStringTree()
Convert the whole expression tree to a string.
std::shared_ptr< Expression > left
Pointer to left subexpression.
std::shared_ptr< Expression > getParent()
Get the parent expression of the current expression.
bool hasRight()
Check if the current expression has a right subexpression.
bool isBinary()
Check if the current expression is a binary expression.
std::shared_ptr< Expression > cloneTree()
Create a clone of the whole expression tree and returns the pointer to the node where it is called.
bool isVar()
Check if the current expression is a variable.
void setParent(std::shared_ptr< Expression > parent)
Sets the parent node expression of this expression.
std::shared_ptr< Expression > clone()
Create a clone of the current expression and subexpressions.
std::string value
The current expression.
std::string toStringMinimal()
Convert the current expression to a string with minimal parentheses.
bool hasLeft()
Check if the current expression has a left subexpression.
void setLeft(std::shared_ptr< Expression > left, std::shared_ptr< Expression > parent)
Sets the left child expression of this expression.
std::shared_ptr< Expression > parent
Pointer to parent expression.