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