138 lines
3 KiB
Markdown
138 lines
3 KiB
Markdown
# Gotlin
|
|
|
|
`Gotlin` is a small Kotlin-like frontend implemented in Go that targets the Go toolchain.
|
|
|
|
This is the practical boundary of the prototype:
|
|
|
|
- It is a Kotlin-flavored language frontend.
|
|
- It targets the Go toolchain by generating valid Go source and building through `go build`.
|
|
- It is not a direct integration into Go's internal `cmd/compile` backend APIs.
|
|
|
|
## Supported language slice
|
|
|
|
- `fun` declarations
|
|
- `val` and `var`
|
|
- `Int`, `String`, `Boolean`, `Unit`
|
|
- function types like `(String) -> Unit`
|
|
- `if`, `else`, `while`
|
|
- function calls
|
|
- lambdas like `{ x: Int -> println(x) }` and `{ println(it) }`
|
|
- `class` with primary-constructor fields and methods
|
|
- `interface` with method signatures
|
|
- `println(...)`
|
|
- arithmetic, comparison, and boolean operators
|
|
|
|
## Example
|
|
|
|
```kotlin
|
|
package demo
|
|
|
|
fun fib(n: Int): Int {
|
|
if (n < 2) {
|
|
return n
|
|
}
|
|
return fib(n - 1) + fib(n - 2)
|
|
}
|
|
|
|
fun main() {
|
|
println(fib(8))
|
|
}
|
|
```
|
|
|
|
Imports from Go packages are supported:
|
|
|
|
```kotlin
|
|
package demo
|
|
|
|
import strings
|
|
|
|
fun main() {
|
|
println(strings.ToUpper("gotlin"))
|
|
}
|
|
```
|
|
|
|
HTTP server example:
|
|
|
|
```kotlin
|
|
package demo.web
|
|
|
|
import fmt
|
|
import net.http
|
|
|
|
fun helloHandler(w: http.ResponseWriter, r: *http.Request) {
|
|
fmt.Fprintln(w, "hello from gotlin")
|
|
}
|
|
|
|
fun main() {
|
|
http.HandleFunc("/", helloHandler)
|
|
fmt.Println("serving http://localhost:8080")
|
|
http.ListenAndServe(":8080", http.DefaultServeMux)
|
|
}
|
|
```
|
|
|
|
Classes and interfaces:
|
|
|
|
```kotlin
|
|
package demo
|
|
|
|
interface Greeter {
|
|
fun greet(name: String): String
|
|
}
|
|
|
|
class ConsoleGreeter(val prefix: String) {
|
|
fun greet(name: String): String {
|
|
return prefix + name
|
|
}
|
|
}
|
|
|
|
fun main() {
|
|
val greeter: Greeter = ConsoleGreeter("hello, ")
|
|
println(greeter.greet("gotlin"))
|
|
}
|
|
```
|
|
|
|
```bash
|
|
go run ./cmd/gotlinc build ./examples/hello.gt
|
|
./hello
|
|
```
|
|
|
|
Emit Go source instead:
|
|
|
|
```bash
|
|
go run ./cmd/gotlinc build -src ./examples/hello.gt -o /tmp/hello.go
|
|
go run /tmp/hello.go
|
|
```
|
|
|
|
Run directly:
|
|
|
|
```bash
|
|
go run ./cmd/gotlinc run ./examples/hello.gt
|
|
```
|
|
|
|
Language server:
|
|
|
|
```bash
|
|
go build -o ./bin/gotlin-lsp ./cmd/gotlin-lsp
|
|
./bin/gotlin-lsp
|
|
```
|
|
|
|
VS Code extension:
|
|
|
|
```bash
|
|
cd ./tools/vscode-gotlin
|
|
npm install
|
|
npm run build
|
|
```
|
|
|
|
## Notes
|
|
|
|
- `val` and `var` currently compile to the same local-variable semantics in Go.
|
|
- Type inference is local to declarations without an explicit type.
|
|
- Top-level declarations currently support functions, classes, and interfaces.
|
|
- `gotlinc build` produces an executable by default. If `-o` is omitted, the output name is derived from the input file name.
|
|
- `gotlinc build -src` emits Go source instead of a binary.
|
|
- `gotlinc` supports `build` and `run`, and defaults to `build` if no subcommand is given.
|
|
- Gotlin source files use the `.gt` extension.
|
|
- `gotlin-lsp` provides diagnostics, hover, and go-to-definition over stdio.
|
|
- `gotlin-lsp` can optionally use `gopls` for hover and definition on Go-imported symbols.
|
|
- the VS Code extension adds syntax highlighting, snippets, and launches the LSP for `.gt` files.
|