6502-opcodes/src/main/scala/com/htmlism/mos6502/dsl/Statement.scala

62 lines
1.6 KiB
Scala
Raw Normal View History

2020-08-14 20:54:29 +00:00
package com.htmlism.mos6502.dsl
2023-10-03 20:15:10 +00:00
import cats.syntax.all.*
2020-08-15 06:04:21 +00:00
2020-08-15 04:41:18 +00:00
import com.htmlism.mos6502.model.Instruction
2020-08-14 20:54:29 +00:00
2022-02-14 23:11:45 +00:00
sealed trait Statement:
2020-08-14 21:08:59 +00:00
def toAsm: String
2020-08-15 05:27:13 +00:00
def toTriplet: (String, Option[String], Option[String])
2020-08-14 20:54:29 +00:00
2022-02-14 23:11:45 +00:00
object Statement:
2020-08-16 06:07:51 +00:00
val indent: String =
" "
2022-02-14 23:11:45 +00:00
case class UnaryInstruction(instruction: Instruction, comment: Option[String]) extends Statement:
def toAsm: String =
2020-08-14 21:08:59 +00:00
val left =
instruction.toString
2020-08-14 20:54:29 +00:00
2022-02-14 23:11:45 +00:00
comment match
2020-08-14 21:08:59 +00:00
case Some(c) =>
2020-08-16 06:07:51 +00:00
Statement.indent + f"$left%-16s ; " + c
2023-01-02 18:02:03 +00:00
case None =>
2020-08-16 06:07:51 +00:00
Statement.indent + left
2020-08-15 05:27:13 +00:00
def toTriplet: (String, Option[String], Option[String]) =
(instruction.toString, None, comment)
2020-08-14 21:08:59 +00:00
2023-06-19 09:30:17 +00:00
case class InstructionWithOperand[A](instruction: Instruction, operand: A, comment: Option[String])(using
2020-08-15 07:52:11 +00:00
ev: Operand[A]
2022-02-14 23:11:45 +00:00
) extends Statement:
def toAsm: String =
2020-08-14 21:08:59 +00:00
val left =
instruction.toString
val operandStr =
2020-08-15 02:02:34 +00:00
ev.toAddressLiteral(operand)
2020-08-14 21:08:59 +00:00
2022-02-14 23:11:45 +00:00
comment match
2020-08-14 21:08:59 +00:00
case Some(c) =>
2020-08-16 06:07:51 +00:00
Statement.indent + f"$left%-5s $operandStr%-11s; " + c
2023-01-02 18:02:03 +00:00
case None =>
2020-08-16 06:07:51 +00:00
Statement.indent + f"$left%-5s $operandStr"
2020-08-15 05:27:13 +00:00
def toTriplet: (String, Option[String], Option[String]) =
(instruction.toString, ev.toAddressLiteral(operand).some, comment)
2020-08-16 06:17:04 +00:00
2022-02-14 23:11:45 +00:00
case class Label(s: String) extends Statement:
2020-08-16 06:17:04 +00:00
def toAsm: String =
s + ":"
def toTriplet: (String, Option[String], Option[String]) =
(s, None, None)
2020-08-16 15:33:49 +00:00
2022-02-14 23:11:45 +00:00
case class BranchingInstruction(instruction: Instruction, label: String) extends Statement:
2020-08-16 15:33:49 +00:00
def toAsm: String =
Statement.indent + instruction.toString + " " + label
def toTriplet: (String, Option[String], Option[String]) =
(instruction.toString, label.some, None)