M201370367 / beanshell2

Automatically exported from code.google.com/p/beanshell2
0 stars 1 forks source link

Implement support for JLS 15.22.2 #32

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Run the following in BSH 2.1b0 and a Java compiler and runtime:

public class Boolean15_22_2
{
  public static boolean T()
  {
    System.out.println("T");
    return true;
  }

  public static boolean F()
  {
    System.out.println("F");
    return false;
  }

  public static void main(String[] a)
  {
    System.out.println(T() | F());
    System.out.println(F() & T());
    System.out.println(T() ^ T());
  }
}

---

It should print:

T
F
true
F
T
false
T
T
false

In bsh 2.1b0 (and I assume all previous versions), it instead throws a
bsh.InterpreterError on each statement in main:

// Error: Internal Error: unimplemented binary operator                     
bsh.InterpreterError: unimplemented binary operator                         
        at bsh.Primitive.booleanBinaryOperation(Primitive.java:279)         
        at bsh.Primitive.binaryOperationImpl(Primitive.java:244)            
        at bsh.Primitive.binaryOperation(Primitive.java:224)                
        at bsh.BSHBinaryExpression.eval(BSHBinaryExpression.java:139)       
        at bsh.Interpreter.run(Interpreter.java:469)                        
        at bsh.Interpreter.main(Interpreter.java:405)                       
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)      
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:43)
        at java.lang.reflect.Method.invoke(Method.java:616)               

        at jline.ConsoleRunner.main(ConsoleRunner.java:69)                

The problem is that the booleanBinaryOperation in Primitive.java does not
process the BIT_OR, BIT_AND, and XOR tokens (note that the first two names
are misleading, which partly explains the bug).  However, section 15.22.2
(http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.22.1)
explicitly requires all three for boolean operands.  Note that 15.23 and
15.24 explain that the only difference between '&' and '&&', and '|' and
'||', is whether the second operand is always evaluated.  

I have attached a patch that fixes the issue.  Note that the code does
eagerly evaluate, as required.  This part didn't require any modification.
 I will write a test if desired.

Original issue reported on code.google.com by superm401@gmail.com on 20 Apr 2010 at 4:23

Attachments:

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r36.

Original comment by matthew....@gatech.edu on 25 Apr 2010 at 2:46

GoogleCodeExporter commented 9 years ago
milestone=2.1b0

Original comment by pejob...@gmail.com on 25 Feb 2011 at 5:55

GoogleCodeExporter commented 9 years ago

Original comment by pejob...@gmail.com on 20 Oct 2011 at 7:12