sergutsan / JDecafCompiler

A Java precompiler for educational purposes
1 stars 0 forks source link

Method is not preprocessed into static method out of main #6

Open sergutsan opened 10 years ago

sergutsan commented 10 years ago

Surely related to #5

Java Decaf code

int a=1;
print (decimal2binary(a));

String decimal2binary(int number) {
  while (number>0) {
    int bin=number%2;
    binaryString=binaryString+bin;
    number=(number-bin)/2;
  }
  return binaryString;
}

Expected Java Code

...
    public static void main(String[] args)
    {
int a=1;
System.out.print (decimal2binary(a));
}

private static String decimal2binary(int number) {
  while (number>0) {
    int bin=number%2;
    binaryString=binaryString+bin;
    number=(number-bin)/2;
  }
  return binaryString;
}

}

Actual Java Code

...
    public static void main(String[] args)
    {
int a=1;
System.out.print (decimal2binary(a));

String decimal2binary(int number) {
  while (number>0) {
    int bin=number%2;
    binaryString=binaryString+bin;
    number=(number-bin)/2;
  }
  return binaryString;
}

}}

Maybe this is related to the fact the method returns a String?

sergutsan commented 10 years ago

Much simpler way of reproducing this problem:

int a=1;
print (myMethod(a));

String myMethod(int myParameter) {
  return myParameter;
}
sergutsan commented 10 years ago

Simplest code to reproduce issue:

print (myMethod());

String myMethod() {
  return;
}

I said "Maybe this is related to the fact the method returns a String?". If the method myMethod returns an int or a double or a class name (regardles of whether the class is defined or undefined), this issue does not replicate ---so it seems it is actually something to do with String.

Update: it happens too with boxed types such as Integer and Double.

sergutsan commented 10 years ago

Some additional info: Token 'String' is detected as a token of type FUNCTION (same as 'print' or 'println'), I have no idea why but I guess I cannot change it.

PS: While we are this, I wonder what is the rationale behing 'myMethod', ';', and 'Point' being DATA_TYPE.

print (myMethod());
String myMethod() {
  return null;
}
class Point {}
sergutsan commented 10 years ago

The rabbit hole goes deeper. The issue related to String, Integer, etc, but also to the position of the method:

Problematic code

int a = 1;
a = a + 1;
print (myMethod());
myMethod();

String myMethodS() {
  return null;
}

Integer myMethodI() {
  return null;
}

Double myMethodD() {
  return null;
}

Point myMethod() {
  return null;
}

class Point {}

Working code

int a = 1;
a = a + 1;
print (myMethod());
myMethod();

Point myMethod() {
  return null;
}

String myMethodS() {
  return null;
}

Integer myMethodI() {
  return null;
}

Double myMethodD() {
  return null;
}

class Point {}

PS: Any java.lang class seems to be a function: Integer, String, Exception...

sergutsan commented 10 years ago

java.lang.* classes are tokens of type FUNCTION, and methods that return them sometimes are created as methods out of the main code, but they are never private static methods.

private static Point myMethod() {
  return null;
}

String myMethodS() {
  return null;
}

and, as we know, they are not even taken out of the main code unless there is another method, i.e.:

int a = 1;
a = a + 1;
print (myMethod());
myMethod();

String myMethodS() {
  return null;
}

Integer myMethodI() {
  return null;
}

Point myMethod() {
  return null;
}

Double myMethodD() {
  return null;
}

Exception myMethodEx() {
    return null;
}

class Point {}

results in

import java.util.Scanner;
public class tmp
{
            private static Scanner scanner=new Scanner(System.in); static{scanner.useDelimiter(System.getPro\
perty("line.separator"));}
      public static void main(String[] args)
      {
int a = 1;
a = a + 1;
System.out.print (myMethod());
myMethod();

String myMethodS() {
  return null;
}

Integer myMethodI() {
  return null;
}

} private static Point myMethod() {
  return null;
}

Double myMethodD() {
  return null;
}

Exception myMethodEx() {
    return null;
}

}

class Point {private static Scanner scanner=new Scanner(System.in); static{scanner.useDelimiter(System.getProperty("line.separator"));}}
sergutsan commented 10 years ago

I thought ';' was a DATA_TYPE, but now it seems it is an IDENTIFIER too. What an API! :-(