diff --git a/samples/calculator/README.md b/samples/calculator/README.md index 5a6f1a765d9..d21b774dd12 100644 --- a/samples/calculator/README.md +++ b/samples/calculator/README.md @@ -23,10 +23,24 @@ The sample consists of: framework by invoking Gradle from custom "Run Script" build phase, and this framework is imported into the Xcode project. - +## Using the same code with Kotlin/JVM + The library can also be compiled to a `.jar` by Kotlin/JVM compiler with Gradle. Just run from the [sample root dir](../): ``` ./gradlew calculator:jar ``` This will generate a `calculator.jar` in `build/libs/`. + +There is also simple Kotlin/JVM CLI app available in `jvmCliApp` subdirectory. +To build and run it, use +``` +./gradlew calculator:jvmCliApp:run +``` + +To build the distribution: +``` +./gradlew calculator:jvmCliApp:distZip +``` +(the result will be available as +`jvmCliApp/build/distributions/KotlinCalculator.zip`) diff --git a/samples/calculator/jvmCliApp/build.gradle b/samples/calculator/jvmCliApp/build.gradle new file mode 100644 index 00000000000..38d41c3fe4e --- /dev/null +++ b/samples/calculator/jvmCliApp/build.gradle @@ -0,0 +1,24 @@ +buildscript { + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +apply plugin: 'kotlin' +apply plugin: 'application' + +repositories { + mavenCentral() +} + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib" + compile project.parent +} + +applicationName = "KotlinCalculator" + +mainClassName = 'org.konan.calculator.JvmCliKt' +run { + args "2 + 2" +} diff --git a/samples/calculator/jvmCliApp/src/main/kotlin/org/konan/calculator/JvmCli.kt b/samples/calculator/jvmCliApp/src/main/kotlin/org/konan/calculator/JvmCli.kt new file mode 100644 index 00000000000..124b0c9378d --- /dev/null +++ b/samples/calculator/jvmCliApp/src/main/kotlin/org/konan/calculator/JvmCli.kt @@ -0,0 +1,25 @@ +package org.konan.calculator + +import org.konan.arithmeticparser.* + +fun main(args: Array) { + val expression = if (args.isNotEmpty()) { + args.first().also { + println(it) + } + } else { + println("Enter an expression:") + readLine()!! + } + + val result = parseAndCompute(expression) + val computed = result.expression + if (computed != null) { + println(" = $computed") + } else { + println(" = ${result.partialExpression}") + result.remainder?.let { + println("Unable to parse suffix: $it") + } + } +} diff --git a/samples/settings.gradle b/samples/settings.gradle index 5341e8ca20a..522fbcf99f6 100644 --- a/samples/settings.gradle +++ b/samples/settings.gradle @@ -1,6 +1,7 @@ // NOTE: If a new sample uses only platform libs, // please add it into the 'buildSamplesWithPlatfromLibs' task in build.gradle. include ':calculator' +include ':calculator:jvmCliApp' include ':csvparser' include ':gitchurn' include ':globalState'