From 9d038a0160e40a11666908da370b4b1a0dd35e96 Mon Sep 17 00:00:00 2001 From: Mark Canlas Date: Wed, 7 Dec 2022 18:12:49 -0500 Subject: [PATCH] use meta intent dsl now --- .../com/htmlism/firepower/core/MetaIntent.scala | 11 +++++------ .../com/htmlism/firepower/core/Subroutine.scala | 4 ++-- .../com/htmlism/firepower/demo/SnakeEasy6502.scala | 12 ++++++------ 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/firepower-core/src/main/scala/com/htmlism/firepower/core/MetaIntent.scala b/firepower-core/src/main/scala/com/htmlism/firepower/core/MetaIntent.scala index 91a337d..9d01668 100644 --- a/firepower-core/src/main/scala/com/htmlism/firepower/core/MetaIntent.scala +++ b/firepower-core/src/main/scala/com/htmlism/firepower/core/MetaIntent.scala @@ -7,14 +7,13 @@ import com.htmlism.firepower.core.* /** * Anything that can be compiled into an `Intent` */ -sealed trait MetaIntent +sealed trait MetaIntent: + def toIntent: AsmBlock.Intent object MetaIntent: - case class Jump(target: String, description: String, xs: List[MetaIntent.Jump]) extends MetaIntent - - object Jump: - def toIntent(j: Jump): AsmBlock.Intent = - AsmBlock.Intent(None, List(AsmBlock.Intent.Instruction.one("jsr", j.target))) + case class Jump(target: String, description: String, xs: List[MetaIntent]) extends MetaIntent: + def toIntent: AsmBlock.Intent = + AsmBlock.Intent(None, List(AsmBlock.Intent.Instruction.one("jsr", target))) case class Move[A: Definable, B: Definable](src: A, dest: B): def defines: List[Definable.Table] = diff --git a/firepower-core/src/main/scala/com/htmlism/firepower/core/Subroutine.scala b/firepower-core/src/main/scala/com/htmlism/firepower/core/Subroutine.scala index 02a36ce..3ecfad0 100644 --- a/firepower-core/src/main/scala/com/htmlism/firepower/core/Subroutine.scala +++ b/firepower-core/src/main/scala/com/htmlism/firepower/core/Subroutine.scala @@ -4,8 +4,8 @@ import cats.syntax.all.* import com.htmlism.firepower.core.AsmBlock._ -case class Subroutine(name: String, description: String, intents: () => List[MetaIntent.Jump]): - def call: MetaIntent.Jump = +case class Subroutine(name: String, description: String, intents: () => List[MetaIntent]): + def call: MetaIntent = MetaIntent.Jump(name, description, intents()) object Subroutine: diff --git a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/SnakeEasy6502.scala b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/SnakeEasy6502.scala index 40c0954..fba0bbb 100644 --- a/firepower-demo/src/main/scala/com/htmlism/firepower/demo/SnakeEasy6502.scala +++ b/firepower-demo/src/main/scala/com/htmlism/firepower/demo/SnakeEasy6502.scala @@ -10,7 +10,7 @@ import com.htmlism.firepower.core.AsmBlock.Intent import com.htmlism.firepower.core.* object SnakeEasy6502: - val program: List[MetaIntent.Jump] = + val program: List[MetaIntent] = List( init.call, loop.call @@ -31,11 +31,11 @@ object SnakeEasy6502: lazy val initSnake = Subroutine("initSnake", "initializes the snake") - def firstCodeBlock(xs: List[MetaIntent.Jump]): AsmBlock.AnonymousCodeBlock = + def firstCodeBlock(xs: List[MetaIntent]): AsmBlock.AnonymousCodeBlock = AsmBlock - .AnonymousCodeBlock(xs.map(MetaIntent.Jump.toIntent)) + .AnonymousCodeBlock(xs.map(_.toIntent)) - def callGraph(xs: List[MetaIntent.Jump]): List[AsmBlock.NamedCodeBlock] = + def callGraph(xs: List[MetaIntent]): List[AsmBlock.NamedCodeBlock] = callGraphRecur(ListMap.empty, xs) .values .toList @@ -43,9 +43,9 @@ object SnakeEasy6502: @tailrec private def callGraphRecur( callGraph: ListMap[String, AsmBlock.NamedCodeBlock], - todo: List[MetaIntent.Jump] + todo: List[MetaIntent] ): ListMap[String, AsmBlock.NamedCodeBlock] = - todo match + todo.collect { case x: MetaIntent.Jump => x } match case head :: tail => if (callGraph.contains(head.target)) callGraphRecur(callGraph, tail) else