Skip to content

Commit

Permalink
Setup compiler plugin project with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koperagen committed Nov 29, 2022
1 parent 63c3061 commit f2f7efd
Show file tree
Hide file tree
Showing 102 changed files with 3,104 additions and 711 deletions.
2 changes: 2 additions & 0 deletions bridge-generator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ version = "0.9.0-dev"

repositories {
mavenCentral()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap")
}

dependencies {
Expand All @@ -34,6 +35,7 @@ tasks.withType<JavaCompile> {


kotlinPublications {
fairDokkaJars.set(false)
publication {
publicationName.set("api")
artifactId.set("bridge-generator")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.jetbrains.kotlinx.dataframe.plugin

import generateDfFunctionTestStub
import org.jetbrains.kotlinx.dataframe.KotlinTypeFacade
import org.jetbrains.kotlinx.dataframe.api.add
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf

object AddData {
fun test0(): Pair<PluginDataFrameSchema, PluginDataFrameSchema> {
fun KotlinTypeFacade.test0(): Pair<PluginDataFrameSchema, PluginDataFrameSchema> {
val schemaName = "Add0"
val modify = "add(\"\") { 42 }"
val id = "add0"
Expand All @@ -18,7 +19,7 @@ object AddData {
file = "add.kt"
)
}
private val test0Data = test0()
val test0Schema = test0Data.first
val test0After = test0Data.second
private val KotlinTypeFacade.test0Data get() = test0()
val KotlinTypeFacade.test0Schema get() = test0Data.first
val KotlinTypeFacade.test0After get() = test0Data.second
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.jetbrains.kotlinx.dataframe.plugin

import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.api.cast
import org.jetbrains.kotlinx.dataframe.io.readJson
import org.jetbrains.kotlinx.dataframe.plugin.model.Bridge

val bridges by lazy { DataFrame.readJson("/bridges.json".resource()).cast<Bridge>(verify = true) }

fun String.resource() = object {}.javaClass.getResource(this)!!
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.KotlinTypeFacade
import org.jetbrains.kotlinx.dataframe.api.schema
import org.jetbrains.kotlinx.dataframe.plugin.PluginDataFrameSchema
import org.jetbrains.kotlinx.dataframe.plugin.accept
import org.jetbrains.kotlinx.dataframe.plugin.generateSchemaDeclaration
import org.jetbrains.kotlinx.dataframe.plugin.pluginSchema

internal fun generateDfFunctionTestStub(
internal fun KotlinTypeFacade.generateDfFunctionTestStub(
expression: () -> DataFrame<*>,
schemaName: String,
modify: (DataFrame<*>) -> DataFrame<*>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.jetbrains.kotlinx.dataframe.plugin.codeGen

import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.KotlinTypeFacade
import org.jetbrains.kotlinx.dataframe.api.column
import org.jetbrains.kotlinx.dataframe.api.first
import org.jetbrains.kotlinx.dataframe.plugin.PluginDataFrameSchema
import org.jetbrains.kotlinx.dataframe.plugin.bridges
import org.jetbrains.kotlinx.dataframe.plugin.generateSchemaDeclaration
import org.jetbrains.kotlinx.dataframe.plugin.generateTestCode
import org.jetbrains.kotlinx.dataframe.plugin.model.name
import org.jetbrains.kotlinx.dataframe.plugin.model.type
import org.jetbrains.kotlinx.dataframe.plugin.pluginSchema
import java.util.*

fun KotlinTypeFacade.generateSchemaTestStub(name: String, expression: () -> DataFrame<*>): PluginDataFrameSchema {
val approximation by column<String>()
fun writeInterpreter(s: String) {
// val root = File("/home/nikitak/IdeaProjects/dataframe/core/src/main")
// val schemaRender = File(root, "kotlin/org/jetbrains/kotlinx/dataframe/plugin/testing/schemaRender").also { it.mkdirs() }
// File(schemaRender, "$name.kt").writeText(s)
println(s)
}

fun writeTestStub(s: String) {
// val root = File("/home/nikitak/Downloads/kotlin/plugins/kotlin-dataframe")
// val schemaRender = File(root, "testData/diagnostics/schemaRender").also { it.mkdirs() }
// File(schemaRender, "$name.kt")
println(s)
}

val capitalizedName = name.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }

// val repl = object : JupyterReplTestCase() {
// }
val df = expression()
// val df = repl.execRaw("val df = $expression; df") as DataFrame<*>
val declaration = df.generateSchemaDeclaration(capitalizedName)
val schemaTestCode = df.generateTestCode()
//repl.exec(schemaTestCode)

val pluginSchema = df.pluginSchema()
//val jsonString = pluginSchema.toJson()


bridges.first { it.type.name == "DataFrame<T>" }.run {
writeInterpreter("""
package org.jetbrains.kotlinx.dataframe.plugin.testing.schemaRender
import kotlinx.serialization.decodeFromString
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.annotations.AbstractInterpreter
import org.jetbrains.kotlinx.dataframe.annotations.Arguments
import org.jetbrains.kotlinx.dataframe.annotations.Interpretable
import org.jetbrains.kotlinx.dataframe.plugin.*
@Interpretable(${capitalizedName}::class)
public fun $name(): DataFrame<*> {
return TODO("won't run")
}
public class ${capitalizedName} : AbstractInterpreter<$approximation>() {
override fun Arguments.interpret(): $approximation {
return SchemaData.$name()
}
}
""".trimIndent())
}
val schemaDeclaration = declaration.lineSequence().joinToString("\n|")
writeTestStub("""
|import org.jetbrains.kotlinx.dataframe.*
|import org.jetbrains.kotlinx.dataframe.api.*
|import org.jetbrains.kotlinx.dataframe.annotations.*
|import org.jetbrains.kotlinx.dataframe.plugin.testing.*
|import org.jetbrains.kotlinx.dataframe.plugin.testing.schemaRender.*
|
|/*
|$schemaDeclaration
|*/
|
|internal fun schemaTest() {
| val df = $name()
| ${schemaTestCode}
|}
""".trimMargin())

return pluginSchema
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jetbrains.kotlinx.dataframe.plugin.model

import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
import org.jetbrains.kotlinx.dataframe.api.DataRowSchema

@DataSchema
class Bridge(val type: Type,
val approximation: String,
val converter: String,
val lens: String,
val supported: Boolean = false) : DataRowSchema
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.jetbrains.kotlinx.dataframe.plugin.model

import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
import org.jetbrains.kotlinx.dataframe.annotations.GenerateConstructor
import org.jetbrains.kotlinx.dataframe.api.DataRowSchema
import org.jetbrains.kotlinx.dataframe.plugin.model.Parameter

@DataSchema
interface ClassDeclaration : DataRowSchema {
val name: String
val parameters: List<Parameter>

@GenerateConstructor
companion object
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.jetbrains.kotlinx.dataframe.plugin.model

import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
import org.jetbrains.kotlinx.dataframe.api.DataRowSchema

@DataSchema
class Function(
val receiverType: String,
val function: String,
val functionReturnType: Type,
val parameters: List<Parameter>
) : DataRowSchema {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jetbrains.kotlinx.dataframe.plugin.model

import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
import org.jetbrains.kotlinx.dataframe.api.DataRowSchema

@DataSchema
class Parameter(
val name: String,
val returnType: Type,
val defaultValue: String?,
) : DataRowSchema
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.jetbrains.kotlinx.dataframe.plugin.model

import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
import org.jetbrains.kotlinx.dataframe.api.DataRowSchema

@DataSchema
class RefinedFunction(
val receiverType: String,
val function: String,
val functionReturnType: Type,
val parameters: List<Parameter>,
val startingSchema: Parameter
) : DataRowSchema
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jetbrains.kotlinx.dataframe.plugin.model

import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
import org.jetbrains.kotlinx.dataframe.annotations.GenerateConstructor
import org.jetbrains.kotlinx.dataframe.api.DataRowSchema

@DataSchema
interface TestCase : DataRowSchema {
val dfExpression: String
val functionCalls: List<String>

@GenerateConstructor
companion object
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.jetbrains.kotlinx.dataframe.plugin.model

import org.jetbrains.kotlinx.dataframe.annotations.DataSchema

@DataSchema
data class Type(val name: String, val vararg: Boolean)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jetbrains.kotlinx.dataframe.plugin

import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.KotlinTypeFacade
import org.jetbrains.kotlinx.dataframe.api.columnOf
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
import org.jetbrains.kotlinx.dataframe.io.readJson
import org.jetbrains.kotlinx.dataframe.plugin.codeGen.generateSchemaTestStub

object SchemaData {
fun KotlinTypeFacade.schema1(): PluginDataFrameSchema = generateSchemaTestStub(
name = "schema1"
) {
DataFrame.readJson("/functions.json".resource())
}

fun KotlinTypeFacade.schema2(): PluginDataFrameSchema = generateSchemaTestStub(
name = "schema2"
) {
val name by columnOf("name")
val returnType by columnOf("")
val df = dataFrameOf(name, returnType)
val functions by columnOf(df)
val function by columnOf(name, returnType)
val nestedGroup by columnOf(name)
val group by columnOf(nestedGroup)
dataFrameOf(name, functions, function, group)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jetbrains.kotlinx.dataframe.plugin.testing.schemaRender

import kotlinx.serialization.decodeFromString
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.annotations.AbstractInterpreter
import org.jetbrains.kotlinx.dataframe.annotations.Arguments
import org.jetbrains.kotlinx.dataframe.annotations.Interpretable
import org.jetbrains.kotlinx.dataframe.plugin.PluginDataFrameSchema
import org.jetbrains.kotlinx.dataframe.plugin.SchemaData.schema1
import org.jetbrains.kotlinx.dataframe.plugin.pluginJsonFormat

@Interpretable(Schema1::class)
public fun schema1(): DataFrame<*> {
return TODO("won't run")
}

public class Schema1 : AbstractInterpreter<PluginDataFrameSchema>() {
override fun Arguments.interpret(): PluginDataFrameSchema {
return schema1()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jetbrains.kotlinx.dataframe.plugin.testing.schemaRender

import kotlinx.serialization.decodeFromString
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.annotations.AbstractInterpreter
import org.jetbrains.kotlinx.dataframe.annotations.Arguments
import org.jetbrains.kotlinx.dataframe.annotations.Interpretable
import org.jetbrains.kotlinx.dataframe.plugin.*
import org.jetbrains.kotlinx.dataframe.plugin.SchemaData.schema2

@Interpretable(Schema2::class)
public fun schema2(): DataFrame<*> {
return TODO("won't run")
}

public class Schema2 : AbstractInterpreter<PluginDataFrameSchema>() {
override fun Arguments.interpret(): PluginDataFrameSchema {
return schema2()
}
}
Loading

0 comments on commit f2f7efd

Please sign in to comment.