PageRenderTime 42ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 151 lines | 85 code | 27 blank | 39 comment | 27 complexity | 4d53c5b500cde50a21c99c701af80ab0 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 BSHAssignment extends SimpleNode implements ParserConstants
  35. {
  36. public int operator;
  37. BSHAssignment(int id) { super(id); }
  38. public Object eval(
  39. CallStack callstack, Interpreter interpreter)
  40. throws EvalError
  41. {
  42. BSHLHSPrimaryExpression lhsNode =
  43. (BSHLHSPrimaryExpression)jjtGetChild(0);
  44. if ( lhsNode == null )
  45. throw new InterpreterError( "Error, null LHSnode" );
  46. LHS lhs = lhsNode.toLHS( callstack, interpreter);
  47. if ( lhs == null )
  48. throw new InterpreterError( "Error, null LHS" );
  49. Object rhs = ((SimpleNode)jjtGetChild(1)).eval(callstack, interpreter);
  50. if ( rhs == Primitive.VOID )
  51. throw new EvalError("Void assignment.", this);
  52. switch(operator)
  53. {
  54. case ASSIGN:
  55. try {
  56. //Interpreter.debug("lhs assign: "+rhs);
  57. return lhs.assign(rhs);
  58. } catch ( EvalError e ) {
  59. e.reThrow(this);
  60. }
  61. case PLUSASSIGN:
  62. return lhs.assign(operation(lhs.getValue(), rhs, PLUS));
  63. case MINUSASSIGN:
  64. return lhs.assign(operation(lhs.getValue(), rhs, MINUS));
  65. case STARASSIGN:
  66. return lhs.assign(operation(lhs.getValue(), rhs, STAR));
  67. case SLASHASSIGN:
  68. return lhs.assign(operation(lhs.getValue(), rhs, SLASH));
  69. case ANDASSIGN:
  70. case ANDASSIGNX:
  71. return lhs.assign(operation(lhs.getValue(), rhs, BIT_AND));
  72. case ORASSIGN:
  73. case ORASSIGNX:
  74. return lhs.assign(operation(lhs.getValue(), rhs, BIT_OR));
  75. case XORASSIGN:
  76. return lhs.assign(operation(lhs.getValue(), rhs, XOR));
  77. case MODASSIGN:
  78. return lhs.assign(operation(lhs.getValue(), rhs, MOD));
  79. case LSHIFTASSIGN:
  80. case LSHIFTASSIGNX:
  81. return lhs.assign(operation(lhs.getValue(), rhs, LSHIFT));
  82. case RSIGNEDSHIFTASSIGN:
  83. case RSIGNEDSHIFTASSIGNX:
  84. return lhs.assign(operation(lhs.getValue(), rhs, RSIGNEDSHIFT));
  85. case RUNSIGNEDSHIFTASSIGN:
  86. case RUNSIGNEDSHIFTASSIGNX:
  87. return lhs.assign(operation(lhs.getValue(), rhs, RUNSIGNEDSHIFT));
  88. default:
  89. throw new InterpreterError("unimplemented operator in assignment BSH");
  90. }
  91. }
  92. private Object operation(Object lhs, Object rhs, int kind)
  93. throws EvalError
  94. {
  95. /*
  96. Implement String += value;
  97. According to the JLS, value may be anything.
  98. In BeanShell, we'll disallow VOID (undefined) values.
  99. (or should we map them to the empty string?)
  100. */
  101. if ( lhs instanceof String && rhs != Primitive.VOID ) {
  102. if ( kind != PLUS )
  103. throw new EvalError(
  104. "Use of non + operator with String LHS", this);
  105. return (String)lhs + rhs;
  106. }
  107. if ( lhs instanceof Primitive || rhs instanceof Primitive )
  108. if(lhs == Primitive.VOID || rhs == Primitive.VOID)
  109. throw new EvalError(
  110. "Illegal use of undefined object or 'void' literal", this);
  111. else if ( lhs == Primitive.NULL || rhs == Primitive.NULL )
  112. throw new EvalError(
  113. "Illegal use of null object or 'null' literal", this);
  114. if( (lhs instanceof Boolean || lhs instanceof Character ||
  115. lhs instanceof Number || lhs instanceof Primitive) &&
  116. (rhs instanceof Boolean || rhs instanceof Character ||
  117. rhs instanceof Number || rhs instanceof Primitive) )
  118. {
  119. return Primitive.binaryOperation(lhs, rhs, kind);
  120. }
  121. throw new EvalError("Non primitive value in operator: " +
  122. lhs.getClass() + " " + tokenImage[kind] + " " + rhs.getClass(), this);
  123. }
  124. }