KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > core > nodes > OperatorNode


1 package com.genimen.djeneric.tools.generator.core.nodes;
2
3 import com.genimen.djeneric.repository.DjList;
4 import com.genimen.djeneric.tools.generator.core.DjentelParserEngine;
5 import com.genimen.djeneric.tools.generator.core.ParseException;
6 import com.genimen.djeneric.tools.generator.core.SimpleNode;
7 import com.genimen.djeneric.tools.generator.core.util.ParseContext;
8
9 public class OperatorNode extends SimpleNode implements ValueExpression
10 {
11   private String JavaDoc operator;
12
13   public OperatorNode(int i)
14   {
15     super(i);
16   }
17
18   public OperatorNode(DjentelParserEngine p, int i)
19   {
20     super(p, i);
21   }
22
23   public String JavaDoc getName()
24   {
25     return toString();
26   }
27
28   public String JavaDoc toString()
29   {
30     return getOperator() + " (op)";
31   }
32
33   public void setOperator(String JavaDoc operator)
34   {
35     this.operator = operator;
36   }
37
38   public String JavaDoc getOperator()
39   {
40     return operator;
41   }
42
43   public Object JavaDoc getValue(ParseContext context) throws ParseException
44   {
45     ValueExpression left = (ValueExpression) getChild(0);
46     ValueExpression right = (ValueExpression) getChild(1);
47
48     // Concatenation of two functions?
49
if (getOperator().equals("."))
50     {
51       Object JavaDoc leftVal = left.getValue(context);
52       if (!(right instanceof PropertyOrFunctionNode))
53       {
54         throw new ParseException("Can only apply . to a right side function", beginLine, beginColumn);
55       }
56       PropertyOrFunctionNode pof = (PropertyOrFunctionNode) right;
57       try
58       {
59         return pof.evalFunction(leftVal, context);
60       }
61       catch (Exception JavaDoc x)
62       {
63         throw new ParseException(x, beginLine, beginColumn);
64       }
65     }
66
67     Object JavaDoc leftVal = left.getValue(context);
68     Object JavaDoc rightVal = right.getValue(context);
69
70     // First handle null stuff
71
if (leftVal == null || rightVal == null)
72     {
73       return null;
74     }
75
76     // String operation?
77
if ((leftVal instanceof String JavaDoc) || (rightVal instanceof String JavaDoc))
78     {
79       if (getOperator().equals("+")) return leftVal.toString() + rightVal.toString();
80       throw new ParseException("Can not apply " + getOperator() + " to strings", beginLine, beginColumn);
81     }
82
83     // Float op?
84
if ((leftVal instanceof Float JavaDoc) || (rightVal instanceof Float JavaDoc))
85     {
86       float l = toFloat(leftVal).floatValue();
87       float r = toFloat(rightVal).floatValue();
88
89       if (getOperator().equals("+")) return new Float JavaDoc(l + r);
90       if (getOperator().equals("-")) return new Float JavaDoc(l - r);
91       if (getOperator().equals("*")) return new Float JavaDoc(l * r);
92       if (getOperator().equals("/")) return new Float JavaDoc(l / r);
93       throw new ParseException("Can not apply " + getOperator() + " to floats", beginLine, beginColumn);
94     }
95     // Integer op?
96
if ((leftVal instanceof Integer JavaDoc) || (rightVal instanceof Integer JavaDoc))
97     {
98       int l = toInteger(leftVal).intValue();
99       int r = toInteger(rightVal).intValue();
100
101       if (getOperator().equals("+")) return new Integer JavaDoc(l + r);
102       if (getOperator().equals("-")) return new Integer JavaDoc(l - r);
103       if (getOperator().equals("*")) return new Integer JavaDoc(l * r);
104       if (getOperator().equals("/")) return new Integer JavaDoc(l / r);
105       if (getOperator().equals("%")) return new Integer JavaDoc(l % r);
106       throw new ParseException("Can not apply " + getOperator() + " to integers", beginLine, beginColumn);
107     }
108     // List op?
109
if ((leftVal instanceof DjList) && (rightVal instanceof DjList))
110     {
111       DjList l = (DjList) leftVal;
112       DjList r = (DjList) rightVal;
113
114       if (getOperator().equals("+")) return addLists(l, r);
115
116       if (getOperator().equals("-")) return subtractLists(l, r);
117       throw new ParseException("Can not apply " + getOperator() + " to sets", beginLine, beginColumn);
118     }
119     throw new ParseException("Can not apply " + classOnly(leftVal) + getOperator() + classOnly(rightVal), beginLine,
120         beginColumn);
121   }
122
123   DjList addLists(DjList l, DjList r)
124   {
125     DjList lst = new DjList();
126     lst.addAll(l);
127     for (int i = 0; i < r.size(); i++)
128     {
129       if (!lst.contains(r.get(i))) lst.add(r.get(i));
130     }
131     return lst;
132   }
133
134   DjList subtractLists(DjList l, DjList r)
135   {
136     DjList lst = new DjList();
137
138     for (int i = 0; i < l.size(); i++)
139     {
140       if (!r.contains(l.get(i))) lst.add(l.get(i));
141     }
142     return lst;
143   }
144
145 }
Popular Tags