/jEdit/tags/jedit_43_with_gnuregexp_microstarxml/bsh/BSHSwitchStatement.java

# · Java · 136 lines · 76 code · 11 blank · 49 comment · 18 complexity · 50c3eecffc6ac3f750295cee50130ffc MD5 · raw file

  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.", this, callstack );
  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. || primitiveEquals(
  64. switchVal, label.eval( callstack, interpreter ),
  65. callstack, switchExp )
  66. )
  67. {
  68. // execute nodes, skipping labels, until a break or return
  69. while ( child < numchild )
  70. {
  71. node = jjtGetChild(child++);
  72. if ( node instanceof BSHSwitchLabel )
  73. continue;
  74. // eval it
  75. Object value =
  76. ((SimpleNode)node).eval( callstack, interpreter );
  77. // should check to disallow continue here?
  78. if ( value instanceof ReturnControl ) {
  79. returnControl = (ReturnControl)value;
  80. break;
  81. }
  82. }
  83. } else
  84. {
  85. // skip nodes until next label
  86. while ( child < numchild )
  87. {
  88. node = jjtGetChild(child++);
  89. if ( node instanceof BSHSwitchLabel ) {
  90. label = (BSHSwitchLabel)node;
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. if ( returnControl != null && returnControl.kind == RETURN )
  97. return returnControl;
  98. else
  99. return Primitive.VOID;
  100. }
  101. /**
  102. Helper method for testing equals on two primitive or boxable objects.
  103. yuck: factor this out into Primitive.java
  104. */
  105. private boolean primitiveEquals(
  106. Object switchVal, Object targetVal,
  107. CallStack callstack, SimpleNode switchExp )
  108. throws EvalError
  109. {
  110. if ( switchVal instanceof Primitive || targetVal instanceof Primitive )
  111. try {
  112. // binaryOperation can return Primitive or wrapper type
  113. Object result = Primitive.binaryOperation(
  114. switchVal, targetVal, ParserConstants.EQ );
  115. result = Primitive.unwrap( result );
  116. return result.equals( Boolean.TRUE );
  117. } catch ( UtilEvalError e ) {
  118. throw e.toEvalError(
  119. "Switch value: "+switchExp.getText()+": ",
  120. this, callstack );
  121. }
  122. else
  123. return switchVal.equals( targetVal );
  124. }
  125. }