32. この処理系を
Kotlinで実装したった
Wednesday, December 12, 12
33. 実装を持てるインタフェース
public trait Brainfxck {
public fun execute(source: String,
inputStream: InputStream = System.`in`,
outputStream: OutputStream = System.`out`)
}
デフォルトのパラメータ
Wednesday, December 12, 12
34. object BrainfxckImpl : Brainfxck {
override public fun execute(source : String,
inputStream : InputStream,
outputStream : OutputStream) {
// do something
}
}
Wednesday, December 12, 12
35. シングルトン トレイトの実装
object BrainfxckImpl : Brainfxck {
override public fun execute(source : String,
inputStream : InputStream,
outputStream : OutputStream) {
アノテーション
// do something
}
}
Wednesday, December 12, 12
36. private val incrementPointerOperator = {
(runtimeState : RuntimeState) ->
runtimeState.incrementPointer()
}
関数リテラルを変数に割り当てる
private val decrementPointerOperator = {
(runtimeState : RuntimeState) ->
runtimeState.decrementPointer()
}
Wednesday, December 12, 12
37. private val operators = hashMap(
INCREMENT_POINTER to incrementPointerOperator,
DECREMENT_POINTER to decrementPointerOperator,
INCREMENT_VALUE to incrementValueOperator,
DECREMENT_VALUE to decrementValueOperator,
INPUT to inputOperator,
OUTPUT to outputOperator,
LOOP_START to loopStartOperator,
LOOP_END to loopEndOperator
)
Wednesday, December 12, 12
38. 命令語(文字)と
操作(関数リテラル)を
private val operators = hashMap(
マッピング
INCREMENT_POINTER to incrementPointerOperator,
DECREMENT_POINTER to decrementPointerOperator,
INCREMENT_VALUE to incrementValueOperator,
DECREMENT_VALUE to decrementValueOperator,
INPUT to inputOperator,
OUTPUT to outputOperator,
LOOP_START to loopStartOperator,
LOOP_END to loopEndOperator
)
演算子風関数呼び出し
Wednesday, December 12, 12
39. while (!runtimeState.isEndOfProgram()) {
val code = runtimeState.getCurrentProgramCode()
val operator = operators[code] ?: doNothingOperator
operator(runtimeState)
runtimeState.next()
}
Wednesday, December 12, 12
40. マップに配列風アクセス
while (!runtimeState.isEndOfProgram()) {
val code = runtimeState.getCurrentProgramCode()
val operator = operators[code] ?: doNothingOperator
operator(runtimeState)
runtimeState.next()
エルビス演算子
}
Wednesday, December 12, 12
41. デモの前に
Brainf*ckの命令語を
別の文字列に置き換える
Wednesday, December 12, 12