two lines between every subroutine; drop paragraph

This commit is contained in:
Mark Canlas 2022-12-04 18:17:31 -05:00
parent 942cd78c7d
commit c7f3b2c607
5 changed files with 70 additions and 93 deletions

View File

@ -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

View File

@ -1,5 +1,6 @@
foo
bar
alpha
bravo

View File

@ -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 =
""" ___ _ __ ___ __ ___
|/ __|_ _ __ _| |_____ / /| __|/ \_ )
|\__ \ ' \/ _` | / / -_) _ \__ \ () / /

View File

@ -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 =

View File

@ -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