Skip to content

Commit 6eeca99

Browse files
authored
Merge pull request #32 from javecs/feature/template-reply
変数を表示するテンプレートを作成しました。
2 parents 95f86cc + 261f544 commit 6eeca99

5 files changed

Lines changed: 32 additions & 5 deletions

File tree

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ plugins {
1616
}
1717

1818
group 'xyz.javecs.tools'
19-
version '0.1.0'
19+
version '0.1.1'
2020

2121
apply plugin: 'kotlin'
2222
apply plugin: 'antlr'
@@ -36,10 +36,10 @@ repositories {
3636

3737
dependencies {
3838
antlr "org.antlr:antlr4:$antlr_version"
39-
compile "xyz.javecs.tools:expr:0.2.3"
39+
compile "xyz.javecs.tools:expr:0.2.4"
4040
compile "com.google.guava:guava:21.0"
4141
compile "com.atilika.kuromoji:kuromoji-ipadic:0.9.0"
42-
compile "org.antlr:stringtemplate:4.0.2"
42+
compile "org.antlr:ST4:4.0.8"
4343
compile "org.reflections:reflections:0.9.11"
4444
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
4545
testCompile 'junit:junit:4.12'

src/main/kotlin/xyz/javecs/tools/text2expr/Text2Expr.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class Text2Expr(rulePath: String = "rules") {
1111
private val calc = Calculator()
1212

1313
init {
14-
resources(rulePath).forEach { rules.add(RuleBuilder(read(it))) }
14+
val reply = read("templates/reply.st")
15+
resources(rulePath).forEach { rules.add(RuleBuilder(read(it), reply)) }
1516
}
1617

1718
fun eval(text: String, rendered: Boolean = false): String {

src/main/kotlin/xyz/javecs/tools/text2expr/parsers/RuleBuilder.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import xyz.javecs.tools.text2expr.utils.normalize
99
private val tokenizer = Tokenizer()
1010
private val printValue = "これかな?\n<value>"
1111

12+
data class Variable(val key:String, val value:Double)
1213
data class Evaluation(val value: Number = Double.NaN, val rendered: String = "")
1314
class RuleBuilder(source: String, template: String = printValue) {
1415
private val parser = RuleParser()
@@ -63,6 +64,9 @@ class RuleBuilder(source: String, template: String = printValue) {
6364
return if (matches(norm, { (key, value) -> args.add("$key = $value") })) {
6465
args.forEach { calc.eval(it) }
6566
expr().forEach { calc.eval(it) }
67+
renderer.add("variables", calc.variables().map { Variable(it.key, it.value) }.toList())
68+
renderer.add("expr", expr())
69+
renderer.add("text", text)
6670
renderer.add("value", calc.value)
6771
Evaluation(calc.value, renderer.render())
6872
} else {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<if(variables)>
2+
<variables:{v|<v.key> = <v.value>
3+
}>
4+
<endif>
5+
<if(expr)>
6+
こうだから、
7+
<expr:{e|<e>
8+
}>
9+
<endif>
10+
答えは、
11+
<value>

src/test/kotlin/xyz/javecs/tools/text2expr/test/kotlin/Text2ExprTest.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@ class Text2ExprTest {
1818
}
1919

2020
@Test fun eval2() {
21+
val reply = """
22+
|x = 1.0
23+
|y = 1.0
24+
|
25+
|こうだから、
26+
|x+y
27+
|
28+
|答えは、
29+
|2
30+
|
31+
""".trimMargin("|")
2132
assertEquals("2", Text2Expr().eval("1足す1"))
22-
assertEquals("これかな?\n2", Text2Expr().eval("1足す1", rendered = true))
33+
assertEquals(reply, Text2Expr().eval("1足す1", rendered = true))
2334
}
2435

2536
}

0 commit comments

Comments
 (0)