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 ; Change direction: W A S D
lda $00 lda $00
lda $01 ; instruction comment lda $01 ; instruction comment
@ -12,9 +14,9 @@
lda $00 lda $00
lda $01 ; instruction comment lda $01 ; instruction comment
labeled: labeled:
; This is a subroutine description ; This is a subroutine description
lda $00 lda $00
lda $01 ; instruction comment lda $01 ; instruction comment

View File

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

View File

@ -1,5 +1,7 @@
package com.htmlism.firepower.demo package com.htmlism.firepower.demo
import scala.util.chaining._
import cats.syntax.all._ import cats.syntax.all._
import com.htmlism.firepower.demo.asm._ import com.htmlism.firepower.demo.asm._
@ -17,66 +19,62 @@ object PrintPrograms extends ZIOAppDefault:
private val programs = private val programs =
List[(String, String)]( List[(String, String)](
"one-line.txt" -> "one line", "one-line.txt" -> "one line",
"two-lines.txt" -> (Line.mkString _) "two-lines.txt" -> List("foo", "bar")
.apply(List("foo", "bar")), .pipe(Line.mkString),
"two-paragraphs.txt" -> (Line.mkString _) "two-paragraphs.txt" -> List(
.compose(Paragraph.mkLines) List("foo", "bar"),
.apply( 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( List(
Paragraph(List("foo", "bar")), AsmBlock.Intent(
Paragraph(List("alpha", "bravo")) None,
)
),
"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(
List( List(
AsmBlock.Intent( AsmBlock.Intent.Instruction("lda $00", None),
None, AsmBlock.Intent.Instruction("lda $01", "instruction comment".some)
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)
)
)
) )
), ),
NamedCodeBlock( AsmBlock.Intent(
"labeled", "this block has some preamble".some,
"This is a subroutine description".some,
List( List(
AsmBlock.Intent( AsmBlock.Intent.Instruction("lda $00", None),
None, AsmBlock.Intent.Instruction("lda $01", "instruction comment".some)
List( )
AsmBlock.Intent.Instruction("lda $00", None), )
AsmBlock.Intent.Instruction("lda $01", "instruction comment".some) )
) ),
), NamedCodeBlock(
AsmBlock.Intent( "labeled",
"this block has some preamble".some, "This is a subroutine description".some,
List( List(
AsmBlock.Intent.Instruction("lda $00", None), AsmBlock.Intent(
AsmBlock.Intent.Instruction("lda $01", "instruction comment".some) 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 package com.htmlism.firepower.demo.asm
import com.htmlism.firepower.demo.str.Paragraph
sealed trait AsmBlock sealed trait AsmBlock
case class CommentBlock(xs: List[String]) extends 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 case class AnonymousCodeBlock(intents: List[AsmBlock.Intent]) extends AsmBlock
object 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 = def toComment(s: String): String =
"; " + s "; " + s
def withIndent(s: String): String = def withIndent(s: String): String =
" " + s " " + s
def toParagraphs(xs: AsmBlock): List[Paragraph] = def toLines(xs: AsmBlock): List[String] =
xs match xs match
case CommentBlock(ys) => case CommentBlock(ys) =>
List( ys.map(toComment)
Paragraph(ys.map(toComment))
)
case NamedCodeBlock(label, oComment, intents) => case NamedCodeBlock(label, oComment, intents) =>
val headerParagraph = val headerParagraph =
Paragraph( List(label + ":") ++ oComment.map(toComment).map(withIndent).toList
List(label + ":") ++ oComment.map(toComment).map(withIndent).toList
)
val intentParagraphs = val intentParagraphs =
intents interFlatMap(intents)(List(""), Intent.toLines)
.map(Intent.toParagraph)
headerParagraph :: intentParagraphs headerParagraph ::: intentParagraphs
case AnonymousCodeBlock(intents) => case AnonymousCodeBlock(intents) =>
intents interFlatMap(intents)(List(""), Intent.toLines)
.map(Intent.toParagraph)
case class Intent(label: Option[String], instructions: List[Intent.Instruction]) case class Intent(label: Option[String], instructions: List[Intent.Instruction])
object Intent: object Intent:
case class Instruction(code: String, comment: Option[String]) case class Instruction(code: String, comment: Option[String])
def toParagraph(x: Intent): Paragraph = def toLines(x: Intent): List[String] =
Paragraph( x.label.map(toComment).map(withIndent).toList ++ x
x.label.map(toComment).map(withIndent).toList ++ x .instructions
.instructions .map(i => i.code + i.comment.map(toComment).map(" " + _).getOrElse(" "))
.map(i => i.code + i.comment.map(toComment).map(" " + _).getOrElse(" ")) .map(withIndent)
.map(withIndent)
)
object CommentBlock: object CommentBlock:
def fromMultiline(s: String): 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