KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jep > function > If


1 /* @author rich
2  * Created on 18-Nov-2003
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.nfunk.jep.function;
9
10 import org.nfunk.jep.*;
11 import org.nfunk.jep.type.*;
12 import java.util.Stack JavaDoc;
13 /**
14  * The if(condExpr,posExpr,negExpr) function.
15  * The value of trueExpr will be returned if condExpr is >0 (true)
16  * and value of negExpr will be returned if condExpr is &lt;= 0 (false).
17  * <p>
18  * This function performs lazy evaluation so that
19  * only posExpr or negExpr will be evaluated.
20  * For Complex numbers only the real part is used.
21  * <p>
22  * An alternate form if(condExpr,posExpr,negExpr,zeroExpr)
23  * is also availiable. Note most computations
24  * are carried out over floating point doubles so
25  * testing for zero can be dangerous.
26  * <p>
27  * This function implements the SpecialEvaluationI interface
28  * so that it handles setting the value of a variable.
29  * @author Rich Morris
30  * Created on 18-Nov-2003
31  */

32 public class If extends PostfixMathCommand implements SpecialEvaluationI {
33
34     /**
35      *
36      */

37     public If() {
38         super();
39         numberOfParameters = -1;
40     }
41
42     /**
43      * Performs the specified action on an expression tree.
44      * Serves no function in standard JEP but
45      * @param node top node of the tree
46      * @param data The data passed to visitor, typically not used.
47      * @param pv The visitor, can be used decend on the children.
48      * @return top node of the results.
49      * @throws ParseException
50      */

51 // public Node process(Node node,Object data,ParserVisitor pv) throws ParseException
52
// {
53
// return null;
54
// }
55

56     /** For assignment **/
57     public Object JavaDoc evaluate(Node node,Object JavaDoc data,ParserVisitor pv,Stack JavaDoc inStack/*,SymbolTable symTab*/) 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         // get value of argument
64

65         node.jjtGetChild(0).jjtAccept(pv,data);
66         checkStack(inStack); // check the stack
67
Object JavaDoc condVal = inStack.pop();
68         
69         // convert to double
70
double val;
71         if(condVal instanceof Double JavaDoc)
72         {
73             val = ((Double JavaDoc) 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