mirror of
https://github.com/mcanlas/6502-opcodes.git
synced 2025-01-03 08:31:26 +00:00
implement enum show
This commit is contained in:
parent
8794f2ddea
commit
6924f6c47f
@ -1,14 +1,12 @@
|
||||
package com.htmlism.mos6502.dsl
|
||||
|
||||
import cats.implicits._
|
||||
|
||||
sealed trait Color
|
||||
|
||||
object Color {
|
||||
implicit val ev: Operand[Color] =
|
||||
Operand
|
||||
.operandInt
|
||||
.contramap(toByte)
|
||||
.contra(toByte, _.toString)
|
||||
|
||||
def toByte(x: Color): Int =
|
||||
x match {
|
||||
|
@ -1,13 +1,25 @@
|
||||
package com.htmlism.mos6502.dsl
|
||||
|
||||
import cats.Contravariant
|
||||
|
||||
trait Operand[A] {
|
||||
self =>
|
||||
|
||||
def toAddressLiteral(x: A): String
|
||||
|
||||
def toShow(x: A): String
|
||||
|
||||
def operandType: OperandType
|
||||
|
||||
def contra[B](f: B => A, show: B => String): Operand[B] =
|
||||
new Operand[B] {
|
||||
val operandType: OperandType =
|
||||
self.operandType
|
||||
|
||||
def toShow(x: B): String =
|
||||
show(x)
|
||||
|
||||
def toAddressLiteral(x: B): String =
|
||||
self.toAddressLiteral(f(x))
|
||||
}
|
||||
}
|
||||
|
||||
object Operand {
|
||||
@ -22,19 +34,4 @@ object Operand {
|
||||
def toAddressLiteral(x: Int): String =
|
||||
String.format("#$%02x", x)
|
||||
}
|
||||
|
||||
implicit val contra: Contravariant[Operand] =
|
||||
new Contravariant[Operand] {
|
||||
def contramap[A, B](fa: Operand[A])(f: B => A): Operand[B] =
|
||||
new Operand[B] {
|
||||
val operandType: OperandType =
|
||||
fa.operandType
|
||||
|
||||
def toShow(x: B): String =
|
||||
fa.toShow(f(x))
|
||||
|
||||
def toAddressLiteral(x: B): String =
|
||||
fa.toAddressLiteral(f(x))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user