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 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 getName() 24 { 25 return toString(); 26 } 27 28 public String toString() 29 { 30 return getOperator() + " (op)"; 31 } 32 33 public void setOperator(String operator) 34 { 35 this.operator = operator; 36 } 37 38 public String getOperator() 39 { 40 return operator; 41 } 42 43 public Object getValue(ParseContext context) throws ParseException 44 { 45 ValueExpression left = (ValueExpression) getChild(0); 46 ValueExpression right = (ValueExpression) getChild(1); 47 48 if (getOperator().equals(".")) 50 { 51 Object 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 x) 62 { 63 throw new ParseException(x, beginLine, beginColumn); 64 } 65 } 66 67 Object leftVal = left.getValue(context); 68 Object rightVal = right.getValue(context); 69 70 if (leftVal == null || rightVal == null) 72 { 73 return null; 74 } 75 76 if ((leftVal instanceof String ) || (rightVal instanceof String )) 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 if ((leftVal instanceof Float ) || (rightVal instanceof Float )) 85 { 86 float l = toFloat(leftVal).floatValue(); 87 float r = toFloat(rightVal).floatValue(); 88 89 if (getOperator().equals("+")) return new Float (l + r); 90 if (getOperator().equals("-")) return new Float (l - r); 91 if (getOperator().equals("*")) return new Float (l * r); 92 if (getOperator().equals("/")) return new Float (l / r); 93 throw new ParseException("Can not apply " + getOperator() + " to floats", beginLine, beginColumn); 94 } 95 if ((leftVal instanceof Integer ) || (rightVal instanceof Integer )) 97 { 98 int l = toInteger(leftVal).intValue(); 99 int r = toInteger(rightVal).intValue(); 100 101 if (getOperator().equals("+")) return new Integer (l + r); 102 if (getOperator().equals("-")) return new Integer (l - r); 103 if (getOperator().equals("*")) return new Integer (l * r); 104 if (getOperator().equals("/")) return new Integer (l / r); 105 if (getOperator().equals("%")) return new Integer (l % r); 106 throw new ParseException("Can not apply " + getOperator() + " to integers", beginLine, beginColumn); 107 } 108 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 |