3 # Microbenchmak generation for multiple language
4 # Just a quick an dirty Nit script file :)
8 var supers
= new Array[Klass]
9 var all_supers
= new HashSet[Klass]
18 var classes
= new Array[Klass]
22 var superprobs
: Array[Int] = [40, 20, 5]
27 for w
in [0..width
[ do
28 var s
: nullable Klass = null
29 for h
in [0..height
[ do
37 c
.all_supers
.add_all
(s
.all_supers
)
43 for j
in [0..listlen
[ do
44 var c
= classes
[classes
.length
- 1 - (j
% classes
.length
)]
50 # List of instantited class in the order to read
51 var list
= new List[Klass]
53 # List of instantited class in the order to push
54 var unlist
= new List[Klass]
61 var file
: nullable OFStream = null
62 fun write
(str
: String)
68 fun writenit
(name
: String)
70 file
= new OFStream.open
("{name}.nit")
71 write
"class Root\n\tfun id: Int do return 0\nend"
74 if c
.supers
.is_empty
then
76 else for s
in c
.supers
do
79 write
"\tredef fun id do return {c.id}"
82 write
"class L[E]\n\tsuper Root\n\tvar item:E \n\tvar next: nullable L[E]\ninit(item:E,next: nullable L[E])do\n\tself.item = item\n\tself.next=next\nend\nend"
84 write
"fun fill: nullable L[Root]\ndo"
85 write
"\tvar head: nullable L[Root] = null"
86 write
"\tvar l: L[Root]"
88 write
"\tvar l{c} = new L[{c}](new {c}, null)"
89 write
"\thead = new L[Root](l{c}, head)"
92 write
"for x in [0..{arraylen}[ do"
93 for i
in [0..listlen
[ do
95 write
"\tvar l{i} = new L[{c}](new {c}, null)"
96 write
"\thead = new L[Root](l{i}, head)"
102 write
"fun run(head1: nullable L[Root], head2: nullable L[Root]): Int\ndo"
103 write
"\tvar cpt = 0"
104 write
"\tvar y = {loops/list.length}"
105 write
"\twhile y > 0 do"
106 write
"\tvar n1 = head1"
107 write
"\tvar n2 = head2"
108 write
"\twhile not n1 is null do"
109 for i
in [0..listlen
[ do
111 write
"\t\tvar l{i} = n1.item"
112 write
"\t\tvar lc{i} = l{i}.as(L[{c}])"
113 write
"\t\tvar c{i} = lc{i}.item"
114 write
"\t\tn1 = n1.next"
116 write
"\t\tif c{i}.id == {c.id} then cpt += 1"
118 write
"\t\tvar l2_{i} = n2.item"
119 write
"\t\tvar lc2_{i} = l2_{i}.as(L[{c}])"
120 write
"\t\tlc2_{i}.item = c{i}"
121 write
"\t\tn2 = n2.next"
129 write
"var head = fill"
130 write
"var loops = 25"
131 write
"if not args.is_empty then loops = args.first.to_i "
132 write
"for x in [0..loops[ do"
133 write
"\tvar cpt = run(head, head)"
134 write
"\tprint \"\
{x\}:\tcpt
:\
{cpt\}\
""
139 fun writejava
(name
: String, interfaces
: Bool)
142 if interfaces
then cl
= "X"
143 file
= new OFStream.open
("{name}.java")
144 write
"class {name} \{"
146 write
"static interface Root\n\t\{ int id(); \}"
148 write
"static class Root\n\t\{ int id() \{ return 0;\} \}"
152 write
"static interface {c} "
154 write
"static class {c} "
156 if c
.supers
.is_empty
then
157 write
"\textends Root"
158 else for s
in [c
.supers
.first
] do
159 write
"\textends {s}"
163 write
"static class X{c} implements {c}"
166 write
"\tpublic int id() \{ return {c.id}; \}"
169 write
"static class L<E>"
171 write
"implements Root \{"
173 write
"extends Root \{"
175 write
"\tE _item; E item() \{ return _item; \} void set_item(E i) \{ _item = i; \}"
176 write
"\tL<E> _next; L<E> next() \{ return _next; \} void set_next(L<E> n) \{ _next = n; \}"
177 write
"\tL(E i, L<E> n) \{ set_item(i); set_next(n); \}"
178 write
"\tpublic int id() \{ return -1; \}"
181 write
"static L<Root> fill() \{"
182 write
"\tL<Root> head = null;"
184 write
"\tL<{c}> l{c} = new L<{c}>(new {cl}{c}(), null);"
185 write
"\thead = new L<Root>(l{c}, head);"
187 write
"\thead = null;"
188 write
"for (int x=0; x<{arraylen}; x++) \{"
189 for i
in [0..listlen
[ do
191 write
"\tL<{c}> l{i} = new L<{c}>(new {cl}{c}(), null);"
192 write
"\thead = new L<Root>(l{i}, head);"
198 write
"static int run(L<Root> head1, L<Root> head2) \{"
199 write
"\tint cpt = 0;"
200 write
"\tint y = {loops/list.length};"
201 write
"\twhile (y > 0) \{;"
202 write
"\tL<Root> n1 = head1;"
203 write
"\tL<Root> n2 = head2;"
204 write
"\twhile (n1 != null) \{"
205 for i
in [0..listlen
[ do
207 write
"\t\tRoot l{i} = n1.item();"
208 write
"\t\tL<{c}> lc{i} = (L<{c}>)l{i};"
209 write
"\t\t{c} c{i} = lc{i}.item();"
210 write
"\t\tn1 = n1.next();"
212 write
"\t\tif (c{i}.id() == {c.id}) cpt += 1;"
214 write
"\t\tRoot l2_{i} = n2.item();"
215 write
"\t\tL<{c}> lc2_{i} = (L<{c}>)l2_{i};"
216 write
"\t\tlc2_{i}.set_item(c{i});"
217 write
"\t\tn2 = n2.next();"
225 write
"static public void main(String args[]) \{"
226 write
"L<Root> head = fill();"
227 write
"int loops = 25;"
228 write
"if (args.length > 0) loops = Integer.parseInt(args[0]);"
229 write
"for (int x=0; x<loops; x++) \{"
230 write
"\tint cpt = run(head, head);"
231 write
"\tSystem.out.println(\"\
" + x + \":\\t\
" + cpt);"
238 fun writecpp
(name
: String)
240 file
= new OFStream.open
("{name}.cpp")
241 write
"#include <iostream>"
242 write
"#include <stdlib.h>"
243 write
"class Root\n\t\{ public: virtual int id() \{ return 0;\} \};"
246 if c
.supers
.is_empty
then
247 write
"\t: public virtual Root"
248 else for s
in [c
.supers
.first
] do
249 write
"\t: public virtual {s}"
252 write
"\tpublic: virtual int id() \{ return {c.id}; \}"
255 write
"template<class E>"
256 write
"class L: public virtual Root \{ public:"
257 write
"\tE _item; virtual E item() \{ return _item; \} virtual void set_item(E i) \{ _item = i; \}"
258 write
"\tL<E> *_next; virtual L<E> *next() \{ return _next; \} virtual void set_next(L<E> *n) \{ _next = n; \}"
259 write
"\tL(E i, L<E> *n) \{ set_item(i); set_next(n); \}"
262 write
"L<Root*> *fill() \{"
263 write
"\tL<Root*> *head = 0;"
265 write
"\tL<{c}*> *l{c} = new L<{c}*>(new {c}(), 0);"
266 write
"\thead = new L<Root*>(l{c}, head);"
269 write
"for (int x=0; x<{arraylen}; x++) \{"
270 for i
in [0..listlen
[ do
272 write
"\tL<{c}*> *l{i} = new L<{c}*>(new {c}(), 0);"
273 write
"\thead = new L<Root*>(l{i}, head);"
279 write
"int run(L<Root*> *head1, L<Root*> *head2) \{"
280 write
"\tint cpt = 0;"
281 write
"\tint y = {loops/list.length};"
282 write
"\twhile (y > 0) \{;"
283 write
"\tL<Root*> *n1 = head1;"
284 write
"\tL<Root*> *n2 = head2;"
285 write
"\twhile (n1 != 0) \{"
286 for i
in [0..listlen
[ do
288 write
"\t\tRoot *l{i} = n1->item();"
289 write
"\t\tL<{c}*> *lc{i} = dynamic_cast<L<{c}*>*>(l{i});"
290 write
"\t\t{c} *c{i} = lc{i}->item();"
291 write
"\t\tn1 = n1->next();"
293 write
"\t\tif (c{i}->id() == {c.id}) cpt += 1;"
295 write
"\t\tRoot *l2_{i} = n2->item();"
296 write
"\t\tL<{c}*> *lc2_{i} = dynamic_cast<L<{c}*>*>(l2_{i});"
297 write
"\t\tlc2_{i}->set_item(c{i});"
298 write
"\t\tn2 = n2->next();"
306 write
"int main(int argc, char **argv) \{"
307 write
"L<Root*> *head = fill();"
308 write
"int loops = 25;"
309 write
"if (argc > 1) loops = atoi(argv[1]);"
310 write
"for (int x=0; x<loops; x++) \{"
311 write
"\tint cpt = run(head, head);"
312 write
"\tstd::cout << x << \":\\t\
" << cpt << std::endl;"
318 fun writee
(name
: String, se
: Bool)
321 if se
then istk
= " is"
324 file
= new OFStream.open
("{name}/root.e")
326 write
"feature id: INTEGER {istk} do Result := 0 end"
331 file
= new OFStream.open
("{name}/{c}.e")
333 if c
.supers
.is_empty
then
334 write
"\tinherit ROOT"
335 else for s
in [c
.supers
.first
] do
336 write
"\tinherit {s}"
338 write
"\t\tredefine id end"
340 write
"\tid: INTEGER {istk} do Result := {c.id} end"
345 file
= new OFStream.open
("{name}/l.e")
351 write
"\tset_item(i: E) {istk} do item := i end"
353 write
"\tset_next(n: L[E]) {istk} do next := n end"
354 write
"\tmake(i: E; n: L[E]) {istk} do set_item(i); set_next(n); end"
358 file
= new OFStream.open
("{name}/app{name}.e")
359 write
"class APP{name.to_upper}"
361 write
"insert ARGUMENTS"
365 write
"fill: L[ROOT] {istk}"
370 write
"\tcreate Result.make(create \{L[{c}]\}.make(create \{{c}\}, Void), Result)"
372 write
"\tResult := Void;"
373 write
"from i := 0 until i >= {arraylen} loop"
374 for i
in [0..listlen
[ do
376 write
"\tcreate Result.make(create \{L[{c}]\}.make(create \{{c}\}, Void), Result)"
382 write
"run(head1: L[ROOT]; head2: L[ROOT]): INTEGER{istk}"
385 write
"\tn1, n2: L[ROOT]"
386 for i
in [0..listlen
[ do
388 write
"\t\tl{i}: ROOT"
389 write
"\t\tlc{i}: L[{c}]"
390 write
"\t\tc{i}: {c}"
391 write
"\t\tl2_{i}: ROOT"
392 write
"\t\tlc2_{i}: L[{c}]"
395 write
"\tfrom y := {loops/list.length}"
396 write
"\tuntil y <= 0"
399 write
"\tn1 := head1"
400 write
"\tn2 := head2"
401 write
"\tuntil n1 = Void loop"
402 for i
in [0..listlen
[ do
404 write
"\t\tl{i} := n1.item"
405 write
"\t\tlc{i} ?= l{i}"
406 write
"\t\tc{i} := lc{i}.item"
408 write
"\t\tif c{i}.id = {c.id} then Result := Result + 1 end"
410 write
"\t\tl2_{i} := n2.item"
411 write
"\t\tlc2_{i} ?= l2_{i}"
412 write
"\t\tlc2_{i}.set_item(c{i})"
413 write
"\t\tn1 := n1.next"
414 write
"\t\tn2 := n2.next"
424 write
"make(args: ARRAY[STRING]){istk}"
427 write
"head: L[ROOT]"
428 write
"loops: INTEGER"
435 write
"if argument_count > 0 then loops := argument(1).to_integer"
437 write
"if args.count > 1 then loops := args.item(1).to_integer"
439 write
"from x := 0 until x>=loops loop"
440 write
"\tcpt := run(head, head)"
441 write
"\tprint(x.out+\":%T\
"+cpt.out+\"%N\
")"
442 write
"\t x := x + 1"
453 var g
= new Generator
454 var name
= args
.first
455 if args
.length
> 1 then
456 var opts
= args
[1].split_with
("_")
458 var oname
= opt
.substring
(0,1)
459 var val
= opt
.substring_from
(1).to_i
462 else if oname
== "w" then
464 else if oname
== "h" then
467 print
"Option inconnue '{oname}'"
474 g
.writejava
(name
, true)
476 g
.writee
("{name}_se", true)
477 g
.writee
(name
, false)