-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpression.java
More file actions
executable file
·56 lines (50 loc) · 1.97 KB
/
Expression.java
File metadata and controls
executable file
·56 lines (50 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
interface Expression {
/**
* Returns the expression's parent.
* @return the expression's parent
*/
CompoundExpression getParent ();
/**
* Sets the parent be the specified expression.
* @param parent the CompoundExpression that should be the parent of the target object
*/
void setParent (CompoundExpression parent);
/**
* Creates and returns a deep copy of the expression.
* The entire tree rooted at the target node is copied, i.e.,
* the copied Expression is as deep as possible.
* @return the deep copy
*/
Expression deepCopy ();
/**
* Recursively flattens the expression as much as possible
* throughout the entire tree. Specifically, in every multiplicative
* or additive expression x whose first or last
* child c is of the same type as x, the children of c will be added to x, and
* c itself will be removed. This method modifies the expression itself.
*/
void flatten ();
/**
* Creates a String representation by recursively printing out (using indentation) the
* tree represented by this expression, starting at the specified indentation level.
* @param stringBuilder the StringBuilder to use for building the String representation
* @param indentLevel the indentation level (number of tabs from the left margin) at which to start
*/
void convertToString (StringBuilder stringBuilder, int indentLevel);
public default String convertToString (int indentLevel) {
final StringBuilder stringBuilder = new StringBuilder();
convertToString(stringBuilder, indentLevel);
return stringBuilder.toString();
}
/**
* Static helper method to indent a specified number of times from the left margin, by
* appending tab characters to the specified StringBuilder.
* @param sb the StringBuilder to which to append tab characters.
* @param indentLevel the number of tabs to append.
*/
public static void indent (StringBuilder stringBuilder, int indentLevel) {
for (int i = 0; i < indentLevel; i++) {
stringBuilder.append('\t');
}
}
}