-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryExpr.java
More file actions
79 lines (65 loc) · 2.17 KB
/
Copy pathBinaryExpr.java
File metadata and controls
79 lines (65 loc) · 2.17 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package jtree.nodes;
import java.util.function.Consumer;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
@EqualsAndHashCode
@Getter @Setter
public class BinaryExpr extends Node implements Expression {
protected @NonNull Expression left;
protected @NonNull BinaryExpr.Op operation;
private @NonNull Expression right;
public BinaryExpr(Expression left, BinaryExpr.Op operation, Expression right) {
setOperation(operation);
setLeft(left);
setRight(right);
}
@Override
public Precedence precedence() {
return operation.precedence;
}
@Override
public BinaryExpr clone() {
return new BinaryExpr(getLeft().clone(), getOperation(), getRight().clone());
}
@Override
public String toCode() {
return wrap(getLeft()).toCode() + " " + getOperation() + " " + wrap(getRight()).toCode();
}
@RequiredArgsConstructor
public static enum Op { // @formatter:off
OR ("||", Precedence.LOGIC_OR),
AND ("&&", Precedence.LOGIC_AND),
BIT_OR ("|", Precedence.BIT_OR),
XOR ("^", Precedence.BIT_XOR),
BIT_AND ("&", Precedence.BIT_AND),
EQUAL ("==", Precedence.EQUALITY),
NEQUAL ("!=", Precedence.EQUALITY),
LTHAN ("<", Precedence.RELATIONAL),
GTHAN (">", Precedence.RELATIONAL),
LEQUAL ("<=", Precedence.RELATIONAL),
GEQUAL (">=", Precedence.RELATIONAL),
LSHIFT ("<<", Precedence.BIT_SHIFT),
RSHIFT (">>", Precedence.BIT_SHIFT),
URSHIFT (">>>", Precedence.BIT_SHIFT),
PLUS ("+", Precedence.ADDITIVE),
MINUS ("-", Precedence.ADDITIVE),
TIMES ("*", Precedence.MULTIPLICATIVE),
DIVIDE ("/", Precedence.MULTIPLICATIVE),
MODULUS ("%", Precedence.MULTIPLICATIVE);
// @formatter:on
@Getter @Accessors(fluent = true)
private final String toString;
public final Precedence precedence;
}
@Override
public <N extends INode> void accept(TreeVisitor visitor, Node parent, Consumer<N> replacer) {
if(visitor.visitBinaryExpr(this, parent, cast(replacer))) {
getLeft().accept(visitor, this, this::setLeft);
getRight().accept(visitor, this, this::setRight);
}
}
}