1 8 package org.nfunk.jep.function; 9 10 import org.nfunk.jep.*; 11 import org.nfunk.jep.type.*; 12 import java.util.Stack ; 13 32 public class If extends PostfixMathCommand implements SpecialEvaluationI { 33 34 37 public If() { 38 super(); 39 numberOfParameters = -1; 40 } 41 42 51 56 57 public Object evaluate(Node node,Object data,ParserVisitor pv,Stack inStack) throws ParseException 58 { 59 int num = node.jjtGetNumChildren(); 60 if( num < 3 || num > 4) 61 throw new ParseException("If operator must have 3 or 4 arguments."); 62 63 65 node.jjtGetChild(0).jjtAccept(pv,data); 66 checkStack(inStack); Object condVal = inStack.pop(); 68 69 double val; 71 if(condVal instanceof Double ) 72 { 73 val = ((Double ) condVal).doubleValue(); 74 } 75 else if(condVal instanceof Complex) 76 { 77 val = ((Complex) condVal).re(); 78 } 79 else 80 { 81 throw new ParseException("Condition in if operator must be double or complex"); 82 } 83 84 if(val>0.0) 85 { 86 node.jjtGetChild(1).jjtAccept(pv,data); 87 } 88 else if(num ==3 || val <0.0) 89 { 90 node.jjtGetChild(2).jjtAccept(pv,data); 91 } 92 else 93 { 94 node.jjtGetChild(3).jjtAccept(pv,data); 95 } 96 97 return data; 98 } 99 100 } 101 | Popular Tags |