Which version?

Marlon Kranz
InkType

Back

MinJ: A Programming Language for the JVM

A scripting language I designed and built to learn how interpreters work: an ANTLR4 grammar, a tree-walking interpreter in Java, classes, methods and lists, and a GitHub Actions workflow that publishes a fat-JAR for each release.

  • 2024
  • Java 21, ANTLR4, Gradle, ShadowJar, GitHub Actions, JVM
  • GitHub
  • 1 starlatest release v0.3.1

Overview

MinJ (Minimalistic Java) is a scripting language I built to learn how an interpreter works from the inside. It has classes, methods, typed variables, loops, lists and return, and it runs on the JVM. An ANTLR4 grammar of 246 lines generates the lexer and parser, an EvalVisitor of about 730 lines walks the parse tree and executes it, and the result is packaged as one fat-JAR that runs wherever Java 21 does.

I wanted to implement each layer myself instead of only reading about it: lexing, parsing, scoping, type checking and method dispatch.

How it works

Source code goes through three stages:

  1. MinJ.g4 defines the grammar. The Gradle build has ANTLR4 generate MinJLexer and MinJParser from it.
  2. EvalVisitor extends ANTLR's MinJBaseVisitor<Object> and overrides one method per grammar rule. Each visit* call walks its part of the tree and executes it: variable allocation, arithmetic, control flow, method dispatch and object creation.
  3. Main.java reads a .mj file, runs it through the lexer and parser, and calls visitor.visit(tree).

Grammar and execution are separate: the .g4 file defines what a MinJ program looks like, and EvalVisitor defines what it does. A new language construct therefore touches those two files.

The runtime keeps each variable in a Cell that holds its value, declared type, whether it is mutable and whether it is dynamically typed. A return statement throws a ReturnSignal, a RuntimeException subclass, to unwind the call stack, so a return works from inside nested loops and conditionals. When an object is created, it copies its class's field cells, so each instance has its own state.

The language

var declares a mutable, dynamically typed variable and val an immutable one. Explicit types are int, float, double, boolean, char and String. A typed declaration is checked on assignment: assigning a string to an int fails.

Control flow is if/elseif/else, while, for i = 1 to n step s and foreach x in list. Blocks end with end. Boolean operators accept symbols or words (&& or and, || or or, ! or not).

Classes have fields and methods, and new creates an instance. Global functions use func, and a return can list several values. Comments start with // or #, or sit between /* and */.

Examples

FizzBuzz

val limit = 100
for i = 1 to limit do:
  if i % 15 == 0 then:
    print "FizzBuzz"
  elseif i % 3 == 0 then:
    print "Fizz"
  elseif i % 5 == 0 then:
    print "Buzz"
  else:
    print i
  end
end

Classes and methods

class Counter:
  var count = 0
  method inc():
    count = count + 1
  end
  method get():
    return count
  end
end

var c = new Counter()
c.inc()
c.inc()
print c.get()   // 2

Recursive factorial

func factorial(n):
  if n <= 1 then:
    return 1
  end
  return n * factorial(n - 1)
end

print factorial(5)   // 120

Build and releases

The Gradle build uses the ANTLR plugin to regenerate the lexer and parser from MinJ.g4. ShadowJar packages everything, the ANTLR runtime included, into one fat-JAR. A script runs with java -jar minjc-<version>.jar file.mj.

A GitHub Actions workflow runs on pushes to branches named release-v*. It builds the fat-JAR with JDK 21, zips it together with the examples and the README, creates a release tagged with the version from the branch name, and uploads the zip as the release asset. The documentation is published on GitHub Pages.

Extending the language

Adding a construct takes four edits. This is how the while loop was added:

  1. A grammar rule in MinJ.g4:
whileStmt : WHILE expr DO COLON block END ;
  1. Register it in the statement rule:
statement : varDecl | assign | printStmt | ifStmt | whileStmt ;
  1. Lexer tokens:
WHILE : 'while' ;
DO    : 'do' ;
END   : 'end' ;
  1. A visitor override in EvalVisitor.java:
@Override
public Object visitWhileStmt(MinJParser.WhileStmtContext ctx) {
    while ((Boolean) visit(ctx.expr())) {
        visitBlock(ctx.block());
    }
    return null;
}

Then rebuild with ./gradlew clean shadowJar.