Patch adding lambda support with runtime source code access and support libraries
Marcin Kosieradzki
marcin.kosieradzki at gmail.com
Sat Aug 22 08:46:28 PDT 2009
Hello,
I am student at Warsaw University and I am developing project called
jlinq for my master thesis.
The goal of the project is to develop C#-style
language-integrated-query features.
My first approach (2-years ago) to this project was not really
successful, because there was no openjdk :). And I was working at
preprocessor level. But thanks to open source JDK I was able to modify
real compiler and implement this completely my way.
One of the most import language features required for this project is
support for lambda expressions, which can provide their code at
runtime.
I decided to extend javac from openjdk 6 and I have now working
prototype of extended compiler.
I am quite busy at my normal job so I won't be able to activily
contribute or maintain this project, but if someone would be
interested in further development, maintenance or integration of this
patch for example into closures project I can send my code. I would be
happy if someone can use this code for further development. I am also
not going to use in my professional work, because my primary
development language is C#.
I've done my best to keep the code maintanable and easily integratable
into javac. It could be easily changed into for example completely
different syntax, support type inference or whatever.
Below is short example what this compiler can do:
1. If you are using lambda syntax you should add -extdirs option and
point to jlinq.jar which provides classes for runtime representation
of lambdas
2. Some examples of code:
code:
System.out.println(lambda (Integer x) => x + 3);
output:
lambda (x) => x + 3
code:
final int a = 10;
LambdaExpression1<Integer, Integer> ml = lambda (Integer x) => x + a;
// Returns AST representing
LambdaTree tree = ml.getTree();
//The tree can be easily traversed
System.out.println(tree);
//Outputs: lambda (x) => x + 10
Integer b = ml.eval(15); //returns 25
// Eval retains anonymous class semantics and is executed in
optimized, compiled form
//Of course there are more constructs understood by lambda extensions
on the moment: method invocations, field access, binary operators,
literals, lambda parameter reference, outside variable reference,
nested lambda
lambda (Double x) => 10.0 + Math.power(x, 2); //This is also valid construct
//you can also nest lambdas:
Integer c = (lambda (final Integer x) => lambda (Integer y) => x +
y).eval(10).eval(20) //returns 30;
Lambdas can have 0-n arguments.
I am going to add some more features until I complete my thesis.
Please contact me if someone is interested in doing something with
results of my hard work ;).
Regards,
Marcin Kosieradzki
More information about the compiler-dev
mailing list