From d89f7a3e87090b7cae3b3afbd4bd735bbec21908 Mon Sep 17 00:00:00 2001 From: Mark Canlas Date: Mon, 21 Nov 2022 10:36:36 -0500 Subject: [PATCH] use generated code instead --- .../htmlism/scratchpad/CodeGenerator.scala | 4 +- .../scala/com/htmlism/scratchpad/asm.scala | 45 ++++++++++++++----- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/CodeGenerator.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/CodeGenerator.scala index f900c33..f136fc9 100644 --- a/scratchpad/src/main/scala/com/htmlism/scratchpad/CodeGenerator.scala +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/CodeGenerator.scala @@ -15,7 +15,7 @@ object CodeGenerator extends App: val typeParameterList = letters.mkString(", ") - println(s"trait Asm$n[$typeParameterList]:") + println(s"sealed trait Asm$n[$typeParameterList]:") println(" def xs: List[String]") println println(" def oComment: Option[String]") @@ -38,7 +38,7 @@ object CodeGenerator extends App: println(" copy(oComment = Some(s))") println println( - s"case class Asm${n}Instructions[$typeParameterList](xs: List[String], oComment: Option[String]) extends Asm${n}[$typeParameterList]:" + s"case class Asm${n}Instructions[$typeParameterList](xs: List[String], oComment: Option[String] = None) extends Asm${n}[$typeParameterList]:" ) println(s" def comment(s: String): Asm$n[$typeParameterList] =") println(" copy(oComment = Some(s))") diff --git a/scratchpad/src/main/scala/com/htmlism/scratchpad/asm.scala b/scratchpad/src/main/scala/com/htmlism/scratchpad/asm.scala index ac06ab8..f290c56 100644 --- a/scratchpad/src/main/scala/com/htmlism/scratchpad/asm.scala +++ b/scratchpad/src/main/scala/com/htmlism/scratchpad/asm.scala @@ -3,18 +3,47 @@ package com.htmlism.scratchpad sealed trait Asm1[A]: def xs: List[String] - def widenWith[B]: Asm2[A, B] = - Asm2Instructions(xs) + def oComment: Option[String] -case class Asm1Instructions[A](xs: List[String]) extends Asm1[A] + def comment(s: String): Asm1[A] + + def andThen(that: Asm1[A]): Asm1[A] = + AndThen1[A](this, that, None) + + def widenWith[B]: Asm2[A, B] = + Asm2Instructions(xs, oComment) + +case class AndThen1[A](left: Asm1[A], right: Asm1[A], oComment: Option[String]) extends Asm1[A]: + def xs: List[String] = + left.xs ++ right.xs + + def comment(s: String): Asm1[A] = + copy(oComment = Some(s)) + +case class Asm1Instructions[A](xs: List[String], oComment: Option[String] = None) extends Asm1[A]: + def comment(s: String): Asm1[A] = + copy(oComment = Some(s)) sealed trait Asm2[A, B]: def xs: List[String] - def andThen(that: Asm2[A, B]): Asm2[A, B] = - AndThen2(this, that) + def oComment: Option[String] -case class Asm2Instructions[A, B](xs: List[String]) extends Asm2[A, B]: + def comment(s: String): Asm2[A, B] + + def andThen(that: Asm2[A, B]): Asm2[A, B] = + AndThen2[A, B](this, that, None) + +case class AndThen2[A, B](left: Asm2[A, B], right: Asm2[A, B], oComment: Option[String]) extends Asm2[A, B]: + def xs: List[String] = + left.xs ++ right.xs + + def comment(s: String): Asm2[A, B] = + copy(oComment = Some(s)) + +case class Asm2Instructions[A, B](xs: List[String], oComment: Option[String] = None) extends Asm2[A, B]: + def comment(s: String): Asm2[A, B] = + copy(oComment = Some(s)) // TODO not tested // B type needs to be a class type, with evidence, not a case class def swap[AA, BB](f: (R[A], R[B]) => (R[AA], R[BB])): Asm2Instructions[AA, BB] = @@ -24,10 +53,6 @@ object Asm2Instructions: def from[A, B](t2: (R[A], R[B]), xs: List[String]) = Asm2Instructions[A, B](xs) -case class AndThen2[A, B](x: Asm2[A, B], y: Asm2[A, B]) extends Asm2[A, B]: - def xs: List[String] = - x.xs ++ y.xs - trait Asm3[A, B, C]: def xs: List[String]