-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor generator to support multiple generation strategies (#13)
- Loading branch information
Showing
6 changed files
with
136 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package fhetest.Generate | ||
|
||
trait AbsStmt | ||
case class Var() | ||
case class Assign(l: String, r: (Int | Double)) extends AbsStmt | ||
case class AssignVec(l: String, r: (List[Int] | List[Double])) extends AbsStmt | ||
case class Add(l: Var, r: Var) extends AbsStmt | ||
case class Sub(l: Var, r: Var) extends AbsStmt | ||
case class Mul(l: Var, r: Var) extends AbsStmt | ||
case class Rot(l: Var, r: Var) extends AbsStmt | ||
|
||
val V = Var() | ||
|
||
def formatNumber(n: Int | Double): String = n match { | ||
case i: Int => i.toString | ||
case d: Double => f"$d%f" | ||
} | ||
|
||
def allAbsStmts: LazyList[AbsStmt] = LazyList( | ||
Add(V, V), | ||
Sub(V, V), | ||
Mul(V, V), | ||
Rot(V, V), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package fhetest.Generate | ||
|
||
type Template = List[AbsStmt] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package fhetest.Generate | ||
|
||
// Template Generation Strategy | ||
enum Strategy: | ||
case Exhaustive, Random | ||
|
||
extension (s: Strategy) | ||
def getGenerator: TemplateGenerator = s match { | ||
case Strategy.Exhaustive => ExhaustiveGenerator | ||
case Strategy.Random => RandomGenerator | ||
} | ||
|
||
// Template Generator | ||
trait TemplateGenerator { | ||
def generateTemplates(): LazyList[Template] | ||
} | ||
|
||
object ExhaustiveGenerator extends TemplateGenerator { | ||
def generateTemplates(): LazyList[Template] = { | ||
def allTemplatesOfSize(n: Int): LazyList[Template] = n match { | ||
case 1 => allAbsStmts.map(stmt => List(stmt)) | ||
case _ => | ||
for { | ||
stmt <- allAbsStmts | ||
program <- allTemplatesOfSize(n - 1) | ||
} yield stmt :: program | ||
} | ||
LazyList.from(1).flatMap(allTemplatesOfSize) | ||
} | ||
|
||
} | ||
|
||
object RandomGenerator extends TemplateGenerator { | ||
def generateTemplates(): LazyList[Template] = { | ||
??? | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package fhetest.Generate | ||
|
||
import scala.util.Random | ||
|
||
object Utils { | ||
def assignValues(name: String, vxs: (List[Int] | List[Double])): AbsStmt = | ||
AssignVec(name, vxs) | ||
|
||
extension (s: AbsStmt) | ||
def stringify: String = s match | ||
case Assign(l, r) => s"$l = ${formatNumber(r)};" | ||
case AssignVec(l, r) => s"$l = {${r.map(formatNumber).mkString(",")}};" | ||
case Add(l, r) => "x += y;" | ||
case Sub(l, r) => "x -= y;" | ||
case Mul(l, r) => "x *= y;" | ||
case Rot(l, r) => "rotate_left(x, c);" | ||
|
||
extension (t: Template) | ||
def stringify: String = t.map(_.stringify).mkString("") | ||
def getMulDepth: Int = t.count { | ||
case Mul(_, _) => true; case _ => false | ||
} | ||
|
||
def assignRandValues(len: Int, bound: (Int | Double)): Template = { | ||
val lx = Random.between(1, len + 1) | ||
val ly = Random.between(1, len + 1) | ||
val vxs: (List[Int] | List[Double]) = bound match { | ||
case intBound: Int => List.fill(lx)(Random.between(0, intBound)) | ||
case doubleBound: Double => | ||
List.fill(lx)(Random.between(0.0, doubleBound)) | ||
} | ||
val vys: (List[Int] | List[Double]) = bound match { | ||
case intBound: Int => List.fill(ly)(Random.between(0, intBound)) | ||
case doubleBound: Double => | ||
List.fill(ly)(Random.between(0.0, doubleBound)) | ||
} | ||
|
||
val assigned = assignValues("x", vxs) :: t | ||
assigned.flatMap { | ||
case op @ (Add(_, _) | Sub(_, _) | Mul(_, _)) => | ||
assignValues("y", vys) :: op :: Nil | ||
case op @ Rot(_, _) => | ||
Assign("c", Random.between(0, 10)) :: op :: Nil | ||
case s => s :: Nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters