-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomp.kt
More file actions
executable file
·96 lines (81 loc) · 3.1 KB
/
comp.kt
File metadata and controls
executable file
·96 lines (81 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env kscript
//DEPS org.jetbrains.kotlin:kotlin-compiler-embeddable:1.1.3-2, com.beust:klaxon:0.30
import org.jetbrains.kotlin.com.intellij.psi.PsiFileFactory
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.psi.*
import com.beust.klaxon.*
val project = KotlinCoreEnvironment.createForProduction(
Disposer.newDisposable(),
CompilerConfiguration(),
EnvironmentConfigFiles.JVM_CONFIG_FILES
).project
val psiFactory = PsiFileFactory.getInstance(project)
//print("File: "); val rawPath = readLine()!!
val ktFile = psiFactory.createFileFromText("test.kt", KotlinLanguage.INSTANCE, """
package a
import a.b.c
fun main(args: Array<String>) {
println("hello")
}
fun test(): String = "ayy"
fun asdf(arg: String, defArg: Boolean = false) = ""
class A {
}
interface B
val a = "test"
""") as KtFile
abstract class NamedType(open val name: String) {
abstract fun toJson(): JsonObject
override fun toString() = toJson().toJsonString(true)
}
data class Argument(override val name: String, val type: String, val default: String, val mutable: Boolean = true): NamedType(name) {
override fun toJson() = JSON().obj(
"name" to name,
"type" to type,
"default" to default,
"mutable" to mutable
)
override fun toString() = super.toString() // Data class stuff
}
data class Function(override val name: String, val args: Array<Argument>, val returnType: String, val documentation: String = ""): NamedType(name) {
override fun toJson() = JSON().obj(
"name" to name,
"args" to JSON().array(args.map(Argument::toJson)),
"returns" to returnType,
"documentation" to documentation
)
override fun toString() = super.toString() // Data class stuff
}
println("Package Name: ${ktFile.getPackageFqName().asString()}")
val functions = mutableListOf<Function>()
ktFile.getDeclarations().forEach {
when (it) {
is KtFunction -> {
val func = Function(
name = it.getNameIdentifier()?.text ?: "ERROR",
args = it.getValueParameters().map {
Argument(
name = it.name ?: "ERROR",
type = it.getTypeReference()?.text ?: "ERROR",
default = it.getDefaultValue()?.text ?: "<NO DEFAULT>"
)
}.toTypedArray(),
returnType = when {
it.hasDeclaredReturnType() -> it.getTypeReference()?.text ?: "ERROR"
it.hasBlockBody() -> "Unit" // No explicit type + body = Unit
else -> "?" // TODO
},
documentation = ""
)
functions.add(func)
}
else -> println("[WARN] Can't process ${it::class.simpleName?.removePrefix("Kt")?.toLowerCase()?.reversed()?.replaceFirst("y", "ei")?.reversed() ?: "ERROR"}s yet")
}
}
println(json {
obj("functions" to array(functions.map(Function::toJson)))
}.toJsonString(true))