KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > xjep > MacroFunction


1 /* @author rich
2  * Created on 18-Jun-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.lsmp.djep.xjep;
9
10 import org.nfunk.jep.function.PostfixMathCommand;
11 import org.nfunk.jep.*;
12 import java.util.*;
13 /**
14  * A function specified by a string.
15  * For example
16  * <pre>
17  * XJepI jep = new XJep();
18  * j.addFunction("zap",new MacroFunction("zap",1,"x*(x-1)/2",j));
19  * Node node = j.parse("zap(10)");
20  * System.out.println(j.evaluate(node)); // print 45
21  * </pre>
22  * The names of the variables used inside the fuction depends on the number of arguments:
23  * <ul>
24  * <li>One argument variable must be x: <tt>new MacroFunction("sec",1,"1/cos(x)",j)</tt></li>
25  * <li>Two arguments variables must be x or y: <tt>new MacroFunction("myPower",2,"x^y",j)</tt></li>
26  * <li>Three or more arguments variables must be x1, x2, x3,...: <tt>new MacroFunction("add3",3,"x1+x2+x3",j)</tt></li>
27  * </ul>
28  * @author R Morris.
29  * Created on 18-Jun-2003
30  */

31 public class MacroFunction extends PostfixMathCommand
32 {
33     private String JavaDoc name;
34     private Node topNode;
35     private EvaluatorVisitor ev = new EvaluatorVisitor();
36 // private XJep localJep;
37
private XSymbolTable mySymTab;
38     private Variable vars[];
39     
40     public String JavaDoc getName() { return name; }
41     public Node getTopNode() { return topNode; }
42     
43     /**
44      * Create a function specified by a string.
45      * For example <tt>new MacroFunction("sec",1,"1/cos(x)",tu)</tt> creates the function for sec.
46      * Variable names must be x,y for 1 or 2 variables or x1,x2,x3,.. for 3 or more variables.
47      * @param inName name of function
48      * @param nargs number of arguments
49      * @param expression a string representing the expression.
50      * @param inTu a reference to the TreeUtils object.
51      */

52     public MacroFunction(String JavaDoc inName,int nargs,String JavaDoc expression,XJep jep) throws IllegalArgumentException JavaDoc,ParseException
53     {
54         super();
55         name = inName;
56
57         XSymbolTable jepSymTab = (XSymbolTable) jep.getSymbolTable();
58         mySymTab = (XSymbolTable) jepSymTab.newInstance();
59         mySymTab.copyConstants(jepSymTab);
60         XJep localJep = jep.newInstance(mySymTab);
61         numberOfParameters = nargs;
62
63         if(numberOfParameters == 0) {}
64         else if(numberOfParameters == 1)
65             vars = new Variable[]{mySymTab.addVariable("x",null)};
66         else if(numberOfParameters == 2)
67         {
68             vars = new Variable[]{
69                     mySymTab.addVariable("x",null),
70                     mySymTab.addVariable("y",null)};
71         }
72         else
73         {
74             vars = new Variable[numberOfParameters];
75             for(int i=numberOfParameters-1;i>0;)
76                 vars[i] = mySymTab.addVariable("x"+String.valueOf(i),null);
77         }
78
79         topNode = localJep.parse(expression);
80     }
81     
82     /**
83      * Calculates the value of the expression.
84      * @throws ParseException if run.
85      */

86     public void run(Stack stack) throws ParseException
87     {
88
89         if(numberOfParameters == 0) {}
90         else if(numberOfParameters == 1)
91             vars[0].setValue(stack.pop());
92         else if(numberOfParameters == 2)
93         {
94             vars[1].setValue(stack.pop());
95             vars[0].setValue(stack.pop());
96         }
97         else
98         {
99             for(int i=numberOfParameters-1;i>0;)
100                 vars[i].setValue(stack.pop());
101         }
102         try
103         {
104             Object JavaDoc res = ev.getValue(topNode,null,mySymTab);
105             stack.push(res);
106         }
107         catch(Exception JavaDoc e1) { throw new ParseException("MacroFunction eval: "+e1.getMessage()); }
108     }
109 }
110
Popular Tags