Skip to content

Commit

Permalink
switch条件の整形処理を実装
Browse files Browse the repository at this point in the history
resolved #3
  • Loading branch information
love2hina-net committed Aug 13, 2021
1 parent 6a3df4a commit 414f05c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
46 changes: 30 additions & 16 deletions src/main/kotlin/net/love2hina/kotlin/sharon/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,11 @@ internal class Parser(val file: File) {
n.thrownExceptions.forEach { it.accept(this, arg) }

// ステートメント
n.body.ifPresent { it.accept(this, arg) }
n.body.ifPresent {
writer.writeStartElement("code")
it.accept(this, arg)
writer.writeEndElement()
}

writer.writeEndElement()
}
Expand Down Expand Up @@ -469,8 +473,6 @@ internal class Parser(val file: File) {
override fun visit(n: BlockStmt?, arg: Void?) {
n!!

writer.writeStartElement("block")

val positionUnknown = Position(Int.MAX_VALUE, 0)
val rangeUnknown = Range(positionUnknown, positionUnknown)

Expand All @@ -480,8 +482,6 @@ internal class Parser(val file: File) {
x.range.orElse(rangeUnknown).begin.line,
y.range.orElse(rangeUnknown).begin.line)
}.forEach { it.accept(this, arg) }

writer.writeEndElement()
}

override fun visit(n: ExpressionStmt?, arg: Void?) {
Expand Down Expand Up @@ -605,7 +605,9 @@ internal class Parser(val file: File) {
writer.writeStrings(n.condition.toString())
writer.writeEndElement()
// 本文
writer.writeStartElement("code")
n.thenStmt.accept(this, arg)
writer.writeEndElement()

writer.writeEndElement()

Expand All @@ -632,7 +634,9 @@ internal class Parser(val file: File) {
writer.writeStrings(e.condition.toString())
writer.writeEndElement()
// 本文
writer.writeStartElement("code")
e.thenStmt.accept(this, arg)
writer.writeEndElement()

writer.writeEndElement()

Expand All @@ -646,7 +650,9 @@ internal class Parser(val file: File) {
// コメント
e.comment.ifPresent { it.accept(this, arg) }
// 本文
writer.writeStartElement("code")
e.accept(this, arg)
writer.writeEndElement()

writer.writeEndElement()
}
Expand All @@ -659,14 +665,14 @@ internal class Parser(val file: File) {
override fun visit(n: SwitchStmt?, arg: Void?) {
n!!

// コメント
n.comment.ifPresent { it.accept(this, arg) }

// 条件分岐の出力
writer.writeStartElement("condition")
writer.writeAttribute("type", "switch")
writer.writeAttribute("selector", n.selector.toString())

// コメント
n.comment.ifPresent { it.accept(this, arg) }

var caseContinue = false

// 条件エントリ
Expand All @@ -683,7 +689,9 @@ internal class Parser(val file: File) {
writer.writeEndElement()
}
// 本文
writer.writeStartElement("code")
i.statements.forEach { it.accept(this, arg) }
writer.writeEndElement()

caseContinue = i.statements.isEmpty()
if (!caseContinue)
Expand Down Expand Up @@ -728,7 +736,9 @@ internal class Parser(val file: File) {
writer.writeAttribute("expr", it.toString())
}
// 本文
writer.writeStartElement("code")
n.body.accept(this, arg)
writer.writeEndElement()

writer.writeEndElement()
}
Expand All @@ -751,7 +761,9 @@ internal class Parser(val file: File) {
writer.writeEmptyElement("variable")
writer.writeAttribute("expression", n.variable.toString())
// 本文
writer.writeStartElement("code")
n.body.accept(this, arg)
writer.writeEndElement()

writer.writeEndElement()
}
Expand All @@ -771,7 +783,9 @@ internal class Parser(val file: File) {
writer.writeEmptyElement("condition")
writer.writeAttribute("expr", n.condition.toString())
// 本文
writer.writeStartElement("code")
n.body.accept(this, arg)
writer.writeEndElement()

writer.writeEndElement()
}
Expand All @@ -791,7 +805,9 @@ internal class Parser(val file: File) {
writer.writeEmptyElement("condition")
writer.writeAttribute("expr", n.condition.toString())
// 本文
writer.writeStartElement("code")
n.body.accept(this, arg)
writer.writeEndElement()

writer.writeEndElement()
}
Expand All @@ -802,15 +818,14 @@ internal class Parser(val file: File) {
override fun visit(n: BreakStmt?, arg: Void?) {
n!!

// コメント
n.comment.ifPresent { it.accept(this, arg) }

writer.writeStartElement("break")
// ラベル
n.label.ifPresent {
writer.writeAttribute("label", it.asString())
}

// コメント
n.comment.ifPresent { it.accept(this, arg) }

writer.writeEndElement()
}

Expand All @@ -820,15 +835,14 @@ internal class Parser(val file: File) {
override fun visit(n: ContinueStmt?, arg: Void?) {
n!!

// コメント
n.comment.ifPresent { it.accept(this, arg) }

writer.writeStartElement("continue")
// ラベル
n.label.ifPresent {
writer.writeAttribute("label", it.asString())
}

// コメント
n.comment.ifPresent { it.accept(this, arg) }

writer.writeEndElement()
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/powershell/ControlStatement.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class CodesControl : ControlStatement {
[void] Output([DocumentWriter] $docWriter, $target) {
$this.beginTransaction($docWriter)

$nodes = $target.node.Evaluate('block/node()')
$nodes = $target.node.Evaluate('code/node()')

foreach ($node in $nodes) {
# 登場する要素によって出力を分ける
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,22 @@ else if (false) {

String str = "";

/// # switch文
switch (str) {
/// 文字列
case "":
case "ABC":
/// # 文字列の処理
break;
/// 小文字
case "abc":
{
/// # ブロック内
break;
}
/// その他
default:
/// # その他
throw new RuntimeException();
}

Expand Down

0 comments on commit 414f05c

Please sign in to comment.