Add JVM part for calculator sample
Simple CLI Kotlin/JVM application making use of Kotlin parser library. Fix #1222
This commit is contained in:
committed by
SvyatoslavScherbina
parent
60046abda0
commit
cf5e6dac80
@@ -23,10 +23,24 @@ The sample consists of:
|
|||||||
framework by invoking Gradle from custom "Run Script" build phase, and this
|
framework by invoking Gradle from custom "Run Script" build phase, and this
|
||||||
framework is imported into the Xcode project.
|
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.
|
The library can also be compiled to a `.jar` by Kotlin/JVM compiler with Gradle.
|
||||||
Just run from the [sample root dir](../):
|
Just run from the [sample root dir](../):
|
||||||
```
|
```
|
||||||
./gradlew calculator:jar
|
./gradlew calculator:jar
|
||||||
```
|
```
|
||||||
This will generate a `calculator.jar` in `build/libs/`.
|
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`)
|
||||||
|
|||||||
@@ -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"
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package org.konan.calculator
|
||||||
|
|
||||||
|
import org.konan.arithmeticparser.*
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
// NOTE: If a new sample uses only platform libs,
|
// NOTE: If a new sample uses only platform libs,
|
||||||
// please add it into the 'buildSamplesWithPlatfromLibs' task in build.gradle.
|
// please add it into the 'buildSamplesWithPlatfromLibs' task in build.gradle.
|
||||||
include ':calculator'
|
include ':calculator'
|
||||||
|
include ':calculator:jvmCliApp'
|
||||||
include ':csvparser'
|
include ':csvparser'
|
||||||
include ':gitchurn'
|
include ':gitchurn'
|
||||||
include ':globalState'
|
include ':globalState'
|
||||||
|
|||||||
Reference in New Issue
Block a user