Software Development

Create a programming language for the JVM

Because you know, in the end everyone wants to create his own programming language.

I have been interested in parsers and languages for a while. I worked with Xtext, Jetbrains MPS, created a few DSLs etc. etc. I also wrote my PhD thesis on this topic but so far I did not get started creating a complete programming language from scratch.

Why creating a new language?

Well, there are many wrong answers to this question. Mine is divided in two parts: first, I think it can be a great experience to learn a few things more, and second, a programming language is for me a tool to look at reality, describe it and reason about it to understand it better. Laugh if you wish, but for me it is mainly a tool for thinking. Often it is a clunky tool because I get distracted by technical workarounds or details which are hardly relevent or important for what I am trying to do. For example I am getting tired of clicking here and there to generate equals and hashCode methods (yes, I know about Lombok and some other tricks).

Ok kid… but to make a language usable you need a lot of stuff

I think now the barriers to create a programming language and make it usable by sane persons are significantly lower than they used to be. I think that a language needs a great syntax & semantics, sure, but also a large amount of libraries and decent tool support.

To get libraries just make it run on the JVM. Really. If it runs on the JVM you can reuse a gazzillion of libraries. Your programming language get batteries included. Consider also deployment: I am fascinated by many languages but every time I try to deploy something outside the JVM I end up regretting it. Ask my co-maintainer of WorldEngine (a python program) how much fun is to support libraries on Linux, Mac, Windows, across Python versions. A lot of fun.

Of course you need also tool support: it means to me mainly a nice editor and good integration with the platform of reference. I have an advantage here because I have experience developing IDE plugins and I just ended writing a type solver for Java. In addition to that I contribute to JavaParser. So I know a thing or two about tools that I could use down the road for the integration with Java.

In practice I plan to:

  1. Create a plugin for IntelliJ which is aware of references to file in my language and to Java files. The nice thing is that basically I wrote the libraries for doing that already.
  2. Create a Maven plugin because Maven is awful, but life without Maven is even more awful
  3. Write a parser with ANTLR (that’s the easy part)
  4. Write a bytecode generator with ASM (what a great library!)

Ok tell me more about this language

I just started working on it but… I have a basic compiler working and supporting a few constructs.

It is a static language with type inference and this is an example of a piece of valid code that can be already compiled and run:

namespace manga
 
// now print is an alias for call to all the overload variants of println on System.out
import java.lang.System.out.println as print
 
// we define this property in generale: a name is a String
property String : name
 
// this is our new datatype
type MangaCharacter {
    // we refer to the property defined above
    has name
    // we define a new property, UInt is an unsigned int
    has UInt : age
 
    // we overload toString. For short methods it can make sense to use 
    // this shorthand. And we have string interpolation
    String toString() = "#{name}, #{age}"
}
 
// this define a class with a main method
program MangaExample(String[] args) {
    // type inference at work: we are instantiating the class defined above
    // note that the constructor is generated for us
    val ranma = MangaCharacter("Ranma", 16)
    // let's call a java method ( System.out.println(String) ) and use more
    // string interpolation
    print("The protagonist is #{ranma}")
}

So I have a plan, I have something working already and I am having a lot of fun.
Could be idiotic, perhaps, to write yet another language but damn… it is so much fun!

Reference: Create a programming language for the JVM from our JCG partner Federico Tomassetti at the Federico Tomassetti blog.

Federico Tomassetti

Federico has a PhD in Polyglot Software Development. He is fascinated by all forms of software development with a focus on Model-Driven Development and Domain Specific Languages.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button