@@ -11,12 +11,19 @@ import dev.meetree.brainfuck.core.*
1111class InterpreterSpec extends AnyFlatSpec with Matchers with EitherValues :
1212 import Interpreter .eval
1313
14- case class Data (inputs : List [Byte ], outputs : List [Byte ], faulty : Boolean = false ):
15- def read : (Data , Either [InputOutputError , Byte ]) =
14+ type F [A ] = StateT [Id , Data , A ]
15+
16+ given eof : EOF = EOF .NoChange
17+ given InputOutput [F ] with
18+ def read : F [Either [InputOutputError , Option [Byte ]]] = StateT .apply(s => s.read)
19+ def write (byte : Byte ): F [Either [InputOutputError , Unit ]] = StateT .apply(s => s.write(byte))
20+
21+ case class Data (inputs : List [Byte ], outputs : List [Byte ], faulty : Boolean = false )(using eof : EOF ):
22+ def read : (Data , Either [InputOutputError , Option [Byte ]]) =
1623 inputs match
1724 case _ if faulty => (this , Left (InputOutputError .Read (" read" )))
18- case head :: tail => (Data (tail, outputs, faulty), Right (head))
19- case Nil => (this , Right (0 ))
25+ case head :: tail => (Data (tail, outputs, faulty), Right (Some ( head) ))
26+ case Nil => (this , Right (eof.byte ))
2027
2128 def write (byte : Byte ): (Data , Either [InputOutputError , Unit ]) =
2229 if faulty then (this , Left (InputOutputError .Write (" write" )))
@@ -26,11 +33,6 @@ class InterpreterSpec extends AnyFlatSpec with Matchers with EitherValues:
2633 final val empty : Data = Data (Nil , Nil )
2734 final val faulty : Data = Data .empty.copy(faulty = true )
2835
29- type F [A ] = StateT [Id , Data , A ]
30- given InputOutput [F ] with
31- def read : F [Either [InputOutputError , Byte ]] = StateT .apply(s => s.read)
32- def write (byte : Byte ): F [Either [InputOutputError , Unit ]] = StateT .apply(s => s.write(byte))
33-
3436 val interpreter = Interpreter .make[F ]
3537 import interpreter .interpret
3638
0 commit comments