PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/BSHPrimaryExpression.java

#
Java | 118 lines | 54 code | 11 blank | 53 comment | 9 complexity | d42b8ba11e648d6eb6556fa9cd579605 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 BSHPrimaryExpression extends SimpleNode
  35. {
  36. BSHPrimaryExpression(int id) { super(id); }
  37. /**
  38. Evaluate to a value object.
  39. */
  40. public Object eval( CallStack callstack, Interpreter interpreter)
  41. throws EvalError
  42. {
  43. return eval( false, callstack, interpreter );
  44. }
  45. /**
  46. Evaluate to a value object.
  47. */
  48. public LHS toLHS( CallStack callstack, Interpreter interpreter)
  49. throws EvalError
  50. {
  51. Object obj = eval( true, callstack, interpreter );
  52. if ( ! (obj instanceof LHS) )
  53. throw new EvalError("Can't assign to:", this, callstack );
  54. else
  55. return (LHS)obj;
  56. }
  57. /*
  58. Our children are a prefix expression and any number of suffixes.
  59. <p>
  60. We don't eval() any nodes until the suffixes have had an
  61. opportunity to work through them. This lets the suffixes decide
  62. how to interpret an ambiguous name (e.g. for the .class operation).
  63. */
  64. private Object eval( boolean toLHS,
  65. CallStack callstack, Interpreter interpreter)
  66. throws EvalError
  67. {
  68. Object obj = jjtGetChild(0);
  69. int numChildren = jjtGetNumChildren();
  70. for(int i=1; i<numChildren; i++)
  71. obj = ((BSHPrimarySuffix)jjtGetChild(i)).doSuffix(
  72. obj, toLHS, callstack, interpreter);
  73. /*
  74. If the result is a Node eval() it to an object or LHS
  75. (as determined by toLHS)
  76. */
  77. if ( obj instanceof SimpleNode )
  78. if ( obj instanceof BSHAmbiguousName )
  79. if ( toLHS )
  80. obj = ((BSHAmbiguousName)obj).toLHS(
  81. callstack, interpreter);
  82. else
  83. obj = ((BSHAmbiguousName)obj).toObject(
  84. callstack, interpreter);
  85. else
  86. // Some arbitrary kind of node
  87. if ( toLHS )
  88. // is this right?
  89. throw new EvalError("Can't assign to prefix.",
  90. this, callstack );
  91. else
  92. obj = ((SimpleNode)obj).eval(callstack, interpreter);
  93. // return LHS or value object as determined by toLHS
  94. if ( obj instanceof LHS )
  95. if ( toLHS )
  96. return obj;
  97. else
  98. try {
  99. return ((LHS)obj).getValue();
  100. } catch ( UtilEvalError e ) {
  101. throw e.toEvalError( this, callstack );
  102. }
  103. else
  104. return obj;
  105. }
  106. }