PageRenderTime 126ms CodeModel.GetById 40ms app.highlight 45ms RepoModel.GetById 35ms app.codeStats 1ms

/src/org/ooc/middle/walkers/SketchyNosy.java

http://github.com/nddrylliog/ooc
Java | 419 lines | 338 code | 72 blank | 9 comment | 188 complexity | 5a0861eb4d9cb4de315adf5cf35c3759 MD5 | raw file
  1package org.ooc.middle.walkers;
  2
  3import java.io.IOException;
  4
  5import org.ooc.frontend.Visitor;
  6import org.ooc.frontend.model.Add;
  7import org.ooc.frontend.model.AddressOf;
  8import org.ooc.frontend.model.ArrayAccess;
  9import org.ooc.frontend.model.ArrayLiteral;
 10import org.ooc.frontend.model.Assignment;
 11import org.ooc.frontend.model.BinaryCombination;
 12import org.ooc.frontend.model.BinaryNegation;
 13import org.ooc.frontend.model.Block;
 14import org.ooc.frontend.model.BoolLiteral;
 15import org.ooc.frontend.model.BuiltinType;
 16import org.ooc.frontend.model.Case;
 17import org.ooc.frontend.model.Cast;
 18import org.ooc.frontend.model.CharLiteral;
 19import org.ooc.frontend.model.ClassDecl;
 20import org.ooc.frontend.model.CommaSequence;
 21import org.ooc.frontend.model.Compare;
 22import org.ooc.frontend.model.CoverDecl;
 23import org.ooc.frontend.model.Dereference;
 24import org.ooc.frontend.model.Div;
 25import org.ooc.frontend.model.Else;
 26import org.ooc.frontend.model.FloatLiteral;
 27import org.ooc.frontend.model.FlowControl;
 28import org.ooc.frontend.model.For;
 29import org.ooc.frontend.model.Foreach;
 30import org.ooc.frontend.model.FunctionCall;
 31import org.ooc.frontend.model.FunctionDecl;
 32import org.ooc.frontend.model.If;
 33import org.ooc.frontend.model.Import;
 34import org.ooc.frontend.model.Include;
 35import org.ooc.frontend.model.IntLiteral;
 36import org.ooc.frontend.model.InterfaceDecl;
 37import org.ooc.frontend.model.Line;
 38import org.ooc.frontend.model.Match;
 39import org.ooc.frontend.model.MemberAccess;
 40import org.ooc.frontend.model.MemberArgument;
 41import org.ooc.frontend.model.MemberAssignArgument;
 42import org.ooc.frontend.model.MemberCall;
 43import org.ooc.frontend.model.Mod;
 44import org.ooc.frontend.model.Module;
 45import org.ooc.frontend.model.Mul;
 46import org.ooc.frontend.model.Node;
 47import org.ooc.frontend.model.NodeList;
 48import org.ooc.frontend.model.Not;
 49import org.ooc.frontend.model.NullLiteral;
 50import org.ooc.frontend.model.OpDecl;
 51import org.ooc.frontend.model.Parenthesis;
 52import org.ooc.frontend.model.RangeLiteral;
 53import org.ooc.frontend.model.RegularArgument;
 54import org.ooc.frontend.model.Return;
 55import org.ooc.frontend.model.StringLiteral;
 56import org.ooc.frontend.model.Sub;
 57import org.ooc.frontend.model.Ternary;
 58import org.ooc.frontend.model.Type;
 59import org.ooc.frontend.model.Use;
 60import org.ooc.frontend.model.ValuedReturn;
 61import org.ooc.frontend.model.VarArg;
 62import org.ooc.frontend.model.VariableAccess;
 63import org.ooc.frontend.model.VariableDecl;
 64import org.ooc.frontend.model.VersionBlock;
 65import org.ooc.frontend.model.While;
 66import org.ooc.frontend.model.tokens.Token;
 67import org.ooc.frontend.parser.TypeArgument;
 68import org.ooc.middle.structs.MultiMap;
 69import org.ooc.middle.structs.NodeMap;
 70
 71/**
 72 * Whereas Nosy<T> shows where Java abstractions win, SketchyNosy demonstrates
 73 * where Java's abstraction fail.
 74 * 
 75 * A compiler must be *fast*, and Java generics with reflections, dynamic class
 76 * type test etc, just don't cut it. Get used to it. 
 77 * 
 78 * @author Amos Wenger
 79 */
 80public class SketchyNosy implements Visitor {
 81
 82	public final NodeList<Node> stack;
 83	protected Opportunist<Node> oppo;
 84	protected boolean running = true;
 85	
 86	public static  SketchyNosy get(Opportunist<Node> oppo) {
 87		return new SketchyNosy(oppo);
 88	}
 89	
 90	public SketchyNosy(Opportunist<Node> oppo) {
 91		this.stack = new NodeList<Node>(Token.defaultToken);
 92		this.oppo = oppo;
 93	}
 94
 95	public void visitAll(Node node) throws IOException {
 96		stack.push(node);
 97		node.acceptChildren(this);
 98		stack.pop(node);
 99		
100		if(!running) return; // if not running, do nothing
101		if(!oppo.take(node, stack)) running = false; // aborted. (D-Nied. Denied).
102	}
103	
104	public SketchyNosy start() {
105		running = true;
106		return this;
107	}
108	
109	public void visit(Module node) throws IOException {
110		if(node.hasChildren()) visitAll(node);
111		else if(!oppo.take(node, stack)) running = false;
112	}
113
114	public void visit(Add node) throws IOException {
115		if(node.hasChildren()) visitAll(node);
116		else if(!oppo.take(node, stack)) running = false;
117	}
118
119	public void visit(Mul node) throws IOException {
120		if(node.hasChildren()) visitAll(node);
121		else if(!oppo.take(node, stack)) running = false;
122	}
123
124	public void visit(Sub node) throws IOException {
125		if(node.hasChildren()) visitAll(node);
126		else if(!oppo.take(node, stack)) running = false;		
127	}
128
129	public void visit(Div node) throws IOException {
130		if(node.hasChildren()) visitAll(node);
131		else if(!oppo.take(node, stack)) running = false;
132	}
133
134	public void visit(Not node) throws IOException {
135		if(node.hasChildren()) visitAll(node);
136		else if(!oppo.take(node, stack)) running = false;
137	}
138
139	public void visit(FunctionCall node) throws IOException {
140		if(node.hasChildren()) visitAll(node);
141		else if(!oppo.take(node, stack)) running = false;
142	}
143
144	public void visit(MemberCall node) throws IOException {
145		if(node.hasChildren()) visitAll(node);
146		else if(!oppo.take(node, stack)) running = false;
147	}
148
149	public void visit(Parenthesis node) throws IOException {
150		if(node.hasChildren()) visitAll(node);
151		else if(!oppo.take(node, stack)) running = false;
152	}
153
154	public void visit(Assignment node) throws IOException {
155		if(node.hasChildren()) visitAll(node);
156		else if(!oppo.take(node, stack)) running = false;
157	}
158
159	public void visit(ValuedReturn node) throws IOException {
160		if(node.hasChildren()) visitAll(node);
161		else if(!oppo.take(node, stack)) running = false;
162	}
163
164	public void visit(NullLiteral node) throws IOException {
165		if(node.hasChildren()) visitAll(node);
166		else if(!oppo.take(node, stack)) running = false;
167	}
168
169	public void visit(IntLiteral node) throws IOException {
170		if(node.hasChildren()) visitAll(node);
171		else if(!oppo.take(node, stack)) running = false;
172	}
173
174	public void visit(StringLiteral node) throws IOException {
175		if(node.hasChildren()) visitAll(node);
176		else if(!oppo.take(node, stack)) running = false;
177	}
178
179	public void visit(RangeLiteral node) throws IOException {
180		if(node.hasChildren()) visitAll(node);
181		else if(!oppo.take(node, stack)) running = false;
182	}
183
184	public void visit(BoolLiteral node) throws IOException {
185		if(node.hasChildren()) visitAll(node);
186		else if(!oppo.take(node, stack)) running = false;
187	}
188
189	public void visit(CharLiteral node) throws IOException {
190		if(node.hasChildren()) visitAll(node);
191		else if(!oppo.take(node, stack)) running = false;
192	}
193
194	public void visit(Line node) throws IOException {
195		if(node.hasChildren()) visitAll(node);
196		else if(!oppo.take(node, stack)) running = false;
197	}
198
199	public void visit(Include node) throws IOException {
200		if(node.hasChildren()) visitAll(node);
201		else if(!oppo.take(node, stack)) running = false;
202	}
203
204	public void visit(Import node) throws IOException {
205		if(node.hasChildren()) visitAll(node);
206		else if(!oppo.take(node, stack)) running = false;
207	}
208
209	public void visit(If node) throws IOException {
210		if(node.hasChildren()) visitAll(node);
211		else if(!oppo.take(node, stack)) running = false;
212	}
213
214	public void visit(While node) throws IOException {
215		if(node.hasChildren()) visitAll(node);
216		else if(!oppo.take(node, stack)) running = false;
217	}
218
219	public void visit(For node) throws IOException {
220		if(node.hasChildren()) visitAll(node);
221		else if(!oppo.take(node, stack)) running = false;
222	}
223	
224	public void visit(Foreach node) throws IOException {
225		if(node.hasChildren()) visitAll(node);
226		else if(!oppo.take(node, stack)) running = false;
227	}
228
229	public void visit(VariableAccess node) throws IOException {
230		if(node.hasChildren()) visitAll(node);
231		else if(!oppo.take(node, stack)) running = false;
232	}
233
234	public void visit(ArrayAccess node) throws IOException {
235		if(node.hasChildren()) visitAll(node);
236		else if(!oppo.take(node, stack)) running = false;
237	}
238
239	public void visit(VariableDecl node) throws IOException {
240		if(node.hasChildren()) visitAll(node);
241		else if(!oppo.take(node, stack)) running = false;
242	}
243
244	public void visit(FunctionDecl node) throws IOException {
245		if(node.hasChildren()) visitAll(node);
246		else if(!oppo.take(node, stack)) running = false;
247	}
248
249	public void visit(ClassDecl node) throws IOException {
250		if(node.hasChildren()) visitAll(node);
251		else if(!oppo.take(node, stack)) running = false;
252	}
253
254	public void visit(TypeArgument node) throws IOException {
255		if(node.hasChildren()) visitAll(node);
256		else if(!oppo.take(node, stack)) running = false;
257	}
258
259	public void visit(RegularArgument node) throws IOException {
260		if(node.hasChildren()) visitAll(node);
261		else if(!oppo.take(node, stack)) running = false;
262	}
263
264	public void visit(MemberArgument node) throws IOException {
265		if(node.hasChildren()) visitAll(node);
266		else if(!oppo.take(node, stack)) running = false;
267	}
268
269	public void visit(MemberAssignArgument node) throws IOException {
270		if(node.hasChildren()) visitAll(node);
271		else if(!oppo.take(node, stack)) running = false;
272	}
273		
274	public void visit(Type node) throws IOException {
275		if(node.hasChildren()) visitAll(node);
276		else if(!oppo.take(node, stack)) running = false;
277	}
278
279	public void visit(VarArg node) throws IOException {
280		if(node.hasChildren()) visitAll(node);
281		else if(!oppo.take(node, stack)) running = false;
282	}
283
284	public void visit(CoverDecl node) throws IOException {
285		if(node.hasChildren()) visitAll(node);
286		else if(!oppo.take(node, stack)) running = false;
287	}
288
289	public void visit(NodeList<? extends Node> node) throws IOException {
290		if(node.hasChildren()) visitAll(node);
291		else if(!oppo.take(node, stack)) running = false;
292	}
293	
294	public void visit(Block node) throws IOException {
295		if(node.hasChildren()) visitAll(node);
296		else if(!oppo.take(node, stack)) running = false;
297	}
298	
299	public void visit(CommaSequence node) throws IOException {
300		if(node.hasChildren()) visitAll(node);
301		else if(!oppo.take(node, stack)) running = false;
302	}
303
304	public void visit(Mod node) throws IOException {
305		if(node.hasChildren()) visitAll(node);
306		else if(!oppo.take(node, stack)) running = false;
307	}
308
309	public void visit(Return node) throws IOException {
310		if(node.hasChildren()) visitAll(node);
311		else if(!oppo.take(node, stack)) running = false;
312	}
313
314	public void visit(BuiltinType node) throws IOException {
315		if(node.hasChildren()) visitAll(node);
316		else if(!oppo.take(node, stack)) running = false;
317	}
318
319	public void visit(MemberAccess node) throws IOException {
320		if(node.hasChildren()) visitAll(node);
321		else if(!oppo.take(node, stack)) running = false;
322	}
323
324	public void visit(Compare node) throws IOException {
325		if(node.hasChildren()) visitAll(node);
326		else if(!oppo.take(node, stack)) running = false;
327	}
328
329	public void visit(FloatLiteral node) throws IOException {
330		if(node.hasChildren()) visitAll(node);
331		else if(!oppo.take(node, stack)) running = false;
332	}
333
334	public void visit(Cast node) throws IOException {
335		if(node.hasChildren()) visitAll(node);
336		else if(!oppo.take(node, stack)) running = false;
337	}
338
339	public void visit(AddressOf node) throws IOException {
340		if(node.hasChildren()) visitAll(node);
341		else if(!oppo.take(node, stack)) running = false;
342	}
343
344	public void visit(Dereference node) throws IOException {
345		if(node.hasChildren()) visitAll(node);
346		else if(!oppo.take(node, stack)) running = false;
347	}
348
349	public void visit(OpDecl node) throws IOException {
350		if(node.hasChildren()) visitAll(node);
351		else if(!oppo.take(node, stack)) running = false;
352	}
353
354	public void visit(ArrayLiteral node) throws IOException {
355		if(node.hasChildren()) visitAll(node);
356		else if(!oppo.take(node, stack)) running = false;
357	}
358
359	public void visit(Use node) throws IOException {
360		if(node.hasChildren()) visitAll(node);
361		else if(!oppo.take(node, stack)) running = false;
362	}
363
364	public void visit(BinaryCombination node) throws IOException {
365		if(node.hasChildren()) visitAll(node);
366		else if(!oppo.take(node, stack)) running = false;
367	}
368
369	public void visit(Else node) throws IOException {
370		if(node.hasChildren()) visitAll(node);
371		else if(!oppo.take(node, stack)) running = false;
372	}
373
374	public void visit(MultiMap<?, ?> node) throws IOException {
375		if(node.hasChildren()) visitAll(node);
376		else if(!oppo.take(node, stack)) running = false;		
377	}
378
379	public void visit(FlowControl node) throws IOException {
380		if(node.hasChildren()) visitAll(node);
381		else if(!oppo.take(node, stack)) running = false;
382	}
383
384	public void visit(InterfaceDecl node) throws IOException {
385		if(node.hasChildren()) visitAll(node);
386		else if(!oppo.take(node, stack)) running = false;
387	}
388
389	public void visit(Ternary node) throws IOException {
390		if(node.hasChildren()) visitAll(node);
391		else if(!oppo.take(node, stack)) running = false;
392	}
393
394	public void visit(Match node) throws IOException {
395		if(node.hasChildren()) visitAll(node);
396		else if(!oppo.take(node, stack)) running = false;		
397	}
398
399	public void visit(Case node) throws IOException {
400		if(node.hasChildren()) visitAll(node);
401		else if(!oppo.take(node, stack)) running = false;
402	}
403
404	public void visit(VersionBlock node) throws IOException {
405		if(node.hasChildren()) visitAll(node);
406		else if(!oppo.take(node, stack)) running = false;
407	}
408
409	public void visit(NodeMap<?, ? extends Node> node) throws IOException {
410		if(node.hasChildren()) visitAll(node);
411		else if(!oppo.take(node, stack)) running = false;		
412	}
413
414	public void visit(BinaryNegation node) throws IOException {
415		if(node.hasChildren()) visitAll(node);
416		else if(!oppo.take(node, stack)) running = false;
417	}
418	
419}