9dc2d32c7b2667b8fcbde2a86f2909dbfeb10400
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 writecsharp
(name
: String, interfaces
: Bool)
241 if interfaces
then cl
= "X"
242 file
= new OFStream.open
("{name}.cs")
243 write
"class {name} \{"
245 write
"interface Root\n\t\{ int Id(); \}"
247 write
"class Root\n\t\{ public int Id() \{ return 0;\} \}"
251 write
"interface {c} "
255 if c
.supers
.is_empty
then
257 else for s
in [c
.supers
.first
] do
262 write
"class X{c} : {c}"
265 write
"\tpublic int Id() \{ return {c.id}; \}"
275 write
"\tpublic E Item() \{ return _item; \}"
276 write
"\tpublic void SetItem(E i) \{ _item = i; \}"
277 write
"\tL<E> _next;"
278 write
"\tpublic L<E> Next() \{ return _next; \}"
279 write
"\tpublic void SetNext(L<E> n) \{ _next = n; \}"
280 write
"\tpublic L(E i, L<E> n) \{ SetItem(i); SetNext(n); \}"
281 write
"\tpublic int Id() \{ return -1; \}"
284 write
"static L<Root> Fill() \{"
285 write
"\tL<Root> head = null;"
287 write
"\tL<{c}> l{c} = new L<{c}>(new {cl}{c}(), null);"
288 write
"\thead = new L<Root>(l{c}, head);"
290 write
"\thead = null;"
291 write
"for (int x=0; x<{arraylen}; x++) \{"
292 for i
in [0..listlen
[ do
294 write
"\tL<{c}> l{i} = new L<{c}>(new {cl}{c}(), null);"
295 write
"\thead = new L<Root>(l{i}, head);"
301 write
"static int Run(L<Root> head1, L<Root> head2) \{"
302 write
"\tint cpt = 0;"
303 write
"\tint y = {loops/list.length};"
304 write
"\twhile (y > 0) \{;"
305 write
"\tL<Root> n1 = head1;"
306 write
"\tL<Root> n2 = head2;"
307 write
"\twhile (n1 != null) \{"
308 for i
in [0..listlen
[ do
310 write
"\t\tRoot l{i} = n1.Item();"
311 write
"\t\tL<{c}> lc{i} = (L<{c}>)l{i};"
312 write
"\t\t{c} c{i} = lc{i}.Item();"
313 write
"\t\tn1 = n1.Next();"
315 write
"\t\tif (c{i}.Id() == {c.id}) cpt += 1;"
317 write
"\t\tRoot l2_{i} = n2.Item();"
318 write
"\t\tL<{c}> lc2_{i} = (L<{c}>)l2_{i};"
319 write
"\t\tlc2_{i}.SetItem(c{i});"
320 write
"\t\tn2 = n2.Next();"
328 write
"static void Main(string[] args) \{"
329 write
"L<Root> head = Fill();"
330 write
"int loops = 25;"
331 write
"if (args.Length > 0) loops = int.Parse(args[0]);"
332 write
"for (int x=0; x<loops; x++) \{"
333 write
"\tint cpt = Run(head, head);"
334 write
"\tSystem.Console.WriteLine(\"\
" + x + \":\\t\
" + cpt);"
341 fun writescala
(name
: String)
344 file
= new OFStream.open
("{name}.scala")
345 write
"object {name} \{"
346 write
"class Root\n\t\{ def id: Int = 0 \}"
349 if c
.supers
.is_empty
then
350 write
"\textends Root"
351 else for s
in [c
.supers
.first
] do
352 write
"\textends {s}"
355 write
"\toverride def id: Int = {c.id}"
358 write
"class L[E](var _item: E, var _next: L[E]) extends Root \{"
359 write
"\tdef item: E = _item"
360 write
"\tdef set_item(i: E) = this._item = i"
361 write
"\tdef next: L[E] = _next"
362 write
"\tdef set_next(n: L[E]) = this._next = n"
363 write
"\toverride def id: Int = -1"
366 write
"def fill: L[Root] = \{"
367 write
"\tvar head: L[Root] = null"
369 write
"\tvar l{c}: L[{c}] = new L[{c}](new {cl}{c}(), null)"
370 write
"\thead = new L[Root](l{c}, head)"
372 write
"\thead = null"
373 write
"\tfor (x <- 0 to {arraylen}) \{"
374 for i
in [0..listlen
[ do
376 write
"\t\tvar l{i}: L[{c}] = new L[{c}](new {cl}{c}(), null)"
377 write
"\t\thead = new L[Root](l{i}, head)"
380 write
"\treturn head"
383 write
"def run(head1: L[Root], head2: L[Root]): Int = \{"
384 write
"\tvar cpt: Int = 0"
385 write
"\tvar y: Int = {loops/list.length}"
386 write
"\twhile (y > 0) \{"
387 write
"\tvar n1: L[Root] = head1"
388 write
"\tvar n2: L[Root] = head2"
389 write
"\twhile (n1 != null) \{"
390 for i
in [0..listlen
[ do
392 write
"\t\tvar l{i}: Root = n1.item"
393 write
"\t\tvar lc{i}: L[{c}] = l{i}.asInstanceOf[L[{c}]]"
394 write
"\t\tvar c{i}: {c} = lc{i}.item"
395 write
"\t\tn1 = n1.next"
397 write
"\t\tif (c{i}.id == {c.id}) cpt += 1"
399 write
"\t\tvar l2_{i}: Root = n2.item"
400 write
"\t\tvar lc2_{i}: L[{c}] = l2_{i}.asInstanceOf[L[{c}]]"
401 write
"\t\tlc2_{i}.set_item(c{i})"
402 write
"\t\tn2 = n2.next"
410 write
"def main(args: Array[String]) = \{"
411 write
"\tvar head: L[Root] = fill"
412 write
"\tvar loops: Int = 25"
413 write
"\tif (args.length > 0) \{"
414 write
"\t\tloops = Integer.parseInt(args(0))"
416 write
"\tfor (x <- 0 to loops) \{"
417 write
"\t\tvar cpt: Int = run(head, head)"
418 write
"\t\tprintln(\"\
" + x + \":\\t\
" + cpt)"
425 fun writecpp
(name
: String)
427 file
= new OFStream.open
("{name}.cpp")
428 write
"#include <iostream>"
429 write
"#include <stdlib.h>"
430 write
"class Root\n\t\{ public: virtual int id() \{ return 0;\} \};"
433 if c
.supers
.is_empty
then
434 write
"\t: public virtual Root"
435 else for s
in [c
.supers
.first
] do
436 write
"\t: public virtual {s}"
439 write
"\tpublic: virtual int id() \{ return {c.id}; \}"
442 write
"template<class E>"
443 write
"class L: public virtual Root \{ public:"
444 write
"\tE _item; virtual E item() \{ return _item; \} virtual void set_item(E i) \{ _item = i; \}"
445 write
"\tL<E> *_next; virtual L<E> *next() \{ return _next; \} virtual void set_next(L<E> *n) \{ _next = n; \}"
446 write
"\tL(E i, L<E> *n) \{ set_item(i); set_next(n); \}"
449 write
"L<Root*> *fill() \{"
450 write
"\tL<Root*> *head = 0;"
452 write
"\tL<{c}*> *l{c} = new L<{c}*>(new {c}(), 0);"
453 write
"\thead = new L<Root*>(l{c}, head);"
456 write
"for (int x=0; x<{arraylen}; x++) \{"
457 for i
in [0..listlen
[ do
459 write
"\tL<{c}*> *l{i} = new L<{c}*>(new {c}(), 0);"
460 write
"\thead = new L<Root*>(l{i}, head);"
466 write
"int run(L<Root*> *head1, L<Root*> *head2) \{"
467 write
"\tint cpt = 0;"
468 write
"\tint y = {loops/list.length};"
469 write
"\twhile (y > 0) \{;"
470 write
"\tL<Root*> *n1 = head1;"
471 write
"\tL<Root*> *n2 = head2;"
472 write
"\twhile (n1 != 0) \{"
473 for i
in [0..listlen
[ do
475 write
"\t\tRoot *l{i} = n1->item();"
476 write
"\t\tL<{c}*> *lc{i} = dynamic_cast<L<{c}*>*>(l{i});"
477 write
"\t\t{c} *c{i} = lc{i}->item();"
478 write
"\t\tn1 = n1->next();"
480 write
"\t\tif (c{i}->id() == {c.id}) cpt += 1;"
482 write
"\t\tRoot *l2_{i} = n2->item();"
483 write
"\t\tL<{c}*> *lc2_{i} = dynamic_cast<L<{c}*>*>(l2_{i});"
484 write
"\t\tlc2_{i}->set_item(c{i});"
485 write
"\t\tn2 = n2->next();"
493 write
"int main(int argc, char **argv) \{"
494 write
"L<Root*> *head = fill();"
495 write
"int loops = 25;"
496 write
"if (argc > 1) loops = atoi(argv[1]);"
497 write
"for (int x=0; x<loops; x++) \{"
498 write
"\tint cpt = run(head, head);"
499 write
"\tstd::cout << x << \":\\t\
" << cpt << std::endl;"
505 fun writee
(name
: String, se
: Bool)
508 if se
then istk
= " is"
511 file
= new OFStream.open
("{name}/root.e")
513 write
"feature id: INTEGER {istk} do Result := 0 end"
518 file
= new OFStream.open
("{name}/{c}.e")
520 if c
.supers
.is_empty
then
521 write
"\tinherit ROOT"
522 else for s
in [c
.supers
.first
] do
523 write
"\tinherit {s}"
525 write
"\t\tredefine id end"
527 write
"\tid: INTEGER {istk} do Result := {c.id} end"
532 file
= new OFStream.open
("{name}/l.e")
538 write
"\tset_item(i: E) {istk} do item := i end"
540 write
"\tset_next(n: L[E]) {istk} do next := n end"
541 write
"\tmake(i: E; n: L[E]) {istk} do set_item(i); set_next(n); end"
545 file
= new OFStream.open
("{name}/app{name}.e")
546 write
"class APP{name.to_upper}"
548 write
"insert ARGUMENTS"
552 write
"fill: L[ROOT] {istk}"
557 write
"\tcreate Result.make(create \{L[{c}]\}.make(create \{{c}\}, Void), Result)"
559 write
"\tResult := Void;"
560 write
"from i := 0 until i >= {arraylen} loop"
561 for i
in [0..listlen
[ do
563 write
"\tcreate Result.make(create \{L[{c}]\}.make(create \{{c}\}, Void), Result)"
569 write
"run(head1: L[ROOT]; head2: L[ROOT]): INTEGER{istk}"
572 write
"\tn1, n2: L[ROOT]"
573 for i
in [0..listlen
[ do
575 write
"\t\tl{i}: ROOT"
576 write
"\t\tlc{i}: L[{c}]"
577 write
"\t\tc{i}: {c}"
578 write
"\t\tl2_{i}: ROOT"
579 write
"\t\tlc2_{i}: L[{c}]"
582 write
"\tfrom y := {loops/list.length}"
583 write
"\tuntil y <= 0"
586 write
"\tn1 := head1"
587 write
"\tn2 := head2"
588 write
"\tuntil n1 = Void loop"
589 for i
in [0..listlen
[ do
591 write
"\t\tl{i} := n1.item"
592 write
"\t\tlc{i} ?= l{i}"
593 write
"\t\tc{i} := lc{i}.item"
595 write
"\t\tif c{i}.id = {c.id} then Result := Result + 1 end"
597 write
"\t\tl2_{i} := n2.item"
598 write
"\t\tlc2_{i} ?= l2_{i}"
599 write
"\t\tlc2_{i}.set_item(c{i})"
600 write
"\t\tn1 := n1.next"
601 write
"\t\tn2 := n2.next"
611 write
"make(args: ARRAY[STRING]){istk}"
614 write
"head: L[ROOT]"
615 write
"loops: INTEGER"
622 write
"if argument_count > 0 then loops := argument(1).to_integer"
624 write
"if args.count > 1 then loops := args.item(1).to_integer"
626 write
"from x := 0 until x>=loops loop"
627 write
"\tcpt := run(head, head)"
628 write
"\tprint(x.out+\":%T\
"+cpt.out+\"%N\
")"
629 write
"\t x := x + 1"
640 var g
= new Generator
641 var name
= args
.first
642 if args
.length
> 1 then
643 var opts
= args
[1].split_with
("_")
645 var oname
= opt
.substring
(0,1)
646 var val
= opt
.substring_from
(1).to_i
649 else if oname
== "w" then
651 else if oname
== "h" then
654 print
"Option inconnue '{oname}'"
661 g
.writejava
(name
, true)
662 g
.writecsharp
(name
, true)
665 g
.writee
("{name}_se", true)
666 g
.writee
(name
, false)