|
| 1 | +package fhetest.Phase |
| 2 | + |
| 3 | +import fhetest.Utils.* |
| 4 | +import org.twc.terminator.SymbolTable; |
| 5 | +import org.twc.terminator.t2dsl_compiler.*; |
| 6 | + |
| 7 | +import org.twc.terminator.t2dsl_compiler.T2DSLparser.ParseException; |
| 8 | +import org.twc.terminator.t2dsl_compiler.T2DSLparser.T2DSLParser; |
| 9 | +import org.twc.terminator.t2dsl_compiler.T2DSLsyntaxtree.*; |
| 10 | +import java.nio.file.{Files, Paths}; |
| 11 | + |
| 12 | +import java.io.*; |
| 13 | +import javax.print.attribute.EnumSyntax |
| 14 | +import scala.jdk.CollectionConverters._ |
| 15 | +import scala.util.Random |
| 16 | + |
| 17 | +case object Generate { |
| 18 | + |
| 19 | + def apply(backend: Backend, encType: ENC_TYPE, n: Int) = |
| 20 | + for { |
| 21 | + program <- allPrograms.take(n) |
| 22 | + } { |
| 23 | + // withBackendTempDir( |
| 24 | + // backend, |
| 25 | + // { workspaceDir => |
| 26 | + // given DirName = workspaceDir |
| 27 | + // val ast = baseAst |
| 28 | + // // appendToBaseAst(exampleStmt) |
| 29 | + // appendToBaseAst(program) |
| 30 | + // Print(ast, symbolTable, encType, backend) |
| 31 | + // Execute(backend) |
| 32 | + // }, |
| 33 | + // ) |
| 34 | + given DirName = getWorkspaceDir(backend) |
| 35 | + val template = buildTemplate(program) |
| 36 | + val ast = concretizeTemplate(template) |
| 37 | + Print(ast, symbolTable, encType, backend) |
| 38 | + Execute(backend) |
| 39 | + } |
| 40 | + |
| 41 | + def concretizeTemplate(template: Goal): Goal = |
| 42 | + return assignRandIntValues(template, 1000) |
| 43 | + |
| 44 | + // FIXME: This is just a temporary solution for making symbolTable and encType available |
| 45 | + val (_: Goal, symbolTable, encType) = |
| 46 | + val baseStr = """ |
| 47 | + int main(void) { |
| 48 | + EncInt x, y; |
| 49 | + print (x); |
| 50 | + return 0; |
| 51 | + } |
| 52 | + """ |
| 53 | + val baseStream = new ByteArrayInputStream(baseStr.getBytes("UTF-8")) |
| 54 | + Parse(baseStream) |
| 55 | + |
| 56 | + def createNewBaseTemplate(): Goal = { |
| 57 | + val baseStr = """ |
| 58 | + int main(void) { |
| 59 | + EncInt x, y; |
| 60 | + print_batched (x, 5); |
| 61 | + return 0; |
| 62 | + } |
| 63 | + """ |
| 64 | + val baseStream = new ByteArrayInputStream(baseStr.getBytes("UTF-8")) |
| 65 | + Parse(baseStream)._1 |
| 66 | + } |
| 67 | + |
| 68 | + def assignIntValue(template: Goal, vx: Int, vy: Int): Goal = |
| 69 | + val XStr = s"x = $vx;" |
| 70 | + val YStr = s"y = $vy;" |
| 71 | + print(XStr) |
| 72 | + print(YStr) |
| 73 | + val assignments = List(XStr, YStr) |
| 74 | + val stmts = assignments.map(parseStmt) |
| 75 | + val templateStmts = template.f0.f7.nodes |
| 76 | + templateStmts.addAll(0, stmts.asJava) |
| 77 | + return template |
| 78 | + |
| 79 | + // vxs = [1, 2, 3], vys = [4, 5, 6] => x = { 1, 2, 3 }; y = { 4, 5, 6 }; |
| 80 | + def assignIntValues(template: Goal, vxs: List[Int], vys: List[Int]): Goal = |
| 81 | + val XStr = s"x = {${vxs.mkString(",")}};" |
| 82 | + val YStr = s"y = {${vys.mkString(",")}};" |
| 83 | + val assignments = List(XStr, YStr) |
| 84 | + val stmts = assignments.map(parseStmt) |
| 85 | + val templateStmts = template.f0.f7.nodes |
| 86 | + templateStmts.addAll(0, stmts.asJava) |
| 87 | + return template |
| 88 | + |
| 89 | + def assignRandIntValue(template: Goal, bound: Int): Goal = |
| 90 | + val vx = Random.between(0, bound) |
| 91 | + val vy = Random.between(0, bound) |
| 92 | + return assignIntValue(template, vx, vy) |
| 93 | + |
| 94 | + // current length = 5 |
| 95 | + def assignRandIntValues(template: Goal, bound: Int): Goal = |
| 96 | + val vxs = List.fill(5)(Random.between(0, bound)) |
| 97 | + val vys = List.fill(5)(Random.between(0, bound)) |
| 98 | + return assignIntValues(template, vxs, vys) |
| 99 | + |
| 100 | + def parseStmt(stmtStr: String): Statement = |
| 101 | + val input_stream: InputStream = new ByteArrayInputStream( |
| 102 | + stmtStr.getBytes("UTF-8"), |
| 103 | + ) |
| 104 | + T2DSLParser(input_stream).Statement() |
| 105 | + |
| 106 | + def buildTemplate(stmts: List[Statement]): Goal = |
| 107 | + val base = createNewBaseTemplate() |
| 108 | + val baseStmts = base.f0.f7.nodes |
| 109 | + baseStmts.addAll(0, stmts.asJava) |
| 110 | + return base |
| 111 | + |
| 112 | + trait Stmt |
| 113 | + case class Var() |
| 114 | + case class Add(l: Var, r: Var) extends Stmt |
| 115 | + case class Sub(l: Var, r: Var) extends Stmt |
| 116 | + case class Mul(l: Var, r: Var) extends Stmt |
| 117 | + case class Rot(l: Var, r: Var) extends Stmt |
| 118 | + |
| 119 | + def concretize(s: Stmt) = s match |
| 120 | + case Add(l, r) => parseStmt("x += y;") |
| 121 | + case Sub(l, r) => parseStmt("x -= y;") |
| 122 | + case Mul(l, r) => parseStmt("x *= y;") |
| 123 | + // case Rot(l, r) => parseStmt("rotate_left(x, c);") |
| 124 | + |
| 125 | + type Program = List[Stmt] |
| 126 | + val V = Var() |
| 127 | + |
| 128 | +// 가능한 모든 Stmt를 생성하는 함수 |
| 129 | + def allStmts: LazyList[Stmt] = LazyList( |
| 130 | + Add(V, V), |
| 131 | + Sub(V, V), |
| 132 | + Mul(V, V), |
| 133 | + ) |
| 134 | + |
| 135 | +// 주어진 길이에 대해 가능한 모든 프로그램을 생성하는 함수 |
| 136 | + def allProgramsOfSize(n: Int): LazyList[Program] = n match { |
| 137 | + case 1 => allStmts.map(stmt => List(stmt)) |
| 138 | + case _ => |
| 139 | + for { |
| 140 | + stmt <- allStmts |
| 141 | + program <- allProgramsOfSize(n - 1) |
| 142 | + } yield stmt :: program |
| 143 | + } |
| 144 | + |
| 145 | +// 모든 길이에 대해 가능한 모든 프로그램을 생성하는 LazyList |
| 146 | + val allPrograms: LazyList[List[Statement]] = |
| 147 | + LazyList.from(1).flatMap(allProgramsOfSize).map(_.map(concretize)) |
| 148 | + |
| 149 | + def generateTemplate() = ??? |
| 150 | +} |
0 commit comments