1 8 package org.lsmp.djep.xjep.function; 9 10 import java.util.Stack ; 11 12 import org.nfunk.jep.*; 13 import org.nfunk.jep.function.*; 14 15 21 public abstract class SumType extends PostfixMathCommand implements SpecialEvaluationI { 22 23 protected String name; 24 25 public SumType(String funName) 26 { 27 numberOfParameters = -1; 28 name = funName; 29 } 30 31 public SumType() 32 { 33 numberOfParameters = -1; 34 } 35 36 41 public Object evaluate( 42 Node node, 43 Object data, 44 ParserVisitor pv, 45 Stack stack) 46 throws ParseException { 47 48 int numParams = node.jjtGetNumChildren(); 49 if(numParams < 4 || numParams > 5) 50 throw new ParseException(name+": called with invalid number of parameters: "+numParams+" it should be either 4 or 5."); 51 52 Node varNode = node.jjtGetChild(1); 53 Variable var=null; 54 if(varNode instanceof ASTVarNode) 55 var = ((ASTVarNode) varNode).getVar(); 56 else 57 throw new ParseException(name+": second argument should be a variable"); 58 59 node.jjtGetChild(2).jjtAccept(pv,data); 60 checkStack(stack); Object minObj = stack.pop(); 62 double min; 63 if(minObj instanceof Number ) 64 min = ((Number ) minObj).doubleValue(); 65 else throw new ParseException(name+": third argument (min) should evaluate to a number it is "+minObj.toString()); 66 67 node.jjtGetChild(3).jjtAccept(pv,data); 68 checkStack(stack); Object maxObj = stack.pop(); 70 double max; 71 if(maxObj instanceof Number ) 72 max = ((Number ) maxObj).doubleValue(); 73 else throw new ParseException(name+": forth argument (max) should evaluate to a number it is "+minObj.toString()); 74 75 if(min>max) throw new ParseException(name+": min value should be smaller than max value they are "+min+" and "+max+"."); 76 77 if(numParams == 5) 78 { 79 node.jjtGetChild(3).jjtAccept(pv,data); 80 checkStack(stack); Object incObj = stack.pop(); 82 double inc; 83 if(incObj instanceof Number ) 84 inc = ((Number ) incObj).doubleValue(); 85 else throw new ParseException(name+": fifth argument (steps) should evaluate to a number it is "+minObj.toString()); 86 87 evaluate(node.jjtGetChild(0),var,min,max,inc,data,pv,stack); 88 } 89 else 90 evaluate(node,var,min,max,1.0,data,pv,stack); 91 return null; 92 } 93 94 110 111 public Object evaluate( 112 Node node, 113 Variable var, 114 double min, double max, double inc, 115 Object data, 116 ParserVisitor pv, 117 Stack stack) 118 throws ParseException { 119 120 int i=0; 121 double val; 122 Object [] res=new Object [(int) ((max-min)/inc)+1]; 123 for(i=0,val=min;val<=max;++i,val=min+i*inc) 124 { 125 var.setValue(new Double (val)); 126 127 node.jjtGetChild(0).jjtAccept(pv,data); 128 checkStack(stack); res[i] = stack.pop(); 130 } 131 Object ret = evaluate(res); 132 stack.push(ret); 133 return ret; 134 } 135 136 143 public abstract Object evaluate(Object elements[]) throws ParseException; 144 145 148 public void run(Stack s) throws ParseException { 149 throw new ParseException(name+": run method called should not normally happen."); 150 } 151 152 } 153 | Popular Tags |