From 9b60d46e03a655654385c0fd54c2804c2b6b2112 Mon Sep 17 00:00:00 2001 From: Mark Canlas Date: Sun, 16 Aug 2020 01:17:39 -0400 Subject: [PATCH] print out more --- .../mos6502/dsl/AsmDocumentContext.scala | 41 ++++++++++++++----- .../scala/com/htmlism/mos6502/dsl/Color.scala | 30 +++++++------- .../com/htmlism/mos6502/dsl/Mapping.scala | 4 +- .../com/htmlism/mos6502/dsl/DslSpec.scala | 6 +-- .../htmlism/mos6502/dsl/Easy6502Spec.scala | 12 ++---- 5 files changed, 54 insertions(+), 39 deletions(-) diff --git a/src/main/scala/com/htmlism/mos6502/dsl/AsmDocumentContext.scala b/src/main/scala/com/htmlism/mos6502/dsl/AsmDocumentContext.scala index 2077238..0136567 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/AsmDocumentContext.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/AsmDocumentContext.scala @@ -2,7 +2,12 @@ package com.htmlism.mos6502.dsl import scala.collection.mutable.ListBuffer -case class AsmDocument(xs: List[TopLevelAsmDocumentFragment]) +case class AsmDocument(xs: List[TopLevelAsmDocumentFragment]) { + def toAsm: String = + xs + .map(_.toAsm) + .mkString("\n\n") +} class AsmDocumentContext { private val xs: ListBuffer[TopLevelAsmDocumentFragment] = @@ -15,16 +20,28 @@ class AsmDocumentContext { AsmDocument(xs.toList) } -sealed trait TopLevelAsmDocumentFragment - -case class AsmFragment(xs: List[Statement]) extends TopLevelAsmDocumentFragment { - def printOut(): Unit = { - xs.map(_.toAsm) - .foreach(println) - } +sealed trait TopLevelAsmDocumentFragment { + def toAsm: String } -case class DefinitionGroup(comment: String, xs: List[Definition[_]]) extends TopLevelAsmDocumentFragment +case class AsmFragment(xs: List[Statement]) extends TopLevelAsmDocumentFragment { + def toAsm: String = + xs.map(_.toAsm).mkString("\n") +} + +case class DefinitionGroup(comment: String, xs: List[Definition[_]]) extends TopLevelAsmDocumentFragment { + def toAsm: String = { + val commentLine = + "; " + comment + + val definitionLines = + xs + .map(d => f"define ${d.name}%-12s${d.value}") + + (commentLine :: definitionLines) + .mkString("\n") + } +} class DefinitionGroupContext { private val xs: ListBuffer[Definition[_]] = @@ -37,7 +54,11 @@ class DefinitionGroupContext { DefinitionGroup(s, xs.toList) } -case class Definition[A: Operand](name: String, x: A) +case class Definition[A: Operand](name: String, x: A) { + lazy val value: String = + implicitly[Operand[A]] + .toAddressLiteral(x) +} class AsmBlockContext { private val xs: ListBuffer[Statement] = diff --git a/src/main/scala/com/htmlism/mos6502/dsl/Color.scala b/src/main/scala/com/htmlism/mos6502/dsl/Color.scala index 7ddc653..e621ad9 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/Color.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/Color.scala @@ -17,21 +17,21 @@ object Color { def all: NonEmptyList[Color] = NonEmptyList.of( Black, - White, - Red, - Cyan, - Purple, - Green, - Blue, - Yellow, - Orange, - Brown, - LightRed, - DarkGrey, - Grey, - LightGreen, - LightBlue, - LightGrey + White, + Red, + Cyan, + Purple, + Green, + Blue, + Yellow, + Orange, + Brown, + LightRed, + DarkGrey, + Grey, + LightGreen, + LightBlue, + LightGrey ) def label(x: Color): String = diff --git a/src/main/scala/com/htmlism/mos6502/dsl/Mapping.scala b/src/main/scala/com/htmlism/mos6502/dsl/Mapping.scala index 9c90dd6..535aaaf 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/Mapping.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/Mapping.scala @@ -3,8 +3,8 @@ package com.htmlism.mos6502.dsl import cats.data.NonEmptyList /** - * Like an enum, but values are specified - */ + * Like an enum, but values are specified + */ trait Mapping[A] { def comment: String diff --git a/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala b/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala index c3206bc..1d361d8 100644 --- a/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala +++ b/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala @@ -184,9 +184,9 @@ object Direction { def value(x: Direction): Int = x match { - case Up => 0x77 - case Down => 0x61 - case Left => 0x73 + case Up => 0x77 + case Down => 0x61 + case Left => 0x73 case Right => 0x64 } diff --git a/src/test/scala/com/htmlism/mos6502/dsl/Easy6502Spec.scala b/src/test/scala/com/htmlism/mos6502/dsl/Easy6502Spec.scala index 94eda7d..c18ae8f 100644 --- a/src/test/scala/com/htmlism/mos6502/dsl/Easy6502Spec.scala +++ b/src/test/scala/com/htmlism/mos6502/dsl/Easy6502Spec.scala @@ -44,15 +44,9 @@ class Easy6502Spec extends AnyFlatSpec with should.Matchers { } } - doc - .xs - .foreach { - case DefinitionGroup(c, xs) => - println(c) - xs.foreach(println) - case a: AsmFragment => - a.printOut() - } + println( + doc.toAsm + ) } def withAssemblyContext(f: AssemblyContext => Unit): AssemblyContext = {