LogiXpr
Loading...
Searching...
No Matches
expression.cpp
Go to the documentation of this file.
1
6#include "../include/expression.h"
7
8Expression::Expression(std::string value)
9{
10 this->value = value;
11}
12
14{
15 return this->value;
16}
17
19{
20 return this->left != nullptr;
21}
22
24{
25 return this->right != nullptr;
26}
27
29{
30 return this->hasLeft() && this->hasRight();
31}
32
34{
35 // if the expression is a variable, it will be a single character from a-z
36 return this->value.length() == 1 && std::islower(this->value[0]);
37}
38
39std::shared_ptr<Expression> Expression::getParent()
40{
41 return this->parent;
42}
43
44std::shared_ptr<Expression> Expression::getLeft()
45{
46 return this->left;
47}
48
49std::shared_ptr<Expression> Expression::getRight()
50{
51 return this->right;
52}
53
54void Expression::setLeft(std::shared_ptr<Expression> left, std::shared_ptr<Expression> parent)
55{
56 this->left = left;
57 this->left->setParent(parent);
58}
59
60void Expression::setRight(std::shared_ptr<Expression> right, std::shared_ptr<Expression> parent)
61{
62 this->right = right;
63 this->right->setParent(parent);
64}
65
66void Expression::setParent(std::shared_ptr<Expression> parent)
67{
68 this->parent = parent;
69}
70
71std::set<std::string> Expression::getVariables()
72{
73 std::set<std::string> variables;
74 if (this->isVar())
75 variables.insert(this->value);
76
77 if (this->hasLeft())
78 {
79 std::set<std::string> leftVariables = this->getLeft()->getVariables();
80 variables.insert(leftVariables.begin(), leftVariables.end());
81 }
82
83 if (this->hasRight())
84 {
85 std::set<std::string> rightVariables = this->getRight()->getVariables();
86 variables.insert(rightVariables.begin(), rightVariables.end());
87 }
88
89 return variables;
90}
91
92std::shared_ptr<Expression> Expression::clone()
93{
94 std::shared_ptr<Expression> clonedExpression;
95
96 clonedExpression = std::make_shared<Expression>(this->getValue());
97 if (this->hasLeft())
98 clonedExpression->setLeft(this->getLeft()->clone(), clonedExpression);
99 if (this->hasRight())
100 clonedExpression->setRight(this->getRight()->clone(), clonedExpression);
101
102 return clonedExpression;
103}
104
105std::shared_ptr<Expression> Expression::cloneTree()
106{
107 if (!this->getParent())
108 return this->clone();
109 // keep going up the tree until we find the root
110 std::shared_ptr<Expression> root = this->getParent();
111 while (root->getParent())
112 root = root->getParent();
113
114 std::shared_ptr<Expression> clonedTree = root->clone();
115
116 // now, go back down the tree and find the node we called from
117 std::stack<std::shared_ptr<Expression>> stack;
118 stack.push(clonedTree);
119
120 while (!stack.empty())
121 {
122 std::shared_ptr<Expression> current = stack.top();
123 stack.pop();
124
125 if (this->compare(current))
126 return current;
127
128 if (current->hasLeft())
129 stack.push(current->getLeft());
130 if (current->hasRight())
131 stack.push(current->getRight());
132 }
133 return nullptr;
134}
135
136bool Expression::compare(std::shared_ptr<Expression> other)
137{
138 // make sure the expressions don't have the same address
139 if (this == other.get())
140 return false;
141
142 if (this->getValue() != other->getValue())
143 return false;
144
145 if (this->hasLeft() != other->hasLeft())
146 return false;
147
148 if (this->hasRight() != other->hasRight())
149 return false;
150
151 if (this->hasLeft() && !this->getLeft()->compare(other->getLeft()))
152 return false;
153
154 if (this->hasRight() && !this->getRight()->compare(other->getRight()))
155 return false;
156
157 return true;
158}
159
160bool Expression::compareTree(std::shared_ptr<Expression> other)
161{
162 if (!this->getParent())
163 return this->compare(other);
164
165 std::shared_ptr<Expression> root = this->getParent();
166 while (root->getParent())
167 root = root->getParent();
168
169 return root->compare(other);
170}
171
173{
174 if (this->isVar() || this->getValue() == "T" || this->getValue() == "F")
175 return this->getValue();
176
177 std::string expressionString = "";
178 if (this->getValue() == NOT)
179 {
180 expressionString += "!(" + this->getLeft()->toString() + ")";
181 }
182 else
183 {
184 expressionString += "(" + this->getLeft()->toString() + ")";
185 expressionString += " " + this->getValue() + " ";
186 expressionString += "(" + this->getRight()->toString() + ")";
187 }
188 return expressionString;
189}
190
192{
193 if (!this->getParent())
194 return this->toString();
195
196 std::shared_ptr<Expression> root = this->getParent();
197 while (root->getParent())
198 root = root->getParent();
199
200 std::string treeString = root->toString();
201 return treeString;
202}
203
205{
206 if (this->isVar() || this->getValue() == "T" || this->getValue() == "F")
207 return this->getValue();
208
209 std::string expressionString = "";
210
211 if (this->getValue() == NOT)
212 {
213 // check if the left side is a variable or T or F
214 if (this->getLeft()->isVar() || this->getLeft()->getValue() == "T" ||
215 this->getLeft()->getValue() == "F")
216 {
217 expressionString += this->getValue();
218 expressionString += this->getLeft()->toStringMinimal();
219 }
220 else
221 {
222 expressionString += this->getValue();
223 expressionString += "(" + this->getLeft()->toStringMinimal() + ")";
224 }
225 }
226 else
227 {
228 std::string leftMinimal = this->getLeft()->toStringMinimal();
229 std::string op = this->getValue();
230 std::string rightMinimal = this->getRight()->toStringMinimal();
231
232 // check if the left side is a variable or T or F
233 if (!this->getLeft()->isVar() && this->getLeft()->getValue() != "T" &&
234 this->getLeft()->getValue() != "F")
235 {
236 // determine operator precedence
237 if (precedence.at(this->getValue()) >
238 precedence.at(this->getLeft()->getValue()))
239 {
240 leftMinimal = "(" + leftMinimal + ")";
241 }
242 }
243
244 // check if the right side is a variable or T or F
245 if (!this->getRight()->isVar() && this->getRight()->getValue() != "T" &&
246 this->getRight()->getValue() != "F")
247 {
248 // determine operator precedence
249 if (precedence.at(this->getValue()) >
250 precedence.at(this->getRight()->getValue()))
251 {
252 rightMinimal = "(" + rightMinimal + ")";
253 }
254 }
255
256 expressionString += leftMinimal;
257 expressionString += " " + op + " ";
258 expressionString += rightMinimal;
259 }
260 return expressionString;
261}
std::shared_ptr< Expression > right
Pointer to right subexpression.
Definition expression.h:212
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.
Definition expression.cpp:8
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.
Definition expression.h:207
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.
Definition expression.h:197
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.
Definition expression.h:202
#define NOT
Definition expression.h:23