PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/bsh/BSHSwitchStatement.java

#
Java | 109 lines | 54 code | 11 blank | 44 comment | 16 complexity | cf61dba462f8c18a94655df83f815704 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*****************************************************************************
  2. * *
  3. * This file is part of the BeanShell Java Scripting distribution. *
  4. * Documentation and updates may be found at http://www.beanshell.org/ *
  5. * *
  6. * Sun Public License Notice: *
  7. * *
  8. * The contents of this file are subject to the Sun Public License Version *
  9. * 1.0 (the "License"); you may not use this file except in compliance with *
  10. * the License. A copy of the License is available at http://www.sun.com *
  11. * *
  12. * The Original Code is BeanShell. The Initial Developer of the Original *
  13. * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
  14. * (C) 2000. All Rights Reserved. *
  15. * *
  16. * GNU Public License Notice: *
  17. * *
  18. * Alternatively, the contents of this file may be used under the terms of *
  19. * the GNU Lesser General Public License (the "LGPL"), in which case the *
  20. * provisions of LGPL are applicable instead of those above. If you wish to *
  21. * allow use of your version of this file only under the terms of the LGPL *
  22. * and not to allow others to use your version of this file under the SPL, *
  23. * indicate your decision by deleting the provisions above and replace *
  24. * them with the notice and other provisions required by the LGPL. If you *
  25. * do not delete the provisions above, a recipient may use your version of *
  26. * this file under either the SPL or the LGPL. *
  27. * *
  28. * Patrick Niemeyer (pat@pat.net) *
  29. * Author of Learning Java, O'Reilly & Associates *
  30. * http://www.pat.net/~pat/ *
  31. * *
  32. *****************************************************************************/
  33. package bsh;
  34. class BSHSwitchStatement
  35. extends SimpleNode
  36. implements ParserConstants
  37. {
  38. public BSHSwitchStatement(int id) { super(id); }
  39. public Object eval( CallStack callstack, Interpreter interpreter )
  40. throws EvalError
  41. {
  42. int numchild = jjtGetNumChildren();
  43. int child = 0;
  44. SimpleNode switchExp = ((SimpleNode)jjtGetChild(child++));
  45. Object switchVal = switchExp.eval( callstack, interpreter );
  46. /*
  47. Note: this could be made clearer by adding an inner class for the
  48. cases and an object context for the child traversal.
  49. */
  50. // first label
  51. BSHSwitchLabel label;
  52. Object node;
  53. ReturnControl returnControl=null;
  54. // get the first label
  55. if ( child >= numchild )
  56. throw new EvalError("Empty switch statement...");
  57. label = ((BSHSwitchLabel)jjtGetChild(child++));
  58. // while more labels or blocks and haven't hit return control
  59. while ( child < numchild && returnControl == null )
  60. {
  61. // if label is default or equals switchVal
  62. if ( label.isDefault
  63. || label.eval( callstack, interpreter ).equals( switchVal ) )
  64. {
  65. // execute nodes, skipping labels, until a break or return
  66. while ( child < numchild )
  67. {
  68. node = jjtGetChild(child++);
  69. if ( node instanceof BSHSwitchLabel )
  70. continue;
  71. // eval it
  72. Object value =
  73. ((SimpleNode)node).eval( callstack, interpreter );
  74. // should check to disallow continue here?
  75. if ( value instanceof ReturnControl ) {
  76. returnControl = (ReturnControl)value;
  77. break;
  78. }
  79. }
  80. } else
  81. {
  82. // skip nodes until next label
  83. while ( child < numchild )
  84. {
  85. node = jjtGetChild(child++);
  86. if ( node instanceof BSHSwitchLabel ) {
  87. label = (BSHSwitchLabel)node;
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. if ( returnControl != null && returnControl.kind == RETURN )
  94. return returnControl;
  95. else
  96. return Primitive.VOID;
  97. }
  98. }