-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #550 from smeup/feature/display_file_from_cli
Example: display file from CLI
- Loading branch information
Showing
6 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
examples/src/main/kotlin/com/jariko/samples/DisplayFileFromCLI.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,118 @@ | ||
package com.jariko.samples | ||
|
||
import com.smeup.dspfparser.linesclassifier.DSPFField | ||
import com.smeup.dspfparser.linesclassifier.DSPFFieldType | ||
import com.smeup.rpgparser.execution.Configuration | ||
import com.smeup.rpgparser.execution.DspfConfig | ||
import com.smeup.rpgparser.execution.JarikoCallback | ||
import com.smeup.rpgparser.execution.SimpleDspfConfig | ||
import com.smeup.rpgparser.execution.getProgram | ||
import com.smeup.rpgparser.interpreter.DecimalValue | ||
import com.smeup.rpgparser.interpreter.IntValue | ||
import com.smeup.rpgparser.interpreter.OnExfmtResponse | ||
import com.smeup.rpgparser.interpreter.RuntimeInterpreterSnapshot | ||
import com.smeup.rpgparser.interpreter.StringValue | ||
import com.smeup.rpgparser.interpreter.Value | ||
import com.smeup.rpgparser.rpginterop.DirRpgProgramFinder | ||
import java.io.File | ||
|
||
// UI control functions | ||
|
||
class UnknownVariable(name: String) : Exception("`$name`") | ||
class WrongInputSyntax : Exception("Should be: `VAR1=VALUE;VAR2=23`") | ||
|
||
private fun parseInput(input: String): Map<String, String> { | ||
try { | ||
val assignments = input.split(';') | ||
val variablesAndValues = mutableMapOf<String, String>() | ||
|
||
assignments.forEach { | ||
val tokens = it.split('=') | ||
variablesAndValues[tokens[0]] = tokens[1] | ||
} | ||
|
||
return variablesAndValues | ||
} catch (e: Exception) { | ||
throw WrongInputSyntax() | ||
} | ||
} | ||
|
||
private fun printFields(fields: List<DSPFField>) { | ||
println() | ||
print("OUTPUT fields:\n") | ||
fields.filter { it.type == DSPFFieldType.OUTPUT }.forEach { | ||
println("\t|${it.name}=${(it.value as Value).asString().value}|") | ||
} | ||
println() | ||
print("INPUT fields:\n") | ||
fields.filter { it.type == DSPFFieldType.INPUT }.forEach { | ||
println("\t|${it.name}=${(it.value as Value).asString().value}|") | ||
} | ||
println() | ||
println("Insert input: ") | ||
} | ||
|
||
private fun askInputFor(fields: List<DSPFField>): Map<String, Value> { | ||
val variablesAndValues = mutableMapOf<String, Value>() | ||
val line = readln() | ||
val updatedVariables = parseInput(line) | ||
|
||
updatedVariables.keys.forEach { variable -> | ||
fields.find { field -> field.name == variable } ?: throw UnknownVariable(variable) | ||
} | ||
|
||
fields.filter { it.type == DSPFFieldType.INPUT && updatedVariables[it.name] != null }.forEach { | ||
if (it.isNumeric && it.precision!! == 0) | ||
variablesAndValues[it.name] = IntValue(updatedVariables[it.name]!!.toLong()) | ||
if (it.isNumeric && it.precision!! > 0) | ||
variablesAndValues[it.name] = DecimalValue(updatedVariables[it.name]!!.toBigDecimal()) | ||
else if (!it.isNumeric) | ||
variablesAndValues[it.name] = StringValue(updatedVariables[it.name]!!) | ||
} | ||
|
||
return variablesAndValues | ||
} | ||
|
||
private fun onExfmt(fields: List<DSPFField>, runtimeInterpreterSnapshot: RuntimeInterpreterSnapshot): OnExfmtResponse? { | ||
printFields(fields) | ||
while (true) { | ||
try { | ||
val variablesAndValues = askInputFor(fields) | ||
return OnExfmtResponse(runtimeInterpreterSnapshot, variablesAndValues) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
continue | ||
} | ||
} | ||
} | ||
|
||
// Jariko call setup functions | ||
|
||
private fun createDspfConfig(): DspfConfig { | ||
val simpleDspfConfig = SimpleDspfConfig({ }.javaClass.getResource("/metadata")!!.path) | ||
return DspfConfig( | ||
metadataProducer = simpleDspfConfig::getMetadata, | ||
dspfProducer = simpleDspfConfig::dspfProducer | ||
) | ||
} | ||
|
||
private fun createJarikoCallback(): JarikoCallback { | ||
val jarikoCallback = JarikoCallback() | ||
jarikoCallback.onExfmt = ::onExfmt | ||
return jarikoCallback | ||
} | ||
|
||
private fun createConfig(): Configuration { | ||
return Configuration( | ||
dspfConfig = createDspfConfig(), | ||
jarikoCallback = createJarikoCallback() | ||
) | ||
} | ||
|
||
fun main() { | ||
val programSource = "ADD01.rpgle" | ||
val programFinders = listOf(DirRpgProgramFinder(File({ }.javaClass.getResource("/rpg")!!.path))) | ||
val program = getProgram(nameOrSource = programSource, programFinders = programFinders) | ||
|
||
program.singleCall(emptyList(), configuration = createConfig()) | ||
} |
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 @@ | ||
R FMT01 | ||
A 2 2I 0 0 | ||
B 2 2I 0 0 | ||
RESULT 2 2O 0 0 | ||
PGMCTL 1 0I 0 0 |
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,4 @@ | ||
R FMT01 | ||
STR 10 I 0 0 | ||
TRM 10 O 0 0 | ||
PGMCTL 1 0I 0 0 |
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 @@ | ||
FADD01V CF E WORKSTN USROPN | ||
C PGMCTL DOWEQ 0 | ||
C EXFMT FMT01 | ||
C EVAL RESULT=A+B | ||
C ENDDO |
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 @@ | ||
FTRIM01V CF E WORKSTN USROPN | ||
C PGMCTL DOWEQ 0 | ||
C EXFMT FMT01 | ||
C EVAL TRM=%TRIM(STR) | ||
C ENDDO |
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