@@ -16,30 +16,43 @@ import scala.util.Random
16
16
17
17
case object Generate {
18
18
19
- def apply (backend : Backend , encType : ENC_TYPE , n : Int ) =
19
+ def apply (backends : List [ Backend ] , encType : ENC_TYPE , n : Int ) =
20
20
for {
21
- program <- allPrograms .take(n)
21
+ template <- allTempletes .take(n)
22
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)
23
+ // TODO: currently concretize 5 times for each template
24
+ // TODO: store the results into a json file and print it by parsing the json file
25
+ print(" =" * 80 + " \n " )
26
+ print(" <Program>\n " )
27
+ print(toString(template) + " \n " )
28
+ print(" =" * 80 + " \n " )
29
+ for _ <- 0 until 5 do {
30
+ val concretized = concretizeTemplate(template)
31
+ print(toString(concretized) + " \n " )
32
+ print(" -" * 80 + " \n " )
33
+
34
+ val ast = buildTemplate(concretized)
35
+ val result = Interp (ast, 32768 , 65537 )
36
+ print(" CLEAR" + " : " + result + " \n " )
37
+ for {
38
+ backend <- backends
39
+ } {
40
+ withBackendTempDir(
41
+ backend,
42
+ { workspaceDir =>
43
+ given DirName = workspaceDir
44
+ Print (ast, symbolTable, encType, backend)
45
+ val result = Execute (backend)
46
+ print(backend.toString + " : " + result)
47
+ },
48
+ )
49
+ }
50
+ print(" -" * 80 + " \n " )
51
+ }
39
52
}
40
53
41
- def concretizeTemplate (template : Goal ): Goal =
42
- return assignRandIntValues(template, 1000 )
54
+ def concretizeTemplate (template : Templete ): Templete =
55
+ return assignRandIntValues(template, 100 )
43
56
44
57
// FIXME: This is just a temporary solution for making symbolTable and encType available
45
58
val (_ : Goal , symbolTable, encType) =
@@ -65,34 +78,28 @@ case object Generate {
65
78
Parse (baseStream)._1
66
79
}
67
80
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
81
+ def assignIntValue (template : Templete , vx : Int , vy : Int ): Templete =
82
+ val assignments = List (Assign (" x" , vx), Assign (" y" , vy))
83
+ return assignments ++ template
78
84
79
85
// 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 =
86
+ def assignIntValues (
87
+ template : Templete ,
88
+ vxs : List [Int ],
89
+ vys : List [Int ],
90
+ ): Templete =
91
+ val assignments = List (AssignVec (" x" , vxs), AssignVec (" y" , vys))
92
+ return assignments ++ template
93
+
94
+ def assignRandIntValue (template : Templete , bound : Int ): Templete =
90
95
val vx = Random .between(0 , bound)
91
96
val vy = Random .between(0 , bound)
92
97
return assignIntValue(template, vx, vy)
93
98
94
99
// current length = 5
95
- def assignRandIntValues (template : Goal , bound : Int ): Goal =
100
+ // TODO : Currently, it only supports the length of 5
101
+ // TODO : Currently, T2 DSL does not support negative numbers
102
+ def assignRandIntValues (template : Templete , bound : Int ): Templete =
96
103
val vxs = List .fill(5 )(Random .between(0 , bound))
97
104
val vys = List .fill(5 )(Random .between(0 , bound))
98
105
return assignIntValues(template, vxs, vys)
@@ -103,48 +110,54 @@ case object Generate {
103
110
)
104
111
T2DSLParser (input_stream).Statement ()
105
112
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
113
trait Stmt
113
114
case class Var ()
115
+ case class Assign (l : String , r : Int ) extends Stmt
116
+ case class AssignVec (l : String , r : List [Int ]) extends Stmt
114
117
case class Add (l : Var , r : Var ) extends Stmt
115
118
case class Sub (l : Var , r : Var ) extends Stmt
116
119
case class Mul (l : Var , r : Var ) extends Stmt
117
120
case class Rot (l : Var , r : Var ) extends Stmt
118
121
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
122
val V = Var ()
127
123
124
+ def toString (s : Stmt ) = s match
125
+ case Assign (l, r) => s " $l = $r; "
126
+ case AssignVec (l, r) => s " $l = { ${r.mkString(" ," )}}; "
127
+ case Add (l, r) => " x += y;"
128
+ case Sub (l, r) => " x -= y;"
129
+ case Mul (l, r) => " x *= y;"
130
+ // case Rot(l, r) => "rotate_left(x, c);"
131
+ def concretize (s : Stmt ) = parseStmt(toString(s))
132
+
133
+ type Templete = List [Stmt ]
134
+ def toString (t : Templete ): String = t.map(toString).mkString(" \n " )
135
+
136
+ def buildTemplate (temp : Templete ): Goal =
137
+ val stmts = temp.map(toString).map(parseStmt)
138
+ val base = createNewBaseTemplate()
139
+ val baseStmts = base.f0.f7.nodes
140
+ baseStmts.addAll(0 , stmts.asJava)
141
+ return base
142
+
128
143
// 가능한 모든 Stmt를 생성하는 함수
129
144
def allStmts : LazyList [Stmt ] = LazyList (
130
145
Add (V , V ),
131
146
Sub (V , V ),
132
147
Mul (V , V ),
133
148
)
134
149
135
- // 주어진 길이에 대해 가능한 모든 프로그램을 생성하는 함수
136
- def allProgramsOfSize (n : Int ): LazyList [Program ] = n match {
150
+ // 주어진 길이에 대해 가능한 모든 템플릿을 생성하는 함수
151
+ def allTempletesOfSize (n : Int ): LazyList [Templete ] = n match {
137
152
case 1 => allStmts.map(stmt => List (stmt))
138
153
case _ =>
139
154
for {
140
155
stmt <- allStmts
141
- program <- allProgramsOfSize (n - 1 )
156
+ program <- allTempletesOfSize (n - 1 )
142
157
} yield stmt :: program
143
158
}
144
159
145
- // 모든 길이에 대해 가능한 모든 프로그램을 생성하는 LazyList
146
- val allPrograms : LazyList [List [Statement ]] =
147
- LazyList .from(1 ).flatMap(allProgramsOfSize).map(_.map(concretize))
148
-
149
- def generateTemplate () = ???
160
+ // 모든 길이에 대해 가능한 모든 템플릿을 생성하는 LazyList
161
+ val allTempletes : LazyList [Templete ] =
162
+ LazyList .from(1 ).flatMap(allTempletesOfSize)
150
163
}
0 commit comments