From c7f3b2c6078473bebca59cdf394c925f76c44366 Mon Sep 17 00:00:00 2001 From: Mark Canlas Date: Sun, 4 Dec 2022 18:17:31 -0500 Subject: [PATCH] two lines between every subroutine; drop paragraph --- data/annotated-snake.asm | 4 +- data/two-paragraphs.txt | 1 + .../firepower/demo/PrintPrograms.scala | 96 +++++++++---------- .../htmlism/firepower/demo/asm/AsmBlock.scala | 40 ++++---- .../firepower/demo/str/Paragraph.scala | 22 ----- 5 files changed, 70 insertions(+), 93 deletions(-) delete mode 100644 firepower-demo/src/main/scala/com/htmlism/firepower/demo/str/Paragraph.scala diff --git a/data/annotated-snake.asm b/data/annotated-snake.asm index 3d432fe..262ebb2 100644 --- a/data/annotated-snake.asm +++ b/data/annotated-snake.asm @@ -3,8 +3,10 @@ ; \__ \ ' \/ _` | / / -_) _ \__ \ () / / ; |___/_||_\__,_|_\_\___\___/___/\__/___| + ; Change direction: W A S D + lda $00 lda $01 ; instruction comment @@ -12,9 +14,9 @@ lda $00 lda $01 ; instruction comment + labeled: ; This is a subroutine description - lda $00 lda $01 ; instruction comment diff --git a/data/two-paragraphs.txt b/data/two-paragraphs.txt index c1c2b5d..c2bd3f3 100644 --- a/data/two-paragraphs.txt +++ b/data/two-paragraphs.txt @@ -1,5 +1,6 @@ foo bar + alpha bravo diff --git a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/PrintPrograms.scala b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/PrintPrograms.scala index ad270d9..417f40a 100644 --- a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/PrintPrograms.scala +++ b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/PrintPrograms.scala @@ -1,5 +1,7 @@ package com.htmlism.firepower.demo +import scala.util.chaining._ + import cats.syntax.all._ import com.htmlism.firepower.demo.asm._ @@ -17,66 +19,62 @@ object PrintPrograms extends ZIOAppDefault: private val programs = List[(String, String)]( "one-line.txt" -> "one line", - "two-lines.txt" -> (Line.mkString _) - .apply(List("foo", "bar")), - "two-paragraphs.txt" -> (Line.mkString _) - .compose(Paragraph.mkLines) - .apply( + "two-lines.txt" -> List("foo", "bar") + .pipe(Line.mkString), + "two-paragraphs.txt" -> List( + List("foo", "bar"), + List("alpha", "bravo") + ) + .pipe(xxs => AsmBlock.interFlatMap(xxs)(List("", ""), identity)) + .pipe(Line.mkString), + "annotated-snake.asm" -> List( + CommentBlock.fromMultiline(asciiArt), + CommentBlock(List("Change direction: W A S D")), + AnonymousCodeBlock( List( - Paragraph(List("foo", "bar")), - Paragraph(List("alpha", "bravo")) - ) - ), - "annotated-snake.asm" -> (Line.mkString _) - .compose(Paragraph.mkLines) - .compose((xs: List[AsmBlock]) => xs.flatMap(AsmBlock.toParagraphs)) - .apply( - List( - CommentBlock.fromMultiline(asciiArt), - CommentBlock(List("Change direction: W A S D")), - AnonymousCodeBlock( + AsmBlock.Intent( + None, List( - AsmBlock.Intent( - None, - List( - AsmBlock.Intent.Instruction("lda $00", None), - AsmBlock.Intent.Instruction("lda $01", "instruction comment".some) - ) - ), - AsmBlock.Intent( - "this block has some preamble".some, - List( - AsmBlock.Intent.Instruction("lda $00", None), - AsmBlock.Intent.Instruction("lda $01", "instruction comment".some) - ) - ) + AsmBlock.Intent.Instruction("lda $00", None), + AsmBlock.Intent.Instruction("lda $01", "instruction comment".some) ) ), - NamedCodeBlock( - "labeled", - "This is a subroutine description".some, + AsmBlock.Intent( + "this block has some preamble".some, List( - AsmBlock.Intent( - None, - List( - AsmBlock.Intent.Instruction("lda $00", None), - AsmBlock.Intent.Instruction("lda $01", "instruction comment".some) - ) - ), - AsmBlock.Intent( - "this block has some preamble".some, - List( - AsmBlock.Intent.Instruction("lda $00", None), - AsmBlock.Intent.Instruction("lda $01", "instruction comment".some) - ) - ) + AsmBlock.Intent.Instruction("lda $00", None), + AsmBlock.Intent.Instruction("lda $01", "instruction comment".some) + ) + ) + ) + ), + NamedCodeBlock( + "labeled", + "This is a subroutine description".some, + List( + AsmBlock.Intent( + None, + List( + AsmBlock.Intent.Instruction("lda $00", None), + AsmBlock.Intent.Instruction("lda $01", "instruction comment".some) + ) + ), + AsmBlock.Intent( + "this block has some preamble".some, + List( + AsmBlock.Intent.Instruction("lda $00", None), + AsmBlock.Intent.Instruction("lda $01", "instruction comment".some) ) ) ) ) + ) + .map(AsmBlock.toLines) + .pipe(xs => AsmBlock.interFlatMap(xs)(List("", ""), identity)) + .pipe(Line.mkString) ) - lazy val asciiArt = + private lazy val asciiArt = """ ___ _ __ ___ __ ___ |/ __|_ _ __ _| |_____ / /| __|/ \_ ) |\__ \ ' \/ _` | / / -_) _ \__ \ () / / diff --git a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/asm/AsmBlock.scala b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/asm/AsmBlock.scala index f2872c1..bc3c14b 100644 --- a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/asm/AsmBlock.scala +++ b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/asm/AsmBlock.scala @@ -1,7 +1,5 @@ package com.htmlism.firepower.demo.asm -import com.htmlism.firepower.demo.str.Paragraph - sealed trait AsmBlock case class CommentBlock(xs: List[String]) extends AsmBlock @@ -11,47 +9,47 @@ case class NamedCodeBlock(name: String, comment: Option[String], intents: List[A case class AnonymousCodeBlock(intents: List[AsmBlock.Intent]) extends AsmBlock object AsmBlock: + def interFlatMap[A, B](xs: List[A])(x: List[B], f: A => List[B]): List[B] = + xs match + case head :: tail => + f(head) ::: tail.flatMap(a => x ::: f(a)) + + case Nil => + Nil + def toComment(s: String): String = "; " + s def withIndent(s: String): String = " " + s - def toParagraphs(xs: AsmBlock): List[Paragraph] = + def toLines(xs: AsmBlock): List[String] = xs match case CommentBlock(ys) => - List( - Paragraph(ys.map(toComment)) - ) + ys.map(toComment) case NamedCodeBlock(label, oComment, intents) => val headerParagraph = - Paragraph( - List(label + ":") ++ oComment.map(toComment).map(withIndent).toList - ) + List(label + ":") ++ oComment.map(toComment).map(withIndent).toList val intentParagraphs = - intents - .map(Intent.toParagraph) + interFlatMap(intents)(List(""), Intent.toLines) - headerParagraph :: intentParagraphs + headerParagraph ::: intentParagraphs case AnonymousCodeBlock(intents) => - intents - .map(Intent.toParagraph) + interFlatMap(intents)(List(""), Intent.toLines) case class Intent(label: Option[String], instructions: List[Intent.Instruction]) object Intent: case class Instruction(code: String, comment: Option[String]) - def toParagraph(x: Intent): Paragraph = - Paragraph( - x.label.map(toComment).map(withIndent).toList ++ x - .instructions - .map(i => i.code + i.comment.map(toComment).map(" " + _).getOrElse(" ")) - .map(withIndent) - ) + def toLines(x: Intent): List[String] = + x.label.map(toComment).map(withIndent).toList ++ x + .instructions + .map(i => i.code + i.comment.map(toComment).map(" " + _).getOrElse(" ")) + .map(withIndent) object CommentBlock: def fromMultiline(s: String): CommentBlock = diff --git a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/str/Paragraph.scala b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/str/Paragraph.scala deleted file mode 100644 index d936dd3..0000000 --- a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/str/Paragraph.scala +++ /dev/null @@ -1,22 +0,0 @@ -package com.htmlism.firepower.demo.str - -/** - * An abstraction for groups of lines that are eventually separated by newlines, but between paragraphs there's an - * extra newline - */ -case class Paragraph(xs: List[String]) - -object Paragraph: - def apply(xs: String*): Paragraph = - Paragraph(xs.toList) - - def mkLines(ps: List[Paragraph]): List[String] = - interFlatMap(ps)(List(""), _.xs) - - def interFlatMap[A, B](xs: List[A])(x: List[B], f: A => List[B]): List[B] = - xs match - case head :: tail => - f(head) ::: tail.flatMap(a => x ::: f(a)) - - case Nil => - Nil