Mini Kotlin Compiler
Top-level compiler that translates a MiniKotlin parse tree into a Java source file expressed in continuation-passing style (CPS).
CPS overview
Every MiniKotlin function fun f(a: A): R is transformed into:
public static void f(A a, Continuation<R> continuation)Instead of returning a value the function calls continuation.accept(value). The Continuation<T> interface is provided by the stdlib:
public interface Continuation<T> { void accept(T t); }Mutable variables
Mutable var bindings are heap-boxed in a single-element array so that inner lambdas produced by the CPS transformation can close over and mutate them (Java lambdas require effectively-final captures):
var x: Int = 5 -> Integer[] x_0 = new Integer[]{ 5 };
x = x + 1 -> x_0[0] = x_0[0] + 1;Responsibilities
This class handles statement-level compilation:
Function declarations and the Java
main(String[])entry pointreturn,if/else,while,vardeclaration, assignmentStandalone expression statements (e.g.
println(x))
Expression-level compilation is delegated to ExpressionCompiler. Both share a SharedCounters instance so all generated names are unique.
Entry point
val compiler = MiniKotlinCompiler()
val javaSource: String = compiler.compile(programContext)Functions
Compiles an entire MiniKotlin program to a single Java source file.
Visit a parse tree produced by the AddSubExpr labeled alternative in expression.
Visit a parse tree produced by the AndExpr labeled alternative in expression.
Visit a parse tree produced by argumentList.
Visit a parse tree produced by block.
Visit a parse tree produced by the BoolLiteral labeled alternative in primary.
Visit a parse tree produced by the ComparisonExpr labeled alternative in expression.
Visit a parse tree produced by the EqualityExpr labeled alternative in expression.
Visit a parse tree produced by the FunctionCallExpr labeled alternative in expression.
Visit a parse tree produced by functionDeclaration.
Visit a parse tree produced by the IdentifierExpr labeled alternative in primary.
Visit a parse tree produced by ifStatement.
Visit a parse tree produced by the IntLiteral labeled alternative in primary.
Visit a parse tree produced by the MulDivExpr labeled alternative in expression.
Visit a parse tree produced by the NotExpr labeled alternative in expression.
Visit a parse tree produced by the OrExpr labeled alternative in expression.
Visit a parse tree produced by parameter.
Visit a parse tree produced by parameterList.
Visit a parse tree produced by the ParenExpr labeled alternative in primary.
Visit a parse tree produced by the PrimaryExpr labeled alternative in expression.
Visit a parse tree produced by program.
Visit a parse tree produced by returnStatement.
Visit a parse tree produced by statement.
Visit a parse tree produced by the StringLiteral labeled alternative in primary.
Visit a parse tree produced by type.
Visit a parse tree produced by variableAssignment.
Visit a parse tree produced by variableDeclaration.
Visit a parse tree produced by whileStatement.