// examples/loops // BEGIN mycpp output #include "gc_heap.h" using gc_heap::Alloc; using gc_heap::kZeroMask; using gc_heap::StackRoots; #include "my_runtime.h" #include "mylib2.h" using gc_heap::NewStr; using gc_heap::NewList; using gc_heap::NewDict; GLOBAL_STR(str0, "--- list comprehension"); GLOBAL_STR(str1, "len = %d"); GLOBAL_STR(str2, "y[0] = %d"); GLOBAL_STR(str3, "y[-1] = %d"); GLOBAL_STR(str4, "--- list comprehension changing type"); GLOBAL_STR(str5, "[%d]"); GLOBAL_STR(str6, "len = %d"); GLOBAL_STR(str7, "z[0] = %s"); GLOBAL_STR(str8, "z[-1] = %s"); GLOBAL_STR(str9, "one"); GLOBAL_STR(str10, "two"); GLOBAL_STR(str11, "first = %s"); GLOBAL_STR(str12, "a"); GLOBAL_STR(str13, "b"); GLOBAL_STR(str14, ""); GLOBAL_STR(str15, "--- Dict"); GLOBAL_STR(str16, "a"); GLOBAL_STR(str17, "c"); GLOBAL_STR(str18, "b"); GLOBAL_STR(str19, "a = %d"); GLOBAL_STR(str20, "a"); GLOBAL_STR(str21, "b = %d"); GLOBAL_STR(str22, "b"); GLOBAL_STR(str23, "c = %d"); GLOBAL_STR(str24, "c"); GLOBAL_STR(str25, "k = %s"); GLOBAL_STR(str26, "k = %s, v = %d"); GLOBAL_STR(str27, "big"); GLOBAL_STR(str28, "small"); GLOBAL_STR(str29, "hairless"); GLOBAL_STR(str30, "--- iterate over bytes in string"); GLOBAL_STR(str31, "abc"); GLOBAL_STR(str32, "ch = %s"); GLOBAL_STR(str33, "--- iterate over items in list"); GLOBAL_STR(str34, "xx"); GLOBAL_STR(str35, "yy"); GLOBAL_STR(str36, "item = %s"); GLOBAL_STR(str37, "--- tuple unpacking"); GLOBAL_STR(str38, "five"); GLOBAL_STR(str39, "six"); GLOBAL_STR(str40, "- [%d] %s"); GLOBAL_STR(str41, "--- one arg xrange()"); GLOBAL_STR(str42, "%d"); GLOBAL_STR(str43, "--- two arg xrange()"); GLOBAL_STR(str44, "%d"); GLOBAL_STR(str45, "--- three arg xrange()"); GLOBAL_STR(str46, "%d"); GLOBAL_STR(str47, "--- three arg reverse xrange()"); GLOBAL_STR(str48, "reverse %d"); GLOBAL_STR(str49, "--- enumerate()"); GLOBAL_STR(str50, "%d %s"); GLOBAL_STR(str51, "%d %d %s"); GLOBAL_STR(str52, "--- reversed() list"); GLOBAL_STR(str53, "spam"); GLOBAL_STR(str54, "eggs"); GLOBAL_STR(str55, "- %s"); GLOBAL_STR(str56, "--- reversed() list with tuple unpacking"); GLOBAL_STR(str57, "- [%d] %s"); GLOBAL_STR(str58, "result = %d"); GLOBAL_STR(str59, "Ran %d iterations of xrange/enumerate"); namespace loops { // forward declare } // forward declare namespace loops namespace loops { // declare void TestListComp(); void TestDict(); extern List* CATS; void run_tests(); void run_benchmarks(); inline Str* fmt0(int a0) { gBuf.reset(); gBuf.write_const("len = ", 6); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt1(int a0) { gBuf.reset(); gBuf.write_const("y[0] = ", 7); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt2(int a0) { gBuf.reset(); gBuf.write_const("y[-1] = ", 8); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt3(int a0) { gBuf.reset(); gBuf.write_const("[", 1); gBuf.format_d(a0); gBuf.write_const("]", 1); return gBuf.getvalue(); } inline Str* fmt4(int a0) { gBuf.reset(); gBuf.write_const("len = ", 6); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt5(Str* a0) { gBuf.reset(); gBuf.write_const("z[0] = ", 7); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt6(Str* a0) { gBuf.reset(); gBuf.write_const("z[-1] = ", 8); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt7(Str* a0) { gBuf.reset(); gBuf.write_const("first = ", 8); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt8(int a0) { gBuf.reset(); gBuf.write_const("a = ", 4); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt9(int a0) { gBuf.reset(); gBuf.write_const("b = ", 4); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt10(int a0) { gBuf.reset(); gBuf.write_const("c = ", 4); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt11(Str* a0) { gBuf.reset(); gBuf.write_const("k = ", 4); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt12(Str* a0, int a1) { gBuf.reset(); gBuf.write_const("k = ", 4); gBuf.format_s(a0); gBuf.write_const(", v = ", 6); gBuf.format_d(a1); return gBuf.getvalue(); } inline Str* fmt13(Str* a0) { gBuf.reset(); gBuf.write_const("ch = ", 5); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt14(Str* a0) { gBuf.reset(); gBuf.write_const("item = ", 7); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt15(int a0, Str* a1) { gBuf.reset(); gBuf.write_const("- [", 3); gBuf.format_d(a0); gBuf.write_const("] ", 2); gBuf.format_s(a1); return gBuf.getvalue(); } inline Str* fmt16(int a0) { gBuf.reset(); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt17(int a0) { gBuf.reset(); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt18(int a0) { gBuf.reset(); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt19(int a0) { gBuf.reset(); gBuf.write_const("reverse ", 8); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt20(int a0, Str* a1) { gBuf.reset(); gBuf.format_d(a0); gBuf.write_const(" ", 1); gBuf.format_s(a1); return gBuf.getvalue(); } inline Str* fmt21(int a0, int a1, Str* a2) { gBuf.reset(); gBuf.format_d(a0); gBuf.write_const(" ", 1); gBuf.format_d(a1); gBuf.write_const(" ", 1); gBuf.format_s(a2); return gBuf.getvalue(); } inline Str* fmt22(Str* a0) { gBuf.reset(); gBuf.write_const("- ", 2); gBuf.format_s(a0); return gBuf.getvalue(); } inline Str* fmt23(int a0, Str* a1) { gBuf.reset(); gBuf.write_const("- [", 3); gBuf.format_d(a0); gBuf.write_const("] ", 2); gBuf.format_s(a1); return gBuf.getvalue(); } inline Str* fmt24(int a0) { gBuf.reset(); gBuf.write_const("result = ", 9); gBuf.format_d(a0); return gBuf.getvalue(); } inline Str* fmt25(int a0) { gBuf.reset(); gBuf.write_const("Ran ", 4); gBuf.format_d(a0); gBuf.write_const(" iterations of xrange/enumerate", 31); return gBuf.getvalue(); } } // declare namespace loops namespace loops { // define void TestListComp() { List* x = nullptr; List* y = nullptr; List* z = nullptr; List*>* pairs = nullptr; List* first = nullptr; Str* s = nullptr; List* parts = nullptr; List* tmp = nullptr; StackRoots _roots({&x, &y, &z, &pairs, &first, &s, &parts, &tmp}); println_stderr(str0); x = NewList(std::initializer_list{1, 2, 3, 4}); y = Alloc>(); for (ListIter it(x->slice(1)); !it.Done(); it.Next()) { int i = it.Value(); y->append((i * 5)); } println_stderr(fmt0(len(y))); println_stderr(fmt1(y->index(0))); println_stderr(fmt2(y->index(-1))); println_stderr(str4); z = Alloc>(); for (ListIter it(x->slice(1, -1)); !it.Done(); it.Next()) { int i = it.Value(); z->append(fmt3(i)); } println_stderr(fmt4(len(z))); println_stderr(fmt5(z->index(0))); println_stderr(fmt6(z->index(-1))); pairs = NewList*>(std::initializer_list*>{(Alloc>(str9, 1)), (Alloc>(str10, 2))}); first = Alloc>(); for (ListIter*> it(pairs); !it.Done(); it.Next()) { Tuple2* tup0 = it.Value(); s = tup0->at0(); first->append(s); } for (ListIter it(first); !it.Done(); it.Next()) { Str* s2 = it.Value(); StackRoots _for({&s2 }); println_stderr(fmt7(s2)); } parts = NewList(std::initializer_list{str12, nullptr, str13}); tmp = Alloc>(); for (ListIter it(parts); !it.Done(); it.Next()) { Str* s = it.Value(); if (s != nullptr) { tmp->append(s); } } print(str14->join(tmp)); } void TestDict() { Dict* d = nullptr; StackRoots _roots({&d}); println_stderr(str15); d = Alloc>(); d->set(str16, 99); d->set(str17, 42); d->set(str18, 0); println_stderr(fmt8(d->index(str20))); println_stderr(fmt9(d->index(str22))); println_stderr(fmt10(d->index(str24))); for (DictIter it(d); !it.Done(); it.Next()) { Str* k = it.Key(); StackRoots _for({&k }); println_stderr(fmt11(k)); } for (DictIter it(d); !it.Done(); it.Next()) { Str* k = it.Key(); int v = it.Value(); println_stderr(fmt12(k, v)); } } GLOBAL_LIST(Str*, 3, CATS, {str27 COMMA str28 COMMA str29}); void run_tests() { List*>* list_of_tuples = nullptr; int i; Str* item = nullptr; int m; int n; int index; Str* s = nullptr; List* list_of_strings = nullptr; StackRoots _roots({&list_of_tuples, &item, &s, &list_of_strings}); println_stderr(str30); for (StrIter it(str31); !it.Done(); it.Next()) { Str* ch = it.Value(); StackRoots _for({&ch }); println_stderr(fmt13(ch)); } println_stderr(str33); for (ListIter it(NewList(std::initializer_list{str34, str35})); !it.Done(); it.Next()) { Str* item = it.Value(); StackRoots _for({&item }); println_stderr(fmt14(item)); } println_stderr(str37); list_of_tuples = NewList*>(std::initializer_list*>{(Alloc>(5, str38)), (Alloc>(6, str39))}); for (ListIter*> it(list_of_tuples); !it.Done(); it.Next()) { Tuple2* tup1 = it.Value(); i = tup1->at0(); item = tup1->at1(); println_stderr(fmt15(i, item)); } println_stderr(str41); m = 2; n = 3; for (int j = 0; j < (m * 2); ++j) { println_stderr(fmt16(j)); } println_stderr(str43); for (int k = (m + 2); k < (n + 5); ++k) { println_stderr(fmt17(k)); } println_stderr(str45); for (int m = 0; m < 5; m += 2) { println_stderr(fmt18(m)); } println_stderr(str47); for (int m = 0; m > -1; m += -1) { println_stderr(fmt19(m)); } println_stderr(str49); i = 0; for (ListIter it(CATS); !it.Done(); it.Next(), ++i) { Str* c = it.Value(); StackRoots _for({&c }); println_stderr(fmt20(i, c)); } i = 0; for (ListIter*> it(list_of_tuples); !it.Done(); it.Next(), ++i) { Tuple2* pair = it.Value(); StackRoots _for({&pair }); Tuple2* tup2 = pair; index = tup2->at0(); s = tup2->at1(); println_stderr(fmt21(i, index, s)); } println_stderr(str52); list_of_strings = NewList(std::initializer_list{str53, str54}); for (ReverseListIter it(list_of_strings); !it.Done(); it.Next()) { Str* item = it.Value(); StackRoots _for({&item }); println_stderr(fmt22(item)); } println_stderr(str56); for (ReverseListIter*> it(list_of_tuples); !it.Done(); it.Next()) { Tuple2* tup3 = it.Value(); i = tup3->at0(); item = tup3->at1(); println_stderr(fmt23(i, item)); } TestListComp(); TestDict(); } void run_benchmarks() { int n; int result; int i; int j; n = 500000; result = 0; i = 0; while (i < n) { for (int j = 3; j < 10; ++j) { result += j; } j = 0; for (ListIter it(CATS); !it.Done(); it.Next(), ++j) { Str* c = it.Value(); StackRoots _for({&c }); result += j; result += len(c); } i += 1; } println_stderr(fmt24(result)); println_stderr(fmt25(n)); } } // define namespace loops int main(int argc, char **argv) { // gc_heap::gHeap.Init(512); gc_heap::gHeap.Init(128 << 10); // 128 KiB; doubling in size // gc_heap::gHeap.Init(400 << 20); // 400 MiB to avoid garbage collection if (getenv("BENCHMARK")) { fprintf(stderr, "Benchmarking...\n"); loops::run_benchmarks(); } else { loops::run_tests(); } }