1 8 package org.lsmp.djep.xjep; 9 import org.nfunk.jep.*; 10 11 18 public class XOperatorSet extends OperatorSet { 19 20 private void annotateOperators(OperatorSet o) { 21 OP_GT = new XOperator(o.getGT(),XOperator.BINARY+XOperator.LEFT+XOperator.TRANSITIVE); 22 OP_LT = new XOperator(o.getLT(),XOperator.BINARY+XOperator.LEFT+XOperator.TRANSITIVE); 23 OP_EQ = new XOperator(o.getEQ(),XOperator.BINARY+XOperator.LEFT+XOperator.EQUIVILENCE); 24 OP_LE = new XOperator(o.getLE(),XOperator.BINARY+XOperator.LEFT+XOperator.REFLEXIVE+XOperator.TRANSITIVE); 25 OP_GE = new XOperator(o.getGE(),XOperator.BINARY+XOperator.LEFT+XOperator.REFLEXIVE+XOperator.TRANSITIVE); 26 OP_NE = new XOperator(o.getNE(),XOperator.BINARY+XOperator.LEFT+XOperator.SYMMETRIC); 27 28 OP_AND = new XOperator(o.getAnd(),XOperator.BINARY+XOperator.LEFT+XOperator.COMMUTATIVE+XOperator.ASSOCIATIVE+XOperator.USE_BINDING_FOR_PRINT); 29 OP_OR = new XOperator(o.getOr(),XOperator.BINARY+XOperator.LEFT+XOperator.COMMUTATIVE+XOperator.ASSOCIATIVE); 30 OP_NOT = new XOperator(o.getNot(),XOperator.UNARY+XOperator.RIGHT+XOperator.PREFIX+XOperator.SELF_INVERSE); 31 32 OP_ADD = new XOperator(o.getAdd(),XOperator.BINARY+XOperator.LEFT+XOperator.COMMUTATIVE+XOperator.ASSOCIATIVE); 33 OP_SUBTRACT = new XOperator(o.getSubtract(),XOperator.BINARY+XOperator.LEFT+XOperator.COMPOSITE+XOperator.USE_BINDING_FOR_PRINT); 34 OP_UMINUS = new XOperator(o.getUMinus(),XOperator.UNARY+XOperator.RIGHT+XOperator.PREFIX+XOperator.SELF_INVERSE); 35 36 OP_MULTIPLY = new XOperator(o.getMultiply(),XOperator.BINARY+XOperator.LEFT+XOperator.COMMUTATIVE+XOperator.ASSOCIATIVE); 37 OP_DIVIDE = new XOperator(o.getDivide(),XOperator.BINARY+XOperator.LEFT+XOperator.COMPOSITE); 38 OP_MOD = new XOperator(o.getMod(),XOperator.BINARY+XOperator.LEFT); 39 40 OP_UDIVIDE = new XOperator("UDivide","^-1",null,XOperator.UNARY+XOperator.RIGHT+XOperator.PREFIX+XOperator.SELF_INVERSE); 41 42 OP_POWER = new XOperator(o.getPower(),XOperator.BINARY+XOperator.LEFT); 43 44 OP_ASSIGN = new XOperator("=",new XAssign(),XOperator.BINARY+XOperator.RIGHT); 46 OP_DOT = new XOperator(o.getDot(),XOperator.BINARY+XOperator.RIGHT); OP_CROSS = new XOperator(o.getCross(),XOperator.BINARY+XOperator.RIGHT); OP_LIST = new XOperator(o.getList(),XOperator.NARY+XOperator.RIGHT); 50 setPrecedenceTable(new Operator[][] 51 { {OP_UMINUS}, 52 {OP_NOT}, 53 {OP_POWER}, 54 {OP_MULTIPLY,OP_DIVIDE,OP_MOD,OP_DOT,OP_CROSS}, 55 {OP_ADD,OP_SUBTRACT}, 56 {OP_LT,OP_LE}, 57 {OP_GT,OP_GE}, 58 {OP_EQ}, 59 {OP_NE}, 60 {OP_AND}, 61 {OP_OR}, 62 {OP_ASSIGN}, 63 }); 64 66 ((XOperator) OP_ADD).setInverseOp(OP_UMINUS); 68 ((XOperator) OP_ADD).setBinaryInverseOp(OP_SUBTRACT); 69 ((XOperator) OP_SUBTRACT).setRootOp(OP_ADD); 70 ((XOperator) OP_SUBTRACT).setInverseOp(OP_UMINUS); 71 ((XOperator) OP_UMINUS).setRootOp(OP_ADD); 72 ((XOperator) OP_UMINUS).setBinaryInverseOp(OP_SUBTRACT); 73 74 ((XOperator) OP_MULTIPLY).setInverseOp(OP_UDIVIDE); 75 ((XOperator) OP_MULTIPLY).setBinaryInverseOp(OP_DIVIDE); 76 ((XOperator) OP_DIVIDE).setRootOp(OP_MULTIPLY); 77 ((XOperator) OP_DIVIDE).setInverseOp(OP_UDIVIDE); 78 ((XOperator) OP_UDIVIDE).setRootOp(OP_MULTIPLY); 79 ((XOperator) OP_UDIVIDE).setBinaryInverseOp(OP_DIVIDE); 80 81 ((XOperator) OP_UMINUS).setDistributiveOver(OP_ADD); ((XOperator) OP_UMINUS).setDistributiveOver(OP_SUBTRACT); 85 ((XOperator) OP_MULTIPLY).setDistributiveOver(OP_ADD); ((XOperator) OP_MULTIPLY).setDistributiveOver(OP_SUBTRACT); ((XOperator) OP_MULTIPLY).setDistributiveOver(OP_UMINUS); } 89 90 93 public XOperatorSet(OperatorSet opSet) 94 { 95 annotateOperators(opSet); 96 } 97 98 99 public XOperatorSet() 100 { 101 annotateOperators(this); 102 } 103 104 122 123 public static final void setPrecedenceTable(Operator[][] precArray) 124 { 125 for(int i=0;i<precArray.length;++i) 126 for(int j=0;j<precArray[i].length;++j) 127 ((XOperator) precArray[i][j]).setPrecedence(i); 128 } 129 130 133 public void printOperators() 134 { 135 Operator ops[] = getOperators(); 136 int maxPrec = -1; 137 for(int i=0;i<ops.length;++i) 138 if(((XOperator) ops[i]).getPrecedence()>maxPrec) maxPrec=((XOperator) ops[i]).getPrecedence(); 139 for(int j=-1;j<=maxPrec;++j) 140 for(int i=0;i<ops.length;++i) 141 if(((XOperator) ops[i]).getPrecedence()==j) 142 System.out.println(((XOperator) ops[i]).toFullString()); 143 } 144 145 } 146 | Popular Tags |