ParallelME / compiler

ParallelME Compiler
https://parallelme.github.io
Other
4 stars 2 forks source link

Support for lambda expressions #26

Open wcmjunior opened 8 years ago

wcmjunior commented 8 years ago

Since Android M and Android Studio 2.1 have full support for lambda expressions in Java 8, the compiler should be able to handle them. In fact, the user-library was primarily designed to allow functionality transmission, which is basically to enable users to inject its code by instantiating an anonymous object and implementing the function method. Though improvements should be performed in the user-library, it is not necessary to change a single line of code to use the existing structure of function classes (Foreach, Reduce, Map and Filter) as lambda expressions. I have already tested the user-library in Java 8 and it worked fine (and neat!) as I created lambda expressions like:

// Reduce
Int32 ret = array.reduce((element1, element2) -> {
    element1.value += element2.value;
    return element1;
});
// Map
Array<Int16> array2 = array.map(Int16.class, (element -> {
    return new Int16((short) (element.value + 10));
}));
// Foreach
array2.foreach(element -> {
    element.value += 1;
});
// Filter
Array<Int32> array3 = array.filter(element -> {
    return element.value > 10;
});
wcmjunior commented 8 years ago

I spent some time analyzing what must be changed in order to support Java 8 in PM compiler, but its grammar is quite different than the prior version. For this reason, the parser generated by ANTLR from a Java 8 grammar is not fully compatible with the code created in ScopeDrivenListener, thus it is heavilly impacted. I'm still in doubt if the best thing to do is to definitively migrate to Java 8 or keep two front-ends, leaving PM compatible with both Java specifications. The obvious drawback is that we'll multiply the front-end complexity by two, so probably the same with bugs' number.

wcmjunior commented 8 years ago

Just got new insights in Java 8. As far as I know, there is no backward compatibility issues at syntax level, thus we may make our compiler compatible with both specifications (7 and 8) if we migrate to the most recent version of the language. I've read the compatibility guide and found some problems, but none of them were at syntax level from a perspective of Java 8 to 7: http://www.oracle.com/technetwork/java/javase/8-compatibility-guide-2156366.html