2022-12-04 21:49:05 +00:00
|
|
|
package com.htmlism.firepower.demo.asm
|
|
|
|
|
|
|
|
sealed trait AsmBlock
|
|
|
|
|
|
|
|
case class CommentBlock(xs: List[String]) extends AsmBlock
|
|
|
|
|
2022-12-04 22:23:42 +00:00
|
|
|
case class NamedCodeBlock(name: String, comment: Option[String], intents: List[AsmBlock.Intent]) extends AsmBlock
|
2022-12-04 21:56:03 +00:00
|
|
|
|
2022-12-04 22:15:29 +00:00
|
|
|
case class AnonymousCodeBlock(intents: List[AsmBlock.Intent]) extends AsmBlock
|
2022-12-04 21:56:03 +00:00
|
|
|
|
|
|
|
object AsmBlock:
|
2022-12-04 23:17:31 +00:00
|
|
|
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
|
|
|
|
|
2022-12-04 22:23:42 +00:00
|
|
|
def toComment(s: String): String =
|
|
|
|
"; " + s
|
|
|
|
|
|
|
|
def withIndent(s: String): String =
|
|
|
|
" " + s
|
|
|
|
|
2022-12-04 23:17:31 +00:00
|
|
|
def toLines(xs: AsmBlock): List[String] =
|
2022-12-04 21:49:05 +00:00
|
|
|
xs match
|
|
|
|
case CommentBlock(ys) =>
|
2022-12-04 23:17:31 +00:00
|
|
|
ys.map(toComment)
|
2022-12-04 21:49:05 +00:00
|
|
|
|
2022-12-04 22:30:36 +00:00
|
|
|
case NamedCodeBlock(label, oComment, intents) =>
|
2022-12-04 22:23:42 +00:00
|
|
|
val headerParagraph =
|
2022-12-04 23:17:31 +00:00
|
|
|
List(label + ":") ++ oComment.map(toComment).map(withIndent).toList
|
2022-12-04 22:23:42 +00:00
|
|
|
|
|
|
|
val intentParagraphs =
|
2022-12-04 23:26:53 +00:00
|
|
|
intents.map(Intent.toLines)
|
2022-12-04 22:23:42 +00:00
|
|
|
|
2022-12-04 23:26:53 +00:00
|
|
|
interFlatMap(headerParagraph :: intentParagraphs)(List(""), identity)
|
2022-12-04 21:56:03 +00:00
|
|
|
|
2022-12-04 22:30:36 +00:00
|
|
|
case AnonymousCodeBlock(intents) =>
|
2022-12-04 23:17:31 +00:00
|
|
|
interFlatMap(intents)(List(""), Intent.toLines)
|
2022-12-04 22:15:29 +00:00
|
|
|
|
|
|
|
case class Intent(label: Option[String], instructions: List[Intent.Instruction])
|
|
|
|
|
|
|
|
object Intent:
|
|
|
|
case class Instruction(code: String, comment: Option[String])
|
|
|
|
|
2022-12-04 23:17:31 +00:00
|
|
|
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)
|
2022-12-04 22:30:36 +00:00
|
|
|
|
2022-12-04 21:56:03 +00:00
|
|
|
object CommentBlock:
|
2022-12-04 21:49:05 +00:00
|
|
|
def fromMultiline(s: String): CommentBlock =
|
|
|
|
CommentBlock(s.split("\\n").toList)
|