Skip to content

Commit

Permalink
adding comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Jun 30, 2023
1 parent f828271 commit 5dd8db7
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 67 deletions.
7 changes: 5 additions & 2 deletions lionweb-gen-gradle/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Gradle plugin to generate Kotlin classes from LionWeb languages.
Depends on lionweb-gen.
This plugin introduce support for LIonWeb in Kolasu projects.

It permits to:
* Generate Kolasu AST classes froma LIonWeb language definition. This is useful when using a language defined by a LIonweb compatible tool different from Kolasu
* Generate a LIonWeb language definition from Kolasu AST classes. This is useful to make a language defined in Kolasu available to other LIonWeb compatible tools.
4 changes: 1 addition & 3 deletions lionweb-gen-gradle/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
plugins {
id("java-gradle-plugin")
//id("org.gradle.kotlin.kotlin-dsl") version "4.0.14"
id 'org.jetbrains.kotlin.jvm'
id("com.github.gmazzo.buildconfig") version "3.1.0"
id("maven-publish")
Expand All @@ -18,7 +17,6 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")
implementation("com.google.code.gson:gson:$gson_version")
implementation("com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:$kotlin_version-$kspVersion")

}

buildConfig {
Expand Down Expand Up @@ -46,4 +44,4 @@ tasks.named("compileKotlin") {

test {
useJUnitPlatform()
}
}
2 changes: 1 addition & 1 deletion lionweb-gen/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Logic to generate Kotlin classes from LionWeb languages.
Logic to generate Kolasu AST classes from LionWeb languages.
11 changes: 2 additions & 9 deletions lionweb-gen/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,9 @@ dependencies {
}
}

implementation("com.google.devtools.ksp:symbol-processing-api:$kotlin_version-$kspVersion")

// We use the embeddable version because it avoids issues with conflicting versions of dependencies
// that we have when using the normal one
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlin_version")

// implementation("org.jetbrains.kotlin:kotlin-compiler:$kotlin_version") {
// exclude group: "org.jetbrains.kotlin", module: "kotlin-build-common"
// exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib"
// exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib-jdk8"
// }

}

publishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,11 @@ import io.lionweb.lioncore.java.language.Reference

data class KotlinFile(val path: String, val code: String)

/**
* This class generates Kotlin code for a given LIonWeb Language.
*/
class ASTGenerator(val packageName: String, val language: Language) {

private fun typeName(featuresContainer: FeaturesContainer<*>): TypeName {
return when {
featuresContainer.id == StarLasuLWLanguage.ASTNode.id -> {
Node::class.java.asTypeName()
}
featuresContainer.language == this.language -> {
ClassName.bestGuess("$packageName.${featuresContainer.name}")
}
else -> {
TODO()
}
}
}

private fun typeName(dataType: DataType<*>): TypeName {
return when (dataType) {
LionCoreBuiltins.getString() -> {
ClassName.bestGuess("kotlin.String")
}
LionCoreBuiltins.getBoolean() -> {
Boolean::class.java.asTypeName()
}
else -> {
TODO("DataType: $dataType")
}
}
}

fun generateClasses(existingKotlinClasses: Set<String> = emptySet()): Set<KotlinFile> {
val fileSpecBuilder = FileSpec.builder(packageName, "${language.name}AST.kt")
language.elements.forEach { element ->
Expand Down Expand Up @@ -116,4 +91,32 @@ class ASTGenerator(val packageName: String, val language: Language) {
val file = KotlinFile(path = "ast.kt", fileSpecBuilder.build().toString())
return setOf(file)
}

private fun typeName(featuresContainer: FeaturesContainer<*>): TypeName {
return when {
featuresContainer.id == StarLasuLWLanguage.ASTNode.id -> {
Node::class.java.asTypeName()
}
featuresContainer.language == this.language -> {
ClassName.bestGuess("$packageName.${featuresContainer.name}")
}
else -> {
TODO()
}
}
}

private fun typeName(dataType: DataType<*>): TypeName {
return when (dataType) {
LionCoreBuiltins.getString() -> {
ClassName.bestGuess("kotlin.String")
}
LionCoreBuiltins.getBoolean() -> {
Boolean::class.java.asTypeName()
}
else -> {
TODO("DataType: $dataType")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ private val KtDeclaration.fqName: String
"$packageName.$name"
}
}

/**
* This class finds Kotlin classes definitions.
*/
class KotlinCodeProcessor {

fun astClassesDeclaredInFile(code: String): Set<String> {
Expand Down

This file was deleted.

31 changes: 31 additions & 0 deletions lionweb-ksp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
This is a ksp processor (i.e., a lightweight compiler plugin) that find all AST classes and generate the Kotlin code
for the corresponding Language definition.

For example, it could generate something like this:

```
package com.strumenta.math
import com.strumenta.kolasu.lionweb.LanguageGeneratorCommand
import com.strumenta.kolasu.lionweb.KolasuLanguage
import com.strumenta.kolasu.lionweb.LionWebLanguageExporter
import io.lionweb.lioncore.java.language.Language
val kLanguage = KolasuLanguage("com.strumenta.math").apply {
addClass(Expression::class)
addClass(BinaryExpression::class)
addClass(SumExpression::class)
addClass(SubExpression::class)
addClass(IntLiteralExpr::class)
}
val lwLanguage: Language by lazy {
val importer = LionWebLanguageExporter()
importer.export(kLanguage)
}
fun main(args: Array<String>) {
LanguageGeneratorCommand(lwLanguage).main(args)
}
```

0 comments on commit 5dd8db7

Please sign in to comment.