Skip to content

Commit 9e44863

Browse files
committed
simplify expression printer
1 parent 083a79f commit 9e44863

File tree

2 files changed

+24
-34
lines changed

2 files changed

+24
-34
lines changed

src/form/expression.cpp

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#include <sstream>
55

66
Expression::Expression()
7-
: type(Expression::Type::CONSTANT), value(Number::ZERO){};
7+
: type(Expression::Type::CONSTANT), value(Number::ZERO) {};
88

99
Expression::Expression(Type type, const std::string& name, const Number& value)
10-
: type(type), name(name), value(value){};
10+
: type(type), name(name), value(value) {};
1111

1212
Expression::Expression(Type type, const std::string& name,
1313
std::initializer_list<Expression> children)
@@ -197,7 +197,7 @@ void Expression::replaceName(const std::string& from, const std::string& to) {
197197
}
198198

199199
std::ostream& operator<<(std::ostream& out, const Expression& e) {
200-
e.print(out, 0, true, Expression::Type::CONSTANT);
200+
e.print(out, true, Expression::Type::CONSTANT);
201201
return out;
202202
}
203203

@@ -247,24 +247,23 @@ std::pair<Expression, bool> extractSign(const Expression& e) {
247247
return result;
248248
}
249249

250-
void Expression::print(std::ostream& out, size_t index, bool isRoot,
250+
void Expression::print(std::ostream& out, bool isRoot,
251251
Expression::Type parentType) const {
252-
const bool brackets = needsBrackets(index, isRoot, parentType);
252+
const bool brackets = needsBrackets(isRoot, parentType);
253253
if (brackets) {
254254
out << "(";
255255
}
256256
auto extracted = extractSign(*this);
257257
if (extracted.second) {
258258
out << "-";
259259
}
260-
extracted.first.printExtracted(out, index, isRoot, parentType);
260+
extracted.first.printExtracted(out);
261261
if (brackets) {
262262
out << ")";
263263
}
264264
}
265265

266-
void Expression::printExtracted(std::ostream& out, size_t index, bool isRoot,
267-
Expression::Type parentType) const {
266+
void Expression::printExtracted(std::ostream& out) const {
268267
switch (type) {
269268
case Expression::Type::CONSTANT:
270269
out << value;
@@ -273,39 +272,37 @@ void Expression::printExtracted(std::ostream& out, size_t index, bool isRoot,
273272
out << name;
274273
break;
275274
case Expression::Type::FUNCTION:
276-
printChildrenWrapped(out, ",", isRoot, parentType, name + "(", ")");
275+
printChildrenWrapped(out, ",", name + "(", ")");
277276
break;
278277
case Expression::Type::VECTOR:
279-
printChildrenWrapped(out, ",", isRoot, parentType, name + "[", "]");
278+
printChildrenWrapped(out, ",", name + "[", "]");
280279
break;
281280
case Expression::Type::LOCAL:
282-
printChildrenWrapped(out, "); ", isRoot, parentType,
283-
"local(" + name + "=", "");
281+
printChildrenWrapped(out, "); ", "local(" + name + "=", "");
284282
break;
285283
case Expression::Type::SUM:
286-
printChildren(out, "+", isRoot, parentType);
284+
printChildren(out, "+");
287285
break;
288286
case Expression::Type::PRODUCT:
289-
printChildren(out, "*", isRoot, parentType);
287+
printChildren(out, "*");
290288
break;
291289
case Expression::Type::FRACTION:
292-
printChildren(out, "/", isRoot, parentType);
290+
printChildren(out, "/");
293291
break;
294292
case Expression::Type::POWER:
295-
printChildren(out, "^", isRoot, parentType);
293+
printChildren(out, "^");
296294
break;
297295
case Expression::Type::MODULUS:
298-
printChildren(out, "%", isRoot, parentType);
296+
printChildren(out, "%");
299297
break;
300298
case Expression::Type::IF:
301299
assertNumChildren(3);
302-
printChildrenWrapped(out, ",", isRoot, parentType, "if(n==", ")");
300+
printChildrenWrapped(out, ",", "if(n==", ")");
303301
break;
304302
}
305303
}
306304

307-
bool Expression::needsBrackets(size_t index, bool isRoot,
308-
Expression::Type parentType) const {
305+
bool Expression::needsBrackets(bool isRoot, Expression::Type parentType) const {
309306
if (isRoot) {
310307
return false;
311308
}
@@ -344,22 +341,20 @@ bool Expression::needsBrackets(size_t index, bool isRoot,
344341
return true;
345342
}
346343

347-
void Expression::printChildren(std::ostream& out, const std::string& op,
348-
bool isRoot, Expression::Type parentType) const {
344+
void Expression::printChildren(std::ostream& out, const std::string& op) const {
349345
for (size_t i = 0; i < children.size(); i++) {
350346
auto extracted = extractSign(children[i]);
351347
if (i > 0 && (op != "+" || !extracted.second)) {
352348
out << op;
353349
}
354-
children[i].print(out, i, false, type);
350+
children[i].print(out, false, type);
355351
}
356352
}
357353

358354
void Expression::printChildrenWrapped(std::ostream& out, const std::string& op,
359-
bool isRoot, Expression::Type parentType,
360355
const std::string& prefix,
361356
const std::string& suffix) const {
362357
out << prefix;
363-
printChildren(out, op, isRoot, parentType);
358+
printChildren(out, op);
364359
out << suffix;
365360
}

src/form/expression.hpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,15 @@ class Expression {
8888

8989
int compareChildren(const Expression& e) const;
9090

91-
void print(std::ostream& out, size_t index, bool isRoot,
92-
Expression::Type parentType) const;
91+
void print(std::ostream& out, bool isRoot, Expression::Type parentType) const;
9392

94-
void printExtracted(std::ostream& out, size_t index, bool isRoot,
95-
Expression::Type parentType) const;
93+
void printExtracted(std::ostream& out) const;
9694

97-
void printChildren(std::ostream& out, const std::string& op, bool isRoot,
98-
Expression::Type parentType) const;
95+
void printChildren(std::ostream& out, const std::string& op) const;
9996

10097
void printChildrenWrapped(std::ostream& out, const std::string& op,
101-
bool isRoot, Expression::Type parentType,
10298
const std::string& prefix,
10399
const std::string& suffix) const;
104100

105-
bool needsBrackets(size_t index, bool isRoot,
106-
Expression::Type parentType) const;
101+
bool needsBrackets(bool isRoot, Expression::Type parentType) const;
107102
};

0 commit comments

Comments
 (0)