From fffa3d31bb799c0be11fb0bf366e266c91944d2c Mon Sep 17 00:00:00 2001 From: sergeych Date: Tue, 10 Jun 2025 02:06:03 +0400 Subject: [PATCH] - rewritten/fixed buildconfig - less noise - cosmetics in CLI tool --- docs/OOP.md | 71 ++++++------ library/build.gradle.kts | 101 +++++++++++------- .../kotlin/net/sergeych/lyng/Compiler.kt | 2 - .../kotlin/net/sergeych/lyng/ObjClass.kt | 2 - .../kotlin/net/sergeych/lyng/globals.kt | 3 +- lyng/src/commonMain/kotlin/Common.kt | 2 +- .../kotlin/net/sergeych/lyng_cli/Main.kt | 4 +- lyng/src/linuxX64Main/kotlin/Main.kt | 4 +- 8 files changed, 103 insertions(+), 86 deletions(-) diff --git a/docs/OOP.md b/docs/OOP.md index d87d6f6..805f266 100644 --- a/docs/OOP.md +++ b/docs/OOP.md @@ -1,6 +1,40 @@ # OO implementation in Lyng -Basic principles: +Short introduction + + class Point(x,y) { + fun length() { sqrt(x*x + y*y) } + } + + assert( Point is Class ) + val p = Point(3,4) + assert(p is Point) + assertEquals(5, p.length()) + + // we can access the fields: + assert( p.x == 3 ) + assert( p.y == 4 ) + + // we can assign new values to fields: + p.x = 1 + p.y = 1 + assertEquals(sqrt(2), p.length()) + >>> void + + +Let's see in details. The statement `class Point(x,y)` creates a class, +with two field, which are mutable and publicly visible.`(x,y)` here +is the [argument list], same as when defining a function. All together creates a class with +a _constructor_ that requires two parameters for fields. So when creating it with +`Point(10, 20)` we say _calling Point constructor_ with these parameters. + +Form now on `Point` is a class, it's type is `Class`, and we can create instances with it as in the +example above. + +Class point has a _method_, or a _member function_ `length()` that uses its _fields_ `x` and `y` to +calculate the magnitude. + +## Basic principles: - Everything is an instance of some class - Every class except Obj has at least one parent @@ -71,41 +105,6 @@ Regular methods are called on instances as usual `instance.method()`. The method 2. parents method: no guarantee but we enumerate parents in order of appearance; 3. possible extension methods (scoped) -# Defining a new class - -The class is a some data record with named fields and fixed order, in fact. To define a class, -just Provide a name and a record like this: - - // creating new class with main constructor - // with all fields public and mutable: - - struct Point(x,y) - assert( Point is Class ) - - // now we can create instance - val p1 = Point(3,4) - - // is is of the newly created type: - assert( p1 is Point ) - - // we can read and write its fields: - assert( p1.x == 3 ) - assert( p1.y == 4 ) - - p1.y++ - assert( p1.y == 5 ) - - >>> void - -Let's see in details. The statement `struct Point(x,y)` creates a struct, or public class, -with two field, which are mutable and publicly visible, because it is _struct_. `(x,y)` here -is the [argument list], same as when defining a function. All together creates a class with -a _constructor_ that requires two parameters for fields. So when creating it with -`Point(10, 20)` we say _calling Point constructor_ with these parameters. - -Such declaration is identical to `class Point(var x,var y)` which does exactly the same. - - TBD [argument list](declaring_arguments.md) \ No newline at end of file diff --git a/library/build.gradle.kts b/library/build.gradle.kts index a5ce5aa..17141d8 100644 --- a/library/build.gradle.kts +++ b/library/build.gradle.kts @@ -1,17 +1,40 @@ +import com.codingfeline.buildkonfig.compiler.FieldSpec.Type.STRING import com.vanniktech.maven.publish.SonatypeHost import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl import org.jetbrains.kotlin.gradle.dsl.JvmTarget +group = "net.sergeych" +version = "0.2.1-SNAPSHOT" + +buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0") + classpath("com.codingfeline.buildkonfig:buildkonfig-gradle-plugin:latest_version") + } +} + plugins { alias(libs.plugins.kotlinMultiplatform) alias(libs.plugins.androidLibrary) alias(libs.plugins.vanniktech.mavenPublish) kotlin("plugin.serialization") version "2.1.20" + id("com.codingfeline.buildkonfig") version "0.17.1" } -group = "net.sergeych" -version = "0.2.0-SNAPSHOT" +buildkonfig { + packageName = "net.sergeych.lyng" + // objectName = "YourAwesomeConfig" + // exposeObjectWithName = "YourAwesomePublicConfig" + + defaultConfigs { + buildConfigField(STRING, "bcprovider", "codingfeline") + buildConfigField(STRING, "version", version.toString()) + } +} kotlin { jvm() @@ -110,40 +133,40 @@ mavenPublishing { } } } - -val projectVersion by project.extra(provider { - // Compute value lazily - (version as String) -}) - -val generateBuildConfig by tasks.registering { - // Declare outputs safely - val outputDir = layout.buildDirectory.dir("generated/buildConfig/commonMain/kotlin") - outputs.dir(outputDir) - - val version = projectVersion.get() - - // Inputs: Version is tracked as an input - inputs.property("version", version) - - doLast { - val packageName = "net.sergeych.lyng.buildconfig" - val packagePath = packageName.replace('.', '/') - val buildConfigFile = outputDir.get().file("$packagePath/BuildConfig.kt").asFile - - buildConfigFile.parentFile?.mkdirs() - buildConfigFile.writeText( - """ - |package $packageName - | - |object BuildConfig { - | const val VERSION = "$version" - |} - """.trimMargin() - ) - } -} - -tasks.withType().configureEach { - dependsOn(generateBuildConfig) -} +// +//val projectVersion by project.extra(provider { +// // Compute value lazily +// (version as String) +//}) +// +//val generateBuildConfig by tasks.registering { +// // Declare outputs safely +// val outputDir = layout.buildDirectory.dir("generated/buildConfig/commonMain/kotlin") +// outputs.dir(outputDir) +// +// val version = projectVersion.get() +// +// // Inputs: Version is tracked as an input +// inputs.property("version", version) +// +// doLast { +// val packageName = "net.sergeych.lyng.buildconfig" +// val packagePath = packageName.replace('.', '/') +// val buildConfigFile = outputDir.get().file("$packagePath/BuildConfig.kt").asFile +// +// buildConfigFile.parentFile?.mkdirs() +// buildConfigFile.writeText( +// """ +// |package $packageName +// | +// |object BuildConfig { +// | const val VERSION = "$version" +// |} +// """.trimMargin() +// ) +// } +//} +// +//tasks.withType().configureEach { +// dependsOn(generateBuildConfig) +//} diff --git a/library/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt b/library/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt index 3a8bbf7..941ad37 100644 --- a/library/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt +++ b/library/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt @@ -745,7 +745,6 @@ class Compiler( bodyInit?.execute(this) // export public for( (name,record) in objects ) { - println("-- $name $record") when(record.visibility) { Visibility.Public -> { thisObj.publicFields += name @@ -770,7 +769,6 @@ class Compiler( f.name, statement { val context = (thisObj as ObjInstance).instanceContext - println("called on $thisObj") context[f.name]?.value ?: raiseError("field is not initialized: ${f.name}") }, true, diff --git a/library/src/commonMain/kotlin/net/sergeych/lyng/ObjClass.kt b/library/src/commonMain/kotlin/net/sergeych/lyng/ObjClass.kt index 6dfab9c..7ff3570 100644 --- a/library/src/commonMain/kotlin/net/sergeych/lyng/ObjClass.kt +++ b/library/src/commonMain/kotlin/net/sergeych/lyng/ObjClass.kt @@ -23,8 +23,6 @@ class ObjClass( override suspend fun compareTo(context: Context, other: Obj): Int = if (other === this) 0 else -1 override suspend fun callOn(context: Context): Obj { - println("callOn $this constructing....") - println("on context: $context") val instance = ObjInstance(this) instance.instanceContext = context.copy(newThisObj = instance,args = context.args) if (instanceConstructor != null) { diff --git a/library/src/commonMain/kotlin/net/sergeych/lyng/globals.kt b/library/src/commonMain/kotlin/net/sergeych/lyng/globals.kt index 0623503..15809ea 100644 --- a/library/src/commonMain/kotlin/net/sergeych/lyng/globals.kt +++ b/library/src/commonMain/kotlin/net/sergeych/lyng/globals.kt @@ -1,5 +1,4 @@ package net.sergeych.lyng -import net.sergeych.lyng.buildconfig.BuildConfig -val LyngVersion = BuildConfig.VERSION \ No newline at end of file +val LyngVersion = BuildKonfig.version \ No newline at end of file diff --git a/lyng/src/commonMain/kotlin/Common.kt b/lyng/src/commonMain/kotlin/Common.kt index a1b7b9e..adeaf8e 100644 --- a/lyng/src/commonMain/kotlin/Common.kt +++ b/lyng/src/commonMain/kotlin/Common.kt @@ -24,7 +24,7 @@ val baseContext = Context().apply { } } -class LyngCLI(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand() { +class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand() { override val printHelpOnEmptyArgs = true diff --git a/lyng/src/jvmMain/kotlin/net/sergeych/lyng_cli/Main.kt b/lyng/src/jvmMain/kotlin/net/sergeych/lyng_cli/Main.kt index 933c422..e639d54 100644 --- a/lyng/src/jvmMain/kotlin/net/sergeych/lyng_cli/Main.kt +++ b/lyng/src/jvmMain/kotlin/net/sergeych/lyng_cli/Main.kt @@ -2,8 +2,8 @@ package net.sergeych.lyng_cli import com.github.ajalt.clikt.core.main import kotlinx.coroutines.runBlocking -import net.sergeych.LyngCLI +import net.sergeych.Lyng fun main(args: Array) { - LyngCLI({ runBlocking { it() } }).main(args) + Lyng({ runBlocking { it() } }).main(args) } \ No newline at end of file diff --git a/lyng/src/linuxX64Main/kotlin/Main.kt b/lyng/src/linuxX64Main/kotlin/Main.kt index 86c5caa..81d9243 100644 --- a/lyng/src/linuxX64Main/kotlin/Main.kt +++ b/lyng/src/linuxX64Main/kotlin/Main.kt @@ -1,7 +1,7 @@ import com.github.ajalt.clikt.core.main import kotlinx.coroutines.runBlocking -import net.sergeych.LyngCLI +import net.sergeych.Lyng fun main(args: Array) { - LyngCLI( { runBlocking { it() } }).main(args) + Lyng( { runBlocking { it() } }).main(args) } \ No newline at end of file