/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

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