-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: packages, calls and more in progress
- Loading branch information
1 parent
0825444
commit ebb191d
Showing
19 changed files
with
192 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
kode-kotlin/src/commonMain/kotlin/software/guimauve/kode/usecases/GenerateKotlinUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package software.guimauve.kode.usecases | ||
|
||
import software.guimauve.kode.dsl.KodeGenerator | ||
import software.guimauve.kode.kotlin.KotlinVisitor | ||
|
||
class GenerateKotlinUseCase : IGenerateCodeUseCase { | ||
|
||
override fun invoke(input: KodeGenerator): String = KotlinVisitor.visit(input) | ||
|
||
} |
57 changes: 40 additions & 17 deletions
57
kode-kotlin/src/commonTest/kotlin/software/guimauve/kode/kotlin/KotlinVisitorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,57 @@ | ||
package software.guimauve.kode.kotlin | ||
|
||
import software.guimauve.kode.dsl.`class` | ||
import software.guimauve.kode.dsl.dataClass | ||
import software.guimauve.kode.dsl.file | ||
import software.guimauve.kode.dsl.function | ||
import software.guimauve.kode.dsl.Package | ||
import software.guimauve.kode.dsl.`package` | ||
import software.guimauve.kode.dsl.type | ||
import software.guimauve.kode.dsl.unaryPlus | ||
import kotlin.test.Test | ||
|
||
class KotlinVisitorTest { | ||
|
||
@Test | ||
fun main() { | ||
file { | ||
`package`("com.example") | ||
imports("java.util.*", "kotlin.*") | ||
`class`("MyClass") { | ||
function("myFunction") { | ||
fun testPackage() { | ||
fun printPackage(pkg: Package) { | ||
println(pkg.name) | ||
println("files: " + pkg.files.map { it.key + " -> " + KotlinVisitor.visit(it.value) }) | ||
pkg.packages.forEach { printPackage(it) } | ||
} | ||
|
||
val basePackage = "com.example" | ||
`package`(basePackage) { | ||
file("Application.kt") { | ||
import("$basePackage.plugins") | ||
import("io.ktor.server.application.*") | ||
import("io.ktor.server.netty.*") | ||
|
||
function("main") { | ||
argument("args", type("Array") { | ||
parameter(type("String")) | ||
}) | ||
call("EngineMain.main") { | ||
parameters(+"args") | ||
} | ||
} | ||
} | ||
dataClass("MyDataClass") { | ||
function("myDataFunction") { | ||
|
||
function("Application.module") { | ||
call("configureI18n") | ||
// ... | ||
} | ||
} | ||
function("main") { | ||
`package`("plugins") { | ||
file("I18n.kt") { | ||
import("dev.kaccelero.plugins.I18n") | ||
import("io.ktor.server.application.*") | ||
import("java.util.*") | ||
|
||
function("Application.configureI18n") { | ||
call("install") { | ||
parameter(+"I18n") | ||
} | ||
} | ||
} | ||
} | ||
}.let { | ||
println(KotlinVisitor.visit(it)) | ||
} | ||
|
||
}.let(::printPackage) | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
kode/src/commonMain/kotlin/software/guimauve/kode/dsl/Call.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package software.guimauve.kode.dsl | ||
|
||
@KodeDsl | ||
class Call internal constructor( | ||
val name: String, | ||
) : Instruction { | ||
|
||
val parameters = mutableListOf<Instruction>() | ||
|
||
fun parameter(instruction: Instruction) = parameters.add(instruction) | ||
fun parameters(vararg instructions: Instruction) = this.parameters.addAll(instructions) | ||
|
||
} |
8 changes: 6 additions & 2 deletions
8
kode/src/commonMain/kotlin/software/guimauve/kode/dsl/Class.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
package software.guimauve.kode.dsl | ||
|
||
@KodeDsl | ||
class Class( | ||
class Class internal constructor( | ||
val name: String, | ||
val data: Boolean = false, | ||
) : Declaration, DeclarationContainer { | ||
|
||
override val declarations = mutableListOf<Declaration>() | ||
val declarations = mutableListOf<Declaration>() | ||
|
||
override fun declaration(declaration: Declaration) { | ||
declarations.add(declaration) | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
kode/src/commonMain/kotlin/software/guimauve/kode/dsl/Declaration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package software.guimauve.kode.dsl | ||
|
||
sealed interface Declaration : KodeGenerator | ||
sealed interface Declaration : Instruction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 9 additions & 2 deletions
11
kode/src/commonMain/kotlin/software/guimauve/kode/dsl/Function.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
package software.guimauve.kode.dsl | ||
|
||
@KodeDsl | ||
class Function(val name: String) : Declaration, DeclarationContainer { | ||
class Function internal constructor(val name: String) : Declaration, InstructionContainer { | ||
|
||
override val declarations = mutableListOf<Declaration>() | ||
val arguments = mutableListOf<Variable>() | ||
val instructions = mutableListOf<Instruction>() | ||
|
||
override fun instruction(instruction: Instruction) { | ||
instructions.add(instruction) | ||
} | ||
|
||
fun argument(name: String, type: Type) = arguments.add(Variable(name, type)) | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
kode/src/commonMain/kotlin/software/guimauve/kode/dsl/Instruction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package software.guimauve.kode.dsl | ||
|
||
sealed interface Instruction : KodeGenerator |
13 changes: 13 additions & 0 deletions
13
kode/src/commonMain/kotlin/software/guimauve/kode/dsl/InstructionContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package software.guimauve.kode.dsl | ||
|
||
interface InstructionContainer : DeclarationContainer { | ||
|
||
fun instruction(instruction: Instruction) | ||
|
||
override fun declaration(declaration: Declaration) = | ||
instruction(declaration) | ||
|
||
fun call(name: String, init: Call.() -> Unit = {}) = | ||
instruction(Call(name).apply(init)) | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
kode/src/commonMain/kotlin/software/guimauve/kode/dsl/Package.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package software.guimauve.kode.dsl | ||
|
||
@KodeDsl | ||
class Package internal constructor( | ||
val name: String, | ||
) { | ||
|
||
val packages = mutableListOf<Package>() | ||
val files = mutableMapOf<String, File>() | ||
|
||
fun `package`(name: String, init: Package.() -> Unit = {}) = | ||
packages.add(Package("${this.name}.$name").apply(init)) | ||
|
||
fun file(name: String, init: File.() -> Unit = {}) = | ||
files.put(name, File().apply(init).apply { `package`(this@Package.name) }) | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
kode/src/commonMain/kotlin/software/guimauve/kode/dsl/Text.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package software.guimauve.kode.dsl | ||
|
||
class Text internal constructor( | ||
val text: String, | ||
) : Instruction |
13 changes: 13 additions & 0 deletions
13
kode/src/commonMain/kotlin/software/guimauve/kode/dsl/Type.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package software.guimauve.kode.dsl | ||
|
||
@KodeDsl | ||
class Type internal constructor( | ||
val name: String, | ||
) : KodeGenerator { | ||
|
||
val parameters = mutableListOf<Type>() | ||
|
||
fun parameter(type: Type) = parameters.add(type) | ||
fun parameters(vararg types: Type) = this.parameters.addAll(types) | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
kode/src/commonMain/kotlin/software/guimauve/kode/dsl/Variable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package software.guimauve.kode.dsl | ||
|
||
@KodeDsl | ||
class Variable internal constructor( | ||
val name: String, | ||
val type: Type, | ||
) : Declaration |
6 changes: 6 additions & 0 deletions
6
kode/src/commonMain/kotlin/software/guimauve/kode/usecases/IGenerateCodeUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package software.guimauve.kode.usecases | ||
|
||
import dev.kaccelero.usecases.IUseCase | ||
import software.guimauve.kode.dsl.KodeGenerator | ||
|
||
interface IGenerateCodeUseCase : IUseCase<KodeGenerator, String> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters