KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* @author rich
2  * Created on 16-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.lsmp.djep.xjep;
9 import org.nfunk.jep.*;
10 import org.nfunk.jep.function.*;
11 import java.util.Stack JavaDoc;
12 /**
13  * This class is used to create nodes of specified types.
14  * It can be subclassed to change the nature of how nodes
15  * are constructed. Generally there are two methods for creating
16  * nodes, methods which take an existing node and methods which
17  * take the components.
18  *
19  * @author Rich Morris
20  * Created on 16-Nov-2003
21  */

22 public class NodeFactory {
23
24     public NodeFactory() {}
25     
26     /**
27      * Sets the children of node to be those specified in array.
28      * @param node the node whos children will be set.
29      * @param children an array of nodes which will be the children of the node.
30      */

31     public void copyChildren(Node node,Node children[])
32     {
33         int nchild = children.length;
34         node.jjtOpen();
35         for(int i=0;i<nchild;++i)
36         {
37             children[i].jjtSetParent(node);
38             node.jjtAddChild(children[i],i);
39         }
40         node.jjtClose();
41     }
42     
43     /** Creates an ASTConstant node with specified value.
44      * This method should be overwritten by subclasses.
45      **/

46     public ASTConstant buildConstantNode(Object JavaDoc value) throws ParseException
47     {
48         ASTConstant node = new ASTConstant(ParserTreeConstants.JJTCONSTANT);
49         node.setValue(value);
50         return node;
51     }
52
53     /** Create an ASTConstant with same value as argument. **/
54     public ASTConstant buildConstantNode(ASTConstant node) throws ParseException
55     {
56         return buildConstantNode(node.getValue());
57     }
58
59
60     /** Creates a ASTConstant whose value of applying the operator to its arguments. */
61     public ASTConstant buildConstantNode(PostfixMathCommandI pfmc,Node children[]) throws IllegalArgumentException JavaDoc,ParseException
62     {
63         Stack JavaDoc stack = new Stack JavaDoc();
64         for(int i=0;i<children.length;++i)
65         {
66             if(!(children[i] instanceof ASTConstant))
67                 throw new ParseException("buildConstantNode: arguments must all be constant nodes");
68             stack.push(((ASTConstant) children[i]).getValue());
69         }
70         pfmc.setCurNumberOfParameters(children.length);
71         pfmc.run(stack);
72         return buildConstantNode(stack.pop());
73     }
74
75     /** Creates a ASTConstant whose value of applying the operator to its arguments. */
76     public ASTConstant buildConstantNode(Operator op,Node children[]) throws IllegalArgumentException JavaDoc,ParseException
77     {
78         return buildConstantNode(op.getPFMC(),children);
79     }
80
81     /** Creates a ASTConstant whose value of applying binary operator to its arguments. */
82     public ASTConstant buildConstantNode(Operator op,Node child1,Node child2) throws IllegalArgumentException JavaDoc,ParseException
83     {
84         return buildConstantNode(op.getPFMC(),new Node[]{child1,child2});
85     }
86
87     /** Creates a ASTConstant whose value of applying a unary operator to its arguments. */
88     public ASTConstant buildConstantNode(Operator op,Node child1) throws IllegalArgumentException JavaDoc,ParseException
89     {
90         return buildConstantNode(op.getPFMC(),new Node[]{child1});
91     }
92
93     /** creates a new ASTVarNode with the same name as argument. */
94     public ASTVarNode buildVariableNode(ASTVarNode node) throws ParseException
95     {
96            return buildVariableNode(node.getVar());
97     }
98     
99     /** creates a new ASTVarNode with a given variable.
100      * This method should be subclassed
101      */

102     public ASTVarNode buildVariableNode(Variable var) throws ParseException
103     {
104         ASTVarNode node = new ASTVarNode(ParserTreeConstants.JJTVARNODE);
105         node.setVar(var);
106         return node;
107     }
108
109     /**
110      * Builds a operator node with n arguments
111      * This method should be subclassed
112      * @param name of function.
113      * @param pfmc PostfixMathCommand for function.
114      * @param arguments the arguments to the function.
115      * @return top Node of expression
116      */

117     
118     public ASTFunNode buildOperatorNode(Operator op,Node[] arguments) throws ParseException
119     {
120         ASTFunNode res = new ASTFunNode(ParserTreeConstants.JJTFUNNODE);
121         res.setOperator(op);
122         copyChildren(res,arguments);
123         return res;
124     }
125     
126     /**
127      * Builds a function with n arguments
128      * This method should be subclassed
129      * @param name of function.
130      * @param pfmc PostfixMathCommand for function.
131      * @param arguments the arguments to the function.
132      * @return top Node of expression
133      */

134
135     public ASTFunNode buildFunctionNode(String JavaDoc name,PostfixMathCommandI pfmc,Node[] arguments) throws ParseException
136     {
137         ASTFunNode res = new ASTFunNode(ParserTreeConstants.JJTFUNNODE);
138         res.setFunction(name,pfmc);
139         copyChildren(res,arguments);
140         return res;
141     }
142
143
144     /** An unfinished node. Caller has responsability for
145      * filling in the children. */

146     public ASTFunNode buildUnfinishedOperatorNode(Operator op) throws ParseException
147     {
148         ASTFunNode res = new ASTFunNode(ParserTreeConstants.JJTFUNNODE);
149         res.setOperator(op);
150         return res;
151     }
152     
153     /** creates a unary function. */
154     
155     public ASTFunNode buildOperatorNode(Operator op,Node child) throws ParseException
156     {
157         return buildOperatorNode(op,new Node[]{child});
158     }
159     
160     /** creates a binary function. */
161     
162     public ASTFunNode buildOperatorNode(Operator op,Node lhs,Node rhs) throws ParseException
163     {
164         return buildOperatorNode(op,new Node[]{lhs,rhs});
165     }
166     
167     /**
168      * Builds a function with n arguments and same fun as specified in arguments.
169      * @param node the properties (name and pfmc) of this node will be copied.
170      * @param arguments the arguments to the function.
171      * @return top Node of expression
172      */

173     public ASTFunNode buildFunctionNode(ASTFunNode node,Node[] arguments) throws ParseException
174     {
175         if(node.getOperator()!=null)
176             return buildOperatorNode(node.getOperator(),arguments);
177         return buildFunctionNode(node.getName(),node.getPFMC(),arguments);
178     }
179
180 }
181
Popular Tags