Fix tabbing.... Please fix your editors so they do tabbing correctly!!! (Spaces...
[IRC.git] / Robust / src / IR / Tree / OpNode.java
1 package IR.Tree;
2 import IR.Operation;
3 import IR.TypeDescriptor;
4
5 public class OpNode extends ExpressionNode {
6   ExpressionNode left;
7   ExpressionNode right;
8   Operation op;
9   TypeDescriptor td;
10   TypeDescriptor lefttype;
11   TypeDescriptor righttype;
12
13   public OpNode(ExpressionNode l, ExpressionNode r, Operation o) {
14     left=l;
15     right=r;
16     op=o;
17   }
18
19   public OpNode(ExpressionNode l, Operation o) {
20     left=l;
21     right=null;
22     op=o;
23   }
24
25   public ExpressionNode getLeft() {
26     return left;
27   }
28
29   public ExpressionNode getRight() {
30     return right;
31   }
32
33   public Operation getOp() {
34     return op;
35   }
36
37   public String printNode(int indent) {
38     if (right==null)
39       return op.toString()+"("+left.printNode(indent)+")";
40     else
41       return left.printNode(indent)+" "+op.toString()+" "+right.printNode(indent);
42   }
43
44   public void setLeftType(TypeDescriptor argtype) {
45     this.lefttype=argtype;
46   }
47
48   public TypeDescriptor getLeftType() {
49     return lefttype;
50   }
51
52   public void setRightType(TypeDescriptor argtype) {
53     this.righttype=argtype;
54   }
55
56   public TypeDescriptor getRightType() {
57     return righttype;
58   }
59
60   public TypeDescriptor getType() {
61     return td;
62   }
63
64   public void setType(TypeDescriptor td) {
65     this.td=td;
66   }
67
68   public int kind() {
69     return Kind.OpNode;
70   }
71
72   public Long evaluate() {
73     eval = null;
74     Long l = this.left.evaluate();
75     if(l != null) {
76       if (this.op.getOp() == Operation.LOGIC_NOT)
77         eval = Long.valueOf(l.longValue() > 0?0:1);
78       else if (this.op.getOp() == Operation.COMP)
79         eval = Long.valueOf((long)(~l.longValue()));
80       else if (this.op.getOp() == Operation.UNARYMINUS)
81         eval = Long.valueOf(-l.longValue() );
82       else if (this.op.getOp() == Operation.UNARYPLUS)
83         eval = Long.valueOf(+l.longValue());
84       else {
85         Long r = this.right.evaluate();
86         if(r != null) {
87           //if (this.op.getOp() == Operation.LOGIC_OR)
88           //  return Long.valueOf((long)(l.longValue() || r.longValue()));
89           //else if (this.op.getOp() == Operation.LOGIC_AND)
90           //  return Long.valueOf((long)(l.longValue() && r.longValue()));
91           /*else */ if (this.op.getOp() == Operation.BIT_OR)
92             eval = Long.valueOf(l.longValue() | r.longValue());
93           else if (this.op.getOp() == Operation.BIT_XOR)
94             eval = Long.valueOf(l.longValue() ^ r.longValue());
95           else if (this.op.getOp() == Operation.BIT_AND)
96             eval = Long.valueOf(l.longValue() & r.longValue());
97           else if (this.op.getOp() == Operation.EQUAL)
98             eval = Long.valueOf((l.longValue() == r.longValue())?1:0);
99           else if (this.op.getOp() == Operation.NOTEQUAL)
100             eval = Long.valueOf((l.longValue() != r.longValue())?1:0);
101           else if (this.op.getOp() == Operation.LT)
102             eval = Long.valueOf((l.longValue() < r.longValue())?1:0);
103           else if (this.op.getOp() == Operation.GT)
104             eval = Long.valueOf((l.longValue() > r.longValue())?1:0);
105           else if (this.op.getOp() == Operation.LTE)
106             eval = Long.valueOf((l.longValue() <= r.longValue())?1:0);
107           else if (this.op.getOp() == Operation.GTE)
108             eval = Long.valueOf((l.longValue() >= r.longValue())?1:0);
109           else if (this.op.getOp() == Operation.LEFTSHIFT)
110             eval = Long.valueOf(l.longValue() << r.longValue());
111           else if (this.op.getOp() == Operation.RIGHTSHIFT)
112             eval = Long.valueOf(l.longValue() >> r.longValue());
113           else if (this.op.getOp() == Operation.URIGHTSHIFT)
114             eval = Long.valueOf(l.longValue() >>> r.longValue());
115           else if (this.op.getOp() == Operation.SUB)
116             eval = Long.valueOf(l.longValue() - r.longValue());
117           else if (this.op.getOp() == Operation.ADD)
118             eval = Long.valueOf(l.longValue() + r.longValue());
119           else if (this.op.getOp() == Operation.MULT)
120             eval = Long.valueOf(l.longValue() * r.longValue());
121           else if (this.op.getOp() == Operation.DIV)
122             eval = Long.valueOf(l.longValue() / r.longValue());
123           else if (this.op.getOp() == Operation.MOD)
124             eval = Long.valueOf(l.longValue() % r.longValue());
125           else if (this.op.getOp() == Operation.ASSIGN)
126             eval = Long.valueOf(r.longValue());
127         }
128       }
129     }
130     return eval;
131   }
132 }