1 23 24 package org.objectweb.medor.expression.lib; 25 26 import org.objectweb.jorm.type.api.PType; 27 import org.objectweb.medor.expression.api.ExpressionException; 28 import org.objectweb.medor.expression.api.MalformedExpressionException; 29 import org.objectweb.medor.expression.api.Operand; 30 import org.objectweb.medor.expression.api.ParameterOperand; 31 import org.objectweb.medor.expression.api.TypingException; 32 import org.objectweb.medor.expression.api.UnaryArithmeticOperator; 33 import org.objectweb.medor.expression.api.Expression; 34 import org.objectweb.medor.expression.type.ExpressionTypeHelper; 35 36 40 public abstract class BasicUnaryArithmeticOperator 41 extends BasicUnaryOperator 42 implements UnaryArithmeticOperator { 43 44 public BasicUnaryArithmeticOperator() { 45 } 46 47 public BasicUnaryArithmeticOperator(PType type) { 48 super(type); 49 } 50 51 public BasicUnaryArithmeticOperator(Expression e) { 52 super(e); 53 } 54 55 public BasicUnaryArithmeticOperator(PType type, Expression e) { 56 super(type, e); 57 } 58 59 62 public org.objectweb.medor.expression.api.Operand evaluate(ParameterOperand[] pos, Object o) 63 throws ExpressionException { 64 try { 65 Operand subResult = expressions[0].evaluate(pos, o); 66 switch (subResult.getType().getTypeCode()) { 67 case PType.TYPECODE_DOUBLE: 68 result.setValue(evaluate(subResult.getDouble())); 69 break; 70 case PType.TYPECODE_FLOAT: 71 result.setValue(evaluate(subResult.getFloat())); 72 break; 73 case PType.TYPECODE_BYTE: 74 result.setValue(evaluate(subResult.getByte())); 75 break; 76 case PType.TYPECODE_SHORT: 77 result.setValue(evaluate(subResult.getShort())); 78 break; 79 case PType.TYPECODE_INT: 80 result.setValue(evaluate(subResult.getInt())); 81 break; 82 case PType.TYPECODE_LONG: 83 result.setValue(evaluate(subResult.getLong())); 84 break; 85 default: 86 throw new MalformedExpressionException( 87 "Unauthorized expression element"); 88 } 89 } catch (NullPointerException e) { 90 throw new IllegalStateException ("Unevaluable Expression: Not compiled"); 91 } 92 return result; 93 } 94 95 public Operand compileExpression() 96 throws ExpressionException, MalformedExpressionException { 97 if (expressions[0] != null) { 98 expressions[0].compileExpression(); 99 if (ExpressionTypeHelper.isArithmeticType(expressions[0].getType())) { 100 type = expressions[0].getType(); 101 result = new BasicVariableOperand(type); 102 verified = true; 103 } else 104 throw new TypingException("Attempt an aritmetic type"); 105 } else 106 throw new MalformedExpressionException("null childeren value"); return result; 108 } 109 } 110 | Popular Tags |