Changing the Java language using OpenJDK – a small experiment

Of late, I have been fascinated with languages, compilers, and specifically parsing and code generation. To that end, I started looking into how the Java language itself might be modified. It turns out that Java itself uses a handwritten lexer and parser with a very well-defined tree structure for the AST. That being said, good documentation if hard to come by.

I chanced upon this gem of a blog – Making Java esoteric which shows a simple example of how to add a maybe keyword to Java so that it may randomly execute statements and/or blocks.

I was inspired enough to try out my first hack on OpenJDK. To that end, I decided to implement a when statement that would essentially act as its Common Lisp equivalent.

My attempt at implementing the when keyword didn’t go down so well since it appears that the word has been used in umpteen places in the JDK itself. All right, let’s just use the Russian equivalent, kogda meaning essentially the same.

My basic logic was to define the new keyword, kogda that would work with block statements or simple statements, and then offload the actual work to the if statement. So here were the code changes in order:

First off the lexer:
/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/Tokens.java):

 public enum TokenKind implements Formattable, Filter<TokenKind> {
    ...
    KOGDA("kogda"),
    ...
}

And then the parser:/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParer.java:

 List<JCStatement> blockStatement() {
    ...
    case KOGDA:
    ...
    case ASSERT:
            return List.of(parseSimpleStatement());
    ...
}

and

 public JCStatement parseSimpleStatement() {
   ...
     case KOGDA: {
                nextToken();
                JCExpression cond = parExpression();
                JCStatement body = parseStatementAsBlock();
                return F.at(pos).Kogda(cond, body);
            }
   ...
}

Nothing special here – we simply read in the next token, get the conditional part, and then get the body of the when expression/statement.

And then finally, langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeMaker.java:

public JCIf Kogda(JCExpression cond, JCStatement body) {
        JCIf tree = new JCIf(cond, body, null);
        tree.pos = pos;

        return tree;
    }

This is the actual meat of the logic per se. In this case, I simply offload the work to the reliable if expression and insert null for the else part. That’s all there is to it!

Then we build the new version of the JDK:

Macushla:9dev z0ltan$ pwd
/Users/z0ltan/Projects/Resources/Languages/Java/9dev

Macushla:9dev z0ltan$ make clean && make images
<output elided>

Macushla:9dev z0ltan$ cd build/macosx-x86_64-normal-server-release/jdk/bin/

Macushla:bin z0ltan$ ./javac -version
javac 9-internal

Here’s our little test program to test out:

Macushla:bin z0ltan$ cat HelloKogda.java
public class HelloKogda {
    public static void main(String[] args) {
        kogda (1 < 2) {
            System.out.println("Yes, 1 < 2");
        }

        kogda (2 > 100) {
            System.out.println("2 > 100");
        }

        kogda (1 < 10 && 1 == 1) System.out.println("Booyah!");
    }
}

And finally, compiling and running the test code using our custom-built JDK:

Macushla:bin z0ltan$ ./javac HelloKogda.java
Macushla:bin z0ltan$ ./java -cp . HelloKogda
Yes, 1 < 2
Booyah!

Nice! There was just one thing that I was not completely happy with – JShell. In theory, it should be trivial to modify JShell to reflect these same changes, but I had immense trouble trying to get the jdk.jshell project pick up the changes from my modified jdk.compiler project instead of the default JDK (which, of course, does not contain my changes). Maybe when I work my head around the internals of the whole ecosystem, I will post an update here. For now, this was a fun experiment!

Advertisements
Changing the Java language using OpenJDK – a small experiment