KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > evaluator > CreateJumpTargetVisitor


1 /*******************************************************************************
2  * BEGIN LICENSE BLOCK *** Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public License Version
5  * 1.0 (the "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  * http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
11  * the specific language governing rights and limitations under the License.
12  *
13  * Copyright (C) 2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
14  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
15  * Copyright (C) 2006 Charles Oliver Nutter <headius@headius.com>
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either of the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in
20  * which case the provisions of the GPL or the LGPL are applicable instead of
21  * those above. If you wish to allow use of your version of this file only under
22  * the terms of either the GPL or the LGPL, and not to allow others to use your
23  * version of this file under the terms of the CPL, indicate your decision by
24  * deleting the provisions above and replace them with the notice and other
25  * provisions required by the GPL or the LGPL. If you do not delete the
26  * provisions above, a recipient may use your version of this file under the
27  * terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ****
28  ******************************************************************************/

29 package org.jruby.evaluator;
30
31 import java.util.Iterator JavaDoc;
32 import org.jruby.ast.ArgsCatNode;
33 import org.jruby.ast.BeginNode;
34 import org.jruby.ast.BinaryOperatorNode;
35 import org.jruby.ast.BlockPassNode;
36 import org.jruby.ast.BreakNode;
37 import org.jruby.ast.CallNode;
38 import org.jruby.ast.CaseNode;
39 import org.jruby.ast.ClassNode;
40 import org.jruby.ast.ClassVarAsgnNode;
41 import org.jruby.ast.ClassVarDeclNode;
42 import org.jruby.ast.Colon2Node;
43 import org.jruby.ast.ConstDeclNode;
44 import org.jruby.ast.DAsgnNode;
45 import org.jruby.ast.DefinedNode;
46 import org.jruby.ast.DotNode;
47 import org.jruby.ast.EnsureNode;
48 import org.jruby.ast.EvStrNode;
49 import org.jruby.ast.FCallNode;
50 import org.jruby.ast.FlipNode;
51 import org.jruby.ast.ForNode;
52 import org.jruby.ast.GlobalAsgnNode;
53 import org.jruby.ast.IfNode;
54 import org.jruby.ast.InstAsgnNode;
55 import org.jruby.ast.IterNode;
56 import org.jruby.ast.LocalAsgnNode;
57 import org.jruby.ast.Match2Node;
58 import org.jruby.ast.Match3Node;
59 import org.jruby.ast.MatchNode;
60 import org.jruby.ast.MultipleAsgnNode;
61 import org.jruby.ast.NewlineNode;
62 import org.jruby.ast.Node;
63 import org.jruby.ast.NodeTypes;
64 import org.jruby.ast.NotNode;
65 import org.jruby.ast.OpAsgnNode;
66 import org.jruby.ast.OpElementAsgnNode;
67 import org.jruby.ast.OptNNode;
68 import org.jruby.ast.RescueBodyNode;
69 import org.jruby.ast.RescueNode;
70 import org.jruby.ast.ReturnNode;
71 import org.jruby.ast.SClassNode;
72 import org.jruby.ast.SValueNode;
73 import org.jruby.ast.SplatNode;
74 import org.jruby.ast.ToAryNode;
75 import org.jruby.ast.UntilNode;
76 import org.jruby.ast.WhenNode;
77 import org.jruby.ast.WhileNode;
78
79 public class CreateJumpTargetVisitor {
80     public static void setJumpTarget(Object JavaDoc target, Node node) {
81         bigloop: do {
82             if (node == null) return;
83
84             switch (node.nodeId) {
85                 case NodeTypes.ANDNODE:
86                 case NodeTypes.OPASGNANDNODE:
87                 case NodeTypes.OPASGNORNODE:
88                 case NodeTypes.ORNODE:
89                     setJumpTarget(target, ((BinaryOperatorNode)node).getFirstNode());
90                     node = ((BinaryOperatorNode)node).getSecondNode();
91                     continue bigloop;
92                 case NodeTypes.ARGSCATNODE:
93                     setJumpTarget(target, ((ArgsCatNode)node).getFirstNode());
94                     node = ((ArgsCatNode)node).getSecondNode();
95                     continue bigloop;
96                 case NodeTypes.ARRAYNODE:
97                 case NodeTypes.BLOCKNODE:
98                 case NodeTypes.DREGEXPNODE:
99                 case NodeTypes.DSTRNODE:
100                 case NodeTypes.DSYMBOLNODE:
101                 case NodeTypes.DXSTRNODE:
102                 case NodeTypes.HASHNODE:
103                     for (Iterator JavaDoc iter = node.childNodes().iterator(); iter.hasNext();) {
104                         setJumpTarget(target, (Node)iter.next());
105                     }
106                     return;
107                 case NodeTypes.BEGINNODE:
108                     node = ((BeginNode)node).getBodyNode();
109                     continue bigloop;
110                 case NodeTypes.BLOCKPASSNODE:
111                     setJumpTarget(target, ((BlockPassNode)node).getArgsNode());
112                     setJumpTarget(target, ((BlockPassNode)node).getBodyNode());
113                     return;
114                 case NodeTypes.BREAKNODE:
115                     node = ((BreakNode)node).getValueNode();
116                     continue bigloop;
117                 case NodeTypes.CONSTDECLNODE:
118                     node = ((ConstDeclNode)node).getValueNode();
119                     continue bigloop;
120                 case NodeTypes.CLASSNODE:
121                     setJumpTarget(target, ((ClassNode)node).getSuperNode());
122                     node = ((ClassNode)node).getBodyNode();
123                     continue bigloop;
124                 case NodeTypes.CLASSVARASGNNODE:
125                     node = ((ClassVarAsgnNode)node).getValueNode();
126                     continue bigloop;
127                 case NodeTypes.CLASSVARDECLNODE:
128                     node = ((ClassVarDeclNode)node).getValueNode();
129                     continue bigloop;
130                 case NodeTypes.CALLNODE:
131                     setJumpTarget(target, ((CallNode)node).getIterNode());
132                     node = ((CallNode)node).getReceiverNode();
133                     continue bigloop;
134                 case NodeTypes.CASENODE:
135                     setJumpTarget(target, ((CaseNode)node).getCaseNode());
136                     node = ((CaseNode)node).getFirstWhenNode();
137                     continue bigloop;
138                 case NodeTypes.COLON2NODE:
139                     node = ((Colon2Node)node).getLeftNode();
140                     continue bigloop;
141                 case NodeTypes.DASGNNODE:
142                     node = ((DAsgnNode)node).getValueNode();
143                     continue bigloop;
144                 case NodeTypes.DEFINEDNODE:
145                     node = ((DefinedNode)node).getExpressionNode();
146                     continue bigloop;
147                 case NodeTypes.DOTNODE:
148                     setJumpTarget(target, ((DotNode)node).getBeginNode());
149                     node = ((DotNode)node).getEndNode();
150                     continue bigloop;
151                 case NodeTypes.ENSURENODE:
152                     setJumpTarget(target, ((EnsureNode)node).getBodyNode());
153                     node = ((EnsureNode)node).getEnsureNode();
154                     continue bigloop;
155                 case NodeTypes.EVSTRNODE:
156                     node = ((EvStrNode)node).getBody();
157                     continue bigloop;
158                 case NodeTypes.FCALLNODE:
159                     setJumpTarget(target, ((FCallNode)node).getIterNode());
160                     return;
161                 case NodeTypes.FLIPNODE:
162                     setJumpTarget(target, ((FlipNode)node).getBeginNode());
163                     node = ((FlipNode)node).getEndNode();
164                     continue bigloop;
165                 case NodeTypes.FORNODE:
166                     setJumpTarget(target, ((ForNode)node).getBodyNode());
167                     setJumpTarget(target, ((ForNode)node).getIterNode());
168                     node = ((ForNode)node).getVarNode();
169                     continue bigloop;
170                 case NodeTypes.GLOBALASGNNODE:
171                     node = ((GlobalAsgnNode)node).getValueNode();
172                     continue bigloop;
173                 case NodeTypes.INSTASGNNODE:
174                     node = ((InstAsgnNode)node).getValueNode();
175                     continue bigloop;
176                 case NodeTypes.IFNODE:
177                     setJumpTarget(target, ((IfNode)node).getCondition());
178                     setJumpTarget(target, ((IfNode)node).getThenBody());
179                     node = ((IfNode)node).getElseBody();
180                     continue bigloop;
181                 case NodeTypes.ITERNODE:
182                     setJumpTarget(target, ((IterNode)node).getBodyNode());
183                     node = ((IterNode)node).getVarNode();
184                     continue bigloop;
185                 case NodeTypes.LOCALASGNNODE:
186                     node = ((LocalAsgnNode)node).getValueNode();
187                     continue bigloop;
188                 case NodeTypes.MULTIPLEASGNNODE:
189                     node = ((MultipleAsgnNode)node).getValueNode();
190                     continue bigloop;
191                 case NodeTypes.MATCH2NODE:
192                     setJumpTarget(target, ((Match2Node)node).getReceiverNode());
193                     node = ((Match2Node)node).getValueNode();
194                     continue bigloop;
195                 case NodeTypes.MATCH3NODE:
196                     setJumpTarget(target, ((Match3Node)node).getReceiverNode());
197                     node = ((Match3Node)node).getValueNode();
198                     continue bigloop;
199                 case NodeTypes.MATCHNODE:
200                     node = ((MatchNode)node).getRegexpNode();
201                     continue bigloop;
202                 case NodeTypes.NEWLINENODE:
203                     node = ((NewlineNode)node).getNextNode();
204                     continue bigloop;
205                 case NodeTypes.NOTNODE:
206                     node = ((NotNode)node).getConditionNode();
207                     continue bigloop;
208                 case NodeTypes.OPELEMENTASGNNODE:
209                     setJumpTarget(target, ((OpElementAsgnNode)node).getReceiverNode());
210                     node = ((OpElementAsgnNode)node).getValueNode();
211                     continue bigloop;
212                 case NodeTypes.OPASGNNODE:
213                     setJumpTarget(target, ((OpAsgnNode)node).getReceiverNode());
214                     node = ((OpAsgnNode)node).getValueNode();
215                     continue bigloop;
216                 case NodeTypes.OPTNNODE:
217                     node = ((OptNNode)node).getBodyNode();
218                     continue bigloop;
219                 case NodeTypes.RESCUEBODYNODE:
220                     setJumpTarget(target, ((RescueBodyNode)node).getBodyNode());
221                     setJumpTarget(target, ((RescueBodyNode)node).getExceptionNodes());
222                     node = ((RescueBodyNode)node).getOptRescueNode();
223                     continue bigloop;
224                 case NodeTypes.RESCUENODE:
225                     setJumpTarget(target, ((RescueNode)node).getBodyNode());
226                     setJumpTarget(target, ((RescueNode)node).getElseNode());
227                     node = ((RescueNode)node).getRescueNode();
228                     continue bigloop;
229                 case NodeTypes.RETURNNODE:
230                     ((ReturnNode)node).setTarget(target);
231                     return;
232                 case NodeTypes.SCLASSNODE:
233                     setJumpTarget(target, ((SClassNode)node).getReceiverNode());
234                     node = ((SClassNode)node).getBodyNode();
235                     continue bigloop;
236                 case NodeTypes.SPLATNODE:
237                     node = ((SplatNode)node).getValue();
238                     continue bigloop;
239                 case NodeTypes.SVALUENODE:
240                     node = ((SValueNode)node).getValue();
241                     continue bigloop;
242                 case NodeTypes.TOARYNODE:
243                     node = ((ToAryNode)node).getValue();
244                     continue bigloop;
245                 case NodeTypes.UNTILNODE:
246                     setJumpTarget(target, ((UntilNode)node).getConditionNode());
247                     node = ((UntilNode)node).getBodyNode();
248                     continue bigloop;
249                 case NodeTypes.WHENNODE:
250                     setJumpTarget(target, ((WhenNode)node).getBodyNode());
251                     setJumpTarget(target, ((WhenNode)node).getExpressionNodes());
252                     node = ((WhenNode)node).getNextCase();
253                     continue bigloop;
254                 case NodeTypes.WHILENODE:
255                     setJumpTarget(target, ((WhileNode)node).getConditionNode());
256                     node = ((WhileNode)node).getBodyNode();
257                     continue bigloop;
258                 default:
259                     return;
260             }
261         } while (node != null);
262     }
263 }
Popular Tags