From 45af484839d1e75c5b58e342e7982e70207704b9 Mon Sep 17 00:00:00 2001 From: Mark Canlas Date: Sun, 16 Aug 2020 01:51:05 -0400 Subject: [PATCH] add definition literal support --- src/main/scala/com/htmlism/mos6502/dsl/Address.scala | 6 ++++++ .../com/htmlism/mos6502/dsl/AsmDocumentContext.scala | 2 +- src/main/scala/com/htmlism/mos6502/dsl/Operand.scala | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/scala/com/htmlism/mos6502/dsl/Address.scala b/src/main/scala/com/htmlism/mos6502/dsl/Address.scala index 8277c64..6435f76 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/Address.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/Address.scala @@ -11,6 +11,9 @@ object ZeroAddress { def toShow(x: ZeroAddress): String = String.format("global address 0x%02x", x.n) + def toDefinitionLiteral(x: ZeroAddress): String = + toAddressLiteral(x) + def toAddressLiteral(x: ZeroAddress): String = String.format("$%02x", x.n) } @@ -27,6 +30,9 @@ object GlobalAddress { def toShow(x: GlobalAddress): String = String.format("global address 0x%04x", x.n) + def toDefinitionLiteral(x: GlobalAddress): String = + toAddressLiteral(x) + def toAddressLiteral(x: GlobalAddress): String = String.format("$%04x", x.n) } diff --git a/src/main/scala/com/htmlism/mos6502/dsl/AsmDocumentContext.scala b/src/main/scala/com/htmlism/mos6502/dsl/AsmDocumentContext.scala index 0136567..8b1225d 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/AsmDocumentContext.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/AsmDocumentContext.scala @@ -57,7 +57,7 @@ class DefinitionGroupContext { case class Definition[A: Operand](name: String, x: A) { lazy val value: String = implicitly[Operand[A]] - .toAddressLiteral(x) + .toDefinitionLiteral(x) } class AsmBlockContext { diff --git a/src/main/scala/com/htmlism/mos6502/dsl/Operand.scala b/src/main/scala/com/htmlism/mos6502/dsl/Operand.scala index 6cda8c7..b073959 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/Operand.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/Operand.scala @@ -9,6 +9,8 @@ trait Operand[A] { def operandType: OperandType + def toDefinitionLiteral(x: A): String + def contra[B](f: B => A, show: B => String): Operand[B] = new Operand[B] { val operandType: OperandType = @@ -17,6 +19,9 @@ trait Operand[A] { def toShow(x: B): String = show(x) + def toDefinitionLiteral(x: B): String = + toAddressLiteral(x) + def toAddressLiteral(x: B): String = self.toAddressLiteral(f(x)) } @@ -31,6 +36,9 @@ object Operand { def toShow(x: Int): String = x.toString + def toDefinitionLiteral(x: Int): String = + x.toString + def toAddressLiteral(x: Int): String = String.format("#$%02x", x) }