Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gaultier/micro-kotlin
A small and fast Kotlin compiler in C99. Still experimental!
https://github.com/gaultier/micro-kotlin
c99 compiler kotlin
Last synced: 2 months ago
JSON representation
A small and fast Kotlin compiler in C99. Still experimental!
- Host: GitHub
- URL: https://github.com/gaultier/micro-kotlin
- Owner: gaultier
- Created: 2023-06-19T05:54:48.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-12-06T15:53:26.000Z (about 1 year ago)
- Last Synced: 2024-10-16T06:29:42.788Z (3 months ago)
- Topics: c99, compiler, kotlin
- Language: C
- Homepage:
- Size: 1.16 MB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# A fast, small Kotlin compiler written in C
A statically linked, dependencyless, 200 Kib (stripped) executable using < 10 MiB of memory, with pprof memory profiling included.
**Caution: Not production grade! Not ready!**
My third (and last?) take on a Kotlin compiler, in C. It goes much further than the [two](https://github.com/gaultier/kotlin-rs) [previous](https://github.com/gaultier/microkt) attempts both in terms of language support and in terms of implementation:
- Expressions, statements, control flow, and functions (including recursion) are implemented
- It implements Stack Map Frames contrary the [kotlin-rs](https://github.com/gaultier/kotlin-rs) so the latest JVM versions work seamlessly with it and bytecode generated by the compiler is verified at load time by the JVM
- It implements type constraints in the type checker to infer which function the user intended to call (and the rules in Kotlin for that are so very complex). That's one of the thorniest topics in Kotlin due to the language trying to have every programming language feature under the sun, namely: type inference, function overloading, default parameters, generics, implicit callers (`it` and such), variadic functions, object inheritance with method overriding, etc.
- It explores the class path so that external libraries can be used including the Java and Kotlin standard libraries
- Some Java/Kotlin interop is supported (mostly, calling Java functions from Kotlin)
- It parses and understands .jmod, .class, .jar files even with compression
- Out-of-order definitions of functions and variables are supported
- Some support for function inlining is supported (inlining the body of a called function)
- All allocations are done with an arena allocator and there is support for a memory dump with stacktraces
- It's only 10k lines of code and the final stripped executable is 145 Kib!See [TODO.md](TODO.md) for details.
Such code works:
```kotlin
fun fibonacci_rec(n: Int) : Int {
if (n == 1) {
return 1
}
return n + fibonacci_rec(n-1)
}fun main() {
println("Hello, world!")
println(fibonacci_rec(10))
}
```## Quickstart
*Requirements: A C99 compiler, the Kotlin standard library, the Java standard library, optionally: zlib (to read jar files that have compression).*
```sh
# Pick whatever variant and version of the JDK you like here. We only need to get the Kotlin & Java standard library files.
$ sudo apt install kotlin openjdk-21-jdk
$ make
$ ./micro-kotlin --java-home /usr/lib/jvm/java-21-openjdk-amd64/ -c /usr/share/java/kotlin-stdlib.jar demo.kt
$ java DemoKt
Hello, world!
12
72
144
55
1
2
255
```See `demo.kt` to see what language constructs are supported at the moment.
```sh
$ micro-kotlin --help
A small compiler for the Kotlin programming language.micro-kotlin [OPTIONS]
EXAMPLES:
micro-kotlin -j /usr/lib/jvm/java-21-openjdk-amd64/ -c /usr/share/java/kotlin-stdlib.jar main.ktOPTIONS:
-v, --verbose Verbose.
-m, --memory-usage Debug memory usage by printing a heap dump in the pprof format.
-h, --help Print this help message and exit.
-c, --classpath Load additional classpath entries, which are colon separated.
-j, --java-home Java home (the root of the Java installation).
```## LICENSE
BSD-3.