This is an internship assignment for implementing a CPS-style (Continuation-Passing Style) compiler from MiniKotlin (a subset of Kotlin) to Java.
The goal is to implement a compiler that translates MiniKotlin source code into Java, where all functions are expressed using continuation-passing style.
samples/- Example MiniKotlin programssrc/main/antlr/MiniKotlin.g4- Grammar definition for MiniKotlinsrc/main/kotlin/compiler/- Compiler implementationsrc/test/- Testing frameworkstdlib/- Standard library withPreludeclass containing CPS function examples
Implement the MiniKotlinCompiler to translate MiniKotlin to Java such that:
- All functions use continuation-passing style
- The semantics of operators follows Kotlin
See Prelude in the stdlib for examples of how CPS functions should look.
Suppose that the MiniKotlin code looks like this:
fun factorial(n: Int): Int {
if (n <= 1) {
return 1
} else {
return n * factorial(n - 1)
}
}
// Main logic
fun main(): Unit {
val result: Int = factorial(5)
println(result)
// Arithmetic and logical expressions
val a: Int = 10 + 5
val b: Boolean = a > 10
println(a)
}Then the supposed implementation can look like this:
public static void factorial(Integer n, Continuation<Integer> __continuation) {
if ((n <= 1)) {
__continuation.accept(1);
return;
}
else {
factorial((n - 1), (arg0) -> {
__continuation.accept((n * arg0));
return;
});
}
}
public static void main(String[] args) {
factorial(5, (arg0) -> {
Integer result = arg0;
Prelude.println(result, (arg1) -> {
Integer a = (10 + 5);
Boolean b = (a > 10);
Prelude.println(a, (arg2) -> {
});
});
});
}# Build the project
./gradlew build
# Run with default example
./gradlew run
# Run with a specific file
./gradlew run --args="samples/example.mini"
# Run tests
./gradlew testThe task will be tested on a hidden set of tests.