Skip to content

Commit

Permalink
Support Plain Ops (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maokami committed Feb 20, 2024
1 parent ca796ca commit 0cf400f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/main/scala/fhetest/Generate/AbsStatement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ 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 AddP(l: Var, r: Var) extends AbsStmt
case class Sub(l: Var, r: Var) extends AbsStmt
case class SubP(l: Var, r: Var) extends AbsStmt
case class Mul(l: Var, r: Var) extends AbsStmt
case class MulP(l: Var, r: Var) extends AbsStmt
case class Rot(l: Var, r: Var) extends AbsStmt

val V = Var()
Expand All @@ -18,7 +21,10 @@ def formatNumber(n: Int | Double): String = n match {

def allAbsStmts: LazyList[AbsStmt] = LazyList(
Add(V, V),
AddP(V, V),
Sub(V, V),
SubP(V, V),
Mul(V, V),
MulP(V, V),
Rot(V, V),
)
25 changes: 19 additions & 6 deletions src/main/scala/fhetest/Generate/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ package fhetest.Generate
import scala.util.Random

object Utils {
def assignValues(name: String, vxs: (List[Int] | List[Double])): AbsStmt =
AssignVec(name, vxs)
def assignValue(name: String, v: (Int | Double)): AbsStmt =
Assign(name, v)
def assignValues(name: String, vs: (List[Int] | List[Double])): AbsStmt =
AssignVec(name, vs)

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);"
case Add(_, _) => "x += y;"
case AddP(_, _) => "x += yP;"
case Sub(_, _) => "x -= y;"
case SubP(_, _) => "x -= yP;"
case Mul(_, _) => "x *= y;"
case MulP(_, _) => "x *= yP;"
case Rot(_, _) => "rotate_left(x, c);"

extension (t: Template)
def stringify: String = t.map(_.stringify).mkString("")
Expand All @@ -24,6 +29,8 @@ object Utils {
def assignRandValues(len: Int, bound: (Int | Double)): Template = {
val lx = Random.between(1, len + 1)
val ly = Random.between(1, len + 1)

// Generate Random Values
val vxs: (List[Int] | List[Double]) = bound match {
case intBound: Int => List.fill(lx)(Random.between(0, intBound))
case doubleBound: Double =>
Expand All @@ -34,11 +41,17 @@ object Utils {
case doubleBound: Double =>
List.fill(ly)(Random.between(0.0, doubleBound))
}
val vyP = bound match {
case intBound: Int => Random.between(0, intBound)
case doubleBound: Double => 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 @ (AddP(_, _) | SubP(_, _) | MulP(_, _)) =>
assignValue("yP", vyP) :: op :: Nil
case op @ Rot(_, _) =>
Assign("c", Random.between(0, 10)) :: op :: Nil
case s => s :: Nil
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/fhetest/Phase/Generate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ case class Generate(
// TODO : This boilerplate code is really ugly. But I cannot find a better way to do this.
val baseStrFront = encType match {
case ENC_TYPE.ENC_INT =>
"int main(void) { EncInt x, y; int c; "
"int main(void) { EncInt x, y; int yP; int c; "
case ENC_TYPE.ENC_DOUBLE =>
"int main(void) { EncDouble x, y; int c; "
"int main(void) { EncDouble x, y; double yP; int c; "
case ENC_TYPE.None => throw new Exception("encType is not set")
}
val baseStrBack = " print_batched (x, 10); return 0; } "
val baseStrBack = " print_batched (x, 20); return 0; } "
val baseStr = baseStrFront + baseStrBack

val symbolTable = boilerplate()._2
Expand Down

0 comments on commit 0cf400f

Please sign in to comment.